aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/trace.c
blob: dadb09d863484b0abc7f17cf2e02e917e352f4f7 (plain) (blame)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include <linux/module.h>

#include <litmus/litmus.h>
#include <litmus/trace.h>

/******************************************************************************/
/*                          Allocation                                        */
/******************************************************************************/

struct ft_buffer* trace_ts_buf = NULL;

static unsigned int ts_seq_no = 0;

static inline void __save_timestamp(unsigned long event, uint8_t type)
{
	unsigned int seq_no;
	struct timestamp *ts;
	seq_no = fetch_and_inc((int *) &ts_seq_no);
	if (ft_buffer_start_write(trace_ts_buf, (void**)  &ts)) {
		ts->event     = event;
		ts->timestamp = ft_timestamp();
		ts->seq_no    = seq_no;
		ts->cpu       = raw_smp_processor_id();
		ts->task_type = type;
		ft_buffer_finish_write(trace_ts_buf, ts);
	}
}

feather_callback void save_timestamp(unsigned long event)
{
	__save_timestamp(event, TSK_UNKNOWN);
}

feather_callback void save_timestamp_def(unsigned long event, unsigned long type)
{
	__save_timestamp(event, (uint8_t) type);
}

feather_callback void save_timestamp_task(unsigned long event, unsigned long t_ptr)
{
	int rt = is_realtime((struct task_struct *) t_ptr);
	__save_timestamp(event, rt ? TSK_RT : TSK_BE);
}

static struct ft_buffer* alloc_ft_buffer(unsigned int count, size_t size)
{
	struct ft_buffer* buf;
	size_t total = (size + 1) * count;
	char* mem;
	int order = 0, pages = 1;

	buf = kmalloc(sizeof(struct ft_buffer), GFP_KERNEL);
	if (!buf)
		return NULL;

	total = (total / PAGE_SIZE) + (total % PAGE_SIZE != 0);
	while (pages < total) {
		order++;
		pages *= 2;
	}

	mem = (char*) __get_free_pages(GFP_KERNEL, order);
	if (!mem) {
		kfree(buf);
		return NULL;
	}

	if (!init_ft_buffer(buf, count, size,
			    mem + (count * size),  /* markers at the end */
			    mem)) {                /* buffer objects     */
		free_pages((unsigned long) mem, order);
		kfree(buf);
		return NULL;
	}
	return buf;
}

static void free_ft_buffer(struct ft_buffer* buf)
{
	int order = 0, pages = 1;
	size_t total;

	if (buf) {
		total = (buf->slot_size + 1) * buf->slot_count;
		total = (total / PAGE_SIZE) + (total % PAGE_SIZE != 0);
		while (pages < total) {
			order++;
			pages *= 2;
		}
		free_pages((unsigned long) buf->buffer_mem, order);
		kfree(buf);
	}
}


/******************************************************************************/
/*                        DEVICE FILE DRIVER                                  */
/******************************************************************************/

#define NO_TIMESTAMPS (2 << 19) /* that should be 8 megs of ram, we may not get
				 * as much */

static DECLARE_MUTEX(feather_lock);
static int use_count = 0;

/* used for draining the FT buffers */
static int enabled_events = 0;

static int trace_release(struct inode *in, struct file *filp)
{
	int err 		= -EINVAL;

	if (down_interruptible(&feather_lock)) {
		err = -ERESTARTSYS;
		goto out;
	}

	printk(KERN_ALERT "%s/%d disconnects from feather trace device. "
	       "use_count=%d\n",
	       current->comm, current->pid, use_count);

	if (use_count == 1) {
		/* disable events */
		ft_disable_all_events();
		enabled_events = 0;

		/* wait for any pending events to complete */
		set_current_state(TASK_UNINTERRUPTIBLE);
		schedule_timeout(HZ);

		printk(KERN_ALERT "Failed trace writes: %u\n",
		       trace_ts_buf->failed_writes);

		free_ft_buffer(trace_ts_buf);
		trace_ts_buf = NULL;
	}

	/* dummy entry to make linker happy */
	ft_event0(666, save_timestamp);

	use_count--;
	up(&feather_lock);
out:
	return err;
}

