Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/power/clock.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname power/clock.c 1990/0227
## diff -e /dev/null /n/bootesdump/1990/0227/sys/src/9/mips/clock.c
0a
#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 = ms/MS2HZ;
	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	/* later, make this a profiling clock */
#define	TIME2	1024
#define	CTR(x)	((x)<<6)	/* which counter x */
#define	SET16	0x30		/* lsbyte then msbyte */
#define	MODE2	0x04		/* interval timer */

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;
}

void
clock(ulong n)
{
	int i;
	Alarm *a;
	void (*f)(void*);
	Proc *p;

	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--;
				while(a && a->dt<=0){
					f = a->f;	/* avoid race with cancel */
					if(f)
						(*f)(a);
					delete(&m->alarm, 0, a);
					a->busy = 0;
					a = m->alarm;
				}
			}
			unlock(&m->alarmlock);
		}
		return;
	}
	if(n & INTR4){
		i = *CLRTIM1;
		return;
	}
}
.
## diffname power/clock.c 1990/0614
## diff -e /n/bootesdump/1990/0227/sys/src/9/mips/clock.c /n/bootesdump/1990/0614/sys/src/9/mips/clock.c
20c
	a->dt = MS2TK(ms);
.
## diffname power/clock.c 1990/0728
## diff -e /n/bootesdump/1990/0614/sys/src/9/mips/clock.c /n/bootesdump/1990/0728/sys/src/9/mips/clock.c
181,182c
				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);
