This source file includes following definitions.
- kmstartup
- sysctl_doprof
- sys_profil
- addupc_intr
- addupc_task
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 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/user.h>
40 #include <sys/mount.h>
41 #include <sys/syscallargs.h>
42
43 #include <machine/cpu.h>
44
45 #ifdef GPROF
46 #include <sys/malloc.h>
47 #include <sys/gmon.h>
48 #include <uvm/uvm_extern.h>
49
50
51
52
53 struct gmonparam _gmonparam = { GMON_PROF_OFF };
54
55 extern char etext[];
56
57
58 void
59 kmstartup(void)
60 {
61 char *cp;
62 struct gmonparam *p = &_gmonparam;
63 int size;
64
65
66
67
68
69 p->lowpc = ROUNDDOWN(KERNBASE, HISTFRACTION * sizeof(HISTCOUNTER));
70 p->highpc = ROUNDUP((u_long)etext, HISTFRACTION * sizeof(HISTCOUNTER));
71 p->textsize = p->highpc - p->lowpc;
72 printf("Profiling kernel, textsize=%ld [%lx..%lx]\n",
73 p->textsize, p->lowpc, p->highpc);
74 p->kcountsize = p->textsize / HISTFRACTION;
75 p->hashfraction = HASHFRACTION;
76 p->fromssize = p->textsize / HASHFRACTION;
77 p->tolimit = p->textsize * ARCDENSITY / 100;
78 if (p->tolimit < MINARCS)
79 p->tolimit = MINARCS;
80 else if (p->tolimit > MAXARCS)
81 p->tolimit = MAXARCS;
82 p->tossize = p->tolimit * sizeof(struct tostruct);
83 size = p->kcountsize + p->fromssize + p->tossize;
84 cp = (char *)uvm_km_zalloc(kernel_map, round_page(size));
85 if (cp == 0) {
86 printf("No memory for profiling.\n");
87 return;
88 }
89 p->tos = (struct tostruct *)cp;
90 cp += p->tossize;
91 p->kcount = (u_short *)cp;
92 cp += p->kcountsize;
93 p->froms = (u_short *)cp;
94 }
95
96
97
98
99 int
100 sysctl_doprof(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
101 size_t newlen)
102 {
103 struct gmonparam *gp = &_gmonparam;
104 int error;
105
106
107 if (namelen != 1)
108 return (ENOTDIR);
109
110 switch (name[0]) {
111 case GPROF_STATE:
112 error = sysctl_int(oldp, oldlenp, newp, newlen, &gp->state);
113 if (error)
114 return (error);
115 if (gp->state == GMON_PROF_OFF)
116 stopprofclock(&proc0);
117 else
118 startprofclock(&proc0);
119 return (0);
120 case GPROF_COUNT:
121 return (sysctl_struct(oldp, oldlenp, newp, newlen,
122 gp->kcount, gp->kcountsize));
123 case GPROF_FROMS:
124 return (sysctl_struct(oldp, oldlenp, newp, newlen,
125 gp->froms, gp->fromssize));
126 case GPROF_TOS:
127 return (sysctl_struct(oldp, oldlenp, newp, newlen,
128 gp->tos, gp->tossize));
129 case GPROF_GMONPARAM:
130 return (sysctl_rdstruct(oldp, oldlenp, newp, gp, sizeof *gp));
131 default:
132 return (EOPNOTSUPP);
133 }
134
135 }
136 #endif
137
138
139
140
141
142
143
144
145 int
146 sys_profil(struct proc *p, void *v, register_t *retval)
147 {
148 struct sys_profil_args
149
150
151
152
153 *uap = v;
154 struct uprof *upp;
155 int s;
156
157 if (SCARG(uap, scale) > (1 << 16))
158 return (EINVAL);
159 if (SCARG(uap, scale) == 0) {
160 stopprofclock(p);
161 return (0);
162 }
163 upp = &p->p_stats->p_prof;
164
165
166 s = splstatclock();
167 upp->pr_off = SCARG(uap, offset);
168 upp->pr_scale = SCARG(uap, scale);
169 upp->pr_base = (caddr_t)SCARG(uap, samples);
170 upp->pr_size = SCARG(uap, size);
171 startprofclock(p);
172 splx(s);
173
174 return (0);
175 }
176
177
178
179
180
181
182 #define PC_TO_INDEX(pc, prof) \
183 ((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
184 (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
185
186
187
188
189
190
191
192
193 void
194 addupc_intr(struct proc *p, u_long pc)
195 {
196 struct uprof *prof;
197
198 prof = &p->p_stats->p_prof;
199 if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size)
200 return;
201
202 prof->pr_addr = pc;
203 prof->pr_ticks++;
204 atomic_setbits_int(&p->p_flag, P_OWEUPC);
205 need_proftick(p);
206 }
207
208
209
210
211
212
213 void
214 addupc_task(struct proc *p, u_long pc, u_int nticks)
215 {
216 struct uprof *prof;
217 caddr_t addr;
218 u_int i;
219 u_short v;
220
221
222 if ((p->p_flag & P_PROFIL) == 0 || nticks == 0)
223 return;
224
225 prof = &p->p_stats->p_prof;
226 if (pc < prof->pr_off ||
227 (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size)
228 return;
229
230 addr = prof->pr_base + i;
231 if (copyin(addr, (caddr_t)&v, sizeof(v)) == 0) {
232 v += nticks;
233 if (copyout((caddr_t)&v, addr, sizeof(v)) == 0)
234 return;
235 }
236 stopprofclock(p);
237 }