2 // Copyright (c) 2010-2017 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <rte_cycles.h>
20 #include "task_base.h"
21 #include "task_init.h"
22 #include "handle_irq.h"
23 #include "stats_irq.h"
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};
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.
38 static void update_irq_stats(struct task_irq *task, uint64_t irq)
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]) {
50 void task_irq_show_stats(struct task_irq *task_irq, struct input *input)
52 struct irq_bucket *bucket = &task_irq->buffer[!task_irq->task_use_lt];
55 if (bucket->index == 0) {
57 input->reply(input, buf, strlen(buf));
60 for (uint64_t i = 0; i < bucket->index; i++) {
61 sprintf(buf + strlen(buf), "%d; %"PRIu64"""; %ld; %ld; %ld; %ld ;",
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));
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",
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());
83 task_irq->stats_use_lt = !task_irq->task_use_lt;
87 static void irq_stop(struct task_base *tbase)
89 struct task_irq *task = (struct task_irq *)tbase;
91 uint32_t lcore_id = rte_lcore_id();
92 uint64_t lat, max_lat = 0, tot_lat = 0;
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)
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();
114 plog_info("%d; %ld; %ld\n", lcore_id, lat,
115 (bucket->info[i].tsc - task->start_tsc) * 1000 / rte_get_tsc_hz());
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);
125 static inline int handle_irq_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
127 struct task_irq *task = (struct task_irq *)tbase;
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];
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;
147 static void init_task_irq(struct task_base *tbase,
148 __attribute__((unused)) struct task_args *targ)
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);
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;
164 static struct task_init task_init_irq = {
166 .init = init_task_irq,
167 .handle = handle_irq_bulk,
169 .flag_features = TASK_FEATURE_NO_RX,
170 .size = sizeof(struct task_irq)
173 static struct task_init task_init_none;
175 __attribute__((constructor)) static void reg_task_irq(void)
177 reg_task(&task_init_irq);