xref: /freebsd/sys/kern/subr_prof.c (revision fdafd315)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysproto.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/resourcevar.h>
40 #include <sys/sysctl.h>
41 
42 #include <machine/cpu.h>
43 
44 /*
45  * Profiling system call.
46  *
47  * The scale factor is a fixed point number with 16 bits of fraction, so that
48  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
49  */
50 #ifndef _SYS_SYSPROTO_H_
51 struct profil_args {
52 	caddr_t	samples;
53 	size_t	size;
54 	size_t	offset;
55 	u_int	scale;
56 };
57 #endif
58 /* ARGSUSED */
59 int
sys_profil(struct thread * td,struct profil_args * uap)60 sys_profil(struct thread *td, struct profil_args *uap)
61 {
62 	struct uprof *upp;
63 	struct proc *p;
64 
65 	if (uap->scale > (1 << 16))
66 		return (EINVAL);
67 
68 	p = td->td_proc;
69 	if (uap->scale == 0) {
70 		PROC_LOCK(p);
71 		stopprofclock(p);
72 		PROC_UNLOCK(p);
73 		return (0);
74 	}
75 	PROC_LOCK(p);
76 	upp = &td->td_proc->p_stats->p_prof;
77 	PROC_PROFLOCK(p);
78 	upp->pr_off = uap->offset;
79 	upp->pr_scale = uap->scale;
80 	upp->pr_base = uap->samples;
81 	upp->pr_size = uap->size;
82 	PROC_PROFUNLOCK(p);
83 	startprofclock(p);
84 	PROC_UNLOCK(p);
85 
86 	return (0);
87 }
88 
89 /*
90  * Scale is a fixed-point number with the binary point 16 bits
91  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
92  * intermediate result is at most 48 bits.
93  */
94 #define	PC_TO_INDEX(pc, prof) \
95 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
96 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
97 
98 /*
99  * Collect user-level profiling statistics; called on a profiling tick,
100  * when a process is running in user-mode.  This routine may be called
101  * from an interrupt context.  We perform the update with an AST
102  * that will vector us to trap() with a context in which copyin and
103  * copyout will work.  Trap will then call addupc_task().
104  *
105  * Note that we may (rarely) not get around to the AST soon enough, and
106  * lose profile ticks when the next tick overwrites this one, but in this
107  * case the system is overloaded and the profile is probably already
108  * inaccurate.
109  */
110 void
addupc_intr(struct thread * td,uintfptr_t pc,u_int ticks)111 addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks)
112 {
113 	struct uprof *prof;
114 
115 	if (ticks == 0)
116 		return;
117 	prof = &td->td_proc->p_stats->p_prof;
118 	PROC_PROFLOCK(td->td_proc);
119 	if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size) {
120 		PROC_PROFUNLOCK(td->td_proc);
121 		return;			/* out of range; ignore */
122 	}
123 
124 	PROC_PROFUNLOCK(td->td_proc);
125 	td->td_profil_addr = pc;
126 	td->td_profil_ticks = ticks;
127 	td->td_pflags |= TDP_OWEUPC;
128 	ast_sched(td, TDA_OWEUPC);
129 }
130 
131 /*
132  * Actually update the profiling statistics.  If the update fails, we
133  * simply turn off profiling.
134  */
135 void
addupc_task(struct thread * td,uintfptr_t pc,u_int ticks)136 addupc_task(struct thread *td, uintfptr_t pc, u_int ticks)
137 {
138 	struct proc *p = td->td_proc;
139 	struct uprof *prof;
140 	caddr_t addr;
141 	u_int i;
142 	u_short v;
143 	int stop = 0;
144 
145 	if (ticks == 0)
146 		return;
147 
148 	PROC_LOCK(p);
149 	if (!(p->p_flag & P_PROFIL)) {
150 		PROC_UNLOCK(p);
151 		return;
152 	}
153 	p->p_profthreads++;
154 	prof = &p->p_stats->p_prof;
155 	PROC_PROFLOCK(p);
156 	if (pc < prof->pr_off ||
157 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
158 		PROC_PROFUNLOCK(p);
159 		goto out;
160 	}
161 
162 	addr = prof->pr_base + i;
163 	PROC_PROFUNLOCK(p);
164 	PROC_UNLOCK(p);
165 	if (copyin(addr, &v, sizeof(v)) == 0) {
166 		v += ticks;
167 		if (copyout(&v, addr, sizeof(v)) == 0) {
168 			PROC_LOCK(p);
169 			goto out;
170 		}
171 	}
172 	stop = 1;
173 	PROC_LOCK(p);
174 
175 out:
176 	if (--p->p_profthreads == 0) {
177 		if (p->p_flag & P_STOPPROF) {
178 			wakeup(&p->p_profthreads);
179 			p->p_flag &= ~P_STOPPROF;
180 			stop = 0;
181 		}
182 	}
183 	if (stop)
184 		stopprofclock(p);
185 	PROC_UNLOCK(p);
186 }
187