xref: /freebsd/sys/kern/subr_prof.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/proc.h>
40 #include <sys/resourcevar.h>
41 #include <sys/sysctl.h>
42 
43 #include <machine/cpu.h>
44 
45 /*
46  * Profiling system call.
47  *
48  * The scale factor is a fixed point number with 16 bits of fraction, so that
49  * 1.0 is represented as 0x10000.  A scale factor of 0 turns off profiling.
50  */
51 #ifndef _SYS_SYSPROTO_H_
52 struct profil_args {
53 	caddr_t	samples;
54 	size_t	size;
55 	size_t	offset;
56 	u_int	scale;
57 };
58 #endif
59 /* ARGSUSED */
60 int
61 sys_profil(struct thread *td, struct profil_args *uap)
62 {
63 	struct uprof *upp;
64 	struct proc *p;
65 
66 	if (uap->scale > (1 << 16))
67 		return (EINVAL);
68 
69 	p = td->td_proc;
70 	if (uap->scale == 0) {
71 		PROC_LOCK(p);
72 		stopprofclock(p);
73 		PROC_UNLOCK(p);
74 		return (0);
75 	}
76 	PROC_LOCK(p);
77 	upp = &td->td_proc->p_stats->p_prof;
78 	PROC_PROFLOCK(p);
79 	upp->pr_off = uap->offset;
80 	upp->pr_scale = uap->scale;
81 	upp->pr_base = uap->samples;
82 	upp->pr_size = uap->size;
83 	PROC_PROFUNLOCK(p);
84 	startprofclock(p);
85 	PROC_UNLOCK(p);
86 
87 	return (0);
88 }
89 
90 /*
91  * Scale is a fixed-point number with the binary point 16 bits
92  * into the value, and is <= 1.0.  pc is at most 32 bits, so the
93  * intermediate result is at most 48 bits.
94  */
95 #define	PC_TO_INDEX(pc, prof) \
96 	((int)(((u_quad_t)((pc) - (prof)->pr_off) * \
97 	    (u_quad_t)((prof)->pr_scale)) >> 16) & ~1)
98 
99 /*
100  * Collect user-level profiling statistics; called on a profiling tick,
101  * when a process is running in user-mode.  This routine may be called
102  * from an interrupt context.  We perform the update with an AST
103  * that will vector us to trap() with a context in which copyin and
104  * copyout will work.  Trap will then call addupc_task().
105  *
106  * Note that we may (rarely) not get around to the AST soon enough, and
107  * lose profile ticks when the next tick overwrites this one, but in this
108  * case the system is overloaded and the profile is probably already
109  * inaccurate.
110  */
111 void
112 addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks)
113 {
114 	struct uprof *prof;
115 
116 	if (ticks == 0)
117 		return;
118 	prof = &td->td_proc->p_stats->p_prof;
119 	PROC_PROFLOCK(td->td_proc);
120 	if (pc < prof->pr_off || PC_TO_INDEX(pc, prof) >= prof->pr_size) {
121 		PROC_PROFUNLOCK(td->td_proc);
122 		return;			/* out of range; ignore */
123 	}
124 
125 	PROC_PROFUNLOCK(td->td_proc);
126 	td->td_profil_addr = pc;
127 	td->td_profil_ticks = ticks;
128 	td->td_pflags |= TDP_OWEUPC;
129 	ast_sched(td, TDA_OWEUPC);
130 }
131 
132 /*
133  * Actually update the profiling statistics.  If the update fails, we
134  * simply turn off profiling.
135  */
136 void
137 addupc_task(struct thread *td, uintfptr_t pc, u_int ticks)
138 {
139 	struct proc *p = td->td_proc;
140 	struct uprof *prof;
141 	caddr_t addr;
142 	u_int i;
143 	u_short v;
144 	int stop = 0;
145 
146 	if (ticks == 0)
147 		return;
148 
149 	PROC_LOCK(p);
150 	if (!(p->p_flag & P_PROFIL)) {
151 		PROC_UNLOCK(p);
152 		return;
153 	}
154 	p->p_profthreads++;
155 	prof = &p->p_stats->p_prof;
156 	PROC_PROFLOCK(p);
157 	if (pc < prof->pr_off ||
158 	    (i = PC_TO_INDEX(pc, prof)) >= prof->pr_size) {
159 		PROC_PROFUNLOCK(p);
160 		goto out;
161 	}
162 
163 	addr = prof->pr_base + i;
164 	PROC_PROFUNLOCK(p);
165 	PROC_UNLOCK(p);
166 	if (copyin(addr, &v, sizeof(v)) == 0) {
167 		v += ticks;
168 		if (copyout(&v, addr, sizeof(v)) == 0) {
169 			PROC_LOCK(p);
170 			goto out;
171 		}
172 	}
173 	stop = 1;
174 	PROC_LOCK(p);
175 
176 out:
177 	if (--p->p_profthreads == 0) {
178 		if (p->p_flag & P_STOPPROF) {
179 			wakeup(&p->p_profthreads);
180 			p->p_flag &= ~P_STOPPROF;
181 			stop = 0;
182 		}
183 	}
184 	if (stop)
185 		stopprofclock(p);
186 	PROC_UNLOCK(p);
187 }
188