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