trace: add internal init and fini interface
[dpdk.git] / lib / librte_eal / common / eal_common_trace_utils.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #include <fnmatch.h>
6 #include <pwd.h>
7 #include <sys/stat.h>
8 #include <time.h>
9
10 #include <rte_common.h>
11 #include <rte_errno.h>
12 #include <rte_string_fns.h>
13
14 #include "eal_filesystem.h"
15 #include "eal_trace.h"
16
17 static bool
18 trace_entry_compare(const char *name)
19 {
20         struct trace_point_head *tp_list = trace_list_head_get();
21         struct trace_point *tp;
22         int count = 0;
23
24         STAILQ_FOREACH(tp, tp_list, next) {
25                 if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
26                         count++;
27                 if (count > 1) {
28                         trace_err("found duplicate entry %s", name);
29                         rte_errno = EEXIST;
30                         return true;
31                 }
32         }
33         return false;
34 }
35
36 bool
37 trace_has_duplicate_entry(void)
38 {
39         struct trace_point_head *tp_list = trace_list_head_get();
40         struct trace_point *tp;
41
42         /* Is duplicate trace name registered */
43         STAILQ_FOREACH(tp, tp_list, next)
44                 if (trace_entry_compare(tp->name))
45                         return true;
46
47         return false;
48 }
49
50 void
51 trace_uuid_generate(void)
52 {
53         struct trace_point_head *tp_list = trace_list_head_get();
54         struct trace *trace = trace_obj_get();
55         struct trace_point *tp;
56         uint64_t sz_total = 0;
57
58         /* Go over the registered trace points to get total size of events */
59         STAILQ_FOREACH(tp, tp_list, next) {
60                 const uint16_t sz = *tp->handle & __RTE_TRACE_FIELD_SIZE_MASK;
61                 sz_total += sz;
62         }
63
64         rte_uuid_t uuid = RTE_UUID_INIT(sz_total, trace->nb_trace_points,
65                 0x4370, 0x8f50, 0x222ddd514176ULL);
66         rte_uuid_copy(trace->uuid, uuid);
67 }
68
69 static int
70 trace_session_name_generate(char *trace_dir)
71 {
72         struct tm *tm_result;
73         time_t tm;
74         int rc;
75
76         tm = time(NULL);
77         if ((int)tm == -1)
78                 goto fail;
79
80         tm_result = localtime(&tm);
81         if (tm_result == NULL)
82                 goto fail;
83
84         rc = rte_strscpy(trace_dir, eal_get_hugefile_prefix(),
85                         TRACE_PREFIX_LEN);
86         if (rc == -E2BIG)
87                 rc = TRACE_PREFIX_LEN;
88         trace_dir[rc++] = '-';
89
90         rc = strftime(trace_dir + rc, TRACE_DIR_STR_LEN - rc,
91                         "%Y-%m-%d-%p-%I-%M-%S", tm_result);
92         if (rc == 0)
93                 goto fail;
94
95         return rc;
96 fail:
97         rte_errno = errno;
98         return -rte_errno;
99 }
100
101 static int
102 trace_dir_default_path_get(char *dir_path)
103 {
104         struct trace *trace = trace_obj_get();
105         uint32_t size = sizeof(trace->dir);
106         struct passwd *pwd;
107         char *home_dir;
108
109         /* First check for shell environment variable */
110         home_dir = getenv("HOME");
111         if (home_dir == NULL) {
112                 /* Fallback to password file entry */
113                 pwd = getpwuid(getuid());
114                 if (pwd == NULL)
115                         return -EINVAL;
116
117                 home_dir = pwd->pw_dir;
118         }
119
120         /* Append dpdk-traces to directory */
121         if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
122                 return -ENAMETOOLONG;
123
124         return 0;
125 }
126
127 int
128 trace_mkdir(void)
129 {
130         struct trace *trace = trace_obj_get();
131         char session[TRACE_DIR_STR_LEN];
132         char *dir_path;
133         int rc;
134
135         if (!trace->dir_offset) {
136                 dir_path = calloc(1, sizeof(trace->dir));
137                 if (dir_path == NULL) {
138                         trace_err("fail to allocate memory");
139                         return -ENOMEM;
140                 }
141
142                 rc = trace_dir_default_path_get(dir_path);
143                 if (rc < 0) {
144                         trace_err("fail to get default path");
145                         free(dir_path);
146                         return rc;
147                 }
148
149         }
150
151         /* Create the path if it t exist, no "mkdir -p" available here */
152         rc = mkdir(trace->dir, 0700);
153         if (rc < 0 && errno != EEXIST) {
154                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
155                 rte_errno = errno;
156                 return -rte_errno;
157         }
158
159         rc = trace_session_name_generate(session);
160         if (rc < 0)
161                 return rc;
162
163         rc = mkdir(trace->dir, 0700);
164         if (rc < 0) {
165                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
166                 rte_errno = errno;
167                 return -rte_errno;
168         }
169
170         RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
171         return 0;
172 }
173