~kris/9p

9hist

ref: d6bbe2d86ff53ed2dcf484ca6c7d4f7f33360caf 9hist/power/clock.c -rw-r--r-- 3.5 KiB
d6bbe2d8 — David du Colombier Plan 9 from Bell Labs 1990-10-22 35 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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"

#include	"ureg.h"

Alarm	*alarmtab;

Alarm*
alarm(int ms, void (*f)(Alarm*), void *arg)
{
	Alarm *a, *w, *pw;
	ulong s;
	if(ms < 0)
		ms = 0;
	a = newalarm();
	a->dt = MS2TK(ms);
	a->f = f;
	a->arg = arg;
	s = splhi();
	lock(&m->alarmlock);
	pw = 0;
	for(w=m->alarm; w; pw=w, w=w->next){
		if(w->dt <= a->dt){
			a->dt -= w->dt;
			continue;
		}
		w->dt -= a->dt;
		break;
	}
	insert(&m->alarm, pw, a);
	unlock(&m->alarmlock);
	splx(s);
	return a;
}

void
cancel(Alarm *a)
{
	a->f = 0;
}

Alarm*
newalarm(void)
{
	int i;
	Alarm *a;

	for(i=0,a=alarmtab; i<conf.nalarm; i++,a++)
		if(a->busy==0 && a->f==0 && canlock(a)){
			if(a->busy){
				unlock(a);
				continue;
			}
			a->f = 0;
			a->arg = 0;
			a->busy = 1;
			unlock(a);
			return a;
		}
	panic("newalarm");
}

void
alarminit(void)
{
	int i;

	alarmtab = ialloc(conf.nalarm*sizeof(Alarm), 0);
	for(i=0; i<conf.nalarm; i++){
		lock(&alarmtab[i]);	/* allocate locks, as they are used at interrupt time */
		unlock(&alarmtab[i]);
	}
}

void
delay(int ms)
{
	ulong t, *p;
	int i;

	ms *= 7000;	/* experimentally determined */
	for(i=0; i<ms; i++)
		;
}

/*
 * AMD 82C54 timer
 *
 * ctr2 is clocked at 3.6864 MHz.
 * ctr2 output clocks ctr0 and ctr1.
 * ctr0 drives INTR2.  ctr1 drives INTR4.
 * To get 100Hz, 36864==9*4096=36*1024 so clock ctr2 every 1024 and ctr0 every 36.
 */

struct Timer{
	uchar	cnt0,
		junk0[3];
	uchar	cnt1,
		junk1[3];
	uchar	cnt2,
		junk2[3];
	uchar	ctl,
		junk3[3];
};


#define	TIME0	(36*MS2HZ/10)
#define	TIME1	0xFFFFFFFF	/* profiling disabled */
#define	TIME2	1024
#define	CTR(x)	((x)<<6)	/* which counter x */
#define	SET16	0x30		/* lsbyte then msbyte */
#define	MODE2	0x04		/* interval timer */


/* #define	PROFILING /**/
#ifdef PROFILING
#undef TIME1
#define	TIME1	211		/* profiling clock; prime; about 10ms per tick */
#define	NPROF	50000
ulong	profcnt[MAXMACH*NPROF];
#endif

void
clockinit(void)
{
	Timer *t;
	int i;

	t = TIMERREG;
	t->ctl = CTR(2)|SET16|MODE2;
	t->cnt2 = TIME2&0xFF;
	t->cnt2 = (TIME2>>8)&0xFF;
	t->ctl = CTR(1)|SET16|MODE2;
	t->cnt1 = TIME1&0xFF;
	t->cnt1 = (TIME1>>8)&0xFF;
	t->ctl = CTR(0)|SET16|MODE2;
	t->cnt0 = TIME0;
	t->cnt0 = (TIME0>>8)&0xFF;
	i = *CLRTIM0;
	i = *CLRTIM1;
	m->ticks = 0;
}



#define NA 10
void
clock(ulong n, ulong pc)
{
	int i, na;
	Alarm *a;
	void (*f)(void*);
	Proc *p;
	Alarm *alist[NA];

	if(n&INTR2){
		i = *CLRTIM0;
		m->ticks++;
		if(m->machno == 0){
			p = m->proc;
			if(p == 0)
				p = m->intrp;
			if(p)
				p->time[p->insyscall]++;
			for(i=1; i<conf.nmach; i++){
				if(active.machs & (1<<i)){
					p = MACHP(i)->proc;
					if(p && p!=m->intrp)
						p->time[p->insyscall]++;
				}
			}
			m->intrp = 0;
			printslave();
		}
		if(active.exiting && active.machs&(1<<m->machno)){
			print("someone's exiting\n");
			exit();
		}
		if(canlock(&m->alarmlock)){
			if(m->alarm){
				a = m->alarm;
				a->dt--;
				for(na = 0; a && a->dt<=0 && na<NA; na++){
					alist[na] = a;
					delete(&m->alarm, 0, a);
					a = m->alarm;
				}
				unlock(&m->alarmlock);
	
				/*  execute alarm functions outside the lock */
				for(i = 0; i < na; i++){
					f = alist[i]->f;	/* avoid race with cancel */
					if(f)
						(*f)(alist[i]);
					alist[i]->busy = 0;
				}
			} else
				unlock(&m->alarmlock);
		}
		return;
	}
	if(n & INTR4){
		extern ulong start;

		i = *CLRTIM1;
#ifdef	PROFILING
		pc -= (ulong)&start;
		pc /= sizeof(ulong);
		if(pc < NPROF)
			profcnt[m->machno*NPROF+pc]++;
#endif
		return;
	}
}