1 /**
2 * Copyright (C) ARM Limited 2010-2013. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 */
10 #include "gator.h"
11 #include <trace/events/irq.h>
13 #define HARDIRQ 0
14 #define SOFTIRQ 1
15 #define TOTALIRQ (SOFTIRQ+1)
17 static ulong hardirq_enabled;
18 static ulong softirq_enabled;
19 static ulong hardirq_key;
20 static ulong softirq_key;
21 static DEFINE_PER_CPU(atomic_t[TOTALIRQ], irqCnt);
22 static DEFINE_PER_CPU(int[TOTALIRQ * 2], irqGet);
24 GATOR_DEFINE_PROBE(irq_handler_exit,
25 TP_PROTO(int irq, struct irqaction *action, int ret))
26 {
27 atomic_inc(&per_cpu(irqCnt, get_physical_cpu())[HARDIRQ]);
28 }
30 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)
31 GATOR_DEFINE_PROBE(softirq_exit, TP_PROTO(struct softirq_action *h, struct softirq_action *vec))
32 #else
33 GATOR_DEFINE_PROBE(softirq_exit, TP_PROTO(unsigned int vec_nr))
34 #endif
35 {
36 atomic_inc(&per_cpu(irqCnt, get_physical_cpu())[SOFTIRQ]);
37 }
39 static int gator_events_irq_create_files(struct super_block *sb, struct dentry *root)
40 {
41 struct dentry *dir;
43 /* irq */
44 dir = gatorfs_mkdir(sb, root, "Linux_irq_irq");
45 if (!dir) {
46 return -1;
47 }
48 gatorfs_create_ulong(sb, dir, "enabled", &hardirq_enabled);
49 gatorfs_create_ro_ulong(sb, dir, "key", &hardirq_key);
51 /* soft irq */
52 dir = gatorfs_mkdir(sb, root, "Linux_irq_softirq");
53 if (!dir) {
54 return -1;
55 }
56 gatorfs_create_ulong(sb, dir, "enabled", &softirq_enabled);
57 gatorfs_create_ro_ulong(sb, dir, "key", &softirq_key);
59 return 0;
60 }
62 static int gator_events_irq_online(int **buffer, bool migrate)
63 {
64 int len = 0, cpu = get_physical_cpu();
66 // synchronization with the irq_exit functions is not necessary as the values are being reset
67 if (hardirq_enabled) {
68 atomic_set(&per_cpu(irqCnt, cpu)[HARDIRQ], 0);
69 per_cpu(irqGet, cpu)[len++] = hardirq_key;
70 per_cpu(irqGet, cpu)[len++] = 0;
71 }
73 if (softirq_enabled) {
74 atomic_set(&per_cpu(irqCnt, cpu)[SOFTIRQ], 0);
75 per_cpu(irqGet, cpu)[len++] = softirq_key;
76 per_cpu(irqGet, cpu)[len++] = 0;
77 }
79 if (buffer)
80 *buffer = per_cpu(irqGet, cpu);
82 return len;
83 }
85 static int gator_events_irq_start(void)
86 {
87 // register tracepoints
88 if (hardirq_enabled)
89 if (GATOR_REGISTER_TRACE(irq_handler_exit))
90 goto fail_hardirq_exit;
91 if (softirq_enabled)
92 if (GATOR_REGISTER_TRACE(softirq_exit))
93 goto fail_softirq_exit;
94 pr_debug("gator: registered irq tracepoints\n");
96 return 0;
98 // unregister tracepoints on error
99 fail_softirq_exit:
100 if (hardirq_enabled)
101 GATOR_UNREGISTER_TRACE(irq_handler_exit);
102 fail_hardirq_exit:
103 pr_err("gator: irq tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");
105 return -1;
106 }
108 static void gator_events_irq_stop(void)
109 {
110 if (hardirq_enabled)
111 GATOR_UNREGISTER_TRACE(irq_handler_exit);
112 if (softirq_enabled)
113 GATOR_UNREGISTER_TRACE(softirq_exit);
114 pr_debug("gator: unregistered irq tracepoints\n");
116 hardirq_enabled = 0;
117 softirq_enabled = 0;
118 }
120 static int gator_events_irq_read(int **buffer)
121 {
122 int len, value;
123 int cpu = get_physical_cpu();
125 len = 0;
126 if (hardirq_enabled) {
127 value = atomic_read(&per_cpu(irqCnt, cpu)[HARDIRQ]);
128 atomic_sub(value, &per_cpu(irqCnt, cpu)[HARDIRQ]);
130 per_cpu(irqGet, cpu)[len++] = hardirq_key;
131 per_cpu(irqGet, cpu)[len++] = value;
132 }
134 if (softirq_enabled) {
135 value = atomic_read(&per_cpu(irqCnt, cpu)[SOFTIRQ]);
136 atomic_sub(value, &per_cpu(irqCnt, cpu)[SOFTIRQ]);
138 per_cpu(irqGet, cpu)[len++] = softirq_key;
139 per_cpu(irqGet, cpu)[len++] = value;
140 }
142 if (buffer)
143 *buffer = per_cpu(irqGet, cpu);
145 return len;
146 }
148 static struct gator_interface gator_events_irq_interface = {
149 .create_files = gator_events_irq_create_files,
150 .online = gator_events_irq_online,
151 .start = gator_events_irq_start,
152 .stop = gator_events_irq_stop,
153 .read = gator_events_irq_read,
154 };
156 int gator_events_irq_init(void)
157 {
158 hardirq_key = gator_events_get_key();
159 softirq_key = gator_events_get_key();
161 hardirq_enabled = 0;
162 softirq_enabled = 0;
164 return gator_events_install(&gator_events_irq_interface);
165 }
167 gator_events_init(gator_events_irq_init);