eed861e2280921a48500c89b215f372fb4e7b96d
[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 static int
122 trace_dir_update(const char *str)
123 {
124         struct trace *trace = trace_obj_get();
125         int rc, remaining;
126
127         remaining = sizeof(trace->dir) - trace->dir_offset;
128         rc = rte_strscpy(&trace->dir[0] + trace->dir_offset, str, remaining);
129         if (rc < 0)
130                 goto fail;
131
132         trace->dir_offset += rc;
133 fail:
134         return rc;
135 }
136
137 int
138 eal_trace_args_save(const char *val)
139 {
140         struct trace *trace = trace_obj_get();
141         struct trace_arg *arg = malloc(sizeof(*arg));
142
143         if (arg == NULL) {
144                 trace_err("failed to allocate memory for %s", val);
145                 return -ENOMEM;
146         }
147
148         arg->val = strdup(val);
149         if (arg->val == NULL) {
150                 trace_err("failed to allocate memory for %s", val);
151                 free(arg);
152                 return -ENOMEM;
153         }
154
155         STAILQ_INSERT_TAIL(&trace->args, arg, next);
156         return 0;
157 }
158
159 void
160 eal_trace_args_free(void)
161 {
162         struct trace *trace = trace_obj_get();
163         struct trace_arg *arg;
164
165         while (!STAILQ_EMPTY(&trace->args)) {
166                 arg = STAILQ_FIRST(&trace->args);
167                 STAILQ_REMOVE_HEAD(&trace->args, next);
168                 free(arg->val);
169                 free(arg);
170         }
171 }
172
173 int
174 trace_args_apply(const char *arg)
175 {
176         if (rte_trace_regexp(arg, true) < 0) {
177                 trace_err("cannot enable trace for %s", arg);
178                 return -1;
179         }
180
181         return 0;
182 }
183
184 int
185 eal_trace_bufsz_args_save(char const *val)
186 {
187         struct trace *trace = trace_obj_get();
188         uint64_t bufsz;
189
190         bufsz = rte_str_to_size(val);
191         if (bufsz == 0) {
192                 trace_err("buffer size cannot be zero");
193                 return -EINVAL;
194         }
195
196         trace->buff_len = bufsz;
197         return 0;
198 }
199
200 void
201 trace_bufsz_args_apply(void)
202 {
203         struct trace *trace = trace_obj_get();
204
205         if (trace->buff_len == 0)
206                 trace->buff_len = 1024 * 1024; /* 1MB */
207 }
208
209 int
210 eal_trace_mode_args_save(const char *val)
211 {
212         struct trace *trace = trace_obj_get();
213         size_t len = strlen(val);
214         unsigned long tmp;
215         char *pattern;
216
217         if (len == 0) {
218                 trace_err("value is not provided with option");
219                 return -EINVAL;
220         }
221
222         pattern = (char *)calloc(1, len + 2);
223         if (pattern == NULL) {
224                 trace_err("fail to allocate memory");
225                 return -ENOMEM;
226         }
227
228         sprintf(pattern, "%s*", val);
229
230         if (fnmatch(pattern, "overwrite", 0) == 0)
231                 tmp = RTE_TRACE_MODE_OVERWRITE;
232         else if (fnmatch(pattern, "discard", 0) == 0)
233                 tmp = RTE_TRACE_MODE_DISCARD;
234         else {
235                 free(pattern);
236                 return -EINVAL;
237         }
238
239         trace->mode = tmp;
240         free(pattern);
241         return 0;
242 }
243
244 int
245 eal_trace_dir_args_save(char const *val)
246 {
247         struct trace *trace = trace_obj_get();
248         uint32_t size = sizeof(trace->dir);
249         char *dir_path = NULL;
250         int rc;
251
252         if (strlen(val) >= size) {
253                 trace_err("input string is too big");
254                 return -ENAMETOOLONG;
255         }
256
257         dir_path = (char *)calloc(1, size);
258         if (dir_path == NULL) {
259                 trace_err("fail to allocate memory");
260                 return -ENOMEM;
261         }
262
263         sprintf(dir_path, "%s/", val);
264         rc = trace_dir_update(dir_path);
265
266         free(dir_path);
267         return rc;
268 }
269
270 int
271 trace_epoch_time_save(void)
272 {
273         struct trace *trace = trace_obj_get();
274         struct timespec epoch = { 0, 0 };
275         uint64_t avg, start, end;
276
277         start = rte_get_tsc_cycles();
278         if (clock_gettime(CLOCK_REALTIME, &epoch) < 0) {
279                 trace_err("failed to get the epoch time");
280                 return -1;
281         }
282         end = rte_get_tsc_cycles();
283         avg = (start + end) >> 1;
284
285         trace->epoch_sec = (uint64_t) epoch.tv_sec;
286         trace->epoch_nsec = (uint64_t) epoch.tv_nsec;
287         trace->uptime_ticks = avg;
288
289         return 0;
290 }
291
292 static int
293 trace_dir_default_path_get(char *dir_path)
294 {
295         struct trace *trace = trace_obj_get();
296         uint32_t size = sizeof(trace->dir);
297         struct passwd *pwd;
298         char *home_dir;
299
300         /* First check for shell environment variable */
301         home_dir = getenv("HOME");
302         if (home_dir == NULL) {
303                 /* Fallback to password file entry */
304                 pwd = getpwuid(getuid());
305                 if (pwd == NULL)
306                         return -EINVAL;
307
308                 home_dir = pwd->pw_dir;
309         }
310
311         /* Append dpdk-traces to directory */
312         if (snprintf(dir_path, size, "%s/dpdk-traces/", home_dir) < 0)
313                 return -ENAMETOOLONG;
314
315         return 0;
316 }
317
318 int
319 trace_mkdir(void)
320 {
321         struct trace *trace = trace_obj_get();
322         char session[TRACE_DIR_STR_LEN];
323         char *dir_path;
324         int rc;
325
326         if (!trace->dir_offset) {
327                 dir_path = calloc(1, sizeof(trace->dir));
328                 if (dir_path == NULL) {
329                         trace_err("fail to allocate memory");
330                         return -ENOMEM;
331                 }
332
333                 rc = trace_dir_default_path_get(dir_path);
334                 if (rc < 0) {
335                         trace_err("fail to get default path");
336                         free(dir_path);
337                         return rc;
338                 }
339
340                 rc = trace_dir_update(dir_path);
341                 free(dir_path);
342                 if (rc < 0)
343                         return rc;
344         }
345
346         /* Create the path if it t exist, no "mkdir -p" available here */
347         rc = mkdir(trace->dir, 0700);
348         if (rc < 0 && errno != EEXIST) {
349                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
350                 rte_errno = errno;
351                 return -rte_errno;
352         }
353
354         rc = trace_session_name_generate(session);
355         if (rc < 0)
356                 return rc;
357         rc = trace_dir_update(session);
358         if (rc < 0)
359                 return rc;
360
361         rc = mkdir(trace->dir, 0700);
362         if (rc < 0) {
363                 trace_err("mkdir %s failed [%s]", trace->dir, strerror(errno));
364                 rte_errno = errno;
365                 return -rte_errno;
366         }
367
368         RTE_LOG(INFO, EAL, "Trace dir: %s\n", trace->dir);
369         return 0;
370 }
371
372 static int
373 trace_meta_save(struct trace *trace)
374 {
375         char file_name[PATH_MAX];
376         FILE *f;
377         int rc;
378
379         rc = snprintf(file_name, PATH_MAX, "%s/metadata", trace->dir);
380         if (rc < 0)
381                 return rc;
382
383         f = fopen(file_name, "w");
384         if (f == NULL)
385                 return -errno;
386
387         rc = rte_trace_metadata_dump(f);
388
389         if (fclose(f))
390                 rc = -errno;
391
392         return rc;
393 }
394
395
396 static inline int
397 trace_file_sz(struct __rte_trace_header *hdr)
398 {
399         return sizeof(struct __rte_trace_stream_header) + hdr->offset;
400 }
401
402 static int
403 trace_mem_save(struct trace *trace, struct __rte_trace_header *hdr,
404                 uint32_t cnt)
405 {
406         char file_name[PATH_MAX];
407         FILE *f;
408         int rc;
409
410         rc = snprintf(file_name, PATH_MAX, "%s/channel0_%d", trace->dir, cnt);
411         if (rc < 0)
412                 return rc;
413
414         f = fopen(file_name, "w");
415         if (f == NULL)
416                 return -errno;
417
418         rc = fwrite(&hdr->stream_header, trace_file_sz(hdr), 1, f);
419         rc = (rc == 1) ?  0 : -EACCES;
420
421         if (fclose(f))
422                 rc = -errno;
423
424         return rc;
425 }
426
427 int
428 rte_trace_save(void)
429 {
430         struct trace *trace = trace_obj_get();
431         struct __rte_trace_header *header;
432         uint32_t count;
433         int rc = 0;
434
435         if (trace->nb_trace_mem_list == 0)
436                 return rc;
437
438         rc = trace_meta_save(trace);
439         if (rc)
440                 return rc;
441
442         rte_spinlock_lock(&trace->lock);
443         for (count = 0; count < trace->nb_trace_mem_list; count++) {
444                 header = trace->lcore_meta[count].mem;
445                 rc =  trace_mem_save(trace, header, count);
446                 if (rc)
447                         break;
448         }
449         rte_spinlock_unlock(&trace->lock);
450         return rc;
451 }