~kris/9p

9hist

ref: 81ed157352c5fec4281a3e27dbb3cf48400a5f5f 9hist/port/portclock.c -rw-r--r-- 3.8 KiB
81ed1573 — David du Colombier Plan 9 from Bell Labs 2003-02-08 23 years ago
                                                                                
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
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "ureg.h"

typedef struct Timers Timers;

struct Timers
{
	Lock;
	Timer	*head;
};

static Timers timers[MAXMACH];

ulong intrcount[MAXMACH];
ulong fcallcount[MAXMACH];

static uvlong
tadd(Timers *tt, Timer *nt)
{
	Timer *t, **last, *pt;

	pt = nil;
	for(last = &tt->head; t = *last; last = &t->next){
		if(t == nt){
			/* timer's changing, remove it before putting it back on */
			*last = t->next;
			break;
		}
		if (t->period == nt->period){
			/* look for another timer at same frequency for combining */
			pt = t;
		}
	}

	if(nt->when == 0){
		/* Try to synchronize periods to reduce # of interrupts */
		assert(nt->period);
		if(pt)
			nt->when = pt->when;
		else
			nt->when = (uvlong)fastticks(nil) + nt->period;
	}
	
	for(last = &tt->head; t = *last; last = &t->next){
		if(t->when > nt->when)
			break;
	}
	nt->next = *last;
	*last = nt;
	if (last == &tt->head)
		return nt->when;
	else
		return 0;
}

/* add of modify a timer */
void
timeradd(Timer *nt)
{
	Timers *tt;
	uvlong when;

	tt = &timers[m->machno];
	ilock(tt);
	when = tadd(tt, nt);
	if (when)
		timerset(when);
	iunlock(tt);
}

void
timerdel(Timer *dt)
{
	Timer *t, **last;
	Timers *tt;

	tt = &timers[m->machno];
	ilock(tt);
	for(last = &tt->head; t = *last; last = &t->next){
		if(t == dt){
			*last = t->next;
			break;
		}
	}
	if (last == &tt->head && tt->head)
		timerset(tt->head->when);
	iunlock(tt);
}

void
hzclock(Ureg *ur)
{
	m->ticks++;
	if(m->proc)
		m->proc->pc = ur->pc;

	if(m->flushmmu){
		if(up)
			flushmmu();
		m->flushmmu = 0;
	}

	accounttime();
	kmapinval();

	if(kproftimer != nil)
		kproftimer(ur->pc);

	if((active.machs&(1<<m->machno)) == 0)
		return;

	if(active.exiting && (active.machs & (1<<m->machno))) {
		print("someone's exiting\n");
		exit(0);
	}

	checkalarms();

	if(up == 0 || up->state != Running)
		return;

	/* i.e. don't deschedule an EDF process here! */
	if(anyready() && !edf->isedf(up)){
		sched();
		splhi();
	}

	/* user profiling clock */
	if(userureg(ur)) {
		(*(ulong*)(USTKTOP-BY2WD)) += TK2MS(1);
		segclock(ur->pc);
	}
}

void
timerintr(Ureg *u, uvlong)
{
	Timer *t;
	Timers *tt;
	uvlong when, now;
	int callhzclock;
	ulong pc;
	static int sofar;

	pc = m->splpc;	/* remember last splhi pc for kernel profiling */

	intrcount[m->machno]++;
	callhzclock = 0;
	tt = &timers[m->machno];
	now = fastticks(nil);
	ilock(tt);
	while(t = tt->head){
		when = t->when;
		if(when > now){
			timerset(when);
			iunlock(tt);
			m->splpc = pc;	/* for kernel profiling */
			if(callhzclock)
				hzclock(u);
			return;
		}
		tt->head = t->next;
		fcallcount[m->machno]++;
		iunlock(tt);
		if (t->f){
			(*t->f)(u, t);
			splhi();
		} else
			callhzclock++;
		ilock(tt);
		if(t->period){
			t->when += t->period;
			tadd(tt, t);
		}
	}
	iunlock(tt);
}

uvlong hzperiod;

void
timersinit(void)
{
	Timer *t;

	hzperiod = ms2fastticks(1000/HZ);

	t = malloc(sizeof(*t));
	t->when = 0;
	t->period = hzperiod;
	t->f = nil;
	timeradd(t);
}

void
addclock0link(void (*f)(void), int ms)
{
	Timer *nt;

	/* Synchronize this to hztimer: reduces # of interrupts */
	nt = malloc(sizeof(Timer));
	nt->when = 0;
	if (ms == 0)
		ms = 1000/HZ;
	nt->period = ms2fastticks(ms);
	nt->f = (void (*)(Ureg*, Timer*))f;

	ilock(&timers[0]);
	tadd(&timers[0], nt);
	/* no need to restart timer:
	 * this one's synchronized with hztimer which is already running
	 */
	iunlock(&timers[0]);
}

/*
 *  This tk2ms avoids overflows that the macro version is prone to.
 *  It is a LOT slower so shouldn't be used if you're just converting
 *  a delta.
 */
ulong
tk2ms(ulong ticks)
{
	uvlong t, hz;

	t = ticks;
	hz = HZ;
	t *= 1000L;
	t = t/hz;
	ticks = t;
	return ticks;
}

ulong
ms2tk(ulong ms)
{
	/* avoid overflows at the cost of precision */
	if(ms >= 1000000000/HZ)
		return (ms/1000)*HZ;
	return (ms*HZ+500)/1000;
}