aboutsummaryrefslogtreecommitdiff
path: root/driver/gator_annotate.c
blob: d8d86afd10a717d59dfe8afce90123dfd96bda41 (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
/**
 * Copyright (C) ARM Limited 2010-2011. 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 <linux/slab.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <asm/current.h>
#include <linux/spinlock.h>

#define ANNOTATE_SIZE	(16*1024)
static DEFINE_SPINLOCK(annotate_lock);
static char *annotateBuf;
static char *annotateBuf0;
static char *annotateBuf1;
static int annotatePos;
static int annotateSel;

static ssize_t annotate_write(struct file *file, char const __user *buf, size_t count, loff_t *offset)
{
	char tempBuffer[512];
	int retval, remaining, size;

	if (*offset)
		return -EINVAL;

	// determine size to capture
	remaining = ANNOTATE_SIZE - annotatePos - 256; // pad for headers and release
	size = count < sizeof(tempBuffer) ? count : sizeof(tempBuffer);
	size = size < remaining ? size : remaining;
	if (size <= 0) {
		wake_up(&gator_buffer_wait);
		return 0;
	}

	// copy from user space
	retval = copy_from_user(tempBuffer, buf, size);
	if (retval == 0) {
		// synchronize shared variables annotateBuf and annotatePos
		spin_lock(&annotate_lock);
		if (annotateBuf) {
			uint32_t tid = current->pid;
			uint32_t tick = gator_master_tick;
			uint64_t time = gator_get_time();
			uint32_t cpuid = smp_processor_id();
			int pos = annotatePos;
			pos += gator_write_packed_int(&annotateBuf[pos], tid);
			pos += gator_write_packed_int(&annotateBuf[pos], tick);
			pos += gator_write_packed_int(&annotateBuf[pos], time);
			pos += gator_write_packed_int(&annotateBuf[pos], time >> 32);
			pos += gator_write_packed_int(&annotateBuf[pos], cpuid);
			pos += gator_write_packed_int(&annotateBuf[pos], size);
			memcpy(&annotateBuf[pos], tempBuffer, size);
			annotatePos = pos + size;
		}
		spin_unlock(&annotate_lock);

		// return the number of bytes written
		retval = size;
	} else {
		retval = -EINVAL;
	}

	return retval;
}

static int annotate_release(struct inode *inode, struct file *file)
{
	int remaining = ANNOTATE_SIZE - annotatePos;
	if (remaining < 16) {
		return -EFAULT;
	}

	spin_lock(&annotate_lock);
	if (annotateBuf) {
		uint32_t tid = current->pid;
		uint32_t tick = gator_master_tick;
		int pos = annotatePos;
		pos += gator_write_packed_int(&annotateBuf[pos], tid);
		pos += gator_write_packed_int(&annotateBuf[pos], tick);
		pos += gator_write_packed_int(&annotateBuf[pos], 0); // time
		pos += gator_write_packed_int(&annotateBuf[pos], 0); // time
		pos += gator_write_packed_int(&annotateBuf[pos], 0); // cpuid
		pos += gator_write_packed_int(&annotateBuf[pos], 0); // size
		annotatePos = pos;
	}
	spin_unlock(&annotate_lock);

	return 0;
}

static const struct file_operations annotate_fops = {
	.write		= annotate_write,
	.release	= annotate_release
};

static int gator_annotate_create_files(struct super_block *sb, struct dentry *root)
{
	annotateBuf = NULL;
	return gatorfs_create_file_perm(sb, root, "annotate", &annotate_fops, 0666);
}

static int gator_annotate_init(void)
{
	annotateBuf0 = kmalloc(ANNOTATE_SIZE, GFP_KERNEL);
	annotateBuf1 = kmalloc(ANNOTATE_SIZE, GFP_KERNEL);
	if (!annotateBuf0 || !annotateBuf1)
		return -1;
	return 0;
}

static int gator_annotate_start(void)
{
	annotatePos = annotateSel = 0;
	annotateBuf = annotateBuf0;
	return 0;
}

static void gator_annotate_stop(void)
{
	spin_lock(&annotate_lock);
	annotateBuf = NULL;
	spin_unlock(&annotate_lock);
}

static void gator_annotate_exit(void)
{
	spin_lock(&annotate_lock);
	kfree(annotateBuf0);
	kfree(annotateBuf1);
	annotateBuf = annotateBuf0 = annotateBuf1 = NULL;
	spin_unlock(&annotate_lock);
}

static int gator_annotate_ready(void)
{
	return annotatePos && annotateBuf;
}

static int gator_annotate_read(char **buffer)
{
	int len;

	if (!gator_annotate_ready())
		return 0;

	annotateSel = !annotateSel;

	if (buffer)
		*buffer = annotateBuf;

	spin_lock(&annotate_lock);
	len = annotatePos;
	annotatePos = 0;
	annotateBuf = annotateSel ? annotateBuf1 : annotateBuf0;
	spin_unlock(&annotate_lock);

	return len;
}