Support packets in flight
[samplevnf.git] / VNFs / DPPD-PROX / stats_l4gen.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 <string.h>
18
19 #include "prox_malloc.h"
20 #include "prox_cfg.h"
21 #include "stats_l4gen.h"
22 #include "task_init.h"
23
24 struct task_l4gen_stats {
25         struct task_base base;
26         struct l4_stats l4_stats;
27 };
28
29 struct stats_l4gen_manager {
30         uint16_t n_l4gen;
31         struct task_l4_stats task_l4_stats[0];
32 };
33
34 extern int last_stat;
35 static struct stats_l4gen_manager *sl4m;
36
37 int stats_get_n_l4gen(void)
38 {
39         return sl4m->n_l4gen;
40 }
41
42 struct task_l4_stats *stats_get_l4_stats(uint32_t i)
43 {
44         return &sl4m->task_l4_stats[i];
45 }
46
47 struct l4_stats_sample *stats_get_l4_stats_sample(uint32_t i, int l)
48 {
49         return &sl4m->task_l4_stats[i].sample[l == last_stat];
50 }
51
52 static struct stats_l4gen_manager *alloc_stats_l4gen_manager(void)
53 {
54         struct lcore_cfg *lconf;
55         uint32_t lcore_id = -1;
56         size_t mem_size;
57         uint32_t n_l4gen = 0;
58         const int socket_id = rte_lcore_to_socket_id(rte_lcore_id());
59
60         lcore_id = -1;
61         while (prox_core_next(&lcore_id, 0) == 0) {
62                 lconf = &lcore_cfg[lcore_id];
63                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
64                         struct task_args *targ = &lconf->targs[task_id];
65
66                         if (!strcmp(targ->task_init->mode_str, "genl4"))
67                                 n_l4gen++;
68                 }
69         }
70
71         mem_size = sizeof(struct stats_l4gen_manager) + sizeof(struct task_l4_stats) * n_l4gen;
72         return prox_zmalloc(mem_size, socket_id);
73 }
74
75 void stats_l4gen_init(void)
76 {
77         struct lcore_cfg *lconf;
78         uint32_t lcore_id = -1;
79
80         sl4m = alloc_stats_l4gen_manager();
81
82         while(prox_core_next(&lcore_id, 0) == 0) {
83                 lconf = &lcore_cfg[lcore_id];
84                 for (uint8_t task_id = 0; task_id < lconf->n_tasks_all; ++task_id) {
85                         struct task_args *targ = &lconf->targs[task_id];
86
87                         if (!strcmp(targ->task_init->mode_str, "genl4")) {
88                                 sl4m->task_l4_stats[sl4m->n_l4gen].task = (struct task_l4gen_stats *)lconf->tasks_all[task_id];
89                                 sl4m->task_l4_stats[sl4m->n_l4gen].lcore_id = lcore_id;
90                                 sl4m->task_l4_stats[sl4m->n_l4gen].task_id = task_id;
91                                 sl4m->n_l4gen++;
92                         }
93                 }
94         }
95 }
96
97 void stats_l4gen_update(void)
98 {
99         uint64_t before, after;
100
101         for (uint16_t i = 0; i < sl4m->n_l4gen; ++i) {
102                 struct task_l4gen_stats *task_l4gen = sl4m->task_l4_stats[i].task;
103
104                 before = rte_rdtsc();
105                 sl4m->task_l4_stats[i].sample[last_stat].stats = task_l4gen->l4_stats;
106                 after = rte_rdtsc();
107
108                 sl4m->task_l4_stats[i].sample[last_stat].tsc = (before >> 1) + (after >> 1);
109         }
110 }