File: timer.c

package info (click to toggle)
ocp 1%3A0.1.21-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,344 kB
  • ctags: 7,862
  • sloc: ansic: 91,449; cpp: 9,729; sh: 3,119; makefile: 2,482
file content (348 lines) | stat: -rw-r--r-- 7,165 bytes parent folder | download | duplicates (2)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* OpenCP Module Player
 * copyright (c) '94-'10 Niklas Beisert <nbeisert@physik.tu-muenchen.de>
 *
 * Systemtimer handlers
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * revision history: (please note changes here)
 *  -nb980510   Niklas Beisert <nbeisert@physik.tu-muenchen.de>
 *    -first release
 *  -doj990328  Dirk Jagdmann  <doj@cubic.org>
 *    -changed interrupt access calls to calls from irq.h
 *  -fd990518   Felix Domke  <tmbinc@gmx.net>
 *    -added CLD after the tmOldTimer-call.
 *     this removed the devwmix*-STRANGEBUG. (finally, i hope)
 *  -fd990817   Felix Domke  <tmbinc@gmx.net>
 *    -added tmSetSecure/tmReleaseSecure to ensure that timer is only
 *     called when not "indos". needed for devpVXD (and some other maybe).
 *  -ss040613   Stian Skjelstad <stian@nixia.no>
 *    -Rewritten to use the posix itimer, and posix signals to fetch them
 *  --ss040907  Stian Skjelstad <stian@nixia.no>
 *    -Use gettimeofday() to calculate cpu-usage, since itimer() uses rounded off values
 */

#include "config.h"
#include <signal.h>
#include <stdio.h>
#include <sys/time.h>
#include "types.h"
#include "irq.h"
#include "imsrtns.h"
#include "timer.h"
#include "poll.h"

#define CLOCK_TICK_RATE 1193180 /* Base hertz of the i8253 or what ever that chip was namned - Stian */

static void (*tmTimerRoutine)()=NULL;
static void (*tmTimerRoutineSlave)()=NULL;
static int secure=0;
#ifdef TIMER_DEBUG
static int tmInited = 0;
#endif

#ifdef DISABLE_SIGALRM
#include "compat.h"
void tmTimerHandler(void)
{
	if (!secure)
		if (tmTimerRoutine)
			tmTimerRoutine();
	if (tmTimerRoutineSlave)
		tmTimerRoutineSlave();
}

int tmInit(void (*rout)(), int timerval)
{
	tmTimerRoutine=rout;
#ifdef TIMER_DEBUG
	tmInited=1;
#endif
	return 1;
}

void tmClose(void)
{
#ifdef TIMER_DEBUG
	tmInited=0;
#endif
	tmTimerRoutine=NULL;
}

void tmSetNewRate(int rate)
{
}

time_t tmGetTimer(void)
{
	return dos_clock(); /* this is somewhat off, but we don't care */
}

int  tmGetCpuUsage(void)
{
	return 0;
}

int pollInit(void (*f)(void))
{
	tmTimerRoutineSlave=f;
	return 1;
}
void pollClose(void)
{
	tmTimerRoutineSlave=NULL;
}

#else

static unsigned long count_to_time(unsigned long ticks)
{
	unsigned long retval;

	if (ticks&0xfffff000)
	{
		ticks*=62500; /* this is safe, since counter on the chip is 16 bit */
		retval = ticks / CLOCK_TICK_RATE;
		retval*=16;
	} else {
		ticks*=1000000;
		retval = ticks / CLOCK_TICK_RATE;
	}
	return retval;
}

static unsigned long time_to_count(unsigned long time)
{
	unsigned long retval;

	if (time<=4000)
	{
		retval = time * CLOCK_TICK_RATE / 1000000;
	} else {
		time/=4000;
		retval = time * CLOCK_TICK_RATE / 2500;
	}
	return retval;
}

static unsigned long tmTimerRate;
static unsigned long tmIntCount;
static unsigned long tmTicker;

static volatile char overload;
static volatile float cpuusage;

static int stackused; /* we don't provide a stack, but is for locking */

