aboutsummaryrefslogtreecommitdiff
path: root/driver/gator_events_irq.c
blob: 435bc86fb5d13c50c313301a24d066ac11ba6d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include "gator.h"
#include <trace/events/irq.h>

#define HARDIRQ		0
#define SOFTIRQ		1
#define TOTALIRQ	(SOFTIRQ+1)

static ulong hardirq_enabled;
static ulong softirq_enabled;
static ulong hardirq_key;
static ulong softirq_key;
static DEFINE_PER_CPU(int[TOTALIRQ], irqCnt);
static DEFINE_PER_CPU(int[TOTALIRQ * 2], irqGet);

GATOR_DEFINE_PROBE(irq_handler_exit, TP_PROTO(int irq,
		struct irqaction *action, int ret))
{
	unsigned long flags;

	// disable interrupts to synchronize with gator_events_irq_read()
	// spinlocks not needed since percpu buffers are used
	local_irq_save(flags);
	per_cpu(irqCnt, smp_processor_id())[HARDIRQ]++;
	local_irq_restore(flags);
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 37)
GATOR_DEFINE_PROBE(softirq_exit, TP_PROTO(struct softirq_action *h, struct softirq_action *vec))
#else
GATOR_DEFINE_PROBE(softirq_exit, TP_PROTO(unsigned int vec_nr))
#endif
{
	unsigned long flags;

	// disable interrupts to synchronize with gator_events_irq_read()
	// spinlocks not needed since percpu buffers are used
	local_irq_save(flags);
	per_cpu(irqCnt, smp_processor_id())[SOFTIRQ]++;
	local_irq_restore(flags);
}

static int gator_events_irq_create_files(struct super_block *sb, struct dentry *root)
{
	struct dentry *dir;

	/* irq */
	dir = gatorfs_mkdir(sb, root, "Linux_irq_irq");
	if (!dir) {
		return -1;
	}
	gatorfs_create_ulong(sb, dir, "enabled", &hardirq_enabled);
	gatorfs_create_ro_ulong(sb, dir, "key", &hardirq_key);

	/* soft irq */
	dir = gatorfs_mkdir(sb, root, "Linux_irq_softirq");
	if (!dir) {
		return -1;
	}
	gatorfs_create_ulong(sb, dir, "enabled", &softirq_enabled);
	gatorfs_create_ro_ulong(sb, dir, "key", &softirq_key);

	return 0;
}

static int gator_events_irq_online(int** buffer)
{
	int len = 0, cpu = smp_processor_id();
	unsigned long flags; // not necessary as we are in interrupt context anyway, but doesn't hurt

	// synchronization with the irq_exit functions is not necessary as the values are being reset
	if (hardirq_enabled) {
		local_irq_save(flags);
		per_cpu(irqCnt, cpu)[HARDIRQ] = 0;
		local_irq_restore(flags);
		per_cpu(irqGet, cpu)[len++] = hardirq_key;
		per_cpu(irqGet, cpu)[len++] = 0;
	}

	if (softirq_enabled) {
		local_irq_save(flags);
		per_cpu(irqCnt, cpu)[SOFTIRQ] = 0;
		local_irq_restore(flags);
		per_cpu(irqGet, cpu)[len++] = softirq_key;
		per_cpu(irqGet, cpu)[len++] = 0;
	}

	if (buffer)
		*buffer = per_cpu(irqGet, cpu);

	return len;
}

static int gator_events_irq_start(void)
{
	// register tracepoints
	if (hardirq_enabled)
		if (GATOR_REGISTER_TRACE(irq_handler_exit))
			goto fail_hardirq_exit;
	if (softirq_enabled)
		if (GATOR_REGISTER_TRACE(softirq_exit))
			goto fail_softirq_exit;
	pr_debug("gator: registered irq tracepoints\n");

	return 0;

	// unregister tracepoints on error
fail_softirq_exit:
	if (hardirq_enabled)
		GATOR_UNREGISTER_TRACE(irq_handler_exit);
fail_hardirq_exit:
	pr_err("gator: irq tracepoints failed to activate, please verify that tracepoints are enabled in the linux kernel\n");

	return -1;
}

static void gator_events_irq_stop(void)
{
	if (hardirq_enabled)
		GATOR_UNREGISTER_TRACE(irq_handler_exit);
	if (softirq_enabled)
		GATOR_UNREGISTER_TRACE(softirq_exit);
	pr_debug("gator: unregistered irq tracepoints\n");

	hardirq_enabled = 0;
	softirq_enabled = 0;
}

static int gator_events_irq_read(int **buffer)
{
	unsigned long flags; // not necessary as we are in interrupt context anyway, but doesn't hurt
	int len, value;
	int cpu = smp_processor_id();

	len = 0;
	if (hardirq_enabled) {
		local_irq_save(flags);
		value = per_cpu(irqCnt, cpu)[HARDIRQ];
		per_cpu(irqCnt, cpu)[HARDIRQ] = 0;
		local_irq_restore(flags);

		per_cpu(irqGet, cpu)[len++] = hardirq_key;
		per_cpu(irqGet, cpu)[len++] = value;
	}

	if (softirq_enabled) {
		local_irq_save(flags);
		value = per_cpu(irqCnt, cpu)[SOFTIRQ];
		per_cpu(irqCnt, cpu)[SOFTIRQ] = 0;
		local_irq_restore(flags);

		per_cpu(irqGet, cpu)[len++] = softirq_key;
		per_cpu(irqGet, cpu)[len++] = value;
	}

	if (buffer)
		*buffer = per_cpu(irqGet, cpu);

	return len;
}

static struct gator_interface gator_events_irq_interface = {
	.create_files = gator_events_irq_create_files,
	.online = gator_events_irq_online,
	.start = gator_events_irq_start,
	.stop = gator_events_irq_stop,
	.read = gator_events_irq_read,
};

int gator_events_irq_init(void)
{
	hardirq_key = gator_events_get_key();
	softirq_key = gator_events_get_key();

	hardirq_enabled = 0;
	softirq_enabled = 0;

	return gator_events_install(&gator_events_irq_interface);
}
gator_events_init(gator_events_irq_init);