New upstream version 17.11-rc3
[deb_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 "ethdev_profile.h"
35
36 /**
37  * This conditional block enables RX queues profiling by tracking wasted
38  * iterations, i.e. iterations which yielded no RX packets. Profiling is
39  * performed using the Instrumentation and Tracing Technology (ITT) API,
40  * employed by the Intel (R) VTune (TM) Amplifier.
41  */
42 #ifdef RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS
43
44 #include <ittnotify.h>
45
46 #define ITT_MAX_NAME_LEN (100)
47
48 /**
49  * Auxiliary ITT structure belonging to Ethernet device and using to:
50  *   -  track RX queue state to determine whether it is wasting loop iterations
51  *   -  begin or end ITT task using task domain and task name (handle)
52  */
53 struct itt_profile_rx_data {
54         /**
55          * ITT domains for each queue.
56          */
57         __itt_domain *domains[RTE_MAX_QUEUES_PER_PORT];
58         /**
59          * ITT task names for each queue.
60          */
61         __itt_string_handle *handles[RTE_MAX_QUEUES_PER_PORT];
62         /**
63          * Flags indicating the queues state. Possible values:
64          *   1 - queue is wasting iterations,
65          *   0 - otherwise.
66          */
67         uint8_t queue_state[RTE_MAX_QUEUES_PER_PORT];
68 };
69
70 /**
71  * The pool of *itt_profile_rx_data* structures.
72  */
73 struct itt_profile_rx_data itt_rx_data[RTE_MAX_ETHPORTS];
74
75
76 /**
77  * This callback function manages ITT tasks collection on given port and queue.
78  * It must be registered with rte_eth_add_rx_callback() to be called from
79  * rte_eth_rx_burst(). To find more comments see rte_rx_callback_fn function
80  * type declaration.
81  */
82 static uint16_t
83 collect_itt_rx_burst_cb(uint16_t port_id, uint16_t queue_id,
84         __rte_unused struct rte_mbuf *pkts[], uint16_t nb_pkts,
85         __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
86 {
87         if (unlikely(nb_pkts == 0)) {
88                 if (!itt_rx_data[port_id].queue_state[queue_id]) {
89                         __itt_task_begin(
90                                 itt_rx_data[port_id].domains[queue_id],
91                                 __itt_null, __itt_null,
92                                 itt_rx_data[port_id].handles[queue_id]);
93                         itt_rx_data[port_id].queue_state[queue_id] = 1;
94                 }
95         } else {
96                 if (unlikely(itt_rx_data[port_id].queue_state[queue_id])) {
97                         __itt_task_end(
98                                 itt_rx_data[port_id].domains[queue_id]);
99                         itt_rx_data[port_id].queue_state[queue_id] = 0;
100                 }
101         }
102         return nb_pkts;
103 }
104
105 /**
106  * Initialization of itt_profile_rx_data for a given Ethernet device.
107  * This function must be invoked when ethernet device is being configured.
108  * Result will be stored in the global array *itt_rx_data*.
109  *
110  * @param port_id
111  *  The port identifier of the Ethernet device.
112  * @param port_name
113  *  The name of the Ethernet device.
114  * @param rx_queue_num
115  *  The number of RX queues on specified port.
116  *
117  * @return
118  *  - On success, zero.
119  *  - On failure, a negative value.
120  */
121 static inline int
122 itt_profile_rx_init(uint16_t port_id, char *port_name, uint8_t rx_queue_num)
123 {
124         uint16_t q_id;
125
126         for (q_id = 0; q_id < rx_queue_num; ++q_id) {
127                 char domain_name[ITT_MAX_NAME_LEN];
128
129                 snprintf(domain_name, sizeof(domain_name),
130                         "RXBurst.WastedIterations.Port_%s.Queue_%d",
131                         port_name, q_id);
132                 itt_rx_data[port_id].domains[q_id]
133                         = __itt_domain_create(domain_name);
134
135                 char task_name[ITT_MAX_NAME_LEN];
136
137                 snprintf(task_name, sizeof(task_name),
138                         "port id: %d; queue id: %d",
139                         port_id, q_id);
140                 itt_rx_data[port_id].handles[q_id]
141                         = __itt_string_handle_create(task_name);
142
143                 itt_rx_data[port_id].queue_state[q_id] = 0;
144
145                 if (!rte_eth_add_rx_callback(
146                         port_id, q_id, collect_itt_rx_burst_cb, NULL)) {
147                         return -rte_errno;
148                 }
149         }
150
151         return 0;
152 }
153 #endif /* RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS */
154
155 int
156 __rte_eth_profile_rx_init(__rte_unused uint16_t port_id,
157         __rte_unused struct rte_eth_dev *dev)
158 {
159 #ifdef RTE_ETHDEV_PROFILE_ITT_WASTED_RX_ITERATIONS
160         return itt_profile_rx_init(
161                 port_id, dev->data->name, dev->data->nb_rx_queues);
162 #endif
163         return 0;
164 }