Added command lat all stats
[samplevnf.git] / VNFs / DPPD-PROX / display_latency_distr.c
1 /*
2 // Copyright (c) 2019 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 <math.h>
18 #include "handle_lat.h"
19 #include "display_latency_distr.h"
20 #include "stats_latency.h"
21 #include "display.h"
22 #include "lconf.h"
23
24 static struct display_page display_page_latency_distr;
25 static struct display_column *stats_latency_distr[LAT_BUCKET_COUNT];
26 static struct display_column *stats_max;
27 static struct display_column *core_col;
28 static struct display_column *name_col;
29 static uint32_t global_min_bucket_id = 0, global_max_bucket_id = LAT_BUCKET_COUNT - 1;
30 static const uint16_t global_nb_buckets_displayed = 15;
31 static uint32_t group_size = 9; //LAT_BUCKET_COUNT / global_nb_buckets_displayed;
32
33 #define UNIT_INT(i)     (((i) * bucket_unit_nsec)/1000)
34 #define UNIT_FRACT(i)   ((((i) * bucket_unit_nsec) % 1000) / 100)
35
36 static void display_latency_distr_draw_frame(struct screen_state *state)
37 {
38         uint32_t n_tasks = stats_get_n_latency();
39         struct lcore_cfg *lconf = NULL;
40         struct task_args *targ;
41         char name[32];
42         char *ptr;
43
44         display_page_init(&display_page_latency_distr);
45
46         struct display_table *core_name = display_page_add_table(&display_page_latency_distr);
47
48         display_table_init(core_name, "Core/task");
49         core_col = display_table_add_col(core_name);
50         name_col = display_table_add_col(core_name);
51         display_column_init(core_col, "Nb", 4);
52         display_column_init(name_col, "Name", 5);
53
54         uint32_t bucket_size = stats_get_latency_bucket_size();
55         struct display_table *stats = display_page_add_table(&display_page_latency_distr);
56         uint32_t bucket_unit_nsec = 1000000000 / (rte_get_tsc_hz() >> bucket_size);
57         if (state->toggle == 0) {
58                 display_table_init(stats, "Statistics per second");
59         } else {
60                 display_table_init(stats, "Total statistics");
61         }
62         char title[64];
63         stats_max = display_table_add_col(stats);
64         snprintf(title, sizeof(title), " MAXIMUM(mic)");
65         display_column_init(stats_max, title, 11);
66         plog_info("Bucket unit is %d nsec, bucket size is %d, freq is %ld\n", bucket_unit_nsec, bucket_size, rte_get_tsc_hz());
67
68         uint32_t i = global_min_bucket_id, first = i, k = 0;
69         while ((i < LAT_BUCKET_COUNT) && (i <= global_max_bucket_id)) {
70                 stats_latency_distr[k] = display_table_add_col(stats);
71                 if (i < LAT_BUCKET_COUNT - group_size) {
72                         snprintf(title, sizeof(title), "%d.%01d-%d.%01d", UNIT_INT(i), UNIT_FRACT(i), UNIT_INT(i + group_size), UNIT_FRACT(i + group_size));
73                 } else {
74                         snprintf(title, sizeof(title), "> %d.%01d", UNIT_INT(i), UNIT_FRACT(i));
75                 }
76                 display_column_init(stats_latency_distr[k++], title, 9);
77                 i += group_size;
78         }
79         display_page_draw_frame(&display_page_latency_distr, n_tasks);
80
81         uint32_t count = 0;
82         lconf = NULL;
83         while (core_targ_next(&lconf, &targ, 0) == 0) {
84                 if (strcmp(targ->task_init->mode_str, "lat") == 0) {
85                         display_column_print_core_task(core_col, count, lconf, targ);
86                         if (targ->id == 0)
87                                 display_column_print(name_col, count, "%s", lconf->name);
88                         count++;
89                 }
90         }
91 }
92
93 static void display_latency_distr_draw_stats(struct screen_state *state)
94 {
95         const uint32_t n_latency = stats_get_n_latency();
96         uint64_t *bucket;
97         uint32_t bucket_id = 0, min_bucket_id = LAT_BUCKET_COUNT - 1, max_bucket_id = 0;
98         struct time_unit tu;
99
100         for (uint32_t count = 0; count < n_latency; ++count) {
101                 if (state->toggle == 0)
102                         tu = stats_latency_get(count)->max.time;
103                 else
104                         tu = stats_latency_tot_get(count)->max.time;
105                 display_column_print(stats_max, count, "%9lu.%03lu", tu.sec * 1000000 + tu.nsec / 1000, tu.nsec % 1000);
106         }
107
108         // Calculate min_bucket_id: id of 1st bucket with data for any tasks
109         // Calculate max_bucket_id: id of last bucket with data for any tasks
110         for (uint i = 0; i < LAT_BUCKET_COUNT; ++i) {
111                 for (uint32_t count = 0; count < n_latency; ++count) {
112                         if (state->toggle == 0)
113                                 bucket = stats_latency_get_bucket(count);
114                         else
115                                 bucket = stats_latency_get_tot_bucket(count);
116                         if (bucket[i] != 0) {
117                                 min_bucket_id = i;
118                                 break;
119                         }
120                 }
121                 if (min_bucket_id != LAT_BUCKET_COUNT - 1)
122                         break;
123         }
124
125         for (uint i = LAT_BUCKET_COUNT; i > 0; i--) {
126                 for (uint32_t count = 0; count < n_latency; ++count) {
127                         if (state->toggle == 0)
128                                 bucket = stats_latency_get_bucket(count);
129                         else
130                                 bucket = stats_latency_get_tot_bucket(count);
131                         if (bucket[i - 1] != 0) {
132                                 max_bucket_id = i - 1;
133                                 break;
134                         }
135                 }
136                 if (max_bucket_id)
137                         break;
138         }
139
140         if (max_bucket_id - min_bucket_id + 1 < global_nb_buckets_displayed) {
141                 max_bucket_id = global_nb_buckets_displayed + min_bucket_id - 1;
142         }
143
144         if ((global_min_bucket_id != min_bucket_id) || (global_max_bucket_id != max_bucket_id)) {
145                 global_min_bucket_id = min_bucket_id;
146                 global_max_bucket_id = max_bucket_id;
147                 // Calculate how many buckets must be grouped together
148                 if (max_bucket_id - min_bucket_id + 1 > global_nb_buckets_displayed)
149                         group_size = ceil(1.0 * (max_bucket_id - min_bucket_id + 1) / global_nb_buckets_displayed);
150                 else
151                         group_size = 1;
152                 display_latency_distr_draw_frame(state);
153                 display_renew();
154                 plog_info("min_bucket_id = %d, max_bucket_id = %d\n", min_bucket_id, max_bucket_id);
155         }
156
157         for (uint32_t count = 0; count < n_latency; ++count) {
158                 if (state->toggle == 0)
159                         bucket = stats_latency_get_bucket(count);
160                 else
161                         bucket = stats_latency_get_tot_bucket(count);
162                 uint32_t i = min_bucket_id, k = 0;
163                 uint64_t nb = 0;
164                 while ((i < LAT_BUCKET_COUNT) && (i <= global_max_bucket_id)){
165                         for (uint32_t j = 0; j <= group_size; j++)
166                                 if (i + j < LAT_BUCKET_COUNT)
167                                         nb += bucket[i+j];
168                         display_column_print(stats_latency_distr[k++], count, "%9lu", nb);
169                         nb = 0;
170                         i += group_size;
171                 }
172         }
173 }
174
175 static int display_latency_distr_get_height(void)
176 {
177         return stats_get_n_latency();
178 }
179
180 static struct display_screen display_screen_latency_distr = {
181         .draw_frame = display_latency_distr_draw_frame,
182         .draw_stats = display_latency_distr_draw_stats,
183         .get_height = display_latency_distr_get_height,
184         .title = "latency_distr",
185 };
186
187 struct display_screen *display_latency_distr(void)
188 {
189         return &display_screen_latency_distr;
190 }