Do not create (empty) latency loss file if disabled
[samplevnf.git] / VNFs / DPPD-PROX / handle_irq.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 <rte_cycles.h>
18
19 #include "lconf.h"
20 #include "task_base.h"
21 #include "task_init.h"
22 #include "handle_irq.h"
23 #include "stats_irq.h"
24 #include "log.h"
25 #include "unistd.h"
26 #include "input.h"
27
28 #define MAX_INTERRUPT_LENGTH    500000  /* Maximum length of an interrupt is (1 / MAX_INTERRUPT_LENGTH) seconds */
29 uint64_t irq_bucket_maxtime_micro[] = {1,5,10,50,100,500,1000,5000,10000,50000,100000,500000,UINT64_MAX};
30 /*
31  *      This module is not handling any packets.
32  *      It loops on rdtsc() and checks whether it has been interrupted
33  *               for more than (1 / MAX_INTERRUPT_LENGTH) sec.
34  *      This is a debugging only task, useful to check if the system h
35  *              as been properly configured.
36 */
37
38 static void update_irq_stats(struct task_irq *task, uint64_t irq)
39 {
40         if (irq > task->stats.max_irq)
41                 task->stats.max_irq = irq;
42         for (uint i = 0; i < IRQ_BUCKETS_COUNT; ++i) {
43                 if (irq < irq_bucket_maxtime_cycles[i]) {
44                         task->stats.irq[i]++;
45                         break;
46                 }
47         }
48 }
49
50 void task_irq_show_stats(struct task_irq *task_irq, struct input *input)
51 {
52         struct irq_bucket *bucket = &task_irq->buffer[!task_irq->task_use_lt];
53         if (input->reply) {
54                 char buf[8192] = {0};
55                 if (bucket->index == 0) {
56                         sprintf(buf, "\n");
57                         input->reply(input, buf, strlen(buf));
58                         buf[0] = 0;
59                 }
60                 for (uint64_t i = 0; i < bucket->index; i++) {
61                         sprintf(buf + strlen(buf), "%d; %"PRIu64"""; %ld; %ld; %ld; %ld ;",
62                                 task_irq->lcore_id,
63                                 i,
64                                 bucket->info[i].lat,
65                                 bucket->info[i].lat * 1000000 / rte_get_tsc_hz(),
66                                 bucket->info[i].tsc - task_irq->start_tsc,
67                                 (bucket->info[i].tsc - task_irq->start_tsc) * 1000 / rte_get_tsc_hz());
68                         sprintf(buf+strlen(buf), "\n");
69                         input->reply(input, buf, strlen(buf));
70                         buf[0] = 0;
71                 }
72         } else {
73                 for (uint64_t i = 0; i < bucket->index; i++)
74                         if (bucket->info[i].lat)
75                                 plog_info("[%d]; Interrupt %"PRIu64": %ld cycles (%ld micro-sec) at %ld cycles (%ld msec)\n",
76                                           task_irq->lcore_id,
77                                           i,
78                                           bucket->info[i].lat,
79                                           bucket->info[i].lat * 1000000 / rte_get_tsc_hz(),
80                                           bucket->info[i].tsc - task_irq->start_tsc,
81                                           (bucket->info[i].tsc - task_irq->start_tsc) * 1000 / rte_get_tsc_hz());
82         }
83         task_irq->stats_use_lt = !task_irq->task_use_lt;
84         bucket->index = 0;
85 }
86
87 static void irq_stop(struct task_base *tbase)
88 {
89         struct task_irq *task = (struct task_irq *)tbase;
90         uint32_t i;
91         uint32_t lcore_id = rte_lcore_id();
92         uint64_t lat, max_lat = 0, tot_lat = 0;
93         int bucket_id;
94         int n_lat = 0;
95
96         if (task->irq_debug) {
97                 plog_info("Stopping core %u\n", lcore_id);
98                 sleep(2);       // Make sure all cores are stopped before starting to write
99                 plog_info("Core ID; Interrupt (nanosec); Time (msec)\n");
100                 for (int j = 0; j < 2; j++) {
101                         // Start dumping the oldest bucket first
102                         if (task->buffer[0].info[0].tsc < task->buffer[1].info[0].tsc)
103                                 bucket_id = j;
104                         else
105                                 bucket_id = !j;
106                         struct irq_bucket *bucket = &task->buffer[bucket_id];
107                         for (i=0; i< bucket->index;i++) {
108                                 if (bucket->info[i].lat != 0) {
109                                         lat = bucket->info[i].lat * 1000000000 / rte_get_tsc_hz();
110                                         if (max_lat < lat)
111                                                 max_lat = lat;
112                                         n_lat++;
113                                         tot_lat += lat;
114                                         plog_info("%d; %ld; %ld\n", lcore_id, lat,
115                                                 (bucket->info[i].tsc - task->start_tsc) * 1000 / rte_get_tsc_hz());
116                                 }
117                         }
118                 }
119                 if (n_lat)
120                         tot_lat = tot_lat / n_lat;
121                 plog_info("Core %u stopped. max lat is %ld and average is %ld\n", lcore_id, max_lat, tot_lat);
122         }
123 }
124
125 static inline int handle_irq_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
126 {
127         struct task_irq *task = (struct task_irq *)tbase;
128         uint64_t tsc1;
129         uint64_t index;
130
131         if (task->stats_use_lt != task->task_use_lt)
132                 task->task_use_lt = task->stats_use_lt;
133         struct irq_bucket *bucket = &task->buffer[task->task_use_lt];
134
135         tsc1 = rte_rdtsc();
136         if ((tsc1 > task->first_tsc) && (task->tsc != 0)) {
137                 update_irq_stats(task, tsc1 - task->tsc);
138                 if (((tsc1 - task->tsc) > task->max_irq) && (bucket->index < MAX_INDEX)) {
139                         bucket->info[bucket->index].tsc = tsc1;
140                         bucket->info[bucket->index++].lat = tsc1 - task->tsc;
141                 }
142         }
143         task->tsc = tsc1;
144         return 0;
145 }
146
147 static void init_task_irq(struct task_base *tbase,
148                           __attribute__((unused)) struct task_args *targ)
149 {
150         struct task_irq *task = (struct task_irq *)tbase;
151         task->start_tsc = rte_rdtsc();
152         task->first_tsc = task->start_tsc + 2 * rte_get_tsc_hz();
153         task->lcore_id = targ->lconf->id;
154         task->irq_debug = targ->irq_debug;
155         // max_irq expressed in cycles
156         task->max_irq = rte_get_tsc_hz() / MAX_INTERRUPT_LENGTH;
157         plog_info("\tusing irq mode with max irq set to %ld cycles\n", task->max_irq);
158
159         for (uint bucket_id = 0; bucket_id < IRQ_BUCKETS_COUNT - 1; bucket_id++)
160                 irq_bucket_maxtime_cycles[bucket_id] = rte_get_tsc_hz() * irq_bucket_maxtime_micro[bucket_id] / 1000000;
161         irq_bucket_maxtime_cycles[IRQ_BUCKETS_COUNT - 1] = UINT64_MAX;
162 }
163
164 static struct task_init task_init_irq = {
165         .mode_str = "irq",
166         .init = init_task_irq,
167         .handle = handle_irq_bulk,
168         .stop = irq_stop,
169         .flag_features = TASK_FEATURE_NO_RX,
170         .size = sizeof(struct task_irq)
171 };
172
173 static struct task_init task_init_none;
174
175 __attribute__((constructor)) static void reg_task_irq(void)
176 {
177         reg_task(&task_init_irq);
178 }