Skip to content

Commit fabc923

Browse files
committed
add isr statistics
1 parent e25fc8b commit fabc923

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-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: 35 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
{
@@ -44,6 +45,7 @@ static struct rt_pic_irq _pirq_hash[MAX_HANDLERS] =
4445
.mode = RT_IRQ_MODE_NONE,
4546
.priority = RT_UINT32_MAX,
4647
.rw_lock = { },
48+
.stat.min_irq_time_ns = ~(0UL),
4749
}
4850
};
4951

@@ -504,10 +506,19 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
504506
rt_err_t err = -RT_EEMPTY;
505507
rt_list_t *handler_nodes;
506508
struct rt_irq_desc *action;
509+
#ifdef RT_USING_PIC_STATISTICS
510+
struct timespec ts;
511+
rt_ubase_t irq_time_ns;
512+
#endif
507513

508514
RT_ASSERT(pirq != RT_NULL);
509515
RT_ASSERT(pirq->pic != RT_NULL);
510516

517+
#ifdef RT_USING_PIC_STATISTICS
518+
RT_ASSERT(rt_ktime_boottime_get_ns(&ts) == RT_EOK);
519+
pirq->stat.current_irq_begin[rt_hw_cpu_id()] = ts.tv_sec * (1000UL * 1000 * 1000) + ts.tv_nsec;
520+
#endif
521+
511522
/* Corrected irq affinity */
512523
rt_bitmap_set_bit(pirq->affinity, rt_hw_cpu_id());
513524

@@ -561,6 +572,20 @@ rt_err_t rt_pic_handle_isr(struct rt_pic_irq *pirq)
561572
err = RT_EOK;
562573
}
563574

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

@@ -1022,7 +1047,6 @@ rt_err_t rt_pic_init(void)
10221047
#if defined(RT_USING_CONSOLE) && defined(RT_USING_MSH)
10231048
static int list_irq(int argc, char**argv)
10241049
{
1025-
rt_ubase_t level;
10261050
rt_size_t irq_nr = 0;
10271051
rt_bool_t dump_all = RT_FALSE;
10281052
const char *const irq_modes[] =
@@ -1074,6 +1098,10 @@ static int list_irq(int argc, char**argv)
10741098
}
10751099
#endif
10761100

1101+
#ifdef RT_USING_PIC_STATISTICS
1102+
rt_kprintf(" max/ns avg/ns min/ns");
1103+
#endif
1104+
10771105
rt_kputs("\n");
10781106

10791107
for (int i = 0; i < RT_ARRAY_SIZE(_pirq_hash); ++i)
@@ -1117,6 +1145,9 @@ static int list_irq(int argc, char**argv)
11171145
{
11181146
rt_kprintf(" %-10d", pirq->isr.action.cpu_counter[cpuid]);
11191147
}
1148+
#endif
1149+
#ifdef RT_USING_PIC_STATISTICS
1150+
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);
11201151
#endif
11211152
rt_kputs("\n");
11221153

@@ -1137,6 +1168,9 @@ static int list_irq(int argc, char**argv)
11371168
{
11381169
rt_kprintf(" %-10d", repeat_isr->action.cpu_counter[cpuid]);
11391170
}
1171+
#endif
1172+
#ifdef RT_USING_PIC_STATISTICS
1173+
rt_kprintf(" --- --- ---");
11401174
#endif
11411175
rt_kputs("\n");
11421176
}

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)