Skip to content

Commit 6101f1f

Browse files
authored
[components][driver]add isr statistics (#8955)
add isr statistics
1 parent ce913b9 commit 6101f1f

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

components/drivers/include/drivers/pic.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ struct rt_pic_isr
9191
struct rt_irq_desc action;
9292
};
9393

94+
#ifdef RT_USING_PIC_STATISTICS
95+
struct rt_pic_irq_statistics
96+
{
97+
rt_ubase_t current_irq_begin[RT_CPUS_NR];
98+
rt_ubase_t max_irq_time_ns;
99+
rt_ubase_t min_irq_time_ns;
100+
rt_ubase_t sum_irq_time_ns;
101+
};
102+
#endif
103+
94104
struct rt_pic_irq
95105
{
96106
int irq;
@@ -120,6 +130,9 @@ struct rt_pic_irq
120130

121131
struct rt_pic *pic;
122132
struct rt_pic_irq *parent;
133+
#ifdef RT_USING_PIC_STATISTICS
134+
struct rt_pic_irq_statistics stat;
135+
#endif
123136
};
124137

125138
void rt_pic_default_name(struct rt_pic *pic);

components/drivers/pic/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ menuconfig RT_USING_PIC
44
depends on RT_USING_DM
55
default n
66

7+
config RT_USING_PIC_STATISTICS
8+
bool "Enable ISR execution time statistics"
9+
depends on RT_USING_PIC
10+
depends on RT_USING_INTERRUPT_INFO
11+
default n
12+
713
config MAX_HANDLERS
814
int "IRQ max handlers"
915
depends on RT_USING_PIC

components/drivers/pic/pic.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <rtdbg.h>
1717

1818
#include <drivers/pic.h>
19+
#include <ktime.h>
1920

2021
struct irq_traps
2122
{
@@ -504,10 +505,19 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
504505
rt_err_t err = -RT_EEMPTY;
505506
rt_list_t *handler_nodes;
506507
struct rt_irq_desc *action;
508+
#ifdef RT_USING_PIC_STATISTICS
509+
struct timespec ts;
510+
rt_ubase_t irq_time_ns;
511+
#endif
507512

508513
RT_ASSERT(pirq != RT_NULL);
509514
RT_ASSERT(pirq->pic != RT_NULL);
510515

516+
#ifdef RT_USING_PIC_STATISTICS
517+
rt_ktime_boottime_get_ns(&ts);
518+
pirq->stat.current_irq_begin[rt_hw_cpu_id()] = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec;
519+
#endif
520+
511521
/* Corrected irq affinity */
512522
rt_bitmap_set_bit(pirq->affinity, rt_hw_cpu_id());
513523

@@ -561,6 +571,20 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
561571
err = RT_EOK;
562572
}
563573

574+
#ifdef RT_USING_PIC_STATISTICS
575+
rt_ktime_boottime_get_ns(&ts);
576+
irq_time_ns = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec - pirq->stat.current_irq_begin[rt_hw_cpu_id()];
577+
pirq->stat.sum_irq_time_ns += irq_time_ns;
578+
if (irq_time_ns < pirq->stat.min_irq_time_ns || pirq->stat.min_irq_time_ns == 0)
579+
{
580+
pirq->stat.min_irq_time_ns = irq_time_ns;
581+
}
582+
if (irq_time_ns > pirq->stat.max_irq_time_ns)
583+
{
584+
pirq->stat.max_irq_time_ns = irq_time_ns;
585+
}
586+
#endif
587+
564588
return err;
565589
}
566590

@@ -1022,7 +1046,6 @@ rt_err_t rt_pic_init(void)
10221046
#if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH)
10231047
static int list_irq(int argc, char**argv)
10241048
{
1025-
rt_ubase_t level;
10261049
rt_size_t irq_nr = 0;
10271050
rt_bool_t dump_all = RT_FALSE;
10281051
const char *const irq_modes[] =
@@ -1074,6 +1097,10 @@ static int list_irq(int argc, char**argv)
10741097
}
10751098
#endif
10761099

1100+
#ifdef RT_USING_PIC_STATISTICS
1101+
rt_kprintf(" max/ns avg/ns min/ns");
1102+
#endif
1103+
10771104
rt_kputs("\n");
10781105

10791106
for (int i = 0; i < RT_ARRAY_SIZE(_pirq_hash); ++i)
@@ -1117,6 +1144,9 @@ static int list_irq(int argc, char**argv)
11171144
{
11181145
rt_kprintf(" %-10d", pirq->isr.action.cpu_counter[cpuid]);
11191146
}
1147+
#endif
1148+
#ifdef RT_USING_PIC_STATISTICS
1149+
rt_kprintf(" %-10d %-10d %-10d", pirq->stat.max_irq_time_ns, pirq->stat.sum_irq_time_ns/pirq->isr.action.counter, pirq->stat.min_irq_time_ns);
11201150
#endif
11211151
rt_kputs("\n");
11221152

@@ -1137,6 +1167,9 @@ static int list_irq(int argc, char**argv)
11371167
{
11381168
rt_kprintf(" %-10d", repeat_isr->action.cpu_counter[cpuid]);
11391169
}
1170+
#endif
1171+
#ifdef RT_USING_PIC_STATISTICS
1172+
rt_kprintf(" --- --- ---");
11401173
#endif
11411174
rt_kputs("\n");
11421175
}

src/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ config RT_USING_SMP
7777
config RT_CPUS_NR
7878
int "Number of CPUs"
7979
default 1
80+
range 1 1 if !RT_USING_SMP && !RT_USING_AMP
8081
help
8182
Number of CPUs in the system
8283

0 commit comments

Comments
 (0)