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>
24 #include <mach/mach4.h>
25 #include <mach/pc_sample.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. */
48 update_waiter (u_short
*sample_buffer
, size_t size
, size_t offset
, u_int scale
)
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
);
58 err
= __mach_setup_thread (__mach_task_self (), profile_thread
,
59 &profile_waiter
, NULL
, NULL
);
66 err
= __task_enable_pc_sampling (__mach_task_self (), &profile_tick
,
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
);
74 samples
= sample_buffer
;
75 maxsamples
= size
/ sizeof *sample_buffer
;
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;
90 __profile_frequency ()
96 profil (u_short
*sample_buffer
, size_t size
, size_t offset
, u_int scale
)
100 __mutex_lock (&lock
);
104 /* Disable profiling. */
106 __thread_suspend (profile_thread
);
107 err
= __task_disable_pc_sampling (__mach_task_self (), &count
);
112 err
= update_waiter (sample_buffer
, size
, offset
, scale
);
114 __mutex_unlock (&lock
);
116 return err
? __hurd_fail (err
) : 0;
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
;
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
);
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
)
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
);
165 if (profile_thread
!= MACH_PORT_NULL
)
167 __mach_port_deallocate (__mach_task_self (), profile_thread
);
168 profile_thread
= MACH_PORT_NULL
;
182 err
= update_waiter (sb
, n
* sizeof *sb
, o
, ss
);
186 text_set_element (_hurd_fork_child_hook
, fork_profil
);