2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 #include <mach_prof.h>
56 #include <mach/task_server.h>
57 #include <mach/thread_act_server.h>
60 #include <kern/thread.h>
61 #include <kern/queue.h>
62 #include <kern/profile.h>
63 #include <kern/sched_prim.h>
65 #include <kern/misc_protos.h>
66 #include <ipc/ipc_space.h>
67 #include <machine/machparam.h>
68 #include <mach/prof.h>
70 thread_t profile_thread_id
= THREAD_NULL
;
71 int profile_sample_count
= 0; /* Provided for looking at from kdb. */
72 extern kern_return_t
task_suspend(task_t task
); /* ack */
75 prof_data_t
pbuf_alloc(void);
78 void profile_thread(void);
79 void send_last_sample_buf(
83 *****************************************************************************
84 * profile_thread is the profile/trace kernel support thread. It is started
85 * by a server/user request through task_sample, or thread_sample. The profile
86 * thread dequeues messages and sends them to the receive_prof thread, in the
87 * server, via the send_samples and send_notices mig interface functions. If
88 * there are no messages in the queue profile thread blocks until wakened by
89 * profile (called in from mach_clock), or last_sample (called by thread/task_
98 queue_entry_t prof_queue_entry
;
103 /* Initialise the queue header for the prof_queue */
104 mpqueue_init(&prof_queue
);
108 /* Dequeue the first buffer. */
110 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
113 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
) {
114 assert_wait((event_t
) profile_thread
, THREAD_UNINT
);
115 thread_block(THREAD_CONTINUE_NULL
);
116 if (current_thread()->wait_result
!= THREAD_AWAKENED
)
122 pbuf
= buf_entry
->p_prof
;
123 kr
= send_samples(pbuf
->prof_port
, (void *)buf_entry
->p_zone
,
124 (mach_msg_type_number_t
)buf_entry
->p_index
);
125 profile_sample_count
+= buf_entry
->p_index
;
126 if (kr
!= KERN_SUCCESS
)
127 printf("send_samples(%x, %x, %d) error %x\n",
128 pbuf
->prof_port
, buf_entry
->p_zone
, buf_entry
->p_index
, kr
);
129 dropped
= buf_entry
->p_dropped
;
131 printf("kernel: profile dropped %d sample%s\n", dropped
,
132 dropped
== 1 ? "" : "s");
133 buf_entry
->p_dropped
= 0;
136 /* Indicate you've finished the dirty job */
137 buf_entry
->p_full
= FALSE
;
138 if (buf_entry
->p_wakeme
)
139 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
143 /* The profile thread has been signalled to exit. Any threads waiting
144 for the last buffer of samples to be acknowledged should be woken
146 profile_thread_id
= THREAD_NULL
;
149 mpdequeue_head(&prof_queue
, &prof_queue_entry
);
151 if ((buf_entry
= (buffer_t
) prof_queue_entry
) == NULLPBUF
)
153 if (buf_entry
->p_wakeme
)
154 thread_wakeup((event_t
) &buf_entry
->p_wakeme
);
159 panic("profile_thread(): halt_self");
164 *****************************************************************************
165 * send_last_sample is the drain mechanism to allow partial profiled buffers
166 * to be sent to the receive_prof thread in the server.
167 *****************************************************************************
171 send_last_sample_buf(prof_data_t pbuf
)
176 if (pbuf
== NULLPROFDATA
)
179 /* Ask for the sending of the last PC buffer.
180 * Make a request to the profile_thread by inserting
181 * the buffer in the send queue, and wake it up.
182 * The last buffer must be inserted at the head of the
183 * send queue, so the profile_thread handles it immediatly.
185 buf_entry
= pbuf
->prof_area
+ pbuf
->prof_index
;
186 buf_entry
->p_prof
= pbuf
;
189 Watch out in case profile thread exits while we are about to
193 if (profile_thread_id
== THREAD_NULL
)
196 buf_entry
->p_wakeme
= 1;
197 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
198 thread_wakeup((event_t
) profile_thread
);
199 assert_wait((event_t
) &buf_entry
->p_wakeme
, THREAD_ABORTSAFE
);
201 thread_block(THREAD_CONTINUE_NULL
);
207 *****************************************************************************
208 * add clock tick parameters to profile/trace buffers. Called from the mach_
209 * clock heritz_tick function. DCI version stores thread, sp, and pc values
210 * into the profile/trace buffers. MACH_PROF version just stores pc values.
211 *****************************************************************************
215 profile(natural_t pc
,
218 natural_t inout_val
= pc
;
221 if (pbuf
== NULLPROFDATA
)
224 /* Inserts the PC value in the buffer of the thread */
225 set_pbuf_value(pbuf
, &inout_val
);
226 switch((int)inout_val
) {
228 if (profile_thread_id
== THREAD_NULL
) {
229 reset_pbuf_area(pbuf
);
233 /* Normal case, value successfully inserted */
237 * The value we have just inserted caused the
238 * buffer to be full, and ready to be sent.
239 * If profile_thread_id is null, the profile
240 * thread has been killed. Since this generally
241 * happens only when the O/S server task of which
242 * it is a part is killed, it is not a great loss
243 * to throw away the data.
245 if (profile_thread_id
== THREAD_NULL
) {
246 reset_pbuf_area(pbuf
);
250 buf_entry
= (buffer_t
) &pbuf
->prof_area
[pbuf
->prof_index
];
251 buf_entry
->p_prof
= pbuf
;
252 mpenqueue_tail(&prof_queue
, &buf_entry
->p_list
);
254 /* Switch to another buffer */
255 reset_pbuf_area(pbuf
);
257 /* Wake up the profile thread */
258 if (profile_thread_id
!= THREAD_NULL
)
259 thread_wakeup((event_t
) profile_thread
);
263 printf("profile : unexpected case\n");
268 *****************************************************************************
269 * pbuf_alloc creates a profile/trace buffer and assoc. zones for storing
271 *****************************************************************************
277 register prof_data_t pbuf
;
279 register natural_t
*zone
;
281 pbuf
= (prof_data_t
)kalloc(sizeof(struct prof_data
));
283 return(NULLPROFDATA
);
284 pbuf
->prof_port
= MACH_PORT_NULL
;
285 for (i
=0; i
< NB_PROF_BUFFER
; i
++) {
286 zone
= (natural_t
*)kalloc(SIZE_PROF_BUFFER
*sizeof(natural_t
));
290 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
291 SIZE_PROF_BUFFER
*sizeof(natural_t
));
292 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
293 return(NULLPROFDATA
);
295 pbuf
->prof_area
[i
].p_zone
= zone
;
296 pbuf
->prof_area
[i
].p_full
= FALSE
;
298 pbuf
->prof_port
= MACH_PORT_NULL
;
303 *****************************************************************************
304 * pbuf_free free memory allocated for storing profile/trace items. Called
305 * when a task is no longer profiled/traced. Pbuf_free tears down the memory
306 * alloced in pbuf_alloc. It does not check to see if the structures are valid
307 * since it is only called by functions in this file.
308 *****************************************************************************
317 ipc_port_release_send(pbuf
->prof_port
);
319 for(i
=0; i
< NB_PROF_BUFFER
; i
++)
320 kfree((vm_offset_t
)pbuf
->prof_area
[i
].p_zone
,
321 SIZE_PROF_BUFFER
*sizeof(natural_t
));
322 kfree((vm_offset_t
)pbuf
, sizeof(struct prof_data
));
325 #endif /* MACH_PROF */
328 *****************************************************************************
329 * Thread_sample is used by MACH_PROF to profile a single thread, and is only
331 *****************************************************************************
337 __unused thread_t thread
,
338 __unused ipc_port_t reply
)
349 * This routine is called every time that a new thread has made
350 * a request for the sampling service. We must keep track of the
351 * correspondance between its identity (thread) and the port
352 * we are going to use as a reply port to send out the samples resulting
353 * from its execution.
358 if (reply
!= MACH_PORT_NULL
) {
359 if (thread
->profiled
) /* yuck! */
360 return KERN_INVALID_ARGUMENT
;
361 /* Start profiling this activation, do the initialization. */
363 if ((thread
->profil_buffer
= pbuf
) == NULLPROFDATA
) {
364 printf("thread_sample: cannot allocate pbuf\n");
365 return KERN_RESOURCE_SHORTAGE
;
368 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
369 printf("mach_sample_thread: cannot set pbuf_nb\n");
372 reset_pbuf_area(pbuf
);
374 pbuf
->prof_port
= reply
;
375 thread
->profiled
= TRUE
;
376 thread
->profiled_own
= TRUE
;
377 if (profile_thread_id
== THREAD_NULL
)
378 profile_thread_id
= kernel_thread(kernel_task
, profile_thread
);
380 if (!thread
->profiled
)
381 return(KERN_INVALID_ARGUMENT
);
383 thread
->profiled
= FALSE
;
384 /* do not stop sampling if thread is not profiled by its own */
386 if (!thread
->profiled_own
)
389 thread
->profiled_own
= FALSE
;
391 send_last_sample_buf(thread
->profil_buffer
);
392 pbuf_free(thread
->profil_buffer
);
393 thread
->profil_buffer
= NULLPROFDATA
;
397 #endif /* MACH_PROF */
400 *****************************************************************************
401 * Task_sample is used to profile/trace tasks - all thread within a task using
402 * a common profile buffer to collect items generated by the hertz_tick. For
403 * each task profiled a profile buffer is created that associates a reply port
404 * (used to send the data to a server thread), task (used for throttling), and
405 * a zone area (used to store profiled/traced items).
406 *****************************************************************************
412 __unused task_t task
,
413 __unused ipc_port_t reply
)
423 prof_data_t pbuf
=task
->profil_buffer
;
425 boolean_t turnon
= (reply
!= MACH_PORT_NULL
);
427 if (task
== TASK_NULL
)
428 return KERN_INVALID_ARGUMENT
;
429 if (turnon
) /* Do we want to profile this task? */
431 pbuf
= pbuf_alloc(); /* allocate a profile buffer */
433 if (task
->task_profiled
) { /* if it is already profiled return so */
435 if (pbuf
!= NULLPROFDATA
)
437 return(KERN_INVALID_ARGUMENT
);
439 if (pbuf
== NULLPROFDATA
) {
441 return KERN_RESOURCE_SHORTAGE
; /* can't allocate a buffer, quit */
443 task
->profil_buffer
= pbuf
;
445 if (!set_pbuf_nb(pbuf
, NB_PROF_BUFFER
-1)) {
450 reset_pbuf_area(pbuf
);
451 pbuf
->prof_port
= reply
; /* assoc. buffer with reply port */
452 } else { /* We want to stop profiling/tracing */
454 if (!task
->task_profiled
) { /* but this task is not being profiled */
456 return(KERN_INVALID_ARGUMENT
);
461 * turnon = FALSE && task_profile = TRUE ||
462 * turnon = TRUE && task_profile = FALSE
465 if (turnon
!= task
->task_profiled
) {
469 if (turnon
&& profile_thread_id
== THREAD_NULL
) /* 1st time thru? */
470 profile_thread_id
= /* then start profile thread. */
471 kernel_thread(kernel_task
, profile_thread
);
472 task
->task_profiled
= turnon
;
473 actual
= task
->thread_count
;
474 for (i
= 0, thread
= (thread_t
)queue_first(&task
->threads
);
476 i
++, thread
= (thread_t
)queue_next(&thr_act
->task_threads
)) {
477 if (!thread
->profiled_own
) {
478 threadt
->profiled
= turnon
;
480 threadt
->profil_buffer
= task
->profil_buffer
;
481 thread
->profiled
= TRUE
;
483 thread
->profiled
= FALSE
;
484 thread
->profil_buffer
= NULLPROFDATA
;
488 if (!turnon
) { /* drain buffers and clean-up */
489 send_last_sample_buf(task
->profil_buffer
);
490 pbuf_free(task
->profil_buffer
);
491 task
->profil_buffer
= NULLPROFDATA
;
498 #endif /* MACH_PROF */