1 /* Low-level statistical profiling support function.  Mach/Hurd version.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <hurd.h>
23 #include <mach/mach4.h>
24 #include <mach/pc_sample.h>
25 #include <lock-intern.h>
26 #include <assert.h>
27 #include <libc-internal.h>
28 
29 
30 #define MAX_PC_SAMPLES	512	/* XXX ought to be exported in kernel hdr */
31 
32 static thread_t profile_thread = MACH_PORT_NULL;
33 static u_short *samples;
34 static size_t maxsamples;
35 static size_t pc_offset;
36 static size_t sample_scale;
37 static sampled_pc_seqno_t seqno;
38 static spin_lock_t lock = SPIN_LOCK_INITIALIZER;
39 static mach_msg_timeout_t collector_timeout; /* ms between collections.  */
40 static int profile_tick;
41 
42 /* Reply port used by profiler thread */
43 static mach_port_t profil_reply_port = MACH_PORT_NULL;
44 
45 /* Forwards */
46 static kern_return_t profil_task_get_sampled_pcs (mach_port_t,
47 						  sampled_pc_seqno_t *,
48 						  sampled_pc_array_t,
49 						  mach_msg_type_number_t *);
50 static void fetch_samples (void);
51 static void profile_waiter (void);
52 
53 /* Enable statistical profiling, writing samples of the PC into at most
54    SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
55    is enabled, the system examines the user PC and increments
56    SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536].  If SCALE is zero,
57    disable profiling.  Returns zero on success, -1 on error.  */
58 
59 static error_t
update_waiter(u_short * sample_buffer,size_t size,size_t offset,u_int scale)60 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
61 {
62   error_t err;
63 
64   if (profile_thread == MACH_PORT_NULL)
65     {
66       if (profil_reply_port == MACH_PORT_NULL)
67 	profil_reply_port = __mach_reply_port ();
68       /* Set up the profiling collector thread.  */
69       err = __thread_create (__mach_task_self (), &profile_thread);
70       if (! err)
71 	err = __mach_setup_thread (__mach_task_self (), profile_thread,
72 				   &profile_waiter, NULL, NULL);
73       if (! err)
74 	err = __mach_setup_tls(profile_thread);
75     }
76   else
77     err = 0;
78 
79   if (! err)
80     {
81       err = __task_enable_pc_sampling (__mach_task_self (), &profile_tick,
82 				       SAMPLED_PC_PERIODIC);
83       if (!err && sample_scale == 0)
84 	/* Profiling was not turned on, so the collector thread was
85 	   suspended.  Resume it.  */
86 	err = __thread_resume (profile_thread);
87       if (! err)
88 	{
89 	  samples = sample_buffer;
90 	  maxsamples = size / sizeof *sample_buffer;
91 	  pc_offset = offset;
92 	  sample_scale = scale;
93 	  /* Calculate a good period for the collector thread.  From TICK
94 	     and the kernel buffer size we get the length of time it takes
95 	     to fill the buffer; translate that to milliseconds for
96 	     mach_msg, and chop it in half for general lag factor.  */
97 	  collector_timeout = MAX_PC_SAMPLES * profile_tick / 1000 / 2;
98 	}
99     }
100 
101   return err;
102 }
103 
104 int
__profile_frequency(void)105 __profile_frequency (void)
106 {
107   return 1000000 / profile_tick;
108 }
libc_hidden_def(__profile_frequency)109 libc_hidden_def (__profile_frequency)
110 
111 int
112 __profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
113 {
114   error_t err;
115 
116   __spin_lock (&lock);
117 
118   if (scale == 0)
119     {
120       /* Disable profiling.  */
121       int count;
122 
123       if (profile_thread != MACH_PORT_NULL)
124 	__thread_suspend (profile_thread);
125 
126       /* Fetch the last set of samples */
127       if (sample_scale)
128 	fetch_samples ();
129 
130       err = __task_disable_pc_sampling (__mach_task_self (), &count);
131       sample_scale = 0;
132       seqno = 0;
133     }
134   else
135     err = update_waiter (sample_buffer, size, offset, scale);
136 
137   __spin_unlock (&lock);
138 
139   return err ? __hurd_fail (err) : 0;
140 }
weak_alias(__profil,profil)141 weak_alias (__profil, profil)
142 
143 static volatile error_t special_profil_failure;
144 
145 /* Fetch PC samples.  This function must be very careful not to depend
146    on Hurd TLS variables.  We arrange that by using a special
147    stub arranged for at the end of this file. */
148 static void
149 fetch_samples (void)
150 {
151   sampled_pc_t pc_samples[MAX_PC_SAMPLES];
152   mach_msg_type_number_t nsamples, i;
153   error_t err;
154 
155   nsamples = MAX_PC_SAMPLES;
156 
157   err = profil_task_get_sampled_pcs (__mach_task_self (), &seqno,
158 				     pc_samples, &nsamples);
159   if (err)
160     {
161       static volatile int a, b;
162 
163       special_profil_failure = err;
164       a = 1;
165       b = 0;
166       while (1)
167 	a = a / b;
168     }
169 
170   for (i = 0; i < nsamples; ++i)
171     {
172       /* Do arithmetic in long long to avoid overflow problems. */
173       long long pc_difference = pc_samples[i].pc - pc_offset;
174       size_t idx = ((pc_difference / 2) * sample_scale) / 65536;
175       if (idx < maxsamples)
176 	++samples[idx];
177     }
178 }
179 
180 
181 /* This function must be very careful not to depend on Hurd TLS
182    variables.  We arrange that by using special stubs arranged for at the
183    end of this file. */
184 static void
profile_waiter(void)185 profile_waiter (void)
186 {
187   mach_msg_header_t msg;
188   mach_port_t timeout_reply_port;
189 
190   timeout_reply_port = __mach_reply_port ();
191 
192   while (1)
193     {
194       __spin_lock (&lock);
195 
196       fetch_samples ();
197 
198       __spin_unlock (&lock);
199 
200       __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
201 		  timeout_reply_port, collector_timeout, MACH_PORT_NULL);
202     }
203 }
204 
205 /* Fork interaction */
206 
207 /* Before fork, lock the interlock so that we are in a clean state. */
208 static void
fork_profil_prepare(void)209 fork_profil_prepare (void)
210 {
211   __spin_lock (&lock);
212 }
213 text_set_element (_hurd_fork_prepare_hook, fork_profil_prepare);
214 
215 /* In the parent, unlock the interlock once fork is complete. */
216 static void
fork_profil_parent(void)217 fork_profil_parent (void)
218 {
219   __spin_unlock (&lock);
220 }
221 text_set_element (_hurd_fork_parent_hook, fork_profil_parent);
222 
223 /* In the child, unlock the interlock, and start a profiling thread up
224    if necessary. */
225 static void
fork_profil_child(void)226 fork_profil_child (void)
227 {
228   u_short *sb;
229   size_t n, o, ss;
230   error_t err;
231 
232   __spin_unlock (&lock);
233 
234   if (profile_thread != MACH_PORT_NULL)
235     {
236       __mach_port_deallocate (__mach_task_self (), profile_thread);
237       profile_thread = MACH_PORT_NULL;
238     }
239 
240   sb = samples;
241   samples = NULL;
242   n = maxsamples;
243   maxsamples = 0;
244   o = pc_offset;
245   pc_offset = 0;
246   ss = sample_scale;
247   sample_scale = 0;
248 
249   if (ss != 0)
250     {
251       err = update_waiter (sb, n * sizeof *sb, o, ss);
252       assert_perror (err);
253     }
254 }
255 text_set_element (_hurd_fork_child_hook, fork_profil_child);
256 
257 
258 
259 
260 /* Special RPC stubs for profile_waiter are made by including the normal
261    source code, with special CPP state to prevent it from doing the
262    usual thing. */
263 
264 /* Include these first; then our #define's will take full effect, not
265    being overridden. */
266 #include <mach/mig_support.h>
267 
268 /* This need not do anything; it is always associated with errors, which
269    are fatal in profile_waiter anyhow. */
270 #define __mig_put_reply_port(foo)
271 
272 /* Use our static variable instead of the usual TLS mechanism for
273    this. */
274 #define __mig_get_reply_port() profil_reply_port
275 
276 /* Make the functions show up as static */
277 #define mig_external static
278 
279 /* Turn off the attempt to generate ld aliasing records. */
280 #undef weak_alias
281 #define weak_alias(a,b)
282 
283 /* And change their names to avoid confusing disasters. */
284 #define __vm_deallocate_rpc profil_vm_deallocate
285 #define __task_get_sampled_pcs profil_task_get_sampled_pcs
286 
287 /* And include the source code */
288 #include <../mach/RPC_task_get_sampled_pcs.c>
289