1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 
8 #include "sbuf.h"
9 
10 #define PCPU_NUM        	4
11 #define TRACE_ELEMENT_SIZE      32	/* byte */
12 #define TRACE_ELEMENT_NUM	((4 * 1024 * 1024 - 64) / TRACE_ELEMENT_SIZE)
13 #define PAGE_SIZE		4096
14 #define PAGE_MASK		(~(PAGE_SIZE - 1))
15 #define MMAP_SIZE		(4 * 1024 * 1024)
16 /*
17 #define MMAP_SIZE 		((TRACE_ELEMENT_SIZE * TRACE_ELEMENT_NUM \
18 				+ PAGE_SIZE - 1) & PAGE_MASK)
19 */
20 #define TRACE_FILE_NAME_LEN	32
21 #define TRACE_FILE_DIR_LEN	(TRACE_FILE_NAME_LEN - 3)
22 #define TRACE_FILE_ROOT		"acrntrace/"
23 #define DEV_PATH_LEN		20
24 #define TIME_STR_LEN		16
25 #define CMD_MAX_LEN		48
26 
27 #define pr_fmt(fmt)             "acrntrace: " fmt
28 #define pr_info(fmt, ...)       printf(pr_fmt(fmt), ##__VA_ARGS__)
29 #define pr_err(fmt, ...)        printf(pr_fmt(fmt), ##__VA_ARGS__)
30 
31 #ifdef DEBUG
32 #define pr_dbg(fmt, ...)        printf(pr_fmt(fmt), ##__VA_ARGS__)
33 #else
34 #define pr_dbg(fmt, ...)
35 #endif
36 
37 /*
38  * flags:
39  * FLAG_TO_REL   - resources need to be release
40  * FLAG_CLEAR_BUF - to clear buffered old data
41  */
42 #define FLAG_TO_REL		(1UL << 0)
43 #define FLAG_CLEAR_BUF		(1UL << 1)
44 
45 #define foreach_dev(dev_id)                                       \
46         for ((dev_id) = 0; (dev_id) < (dev_cnt); (dev_id)++)
47 
48 typedef unsigned char uint8_t;
49 typedef unsigned int uint32_t;
50 typedef unsigned long uint64_t;
51 
52 typedef struct {
53 	uint64_t tsc;
54 	uint64_t id;
55 	union {
56 		struct {
57 			uint32_t a, b, c, d;
58 		};
59 		struct {
60 			uint8_t a1, a2, a3, a4;
61 			uint8_t b1, b2, b3, b4;
62 			uint8_t c1, c2, c3, c4;
63 			uint8_t d1, d2, d3, d4;
64 		};
65 		struct {
66 			uint64_t e;
67 			uint64_t f;
68 		};
69 		char str[16];
70 	};
71 } trace_ev_t;
72 
73 typedef struct {
74 	uint32_t devid;
75 	int exit_flag;
76 	int trace_fd;
77 	shared_buf_t *sbuf;
78 	pthread_mutex_t *sbuf_lock;
79 } param_t;
80 
81 typedef struct {
82 	int dev_fd;
83 	char dev_name[DEV_PATH_LEN];
84 	pthread_t thrd;
85 	param_t param;
86 } reader_struct;
87