summaryrefslogtreecommitdiff
path: root/src/rt-app_utils.c
blob: ea5724d8c52316c78ce65837d46e68e0bfb8c05e (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
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
/* 
This file is part of rt-app - https://launchpad.net/rt-app
Copyright (C) 2010  Giacomo Bagnoli <g.bagnoli@asidev.com>
Copyright (C) 2014  Juri Lelli <juri.lelli@gmail.com>
Copyright (C) 2014  Vincent Guittot <vincent.guittot@linaro.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <errno.h>
#include <string.h>
#include "rt-app_utils.h"

unsigned long
timespec_to_usec(struct timespec *ts)
{
	return lround((ts->tv_sec * 1E9 + ts->tv_nsec) / 1000.0);
}

unsigned long long
timespec_to_usec_ull(struct timespec *ts)
{
	return llround((ts->tv_sec * 1E9 + ts->tv_nsec) / 1000.0);
}

long
timespec_to_usec_long(struct timespec *ts)
{
	return round((ts->tv_sec * 1E9 + ts->tv_nsec) / 1000.0);
}

#ifdef DLSCHED
__u64
timespec_to_nsec(struct timespec *ts)
{
	return round(ts->tv_sec * 1E9 + ts->tv_nsec);
}
#endif

struct timespec
usec_to_timespec(unsigned long usec)
{
	struct timespec ts;

	ts.tv_sec = usec / 1000000;
	ts.tv_nsec = (usec % 1000000) * 1000;

	return ts;
}

struct timespec
msec_to_timespec(unsigned int msec)
{
	struct timespec ts;

	ts.tv_sec = msec / 1000;
	ts.tv_nsec = (msec % 1000) * 1000000;

	return ts;
}

struct timespec
timespec_add(struct timespec *t1, struct timespec *t2)
{
	struct timespec ts;

	ts.tv_sec = t1->tv_sec + t2->tv_sec;
	ts.tv_nsec = t1->tv_nsec + t2->tv_nsec;

	while (ts.tv_nsec >= 1E9) {
		ts.tv_nsec -= 1E9;
		ts.tv_sec++;
	}

	return ts;
}

struct timespec
timespec_sub(struct timespec *t1, struct timespec *t2)
{
	struct timespec ts;

	if (t1->tv_nsec < t2->tv_nsec) {
		ts.tv_sec = t1->tv_sec - t2->tv_sec -1;
		ts.tv_nsec = t1->tv_nsec  + 1000000000 - t2->tv_nsec;
	} else {
		ts.tv_sec = t1->tv_sec - t2->tv_sec;
		ts.tv_nsec = t1->tv_nsec - t2->tv_nsec;
	}

	return ts;

}

int
timespec_lower(struct timespec *what, struct timespec *than)
{
	if (what->tv_sec > than->tv_sec)
		return 0;

	if (what->tv_sec < than->tv_sec)
		return 1;

	if (what->tv_nsec < than->tv_nsec)
		return 1;

	return 0;
}

int64_t
timespec_sub_to_ns(struct timespec *t1, struct timespec *t2)
{
	int64_t diff;

	if (t1->tv_nsec < t2->tv_nsec) {
		diff = 1E9 * (int64_t)((int) t1->tv_sec -
			(int) t2->tv_sec - 1);
		diff += ((int) t1->tv_nsec + 1E9 - (int) t2->tv_nsec);
	} else {
		diff = 1E9 * (int64_t)((int) t1->tv_sec - (int) t2->tv_sec);
		diff += ((int) t1->tv_nsec - (int) t2->tv_nsec);
	}
	return diff;
}


void
log_timing(FILE *handler, timing_point_t *t)
{
	fprintf(handler,
		"%4d %8lu %8lu %8lu %15llu %15llu %15llu %10ld %10lu %10lu",
		t->ind,
		t->perf,
		t->duration,
		t->period,
		t->start_time,
		t->end_time,
		t->rel_start_time,
		t->slack,
		t->c_period,
		t->wu_latency
	);
	fprintf(handler, "\n");
}

pid_t
gettid(void)
{
	return syscall(__NR_gettid);
}

int
string_to_policy(const char *policy_name, policy_t *policy)
{
	if (strcmp(policy_name, "SCHED_OTHER") == 0)
		*policy = other;
	else if (strcmp(policy_name, "SCHED_RR") == 0)
		*policy =  rr;
	else if (strcmp(policy_name, "SCHED_FIFO") == 0)
		*policy =  fifo;
#ifdef DLSCHED
	else if (strcmp(policy_name, "SCHED_DEADLINE") == 0)
		*policy =  deadline;
#endif
	else
		return 1;
	return 0;
}

int
policy_to_string(policy_t policy, char *policy_name)
{
	switch (policy) {
		case other:
			strcpy(policy_name, "SCHED_OTHER");
			break;
		case rr:
			strcpy(policy_name, "SCHED_RR");
			break;
		case fifo:
			strcpy(policy_name, "SCHED_FIFO");
			break;
#ifdef DLSCHED
		case deadline:
			strcpy(policy_name, "SCHED_DEADLINE");
			break;
#endif
		default:
			return 1;
	}
	return 0;
}

int
string_to_resource(const char *name, resource_t *resource)
{
	if (strcmp(name, "mutex") == 0)
		*resource = rtapp_mutex;
	else if (strcmp(name, "signal") == 0)
		*resource = rtapp_signal;
	else if (strcmp(name, "wait") == 0)
		*resource = rtapp_wait;
	else if (strcmp(name, "broadcast") == 0)
		*resource = rtapp_broadcast;
	else if (strcmp(name, "sync") == 0)
		*resource = rtapp_sig_and_wait;
	else if (strcmp(name, "sleep") == 0)
		*resource = rtapp_sleep;
	else if (strcmp(name, "run") == 0)
		*resource = rtapp_run;
	else if (strcmp(name, "timer") == 0)
		*resource = rtapp_timer;
	else
		return 1;
	return 0;
}

int
resource_to_string(resource_t resource, char *resource_name)
{
	switch (resource) {
		case rtapp_mutex:
			strcpy(resource_name, "mutex");
			break;
		case rtapp_wait:
			strcpy(resource_name, "wait");
			break;
		case rtapp_signal:
			strcpy(resource_name, "signal");
			break;
		case rtapp_broadcast:
			strcpy(resource_name, "broadcast");
			break;
		case rtapp_sig_and_wait:
			strcpy(resource_name, "sync");
			break;
		case rtapp_sleep:
			strcpy(resource_name, "sleep");
			break;
		case rtapp_run:
			strcpy(resource_name, "run");
			break;
		case rtapp_timer:
			strcpy(resource_name, "timer");
			break;
		default:
			return 1;
	}
	return 0;
}

void ftrace_write(int mark_fd, const char *fmt, ...)
{
	va_list ap;
	int n, size = BUF_SIZE, ret;
	char *tmp, *ntmp;

	if (mark_fd < 0) {
		log_error("invalid mark_fd");
		exit(EXIT_FAILURE);
	}

	if ((tmp = malloc(size)) == NULL) {
		log_error("Cannot allocate ftrace buffer");
		exit(EXIT_FAILURE);
	}

	while(1) {
		/* Try to print in the allocated space */
		va_start(ap, fmt);
		n = vsnprintf(tmp, BUF_SIZE, fmt, ap);
		va_end(ap);

		/* If it worked return success */
		if (n > -1 && n < size) {
			ret = write(mark_fd, tmp, n);
			free(tmp);
			if (ret < 0) {
				log_error("Cannot write mark_fd: %s\n",
						strerror(errno));
				exit(EXIT_FAILURE);
			} else if (ret < n) {
				log_debug("Cannot write all bytes at once into mark_fd\n");
			}
			return;
		}

		/* Else try again with more space */
		if (n > -1)	/* glibc 2.1 */
			size = n+1;
		else		/* glibc 2.0 */
			size *= 2;

		if ((ntmp = realloc(tmp, size)) == NULL) {
			free(tmp);
			log_error("Cannot reallocate ftrace buffer");
			exit(EXIT_FAILURE);
		} else {
			tmp = ntmp;
		}
	}

}