Merge "Initial support for DPDK 18.05"
[samplevnf.git] / VNFs / DPPD-PROX / stats_task.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef _STATS_TASK_H_
18 #define _STATS_TASK_H_
19
20 #include <inttypes.h>
21
22 #include "clock.h"
23
24 /* The struct task_stats is read/write from the task itself and
25    read-only from the core that collects the stats. Since only the
26    task executing the actual work ever modifies the stats, no locking
27    is required. Both a read and a write are atomic (assuming the
28    correct alignment). From this, it followed that the statistics can
29    be incremented directly by the task itself. In cases where these
30    assumptions do not hold, a possible solution (although slightly
31    less accurate) would be to keep accumulate statistics temporarily
32    in a separate structure and periodically copying the statistics to
33    the statistics core through atomic primitives, for example through
34    rte_atomic32_set(). The accuracy would be determined by the
35    frequency at which the statistics are transferred to the statistics
36    core. */
37
38 struct task_rt_stats {
39         uint32_t        rx_pkt_count;
40         uint32_t        tx_pkt_count;
41         uint32_t        drop_tx_fail;
42         uint32_t        drop_discard;
43         uint32_t        drop_handled;
44         uint32_t        idle_cycles;
45         uint64_t        rx_bytes;
46         uint64_t        tx_bytes;
47         uint64_t        drop_bytes;
48 } __attribute__((packed)) __rte_cache_aligned;
49
50 #ifdef PROX_STATS
51 #define TASK_STATS_ADD_IDLE(stats, cycles) do {                         \
52                 (stats)->idle_cycles += (cycles) + rdtsc_overhead_stats; \
53         } while(0)                                                      \
54
55 #define TASK_STATS_ADD_TX(stats, ntx) do {      \
56                 (stats)->tx_pkt_count += ntx;   \
57         } while(0)                              \
58
59 #define TASK_STATS_ADD_DROP_TX_FAIL(stats, ntx) do {    \
60                 (stats)->drop_tx_fail += ntx;           \
61         } while(0)                                      \
62
63 #define TASK_STATS_ADD_DROP_HANDLED(stats, ntx) do {    \
64                 (stats)->drop_handled += ntx;           \
65         } while(0)                                      \
66
67 #define TASK_STATS_ADD_DROP_DISCARD(stats, ntx) do {    \
68                 (stats)->drop_discard += ntx;           \
69         } while(0)                                      \
70
71 #define TASK_STATS_ADD_RX(stats, ntx) do {      \
72                 (stats)->rx_pkt_count += ntx;   \
73         } while (0)                             \
74
75 #define TASK_STATS_ADD_RX_BYTES(stats, bytes) do {      \
76                 (stats)->rx_bytes += bytes;             \
77         } while (0)                                     \
78
79 #define TASK_STATS_ADD_TX_BYTES(stats, bytes) do {      \
80                 (stats)->tx_bytes += bytes;             \
81         } while (0)                                     \
82
83 #define TASK_STATS_ADD_DROP_BYTES(stats, bytes) do {    \
84                 (stats)->drop_bytes += bytes;           \
85         } while (0)                                     \
86
87 #define START_EMPTY_MEASSURE() uint64_t cur_tsc = rte_rdtsc();
88 #else
89 #define TASK_STATS_ADD_IDLE(stats, cycles) do {} while(0)
90 #define TASK_STATS_ADD_TX(stats, ntx)  do {} while(0)
91 #define TASK_STATS_ADD_DROP_TX_FAIL(stats, ntx)  do {} while(0)
92 #define TASK_STATS_ADD_DROP_HANDLED(stats, ntx)  do {} while(0)
93 #define TASK_STATS_ADD_DROP_DISCARD(stats, ntx)  do {} while(0)
94 #define TASK_STATS_ADD_RX(stats, ntx)  do {} while(0)
95 #define TASK_STATS_ADD_RX_BYTES(stats, bytes)  do {} while(0)
96 #define TASK_STATS_ADD_TX_BYTES(stats, bytes)  do {} while(0)
97 #define TASK_STATS_ADD_DROP_BYTES(stats, bytes) do {} while(0)
98 #define START_EMPTY_MEASSURE()  do {} while(0)
99 #endif
100
101 struct task_stats_sample {
102         uint64_t tsc;
103         uint32_t tx_pkt_count;
104         uint32_t drop_tx_fail;
105         uint32_t drop_discard;
106         uint32_t drop_handled;
107         uint32_t rx_pkt_count;
108         uint32_t empty_cycles;
109         uint64_t rx_bytes;
110         uint64_t tx_bytes;
111         uint64_t drop_bytes;
112 };
113
114 struct task_stats {
115         uint64_t tot_tx_pkt_count;
116         uint64_t tot_drop_tx_fail;
117         uint64_t tot_drop_discard;
118         uint64_t tot_drop_handled;
119         uint64_t tot_rx_pkt_count;
120
121         struct task_stats_sample sample[2];
122
123         struct task_rt_stats *stats;
124         /* flags set if total RX/TX values need to be reported set at
125            initialization time, only need to access stats values in port */
126         uint8_t flags;
127 };
128
129 void stats_task_reset(void);
130 void stats_task_post_proc(void);
131 void stats_task_update(void);
132 void stats_task_init(void);
133
134 int stats_get_n_tasks_tot(void);
135
136 struct task_stats *stats_get_task_stats(uint32_t lcore_id, uint32_t task_id);
137 struct task_stats_sample *stats_get_task_stats_sample(uint32_t lcore_id, uint32_t task_id, int last);
138 void stats_task_get_host_rx_tx_packets(uint64_t *rx, uint64_t *tx, uint64_t *tsc);
139
140 uint64_t stats_core_task_tot_rx(uint8_t lcore_id, uint8_t task_id);
141 uint64_t stats_core_task_tot_tx(uint8_t lcore_id, uint8_t task_id);
142 uint64_t stats_core_task_tot_drop(uint8_t lcore_id, uint8_t task_id);
143 uint64_t stats_core_task_last_tsc(uint8_t lcore_id, uint8_t task_id);
144
145 #endif /* _STATS_TASK_H_ */