trace: implement register API
[dpdk.git] / lib / librte_eal / common / eal_common_trace.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6 #include <sys/queue.h>
7
8 #include <rte_common.h>
9 #include <rte_errno.h>
10 #include <rte_lcore.h>
11 #include <rte_per_lcore.h>
12 #include <rte_string_fns.h>
13
14 #include "eal_trace.h"
15
16 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
17 static RTE_DEFINE_PER_LCORE(char, ctf_field[TRACE_CTF_FIELD_SIZE]);
18 static RTE_DEFINE_PER_LCORE(int, ctf_count);
19
20 static struct trace_point_head tp_list = STAILQ_HEAD_INITIALIZER(tp_list);
21 static struct trace trace;
22
23 int
24 __rte_trace_point_register(rte_trace_point_t *handle, const char *name,
25                 void (*register_fn)(void))
26 {
27         char *field = RTE_PER_LCORE(ctf_field);
28         struct trace_point *tp;
29         uint16_t sz;
30
31         /* Sanity checks of arguments */
32         if (name == NULL || register_fn == NULL || handle == NULL) {
33                 trace_err("invalid arguments");
34                 rte_errno = EINVAL;
35                 goto fail;
36         }
37
38         /* Check the size of the trace point object */
39         RTE_PER_LCORE(trace_point_sz) = 0;
40         RTE_PER_LCORE(ctf_count) = 0;
41         register_fn();
42         if (RTE_PER_LCORE(trace_point_sz) == 0) {
43                 trace_err("missing rte_trace_emit_header() in register fn");
44                 rte_errno = EBADF;
45                 goto fail;
46         }
47
48         /* Is size overflowed */
49         if (RTE_PER_LCORE(trace_point_sz) > UINT16_MAX) {
50                 trace_err("trace point size overflowed");
51                 rte_errno = ENOSPC;
52                 goto fail;
53         }
54
55         /* Are we running out of space to store trace points? */
56         if (trace.nb_trace_points > UINT16_MAX) {
57                 trace_err("trace point exceeds the max count");
58                 rte_errno = ENOSPC;
59                 goto fail;
60         }
61
62         /* Get the size of the trace point */
63         sz = RTE_PER_LCORE(trace_point_sz);
64         tp = calloc(1, sizeof(struct trace_point));
65         if (tp == NULL) {
66                 trace_err("fail to allocate trace point memory");
67                 rte_errno = ENOMEM;
68                 goto fail;
69         }
70
71         /* Initialize the trace point */
72         if (rte_strscpy(tp->name, name, TRACE_POINT_NAME_SIZE) < 0) {
73                 trace_err("name is too long");
74                 rte_errno = E2BIG;
75                 goto free;
76         }
77
78         /* Copy the field data for future use */
79         if (rte_strscpy(tp->ctf_field, field, TRACE_CTF_FIELD_SIZE) < 0) {
80                 trace_err("CTF field size is too long");
81                 rte_errno = E2BIG;
82                 goto free;
83         }
84
85         /* Clear field memory for the next event */
86         memset(field, 0, TRACE_CTF_FIELD_SIZE);
87
88         /* Form the trace handle */
89         *handle = sz;
90         *handle |= trace.nb_trace_points << __RTE_TRACE_FIELD_ID_SHIFT;
91
92         trace.nb_trace_points++;
93         tp->handle = handle;
94
95         /* Add the trace point at tail */
96         STAILQ_INSERT_TAIL(&tp_list, tp, next);
97         __atomic_thread_fence(__ATOMIC_RELEASE);
98
99         /* All Good !!! */
100         return 0;
101 free:
102         free(tp);
103 fail:
104         if (trace.register_errno == 0)
105                 trace.register_errno = rte_errno;
106
107         return -rte_errno;
108 }