ethdev: fix port id type
[dpdk.git] / lib / librte_ether / ethdev_profile.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <rte_config.h>
35
36 #include "ethdev_profile.h"
37
38 /**
39  * This conditional block enables RX queues profiling by tracking wasted
40  * iterations, i.e. iterations which yielded no RX packets. Profiling is
41  * performed using the Instrumentation and Tracing Technology (ITT) API,
42  * employed by the Intel (R) VTune (TM) Amplifier.
43  */
44 #ifdef RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS
45
46 #include <ittnotify.h>
47
48 #define ITT_MAX_NAME_LEN (100)
49
50 /**
51  * Auxiliary ITT structure belonging to Ethernet device and using to:
52  *   -  track RX queue state to determine whether it is wasting loop iterations
53  *   -  begin or end ITT task using task domain and task name (handle)
54  */
55 struct itt_profile_rx_data {
56         /**
57          * ITT domains for each queue.
58          */
59         __itt_domain *domains[RTE_MAX_QUEUES_PER_PORT];
60         /**
61          * ITT task names for each queue.
62          */
63         __itt_string_handle *handles[RTE_MAX_QUEUES_PER_PORT];
64         /**
65          * Flags indicating the queues state. Possible values:
66          *   1 - queue is wasting iterations,
67          *   0 - otherwise.
68          */
69         uint8_t queue_state[RTE_MAX_QUEUES_PER_PORT];
70 };
71
72 /**
73  * The pool of *itt_profile_rx_data* structures.
74  */
75 struct itt_profile_rx_data itt_rx_data[RTE_MAX_ETHPORTS];
76
77
78 /**
79  * This callback function manages ITT tasks collection on given port and queue.
80  * It must be registered with rte_eth_add_rx_callback() to be called from
81  * rte_eth_rx_burst(). To find more comments see rte_rx_callback_fn function
82  * type declaration.
83  */
84 static uint16_t
85 collect_itt_rx_burst_cb(uint16_t port_id, uint16_t queue_id,
86         __rte_unused struct rte_mbuf *pkts[], uint16_t nb_pkts,
87         __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
88 {
89         if (unlikely(nb_pkts == 0)) {
90                 if (!itt_rx_data[port_id].queue_state[queue_id]) {
91                         __itt_task_begin(
92                                 itt_rx_data[port_id].domains[queue_id],
93                                 __itt_null, __itt_null,
94                                 itt_rx_data[port_id].handles[queue_id]);
95                         itt_rx_data[port_id].queue_state[queue_id] = 1;
96                 }
97         } else {
98                 if (unlikely(itt_rx_data[port_id].queue_state[queue_id])) {
99                         __itt_task_end(
100                                 itt_rx_data[port_id].domains[queue_id]);
101                         itt_rx_data[port_id].queue_state[queue_id] = 0;
102                 }
103         }
104         return nb_pkts;
105 }
106
107 /**
108  * Initialization of itt_profile_rx_data for a given Ethernet device.
109  * This function must be invoked when ethernet device is being configured.
110  * Result will be stored in the global array *itt_rx_data*.
111  *
112  * @param port_id
113  *  The port identifier of the Ethernet device.
114  * @param port_name
115  *  The name of the Ethernet device.
116  * @param rx_queue_num
117  *  The number of RX queues on specified port.
118  *
119  * @return
120  *  - On success, zero.
121  *  - On failure, a negative value.
122  */
123 static inline int
124 itt_profile_rx_init(uint16_t port_id, char *port_name, uint8_t rx_queue_num)
125 {
126         uint16_t q_id;
127
128         for (q_id = 0; q_id < rx_queue_num; ++q_id) {
129                 char domain_name[ITT_MAX_NAME_LEN];
130
131                 snprintf(domain_name, sizeof(domain_name),
132                         "RXBurst.WastedIterations.Port_%s.Queue_%d",
133                         port_name, q_id);
134                 itt_rx_data[port_id].domains[q_id]
135                         = __itt_domain_create(domain_name);
136
137                 char task_name[ITT_MAX_NAME_LEN];
138
139                 snprintf(task_name, sizeof(task_name),
140                         "port id: %d; queue id: %d",
141                         port_id, q_id);
142                 itt_rx_data[port_id].handles[q_id]
143                         = __itt_string_handle_create(task_name);
144
145                 itt_rx_data[port_id].queue_state[q_id] = 0;
146
147                 if (!rte_eth_add_rx_callback(
148                         port_id, q_id, collect_itt_rx_burst_cb, NULL)) {
149                         return -rte_errno;
150                 }
151         }
152
153         return 0;
154 }
155 #endif /* RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS */
156
157 int
158 __rte_eth_profile_rx_init(__rte_unused uint16_t port_id,
159         __rte_unused struct rte_eth_dev *dev)
160 {
161 #ifdef RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS
162         return itt_profile_rx_init(
163                 port_id, dev->data->name, dev->data->nb_rx_queues);
164 #endif
165         return 0;
166 }