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