Update.
[glibc.git] / sysdeps / mach / hurd / profil.c
blobd3c5131b10250a3acce41af9ef488bcde4c9025c
1 /* Low-level statistical profiling support function. Mach/Hurd version.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <hurd.h>
24 #include <mach/mach4.h>
25 #include <mach/pc_sample.h>
26 #include <cthreads.h>
27 #include <assert.h>
29 #define MAX_PC_SAMPLES 512 /* XXX ought to be exported in kernel hdr */
31 static thread_t profile_thread = MACH_PORT_NULL;
32 static u_short *samples;
33 static size_t maxsamples;
34 static size_t pc_offset;
35 static size_t sample_scale;
36 static sampled_pc_seqno_t seqno;
37 static struct mutex lock = MUTEX_INITIALIZER;
38 static mach_msg_timeout_t collector_timeout; /* ms between collections. */
39 static int profile_tick;
41 /* Enable statistical profiling, writing samples of the PC into at most
42 SIZE bytes of SAMPLE_BUFFER; every processor clock tick while profiling
43 is enabled, the system examines the user PC and increments
44 SAMPLE_BUFFER[((PC - OFFSET) / 2) * SCALE / 65536]. If SCALE is zero,
45 disable profiling. Returns zero on success, -1 on error. */
47 static error_t
48 update_waiter (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
50 error_t err;
52 if (profile_thread == MACH_PORT_NULL)
54 /* Set up the profiling collector thread. */
55 static void profile_waiter (void);
56 err = __thread_create (__mach_task_self (), &profile_thread);
57 if (! err)
58 err = __mach_setup_thread (__mach_task_self (), profile_thread,
59 &profile_waiter, NULL, NULL);
61 else
62 err = 0;
64 if (! err)
66 err = __task_enable_pc_sampling (__mach_task_self (), &profile_tick,
67 SAMPLED_PC_PERIODIC);
68 if (!err && sample_scale == 0)
69 /* Profiling was not turned on, so the collector thread was
70 suspended. Resume it. */
71 err = __thread_resume (profile_thread);
72 if (! err)
74 samples = sample_buffer;
75 maxsamples = size / sizeof *sample_buffer;
76 pc_offset = offset;
77 sample_scale = scale;
78 /* Calculate a good period for the collector thread. From TICK
79 and the kernel buffer size we get the length of time it takes
80 to fill the buffer; translate that to milliseconds for
81 mach_msg, and chop it in half for general lag factor. */
82 collector_timeout = MAX_PC_SAMPLES * profile_tick / 1000 / 2;
86 return err;
89 int
90 __profile_frequency ()
92 return profile_tick;
95 int
96 profil (u_short *sample_buffer, size_t size, size_t offset, u_int scale)
98 error_t err;
100 __mutex_lock (&lock);
102 if (scale == 0)
104 /* Disable profiling. */
105 int count;
106 __thread_suspend (profile_thread);
107 err = __task_disable_pc_sampling (__mach_task_self (), &count);
108 sample_scale = 0;
109 seqno = 0;
111 else
112 err = update_waiter (sample_buffer, size, offset, scale);
114 __mutex_unlock (&lock);
116 return err ? __hurd_fail (err) : 0;
119 static void
120 profile_waiter (void)
122 sampled_pc_t pc_samples[MAX_PC_SAMPLES];
123 mach_msg_type_number_t nsamples, i;
124 mach_port_t rcv = __mach_reply_port ();
125 mach_msg_header_t msg;
126 error_t err;
128 while (1)
130 __mutex_lock (&lock);
132 nsamples = sizeof pc_samples / sizeof pc_samples[0];
133 err = __task_get_sampled_pcs (__mach_task_self (), &seqno,
134 pc_samples, &nsamples);
135 assert_perror (err);
137 for (i = 0; i < nsamples; ++i)
139 size_t idx = (((pc_samples[i].pc - pc_offset) / 2) *
140 sample_scale / 65536);
141 if (idx < maxsamples)
142 ++samples[idx];
145 __vm_deallocate (__mach_task_self (),
146 (vm_address_t) pc_samples,
147 nsamples * sizeof *pc_samples);
149 __mutex_unlock (&lock);
151 __mach_msg (&msg, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof msg,
152 rcv, collector_timeout, MACH_PORT_NULL);
156 data_set_element (_hurd_fork_locks, lock);
158 static void
159 fork_profil (void)
161 u_short *sb;
162 size_t n, o, ss;
163 error_t err;
165 if (profile_thread != MACH_PORT_NULL)
167 __mach_port_deallocate (__mach_task_self (), profile_thread);
168 profile_thread = MACH_PORT_NULL;
171 sb = samples;
172 samples = NULL;
173 n = maxsamples;
174 maxsamples = 0;
175 o = pc_offset;
176 pc_offset = 0;
177 ss = sample_scale;
178 sample_scale = 0;
180 if (ss != 0)
182 err = update_waiter (sb, n * sizeof *sb, o, ss);
183 assert_perror (err);
186 text_set_element (_hurd_fork_child_hook, fork_profil);