7c86a6648ea0ef5c9e57d6aa6bad88c594c4f8cd
[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 const char *
18 trace_mode_to_string(enum rte_trace_mode mode)
19 {
20         switch (mode) {
21         case RTE_TRACE_MODE_OVERWRITE: return "overwrite";
22         case RTE_TRACE_MODE_DISCARD: return "discard";
23         default: return "unknown";
24         }
25 }
26
27 const char *
28 trace_area_to_string(enum trace_area_e area)
29 {
30         switch (area) {
31         case TRACE_AREA_HEAP: return "heap";
32         case TRACE_AREA_HUGEPAGE: return "hugepage";
33         default: return "unknown";
34         }
35 }
36
37 static bool
38 trace_entry_compare(const char *name)
39 {
40         struct trace_point_head *tp_list = trace_list_head_get();
41         struct trace_point *tp;
42         int count = 0;
43
44         STAILQ_FOREACH(tp, tp_list, next) {
45                 if (strncmp(tp->name, name, TRACE_POINT_NAME_SIZE) == 0)
46                         count++;
47                 if (count > 1) {
48                         trace_err("found duplicate entry %s", name);
49                         rte_errno = EEXIST;
50                         return true;
51                 }
52         }
53         return false;
54 }
55
56 bool
57 trace_has_duplicate_entry(void)
58 {
59         struct trace_point_head *tp_list = trace_list_head_get();
60         struct trace_point *tp;
61
62         /* Is duplicate trace name registered */
63         STAILQ_FOREACH(tp, tp_list, next)
64                 if (trace_entry_compare(tp->name))
65                         return true;
66
67         return false;
68 }
69
70 void
71 trace_uuid_generate(void)
72 {
73         struct trace_point_head *tp_list = trace_list_head_get();
74         struct trace *trace = trace_obj_get();
75         struct trace_point *tp;
76         uint64_t sz_total = 0;
77
78         /* Go over the registered trace points to get total size of events */
79         STAILQ_FOREACH(tp, tp_list, next) {
80                 const uint16_t sz = *tp->handle & __RTE_TRACE_FIELD_SIZE_MASK;
81                 sz_total += sz;
82         }
83
84         rte_uuid_t uuid = RTE_UUID_INIT(sz_total, trace->nb_trace_points,
85                 0x4370, 0x8f50, 0x222ddd514176ULL);
86         rte_uuid_copy(trace->uuid, uuid);
87 }
88
89 static int
90 trace_session_name_generate(char *trace_dir)
91 {
92         struct tm *tm_result;
93         time_t tm;
94         int rc;
95
96         tm = time(NULL);
97         if ((int)tm == -1)
98                 goto fail;
99
100         tm_result = localtime(&tm);
101         if (tm_result == NULL)
102                 goto fail;
103
104         rc = rte_strscpy(trace_dir, eal_get_hugefile_prefix(),
105                         TRACE_PREFIX_LEN);
106         if (rc == -E2BIG)
107                 rc = TRACE_PREFIX_LEN;
108         trace_dir[rc++] = '-';
109
110         rc = strftime(trace_dir + rc, TRACE_DIR_STR_LEN - rc,
111                         "%Y-%m-%d-%p-%I-%M-%S", tm_result);
112         if (rc == 0)
113                 goto fail;
114
115         return rc;
116 fail:
117         rte_errno = errno;
118         return -rte_errno;
119 }
120
121 int
122 eal_trace_args_save(const char *optarg)
123 {
124         struct trace *trace = trace_obj_get();
125         char *trace_args;
126         uint8_t nb_args;
127
128         nb_args = trace->args.nb_args;
129
130         if (nb_args >= TRACE_MAX_ARGS) {
131                 trace_err("ignoring trace %s as limit exceeds", optarg);
132                 return 0;
133         }
134
135         trace_args = calloc(1, (strlen(optarg) + 1));
136         if (trace_args == NULL) {
137                 trace_err("fail to allocate memory for %s", optarg);
138                 return -ENOMEM;
139         }
140
141         memcpy(trace_args, optarg, strlen(optarg));
142         trace->args.args[nb_args++] = trace_args;
143         trace->args.nb_args = nb_args;
144         return 0;
145 }
146
147 void
148 eal_trace_args_free(void)
149 {
150         struct trace *trace = trace_obj_get();
151         int i;
152
153         for (i = 0; i < trace->args.nb_args; i++) {
154                 if (trace->args.args[i]) {
155                         free((void *)trace->args.args[i]);
156                         trace->args.args[i] = NULL;
157                 }
158         }
159 }
160
161 int
162 trace_args_apply(const char *arg)
163 {
164         char *str;
165
166         str = strdup(arg);
167         if (str == NULL)
168                 return -1;
169
170         if (rte_trace_regexp(str, true) < 0) {
171                 trace_err("cannot enable trace for %s", str);
172                 free(str);
173                 return -1;
174         }
175
176         free(str);
177         return 0;
178 }
179
180 int
181 trace_epoch_time_save(void)
182 {
183         struct trace *trace = trace_obj_get();
184         struct timespec epoch = { 0, 0 };
185         uint64_t avg, start, end;
186
187         start = rte_get_tsc_cycles();
188         if (clock_gettime(CLOCK_REALTIME, &epoch) < 0) {
189                 trace_err("failed to get the epoch time");
190                 return -1;
191         }
192         end = rte_get_tsc_cycles();
193         avg = (start + end) >> 1;
194
195         trace->epoch_sec = (uint64_t) epoch.tv_sec;
196         trace->epoch_nsec = (uint64_t) epoch.tv_nsec;
197         trace->uptime_ticks = avg;
198
199         return 0;
200 }
201
202 static int
203 trace_dir_default_path_get(char *dir_path)
204 {
205         struct trace *trace = trace_obj_get();
206         uint32_t size = sizeof(trace->dir);
207         struct passwd *pwd;
208         char *home_dir;
209
210         /* First check for shell environment variable */
211         home_dir = getenv("HOME");
212         if (home_dir == NULL) {
213                 /* Fallback to password file entry */
214                 pwd = getpwuid(getuid());
215                 if (pwd == NULL)
216                         return -EINVAL;
217
218                 home_dir = pwd->pw_dir;
219         }
220
221         /* Append dpdk-traces to directory */
222         if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
223                 return -ENAMETOOLONG;
224
225         return 0;
226 }
227
228 int
229 trace_mkdir(void)
230 {
231         struct trace *trace = trace_obj_get();
232         char session[TRACE_DIR_STR_LEN];
233         char *dir_path;
234         int rc;
235
236         if (!trace->dir_offset) {
237                 dir_path = calloc(1, sizeof(trace->dir));
238                 if (dir_path == NULL) {
239                         trace_err("fail to allocate memory");
240                         return -ENOMEM;
241                 }
242
243                 rc = trace_dir_default_path_get(dir_path);
244                 if (rc < 0) {
245                         trace_err("fail to get default path");
246                         free(dir_path);
247                         return rc;
248                 }
249
250         }
251
252         /* Create the path if it t exist, no "mkdir -p" available here */
253         rc = mkdir(trace->dir, 0700);
254         if (rc < 0 && errno != EEXIST) {
255                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
256                 rte_errno = errno;
257                 return -rte_errno;
258         }
259
260         rc = trace_session_name_generate(session);
261         if (rc < 0)
262                 return rc;
263
264         rc = mkdir(trace->dir, 0700);
265         if (rc < 0) {
266                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
267                 rte_errno = errno;
268                 return -rte_errno;
269         }
270
271         RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
272         return 0;
273 }
274
275 static int
276 trace_meta_save(struct trace *trace)
277 {
278         char file_name[PATH_MAX];
279         FILE *f;
280         int rc;
281
282         rc = snprintf(file_name, PATH_MAX, "%s/metadata", trace->dir);
283         if (rc < 0)
284                 return rc;
285
286         f = fopen(file_name, "w");
287         if (f == NULL)
288                 return -errno;
289
290         rc = rte_trace_metadata_dump(f);
291
292         if (fclose(f))
293                 rc = -errno;
294
295         return rc;
296 }
297
298
299 static inline int
300 trace_file_sz(struct __rte_trace_header *hdr)
301 {
302         return sizeof(struct __rte_trace_stream_header) + hdr->offset;
303 }
304
305 static int
306 trace_mem_save(struct trace *trace, struct __rte_trace_header *hdr,
307                 uint32_t cnt)
308 {
309         char file_name[PATH_MAX];
310         FILE *f;
311         int rc;
312
313         rc = snprintf(file_name, PATH_MAX, "%s/channel0_%d", trace->dir, cnt);
314         if (rc < 0)
315                 return rc;
316
317         f = fopen(file_name, "w");
318         if (f == NULL)
319                 return -errno;
320
321         rc = fwrite(&hdr->stream_header, trace_file_sz(hdr), 1, f);
322         rc = (rc == 1) ?  0 : -EACCES;
323
324         if (fclose(f))
325                 rc = -errno;
326
327         return rc;
328 }
329
330 int
331 rte_trace_save(void)
332 {
333         struct trace *trace = trace_obj_get();
334         struct __rte_trace_header *header;
335         uint32_t count;
336         int rc = 0;
337
338         if (trace->nb_trace_mem_list == 0)
339                 return rc;
340
341         rc = trace_meta_save(trace);
342         if (rc)
343                 return rc;
344
345         rte_spinlock_lock(&trace->lock);
346         for (count = 0; count < trace->nb_trace_mem_list; count++) {
347                 header = trace->lcore_meta[count].mem;
348                 rc =  trace_mem_save(trace, header, count);
349                 if (rc)
350                         break;
351         }
352         rte_spinlock_unlock(&trace->lock);
353         return rc;
354 }