static ssize_t trace_read(struct file *filp, char __user *to, size_t len,
		      loff_t *f_pos)
{
	/* 	we ignore f_pos, this is strictly sequential */
	ssize_t error = 0;
	struct timestamp ts;

	if (down_interruptible(&feather_lock)) {
		error = -ERESTARTSYS;
		goto out;
	}


	while (len >= sizeof(struct timestamp)) {
		if (ft_buffer_read(trace_ts_buf, &ts)) {
			/* FIXME: avoid double copy */
			if (copy_to_user(to, &ts, sizeof(struct timestamp))) {
				error = -EFAULT;
				break;
			} else {
				len    -= sizeof(struct timestamp);
				to     += sizeof(struct timestamp);
				error  += sizeof(struct timestamp);
			}
	        } else if (enabled_events) {
			/* only wait if there are any events enabled */
			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(50);
			if (signal_pending(current)) {
				error = -ERESTARTSYS;
				break;
			}
		} else
			/* nothing left to get, return to user space */
			break;
	}
	up(&feather_lock);
out:
	return error;
}

#define ENABLE_CMD 	0
#define DISABLE_CMD 	1

typedef uint32_t cmd_t;

static ssize_t trace_write(struct file *filp, const char __user *from,
			   size_t len, loff_t *f_pos)
{
	ssize_t error = -EINVAL;
	cmd_t cmd;
	cmd_t id;

	if (len % sizeof(cmd_t) || len < 2 * sizeof(cmd_t))
		goto out;

	if (copy_from_user(&cmd, from, sizeof(cmd_t))) {
		error = -EFAULT;
	        goto out;
	}
	len  -= sizeof(cmd_t);
	from += sizeof(cmd_t);

	if (cmd != ENABLE_CMD && cmd != DISABLE_CMD)
		goto out;

	if (down_interruptible(&feather_lock)) {
		error = -ERESTARTSYS;
		goto out;
	}

	error = sizeof(cmd_t);
	while (len) {
		if (copy_from_user(&id, from, sizeof(cmd_t))) {
			error = -EFAULT;
			goto out;
		}
		len  -= sizeof(cmd_t);
		from += sizeof(cmd_t);
		if (cmd) {
			printk(KERN_INFO
			       "Disabling feather-trace event %d.\n", (int) id);
			ft_disable_event(id);
			enabled_events--;
		} else {
			printk(KERN_INFO
			       "Enabling feather-trace event %d.\n", (int) id);
			ft_enable_event(id);
			enabled_events++;
		}
		error += sizeof(cmd_t);
	}

	up(&feather_lock);
 out:
	return error;
}

static int trace_open(struct inode *in, struct file *filp)
{
	int err = 0;
        unsigned int count = NO_TIMESTAMPS;

	if (down_interruptible(&feather_lock)) {
		err = -ERESTARTSYS;
		goto out;
	}

	while (count && !trace_ts_buf) {
		printk("trace: trying to allocate %u time stamps.\n", count);
		trace_ts_buf = alloc_ft_buffer(count, sizeof(struct timestamp));
		count /= 2;
	}
	if (!trace_ts_buf)
		err = -ENOMEM;
	else
		use_count++;

	up(&feather_lock);
out:
	return err;
}

/******************************************************************************/
/*                          Device Registration                               */
/******************************************************************************/

#define FT_TRACE_MAJOR	252

struct file_operations ft_trace_fops = {
	.owner   = THIS_MODULE,
	.open    = trace_open,
	.release = trace_release,
	.write   = trace_write,
	.read    = trace_read,
};


static int __init register_buffer_dev(const char* name,
				      struct file_operations* fops,
				      int major, int count)
{
	dev_t   trace_dev;
	struct cdev *cdev;
	int error = 0;

	trace_dev = MKDEV(major, 0);
	error     = register_chrdev_region(trace_dev, count, name);
	if (error)
	{
		printk(KERN_WARNING "trace: "
		       "Could not register major/minor number %d\n", major);
		return error;
	}
	cdev = cdev_alloc();
	if (!cdev) {
		printk(KERN_WARNING "trace: "
			"Could not get a cdev for %s.\n", name);
		return -ENOMEM;
	}
	cdev->owner = THIS_MODULE;
	cdev->ops   = fops;
	error = cdev_add(cdev, trace_dev, count);
	if (error) {
		printk(KERN_WARNING "trace: "
			"add_cdev failed for %s.\n", name);
		return -ENOMEM;
	}
	return error;

}

static int __init init_sched_trace(void)
{
	int error = 0;

	printk("Initializing Feather-Trace device\n");

	error = register_buffer_dev("ft_trace", &ft_trace_fops,
				    FT_TRACE_MAJOR, 1);
	return error;
}

module_init(init_sched_trace);