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