aboutsummaryrefslogtreecommitdiffstats
path: root/src/litmus.c
blob: 4d9e986445de99e85e59fa6414a8f116a1ff7630 (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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>


#include <sched.h> /* for cpu sets */

#include "litmus.h"
#include "internal.h"

#define LP(name) {name ## _SEM, #name}

static struct {
	int id;
	const char* name;
} protocol[] = {
	LP(FMLP),
	LP(SRP),
	LP(MPCP),
	LP(MPCP_VS),
	{MPCP_VS_SEM, "MPCP-VS"},
	LP(DPCP),
	LP(PCP),
	LP(DFLP),
	LP(OMLP),
};

#define NUM_PROTOS (sizeof(protocol)/sizeof(protocol[0]))

int lock_protocol_for_name(const char* name)
{
	int i;

	for (i = 0; i < NUM_PROTOS; i++)
		if (strcmp(name, protocol[i].name) == 0)
			return protocol[i].id;

	return -1;
}

const char* name_for_lock_protocol(int id)
{
	int i;

	for (i = 0; i < NUM_PROTOS; i++)
		if (protocol[i].id == id)
			return protocol[i].name;

	return "<UNKNOWN>";
}

int litmus_open_lock(
	obj_type_t protocol,
	int lock_id,
	const char* namespace,
	void *config_param)
{
	int fd, od;

	fd = open(namespace, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
	if (fd < 0)
		return -1;
	od = od_openx(fd, protocol, lock_id, config_param);
	close(fd);
	return od;
}



void show_rt_param(struct rt_task* tp)
{
	printf("rt params:\n\t"
	       "exec_cost:\t%llu\n\tperiod:\t\t%llu\n\tcpu:\t%d\n",
	       tp->exec_cost, tp->period, tp->cpu);
}

void init_rt_task_param(struct rt_task* tp)
{
	/* Defaults:
	 *  - implicit deadline (t->relative_deadline == 0)
	 *  - phase = 0
	 *  - class = RT_CLASS_SOFT
	 *  - budget policy = NO_ENFORCEMENT
	 *  - fixed priority = LITMUS_LOWEST_PRIORITY
	 *  - release policy = TASK_SPORADIC
	 *  - cpu assignment = 0
	 *
	 * User must still set the following fields to non-zero values:
	 *  - tp->exec_cost
	 *  - tp->period
	 *
	 * User must set tp->cpu to the appropriate value for non-global
	 * schedulers. For clusters, set tp->cpu to the first CPU in the
	 * assigned cluster.
	 */

	memset(tp, 0, sizeof(*tp));

	tp->cls = RT_CLASS_SOFT;
	tp->priority = LITMUS_LOWEST_PRIORITY;
	tp->budget_policy = NO_ENFORCEMENT;
	tp->release_policy = TASK_SPORADIC;
}

task_class_t str2class(const char* str)
{
	if      (!strcmp(str, "hrt"))
		return RT_CLASS_HARD;
	else if (!strcmp(str, "srt"))
		return RT_CLASS_SOFT;
	else if (!strcmp(str, "be"))
		return RT_CLASS_BEST_EFFORT;
	else
		return -1;
}

#define NS_PER_MS 1000000

int sporadic_global(lt_t e_ns, lt_t p_ns)
{
	struct rt_task param;

	init_rt_task_param(&param);
	param.exec_cost = e_ns;
	param.period = p_ns;

	return set_rt_task_param(gettid(), &param);
}

int sporadic_partitioned(lt_t e_ns, lt_t p_ns, int partition)
{
	int ret;
	struct rt_task param;

	ret = be_migrate_to_domain(partition);
	check("be_migrate_to_domain()");
	if (ret != 0)
		return ret;

	init_rt_task_param(&param);
	param.exec_cost = e_ns;
	param.period = p_ns;
	param.cpu = domain_to_first_cpu(partition);

	return set_rt_task_param(gettid(), &param);
}

int sporadic_clustered(lt_t e_ns, lt_t p_ns, int cluster)
{
	int ret;
	struct rt_task param;

	ret = be_migrate_to_domain(cluster);
	check("be_migrate_to_cluster()");
	if (ret != 0)
		return ret;

	init_rt_task_param(&param);
	param.exec_cost = e_ns;
	param.period = p_ns;
	param.cpu = domain_to_first_cpu(cluster);

	return set_rt_task_param(gettid(), &param);
}

int init_kernel_iface(void);

int init_litmus(void)
{
	int ret, ret2;

	ret = mlockall(MCL_CURRENT | MCL_FUTURE);
	check("mlockall()");
	ret2 = init_rt_thread();
	return (ret == 0) && (ret2 == 0) ? 0 : -1;
}

int init_rt_thread(void)
{
	int ret;

        ret = init_kernel_iface();
	check("kernel <-> user space interface initialization");
	return ret;
}

void exit_litmus(void)
{
	/* nothing to do in current version */
}