summaryrefslogtreecommitdiff
path: root/src/rt-app_utils.c
blob: 31e418c9d577516776d9a5dc0f6363704d8e0023 (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
/* 
This file is part of rt-app - https://launchpad.net/rt-app
Copyright (C) 2010  Giacomo Bagnoli <g.bagnoli@asidev.com>

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 "rt-app_utils.h"

unsigned int 
timespec_to_msec(struct timespec *ts)
{
	return (ts->tv_sec * 1E9 + ts->tv_nsec) / 1000000;
}

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

unsigned long 
timespec_to_usec(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;
}

void
log_timing(FILE *handler, timing_point_t *t)
{
	fprintf(handler, 
		"%d\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%ld",
		t->ind,
		t->period,
		t->min_et,
		t->max_et,
		t->rel_start_time,
		t->abs_start_time,
		t->end_time,
		t->deadline,
		t->duration,
		t->slack
	);
#ifdef AQUOSA
	fprintf(handler,
		"\t" QRES_TIME_FMT "\t" QRES_TIME_FMT,
		t->budget, 
		t->used_budget
	);
#endif
	fprintf(handler, "\n");
}

#ifdef DLSCHED
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
#ifdef AQUOSA
	else if ( (strcmp(policy_name, "AQUOSA") == 0) || \
		  (strcmp(policy_name, "AQuoSA") == 0))
		*policy =  aquosa;
#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
#ifdef AQUOSA
		case aquosa:
			strcpy(policy_name, "AQuoSA");
			break;
#endif
		default:
			return 1;
	}
	return 0;
}

#endif

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

	if (mark_fd < 0)
		return -1;

	if ((tmp = malloc(size)) == NULL)
		return -1;
	
	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) {
			write(mark_fd, tmp, n);
			free(tmp);
			return 1;
		}
		/* 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);
			return -1;
		} else {
			tmp = ntmp;
		}
	}

}