static void tmTimerHandler(int ignore)
{
	struct timeval pre, pro;

	gettimeofday(&pre, NULL);
	tmTicker+=tmTimerRate;

	tmIntCount+=tmTimerRate;
	if (tmIntCount&0xFFFFc000)
	{
		tmIntCount&=0x3FFF;
		if (tmTimerRoutineSlave)
			tmTimerRoutineSlave();
	}

	if (stackused++)
	{
		stackused--;
		cpuusage=100;
		overload=1;
		return;
	}
	if (!secure)
	{
		if (tmTimerRoutine)
			tmTimerRoutine();
	}
	stackused--;

	if (!overload)
	{
		struct itimerval spec;
		unsigned long spent;

		getitimer(ITIMER_REAL, &spec);
		gettimeofday(&pro, NULL);
		spent=(pro.tv_sec-pre.tv_sec)*1000000+pro.tv_usec-pre.tv_usec;
		cpuusage=0.1*(100.0*spent/spec.it_interval.tv_usec)+0.9*cpuusage;
	} else
		cpuusage=100;
	overload=0;
}

int tmInit(void (*rout)(), int timerval)
{
	struct itimerval spec;

#ifdef TIMER_DEBUG
	if (tmInited)
	{
		fprintf(stderr, "tmInit: we are already inited\n");
	} else {
		tmInited=1;
	}
#endif

	tmTimerRate = timerval;
	tmTicker = -timerval;
#ifdef TIMER_DEBUG
	fprintf(stderr, "tmInit (ticks=%d, time=%ld)\n", timerval, count_to_time(timerval));
#endif
	timerval = count_to_time(timerval);

	tmTimerRoutine=rout;
	tmIntCount=0;

	irqInit(SIGALRM, tmTimerHandler, 1);
	spec.it_interval.tv_sec=0;
	spec.it_interval.tv_usec=timerval;
	spec.it_value.tv_sec=0;
	spec.it_value.tv_usec=timerval;
	setitimer(ITIMER_REAL, &spec, NULL);

	cpuusage=0;

	return 1;
}

static void tmResetTimer(void)
{
#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmResetTimer: we are not inited\n");
#endif
	if (!(tmTimerRoutine&&tmTimerRoutineSlave))
	{
		/* shut down irq */
		struct itimerval spec;

		spec.it_interval.tv_sec=0;
		spec.it_interval.tv_usec=0;
		spec.it_value.tv_sec=0;
		spec.it_value.tv_usec=0;
		setitimer(ITIMER_REAL, &spec, NULL);

		irqDone(SIGALRM);
	} else if (!tmTimerRoutine)
		/* recalc to a less/more aggressive clock */
		tmSetNewRate(17100);

}

void tmClose(void)
{
#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmClose: we are not inited\n");
#endif
	tmTimerRoutine=NULL;
	tmResetTimer();
#ifdef TIMER_DEBUG
	tmInited=0;
#endif
}

void tmSetNewRate(int timerval)
{
	struct itimerval spec;

#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmSetNewRate: we are not inited\n");
#endif

	tmTimerRate = timerval;
#ifdef TIMER_DEBUG
	fprintf(stderr, "tmSetNewRate (ticks=%d, time=%ld)\n", timerval, count_to_time(timerval));
#endif

	timerval = count_to_time(timerval);

	spec.it_interval.tv_sec=0;
	spec.it_interval.tv_usec=timerval;
	spec.it_value.tv_sec=0;
	spec.it_value.tv_usec=timerval;
	setitimer(ITIMER_REAL, &spec, NULL);
}

time_t tmGetTimer(void)
{
	unsigned long tm = tmTimerRate+tmTicker;

	unsigned long tv2;
	struct itimerval spec;

#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmGetTimer: we are not inited\n");
#endif

	getitimer(ITIMER_REAL, &spec);
	tv2 = time_to_count(spec.it_value.tv_usec);

	tm -= tv2;
	return umulshr16(tm, 3600);
}

int tmGetCpuUsage()
{
#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmGetCpuUsage: we are not inited\n");
#endif
	return (char)cpuusage;
}

int pollInit(void (*proc)(void))
{
	tmTimerRoutineSlave=proc;
	if (!tmTimerRoutine) /* If no timer active, just start to count */
		tmInit(NULL, 17100);
	return 1;
}

void pollClose(void)
{
	tmTimerRoutineSlave=NULL;
	tmResetTimer();
}

#endif

void tmSetSecure()
{
#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmSecure: we are not inited\n");
#endif
	secure++;
}

void tmReleaseSecure(void)
{
#ifdef TIMER_DEBUG
	if (!tmInited)
		fprintf(stderr, "tmReleaseSecure: we are not inited\n");
#endif
	if (secure)
		secure--;
}