Merge "[l2l3 stack] implements new nd state machine & nd buffering"
[samplevnf.git] / VNFs / DPPD-PROX / stats_prio.c
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 #include <stddef.h>
18
19 #include "handle_aggregator.h"
20 #include "stats_prio_task.h"
21 #include "prox_cfg.h"
22 #include "prox_globals.h"
23 #include "lconf.h"
24
25 struct lcore_task_stats {
26         struct task_stats task_stats[MAX_TASKS_PER_CORE];
27 };
28
29 struct lcore_prio_task_stats {
30         struct prio_task_stats prio_task_stats[MAX_TASKS_PER_CORE];
31 };
32
33 extern int last_stat;
34 static struct prio_task_stats   prio_task_stats_set[RTE_MAX_LCORE * MAX_TASKS_PER_CORE];
35 static uint8_t nb_prio_tasks_tot;
36
37 int stats_get_n_prio_tasks_tot(void)
38 {
39         return nb_prio_tasks_tot;
40 }
41
42 struct prio_task_stats_sample *stats_get_prio_task_stats_sample(uint32_t prio_task_id, int l)
43 {
44         return &prio_task_stats_set[prio_task_id].sample[l == last_stat];
45 }
46
47 struct prio_task_stats_sample *stats_get_prio_task_stats_sample_by_core_task(uint32_t lcore_id, uint32_t prio_task_id, int l)
48 {
49         for (uint8_t task_id = 0; task_id < nb_prio_tasks_tot; ++task_id) {
50                 if ((prio_task_stats_set[task_id].lcore_id == lcore_id) && (prio_task_stats_set[task_id].task_id == task_id))
51                         return &prio_task_stats_set[prio_task_id].sample[l == last_stat];
52         }
53         return NULL;
54 }
55
56 void stats_prio_task_reset(void)
57 {
58         struct prio_task_stats *cur_task_stats;
59
60         for (uint8_t task_id = 0; task_id < nb_prio_tasks_tot; ++task_id) {
61                 cur_task_stats = &prio_task_stats_set[task_id];
62                 for (int i = 0; i < 8; i++) {
63                         cur_task_stats->tot_drop_tx_fail_prio[i] = 0;
64                         cur_task_stats->tot_rx_prio[i] = 0;
65                 }
66         }
67 }
68
69 uint64_t stats_core_task_tot_drop_tx_fail_prio(uint8_t prio_task_id, uint8_t prio)
70 {
71         return prio_task_stats_set[prio_task_id].tot_drop_tx_fail_prio[prio];
72 }
73
74 uint64_t stats_core_task_tot_rx_prio(uint8_t prio_task_id, uint8_t prio)
75 {
76         return prio_task_stats_set[prio_task_id].tot_rx_prio[prio];
77 }
78
79 void stats_prio_task_post_proc(void)
80 {
81         for (uint8_t task_id = 0; task_id < nb_prio_tasks_tot; ++task_id) {
82                 struct prio_task_stats *cur_task_stats = &prio_task_stats_set[task_id];
83                 const struct prio_task_stats_sample *last = &cur_task_stats->sample[last_stat];
84                 const struct prio_task_stats_sample *prev = &cur_task_stats->sample[!last_stat];
85
86                 for (int i=0; i<8; i++) {
87                         cur_task_stats->tot_rx_prio[i] += last->rx_prio[i] - prev->rx_prio[i];
88                         cur_task_stats->tot_drop_tx_fail_prio[i] += last->drop_tx_fail_prio[i] - prev->drop_tx_fail_prio[i];
89                 }
90         }
91 }
92
93 void stats_prio_task_update(void)
94 {
95         uint64_t before, after;
96
97         for (uint8_t task_id = 0; task_id < nb_prio_tasks_tot; ++task_id) {
98                 struct prio_task_stats *cur_task_stats = &prio_task_stats_set[task_id];
99                 struct prio_task_rt_stats *stats = cur_task_stats->stats;
100                 struct prio_task_stats_sample *last = &cur_task_stats->sample[last_stat];
101
102                 before = rte_rdtsc();
103                 for (int i=0; i<8; i++) {
104                         last->drop_tx_fail_prio[i] = stats->drop_tx_fail_prio[i];
105                         last->rx_prio[i] = stats->rx_prio[i];
106                 }
107                 after = rte_rdtsc();
108                 last->tsc = (before >> 1) + (after >> 1);
109         }
110 }
111
112 void stats_prio_task_init(void)
113 {
114         struct lcore_cfg *lconf;
115         uint32_t lcore_id;
116
117         /* add cores that are receiving from and sending to physical ports first */
118         lcore_id = -1;
119         while(prox_core_next(&lcore_id, 0) == 0) {
120                 lconf = &lcore_cfg[lcore_id];
121                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
122                         struct task_args *targ = &lconf->targs[task_id];
123                         if (strcmp(targ->task_init->mode_str, "aggreg") == 0) {
124                                 struct prio_task_rt_stats *stats = &((struct task_aggregator *)(lconf->tasks_all[task_id]))->stats;
125                                 prio_task_stats_set[nb_prio_tasks_tot].stats = stats;
126                                 prio_task_stats_set[nb_prio_tasks_tot].lcore_id = lcore_id;
127                                 prio_task_stats_set[nb_prio_tasks_tot++].task_id = task_id;
128                         }
129                 }
130         }
131 }