.
178d
173,176c
				for(na = 0; a && a->dt<=0 && na<NA; na++){
					alist[na] = a;
.
144a
	Alarm *alist[NA];
.
141c
	int i, na;
.
137a
#define NA 10
.
## diffname power/clock.c 1990/0731
## diff -e /n/bootesdump/1990/0728/sys/src/9/mips/clock.c /n/bootesdump/1990/0731/sys/src/9/mips/clock.c
195a
#ifdef	PROFILING
		pc -= (ulong)&start;
		pc /= sizeof(ulong);
		if(pc < NPROF)
			profcnt[m->machno*NPROF+pc]++;
#endif
.
194a
		extern ulong start;

.
140c
clock(ulong n, ulong pc)
.
137a


.
116a

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

.
111c
#define	TIME1	0xFFFFFFFF	/* profiling disabled */
.
109a

.
## diffname power/clock.c 1990/0802
## diff -e /n/bootesdump/1990/0731/sys/src/9/mips/clock.c /n/bootesdump/1990/0802/sys/src/9/mips/clock.c
119c
/* #define	PROFILING /**/
.
## diffname power/clock.c 1990/1106
## diff -e /n/bootesdump/1990/0802/sys/src/9/mips/clock.c /n/bootesdump/1990/1106/sys/src/9/mips/clock.c
195c
				for(i = 0; i < n; i++){
.
191a
				if(a)
					a->dt--;
.
184,188c
			a = m->alarm;
			if(a){
				for(n=0; a && a->dt<=0 && n<NA; n++){
					alist[n] = a;
.
## diffname power/clock.c 1990/1211
## diff -e /n/bootesdump/1990/1106/sys/src/9/mips/clock.c /n/bootesdump/1990/1211/sys/src/9/mips/clock.c
183,204c
		checkalarms();
.
158d
154,156c
	int i;
.
150d
41,79d
10,39d
## diffname power/clock.c 1990/1214
## diff -e /n/bootesdump/1990/1211/sys/src/9/mips/clock.c /n/bootesdump/1990/1214/sys/src/9/mips/clock.c
116a
		USED(i);
.
88a
		USED(i);
.
75a
	USED(i);
.
74a
	USED(i);
.
## diffname power/clock.c 1991/0209
## diff -e /n/bootesdump/1991/0201/sys/src/9/mips/clock.c /n/bootesdump/1991/0209/sys/src/9/power/clock.c
116c
	if(ur->cause & INTR4){
.
113a
		p = m->proc;
		if((ur->status&IEP) && p && p->state==Running){
			if(anyready())
				sched();
			if(u->nnote && (ur->status&KUP))
				notify(ur);
		}
.
106d
102c
					if(p)
.
95,96d
89c
	if(ur->cause & INTR2){
.
84c
clock(Ureg *ur)
.
## diffname power/clock.c 1991/0215
## diff -e /n/bootesdump/1991/0209/sys/src/9/power/clock.c /n/bootesdump/1991/0215/sys/src/9/power/clock.c
111,112c
		if(u && (ur->status&IEP) && u->p && u->p->state==Running){
.
## diffname power/clock.c 1991/0607
## diff -e /n/bootesdump/1991/0215/sys/src/9/power/clock.c /n/bootesdump/1991/0607/sys/src/9/power/clock.c
105a
		duartslave();
.
104d
## diffname power/clock.c 1991/0614
## diff -e /n/bootesdump/1991/0607/sys/src/9/power/clock.c /n/bootesdump/1991/0614/sys/src/9/power/clock.c
112,113c
			if(anyready()){
				if(u->p->hasspin)
					u->p->hasspin = 0;	/* just in case */
				else
					sched();
			}
.
## diffname power/clock.c 1991/0710
## diff -e /n/bootesdump/1991/0614/sys/src/9/power/clock.c /n/bootesdump/1991/0710/sys/src/9/power/clock.c
118,119c
			if(ur->status & KUP){
				(*(ulong*)(USTKTOP-BY2WD)) += TK2MS(1);	/* profiling clock */
				if(u->nnote)
					notify(ur);
			}
.
## diffname power/clock.c 1991/1011
## diff -e /n/bootesdump/1991/0710/sys/src/9/power/clock.c /n/bootesdump/1991/1011/sys/src/9/power/clock.c
131,136d
110a
		kproftimer(ur->pc);
.
50,56d
## diffname power/clock.c 1991/1110
## diff -e /n/bootesdump/1991/1011/sys/src/9/power/clock.c /n/bootesdump/1991/1110/sys/src/9/power/clock.c
114,115c
				notify(ur);
.
## diffname power/clock.c 1991/1112
## diff -e /n/bootesdump/1991/1110/sys/src/9/power/clock.c /n/bootesdump/1991/1112/sys/src/9/power/clock.c
96a
			nrun = (nrdy+nrun)*1000;
			m->load = (m->load+nrun)/2;
.
94a
						nrun++;
					}
.
93c
					if(p) {
.
89a
			}
.
88c
			if(p) {
				nrun++;
.
79c
	int i, nrun = 0;
.
## diffname power/clock.c 1991/1113
## diff -e /n/bootesdump/1991/1112/sys/src/9/power/clock.c /n/bootesdump/1991/1113/sys/src/9/power/clock.c
102c
			m->load = (m->load*19+nrun)/20;
.
## diffname power/clock.c 1991/1114
## diff -e /n/bootesdump/1991/1113/sys/src/9/power/clock.c /n/bootesdump/1991/1114/sys/src/9/power/clock.c
120,121d
118c
			if(ur->status & KUP)
.
## diffname power/clock.c 1992/0321
## diff -e /n/bootesdump/1991/1114/sys/src/9/power/clock.c /n/bootesdump/1992/0321/sys/src/9/power/clock.c
2c
#include	"../port/lib.h"
.
## diffname power/clock.c 1992/0509
## diff -e /n/bootesdump/1992/0321/sys/src/9/power/clock.c /n/bootesdump/1992/0509/sys/src/9/power/clock.c
85a
		if(m->ticks&(1<<4))
			LEDON(LEDpulse);
		else
			LEDOFF(LEDpulse);

.
## diffname power/clock.c 1992/0711
## diff -e /n/bootesdump/1992/0509/sys/src/9/power/clock.c /n/bootesdump/1992/0711/sys/src/9/power/clock.c
13d
## diffname power/clock.c 1992/0805
## diff -e /n/bootesdump/1992/0711/sys/src/9/power/clock.c /n/bootesdump/1992/0805/sys/src/9/power/clock.c
88a
		if(m->proc)
			m->proc->pc = ur->pc;
.
## diffname power/clock.c 1992/0812
## diff -e /n/bootesdump/1992/0805/sys/src/9/power/clock.c /n/bootesdump/1992/0812/sys/src/9/power/clock.c
113c
			exit(0);
.
## diffname power/clock.c 1992/0816
## diff -e /n/bootesdump/1992/0812/sys/src/9/power/clock.c /n/bootesdump/1992/0816/sys/src/9/power/clock.c
117c
		if(u && (ur->status&IEP) && u->p->state==Running){
.
## diffname power/clock.c 1992/0828
## diff -e /n/bootesdump/1992/0816/sys/src/9/power/clock.c /n/bootesdump/1992/0828/sys/src/9/power/clock.c
125c
				(*(ulong*)(USTKTOP-BY2WD)) += TK2MS(1);	
.
123a
			/* user profiling clock */
.
118c
			if(anyready()) {
.
## diffname power/clock.c 1992/1125
## diff -e /n/bootesdump/1992/0828/sys/src/9/power/clock.c /n/bootesdump/1992/1125/sys/src/9/power/clock.c
110a
		if((active.machs&(1<<m->machno)) == 0)
			return;
.
## diffname power/clock.c 1993/0501
## diff -e /n/bootesdump/1992/1125/sys/src/9/power/clock.c /n/fornaxdump/1993/0501/sys/src/brazil/power/clock.c
138c

	if(anyready())
		sched();

	/* user profiling clock */
	if(ur->status & KUP)
		(*(ulong*)(USTKTOP-BY2WD)) += TK2MS(1);	
.
135,136c
	checkalarms();
	kproftimer(ur->pc);

	if(up == 0 || (ur->status&IEP) == 0 || up->state != Running)
.
132,133d
130a

	if(active.exiting && active.machs&(1<<m->machno)) {
		print("someone's exiting\n");
		exit(0);
.
110,129c
		nrun = (nrdy+nrun)*1000;
		m->load = (m->load*19+nrun)/20;
	}
	duartslave();
	if((active.machs&(1<<m->machno)) == 0)
.
107,108d
92,104c
	m->ticks++;
	if(m->proc)
		m->proc->pc = ur->pc;

	if(m->machno == 0) {
		p = m->proc;
		if(p) {
			nrun++;
			p->time[p->insyscall]++;
		}
		for(i=1; i<conf.nmach; i++) {
			if(active.machs & (1<<i)) {
				p = MACHP(i)->proc;
				if(p) {
					p->time[p->insyscall]++;
					nrun++;
.
81,90c
	if((ur->cause&INTR2) == 0) {
		if(ur->cause & INTR4) {
			i = *CLRTIM1;
			USED(i);
		}
		return;
	}
	nrun = 0;
	i = *CLRTIM0;
	USED(i);
.
79a
	int i, nrun;
.
78d
73,74d
48,49d
26c
 * To get 100Hz, 36864==9*4096=36*1024 so clock ctr2
 * every 1024 and ctr0 every 36.
.
23,24c
 * ctr2 is clocked at 3.6864 MHz. ctr2 output clocks ctr0 and ctr1.
.
15c
	ms *= 7000;		/* experimentally determined */
.
## diffname power/clock.c 1995/0104
## diff -e /n/fornaxdump/1993/0501/sys/src/brazil/power/clock.c /n/fornaxdump/1995/0104/sys/src/brazil/power/clock.c
92,109c
	accounttime();
.
74,75c
	int i;
.
## diffname power/clock.c 1995/0108
## diff -e /n/fornaxdump/1995/0104/sys/src/brazil/power/clock.c /n/fornaxdump/1995/0108/sys/src/brazil/power/clock.c
83d
## diffname power/clock.c 1995/0109
## diff -e /n/fornaxdump/1995/0108/sys/src/brazil/power/clock.c /n/fornaxdump/1995/0109/sys/src/brazil/power/clock.c
106c
	if(anyready() && !up->inlock)
.
## diffname power/clock.c 1995/0110
## diff -e /n/fornaxdump/1995/0109/sys/src/brazil/power/clock.c /n/fornaxdump/1995/0110/sys/src/brazil/power/clock.c
106c
	if(anyready())
.
## diffname power/clock.c 1997/0327 # deleted
## diff -e /n/fornaxdump/1995/0110/sys/src/brazil/power/clock.c /n/emeliedump/1997/0327/sys/src/brazil/power/clock.c
1,112d

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].