aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/color_proc.c
blob: 25915e714821bd2b478411c4c557dff910857369 (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
#include <linux/module.h>
#include <linux/sysctl.h>
#include <linux/slab.h>

#include <litmus/sched_trace.h>


#define SPERIOD_LEN 7
#define SPERIOD_FILE "period"
#define SWCET_LEN 5
#define SWCET_FILE "wcet"

static unsigned long *server_wcet;
static unsigned long *server_period;

static struct ctl_table litmus_table[] =
{
	{
		.procname	= "color",
		.mode		= 0555,
	},
	{ }
};

static struct ctl_table litmus_dir_table[] = {
	{
		.procname	= "litmus",
		.mode		= 0555,
		.child		= litmus_table,
	},
	{ }
};

int color_server_params(int cpu, unsigned long *wcet, unsigned long *period)
{
	if (cpu >= num_online_cpus()) {
		printk(KERN_WARNING "Cannot access illegal CPU: %d\n", cpu);
		return -EFAULT;
	}

	if (server_wcet[cpu] == ULONG_MAX || server_period[cpu] == ULONG_MAX) {
		printk(KERN_WARNING "Server %d is uninitialized!\n", cpu);
		return -EPERM;
	}

	*wcet = server_wcet[cpu];
	*period = server_period[cpu];

	TRACE("For %d: %lu, %lu\n", cpu, server_wcet[cpu], server_period[cpu]);

	return 0;
}

static void __init init_server_entry(struct ctl_table *entry,
				     unsigned long *parameter,
				     char *name)
{
	entry->procname = name;
	entry->mode = 0666;
	entry->proc_handler = proc_doulongvec_minmax;
	entry->data = parameter;
	entry->maxlen = sizeof(unsigned long);
}

static int __init init_cpu_entry(struct ctl_table *cpu_table, int cpu)
{
	char *name;
	size_t size;
	struct ctl_table *server_table, *entry;

	server_wcet[cpu] = ULONG_MAX;
	server_period[cpu] = ULONG_MAX;

	printk(KERN_INFO "Creating cpu %d\n", cpu);

	size = sizeof(ctl_table) * 3;
	server_table = kmalloc(size, GFP_ATOMIC);
	if (!server_table) {
		printk(KERN_WARNING "Could not allocate "
		       "color server proc for CPU %d.\n", cpu);
		return -ENOMEM;
	}
	memset(server_table, 0, size);

	/* Server WCET */
	name = kmalloc(SWCET_LEN, GFP_ATOMIC);
	if (!name) {
		return -ENOMEM;
	}
	strcpy(name, SWCET_FILE);
	entry = &server_table[0];
	init_server_entry(entry, &server_wcet[cpu], name);


	/* Server period */
	name = kmalloc(SPERIOD_LEN, GFP_ATOMIC);
	if (!name) {
		return -ENOMEM;
	}
	strcpy(name, SPERIOD_FILE);
	entry = &server_table[1];
	init_server_entry(entry, &server_period[cpu], name);

	name = kmalloc(3, GFP_ATOMIC);
	if (!name) {
		return -ENOMEM;
	}
	snprintf(name, 2, "%d", cpu);
	cpu_table->procname = name;
	cpu_table->mode = 0555;
	cpu_table->child = server_table;

	return 0;
}

static int __init init_server_entries(struct ctl_table *cpu_tables)
{
	size_t size;
	int ret, cpu;
	struct ctl_table *cpu_table;

	size = sizeof(unsigned long) * num_online_cpus();
	server_wcet = kmalloc(size, GFP_ATOMIC);
	server_period = kmalloc(size, GFP_ATOMIC);
	if (!server_wcet || !server_period) {
		printk(KERN_WARNING "Could not allocate server parameters.\n");
		return -ENOMEM;
	}

	for_each_online_cpu(cpu) {
		cpu_table = &cpu_tables[cpu];
		ret = init_cpu_entry(cpu_table, cpu);
		if (ret) {
			return ret;
		}
	}
	return 0;
}


static struct ctl_table_header *litmus_sysctls;

static int __init litmus_sysctl_init(void)
{
	int ret;
	size_t size;
	struct ctl_table *cpu_tables;

	printk(KERN_INFO "Registering LITMUS^RT sysctl.\n");

	size = sizeof(ctl_table) * (num_online_cpus() + 2);
	cpu_tables = kmalloc(size, GFP_ATOMIC);
	if (!cpu_tables) {
		printk(KERN_WARNING "Could not allocate color CPU proc.\n");
	}
	memset(cpu_tables, 0, size);

	ret = init_server_entries(cpu_tables);
	if (ret) {
		return ret;
	}
	litmus_table->child = cpu_tables;

	litmus_sysctls = register_sysctl_table(litmus_dir_table);
	if (!litmus_sysctls) {
		printk(KERN_WARNING "Could not register LITMUS^RT sysctl.\n");
		return -EFAULT;
	}
	return 0;
}

module_init(litmus_sysctl_init);