~kris/9p

9hist

ref: 2387cf3d28f3a9b308b4e7ca76a6a38e7c8a65f3 9hist/port/devkprof.c -rw-r--r-- 2.6 KiB
2387cf3d — David du Colombier Plan 9 from Bell Labs 1991-04-07 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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"

#include	"devtab.h"

#define	MAXPC	(100*1024L)
#define	RES	8
#define	LRES	3
#define	NBUF	(MAXPC>>LRES)

long		timerbuf[NBUF];

enum{
	Kprofdirqid,
	Kprofdataqid,
	Kprofstartqid,
	Kprofstartclrqid,
	Kprofstopqid,
	Nkproftab=Kprofstopqid,
	Kprofmaxqid,
};
Dirtab kproftab[Nkproftab]={
	"kpdata",	Kprofdataqid,		NBUF*sizeof timerbuf[0],	0600,
	"kpstart",	Kprofstartqid,		0,		0600,
	"kpstartclr",	Kprofstartclrqid,	0,		0600,
	"kpstop",	Kprofstopqid,		0,		0600,
};

void kproftimer(ulong);

void
kprofreset(void)
{
	kprofp = kproftimer;
}

void
kprofinit(void)
{
	extern void *etext;
	if((((unsigned long)&etext)-KTZERO)>MAXPC)
		print("kernel profiling limited to %lud\n", MAXPC);
}

Chan *
kprofattach(char *spec)
{
	return devattach('t', spec);
}
Chan *
kprofclone(Chan *c, Chan *nc)
{
	return devclone(c, nc);
}

int
kprofwalk(Chan *c, char *name)
{
	return devwalk(c, name, kproftab, (long)Nkproftab, devgen);
}

void
kprofstat(Chan *c, char *db)
{
	devstat(c, db, kproftab, (long)Nkproftab, devgen);
}

Chan *
kprofopen(Chan *c, int omode)
{
	if(c->qid == CHDIR){
		if(omode != OREAD)
			error(0, Eperm);
	}
	c->mode = openmode(omode);
	c->flag |= COPEN;
	c->offset = 0;
	return c;
}

void
kprofcreate(Chan *c, char *name, int omode, ulong perm)
{
	error(0, Eperm);
}

void
kprofremove(Chan *c)
{
	error(0, Eperm);
}

void
kprofwstat(Chan *c, char *dp)
{
	error(0, Eperm);
}

void
kprofclose(Chan *c)
{
}

void
kprofuserstr(Error *e, char *buf)
{
	consuserstr(e, buf);
}

void	 
kproferrstr(Error *e, char *buf)
{
	rooterrstr(e, buf);
}

long
kprofread(Chan *c, void *a, long n)
{
	switch((int)(c->qid&~CHDIR)){
	case Kprofdirqid:
		return devdirread(c, a, n, kproftab, Nkproftab, devgen);
	case Kprofdataqid:
		if(c->offset >= NBUF*sizeof timerbuf[0]){
			n = 0;
			break;
		}
		if(c->offset+n > NBUF*sizeof timerbuf[0])
			n = NBUF*sizeof timerbuf[0]-c->offset;
		memmove(a, ((char *)timerbuf)+c->offset, n);
		break;
	default:
		n=0;
		break;
	}
	return n;
}

long
kprofwrite(Chan *c, char *a, long n)
{
	switch((int)(c->qid&~CHDIR)){
	case Kprofstartclrqid:
		memset((char *)timerbuf, 0, NBUF*sizeof timerbuf[0]);
	case Kprofstartqid:
		duartstarttimer();
		break;
	case Kprofstopqid:
		duartstoptimer();
		break;
	default:
		error(0, Ebadusefd);
	}
	return n;
}

void
kproftimer(ulong pc)
{
	/*
	 *  if the pc is coming out of slplo pr splx, then use
	 *  the pc saved when we went splhi.
	 */
	if(pc>=(ulong)spllo && pc<=(ulong)spldone)
		pc = m->splpc;

	timerbuf[0]++;
	if(KTZERO<=pc && pc<KTZERO+MAXPC){
		pc -= KTZERO;
		pc >>= LRES;
		timerbuf[pc]++;
	} else
		timerbuf[1]++;
}