aboutsummaryrefslogtreecommitdiffstats
path: root/include/litmus.h
blob: ca8328f657bf53bc92fa2dc7cec10a116d7cbfd9 (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
#ifndef LITMUS_H
#define LITMUS_H

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>
#include <stdint.h>
#include <setjmp.h>

/* Include kernel header.
 * This is required for the rt_param
 * and control_page structures.
 */
#include "litmus/rt_param.h"
#include "litmus/signal.h"

#include "asm/cycles.h" /* for null_call() */

typedef int pid_t;	 /* PID of a task */

/* obtain the PID of a thread */
pid_t gettid(void);

/* migrate to partition */
int be_migrate_to(int target_cpu);

int set_rt_task_param(pid_t pid, struct rt_task* param);
int get_rt_task_param(pid_t pid, struct rt_task* param);

/* setup helper */

/* times are given in ms */
int sporadic_task(
		lt_t e, lt_t p, lt_t phase,
		int partition, task_class_t cls,
		budget_policy_t budget_policy,
		budget_signal_policy_t budget_signal_policy,
		int set_cpu_set);

/* times are given in ns */
int sporadic_task_ns(
		lt_t e, lt_t p, lt_t phase,
		int cpu, task_class_t cls,
		budget_policy_t budget_policy,
		budget_signal_policy_t budget_signal_policy,
		int set_cpu_set);

/* budget enforcement off by default in these macros */
#define sporadic_global(e, p) \
	sporadic_task(e, p, 0, 0, RT_CLASS_SOFT, NO_ENFORCEMENT, NO_SIGNALS, 0)
#define sporadic_partitioned(e, p, cpu) \
	sporadic_task(e, p, 0, cpu, RT_CLASS_SOFT, NO_ENFORCEMENT, NO_SIGNALS, 1)

/* file descriptor attached shared objects support */
typedef enum  {
	FMLP_SEM	= 0,
	SRP_SEM		= 1,
} obj_type_t;

int od_openx(int fd, obj_type_t type, int obj_id, void* config);
int od_close(int od);

static inline int od_open(int fd, obj_type_t type, int obj_id)
{
	return od_openx(fd, type, obj_id, 0);
}

/* real-time locking protocol support */
int litmus_lock(int od);
int litmus_unlock(int od);

/* job control*/
int get_job_no(unsigned int* job_no);
int wait_for_job_release(unsigned int job_no);
int sleep_next_period(void);

/*  library functions */
int  init_litmus(void);
int  init_rt_thread(void);
void exit_litmus(void);

/* A real-time program. */
typedef int (*rt_fn_t)(void*);

/* These two functions configure the RT task to use enforced exe budgets */
int create_rt_task(rt_fn_t rt_prog, void *arg, int cpu, int wcet, int period);
int __create_rt_task(rt_fn_t rt_prog, void *arg, int cpu, int wcet,
		     int period, task_class_t cls);

/*	per-task modes */
enum rt_task_mode_t {
	BACKGROUND_TASK = 0,
	LITMUS_RT_TASK  = 1
};
int task_mode(int target_mode);

void show_rt_param(struct rt_task* tp);
task_class_t str2class(const char* str);

/* non-preemptive section support */
void enter_np(void);
void exit_np(void);
int  requested_to_preempt(void);

/* task system support */
int wait_for_ts_release(void);
int release_ts(lt_t *delay);

#define __NS_PER_MS 1000000

static inline lt_t ms2lt(unsigned long milliseconds)
{
	return __NS_PER_MS * milliseconds;
}

/* CPU time consumed so far in seconds */
double cputime(void);

/* wall-clock time in seconds */
double wctime(void);

/* semaphore allocation */

static inline int open_fmlp_sem(int fd, int name)
{
	return od_open(fd, FMLP_SEM, name);
}

static inline int open_srp_sem(int fd, int name)
{
	return od_open(fd, SRP_SEM, name);
}


/* syscall overhead measuring */
int null_call(cycles_t *timestamp);

/*
 * get control page:
 * atm it is used only by preemption migration overhead code
 * but it is very general and can be used for different purposes
 */
struct control_page* get_ctrl_page(void);


/* Litmus signal handling */

typedef struct litmus_sigjmp
{
	sigjmp_buf env;
	struct litmus_sigjmp *prev;
} litmus_sigjmp_t;

void push_sigjmp(litmus_sigjmp_t* buf);
litmus_sigjmp_t* pop_sigjmp(void);

typedef void (*litmus_sig_handler_t)(int);
typedef void (*litmus_sig_actions_t)(int, siginfo_t *, void *);

/* ignore specified signals. all signals raised while ignored are dropped */
void ignore_litmus_signals(unsigned long litmus_sig_mask);

/* register a handler for the given set of litmus signals */
void activate_litmus_signals(unsigned long litmus_sig_mask,
				litmus_sig_handler_t handler);

/* register an action signal handler for a given set of signals */
void activate_litmus_signal_actions(unsigned long litmus_sig_mask,
				litmus_sig_actions_t handler);

/* Block a given set of litmus signals. Any signals raised while blocked
 * are queued and delivered after unblocking. Call ignore_litmus_signals()
 * before unblocking if you wish to discard these. Blocking may be
 * useful to protect COTS code in Litmus that may not be able to deal
 * with exception-raising signals.
 */
void block_litmus_signals(unsigned long litmus_sig_mask);

/* Unblock a given set of litmus signals. */
void unblock_litmus_signals(unsigned long litmus_sig_mask);

#define SIG_BUDGET_MASK			0x00000001
/* more ... */

#define ALL_LITMUS_SIG_MASKS	(SIG_BUDGET_MASK)

/* Try/Catch structures useful for implementing abortable jobs.
 * Should only be used in legitimate cases. ;)
 */
#define LITMUS_TRY \
do { \
	int sigsetjmp_ret_##__FUNCTION__##__LINE__; \
	litmus_sigjmp_t lit_env_##__FUNCTION__##__LINE__; \
	push_sigjmp(&lit_env_##__FUNCTION__##__LINE__); \
	sigsetjmp_ret_##__FUNCTION__##__LINE__ = \
		sigsetjmp(lit_env_##__FUNCTION__##__LINE__.env, 1); \
	if (sigsetjmp_ret_##__FUNCTION__##__LINE__ == 0) {

#define LITMUS_CATCH(x) \
	} else if (sigsetjmp_ret_##__FUNCTION__##__LINE__ == (x)) {

#define END_LITMUS_TRY \
	} /* end if-else-if chain */ \
} while(0); /* end do from 'LITMUS_TRY' */

/* Calls siglongjmp(signum). Use with TRY/CATCH.
 * Example:
 *  activate_litmus_signals(SIG_BUDGET_MASK, longjmp_on_litmus_signal);
 */
void longjmp_on_litmus_signal(int signum);

#ifdef __cplusplus
}
#endif




#ifdef __cplusplus
/* Expose litmus exceptions if C++.
 *
 * KLUDGE: We define everything in the header since liblitmus is a C-only
 * library, but this header could be included in C++ code.
 */

#include <exception>

namespace litmus
{
	class litmus_exception: public std::exception
	{
	public:
		litmus_exception() throw() {}
		virtual ~litmus_exception() throw() {}
		virtual const char* what() const throw() { return "litmus_exception";}
	};

	class sigbudget: public litmus_exception
	{
	public:
		sigbudget() throw() {}
		virtual ~sigbudget() throw() {}
		virtual const char* what() const throw() { return "sigbudget"; }
	};

	/* Must compile your program with "non-call-exception". */
	static __attribute__((used))
	void throw_on_litmus_signal(int signum)
	{
		/* We have to unblock the received signal to get more in the future
		 * because we are not calling siglongjmp(), which normally restores
		 * the mask for us.
		 */
		if (SIG_BUDGET == signum) {
			unblock_litmus_signals(SIG_BUDGET_MASK);
			throw sigbudget();
		}
		/* else if (...) */
		else {
			/* silently ignore */
		}
	}

}; /* end namespace 'litmus' */

#endif /* end __cplusplus */

#endif