2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Define the internal interfaces between the profiling support that is
27 * common between the kernel, mach servers, and user space library.
30 #ifndef _PROFILE_INTERNAL_H
31 #define _PROFILE_INTERNAL_H
34 * Allow us not to require stdio.h in kernel/server space, but
35 * use it in user space.
38 #if !defined(MACH_KERNEL) && !defined(_KERNEL)
43 * Scaling factor for the profil system call.
46 #define SCALE_1_TO_1 0x10000L
50 * Forward reference to structures used.
57 struct profile_profil
;
66 typedef enum profile_type
{
73 * Whether to allocate memory in _profile_md_init.
76 typedef enum profile_alloc_mem
{
79 } profile_alloc_mem_t
;
82 * Allocation context block types.
85 typedef enum acontext_type
{
86 ACONTEXT_PROF
, /* 0: prof records */
87 ACONTEXT_GPROF
, /* 1: gprof arcs */
88 ACONTEXT_GFUNC
, /* 2: gprof function headers */
89 ACONTEXT_MISC
, /* 3: misc. allocations */
90 ACONTEXT_PROFIL
, /* 4: profil based allocations */
91 ACONTEXT_DCI
, /* 5: dci based allocations */
92 ACONTEXT_BASIC_BLOCK
, /* 6: basic block allocations */
93 ACONTEXT_CALLBACK
, /* 7: callback structures */
94 ACONTEXT_MAX
= 32 /* # allocation contexts */
97 #define ACONTEXT_FIRST ACONTEXT_PROF
99 #define ACONTEXT_NAMES { \
135 * Kgmon control codes
138 typedef enum kgmon_control
{
139 KGMON_UNUSED
, /* insure no 0 is ever used */
140 KGMON_GET_STATUS
, /* return whether or not profiling is active */
141 KGMON_GET_PROFILE_VARS
, /* return the _profile_vars structure */
142 KGMON_GET_PROFILE_STATS
, /* return the _profile_stats structure */
143 KGMON_GET_DEBUG
, /* return whether or not debugging is on */
145 KGMON_SET_PROFILE_ON
= 50, /* turn on profiling */
146 KGMON_SET_PROFILE_OFF
, /* turn off profiling */
147 KGMON_SET_PROFILE_RESET
, /* reset profiling tables */
148 KGMON_SET_DEBUG_ON
, /* turn on debugging */
149 KGMON_SET_DEBUG_OFF
/* turn off debugging */
152 #define KGMON_GET_MIN KGMON_GET_STATUS
153 #define KGMON_GET_MAX KGMON_GET_DEBUG
154 #define KGMON_SET_MIN KGMON_SET_PROFILE_ON
155 #define KGMON_SET_MAX KGMON_SET_DEBUG_OFF
157 #define ENCODE_KGMON(num, control, cpu_thread) \
158 ((num) = ((cpu_thread) << 8) | (control))
160 #define DECODE_KGMON(num, control, cpu_thread) \
162 control = (num) & 0xff; \
163 cpu_thread = (num) >> 8; \
166 #define LEGAL_KGMON(num) (((unsigned long)(num)) <= 0xffff)
169 * Pull in all of the machine dependent types now after defining the enums.
172 #include <profiling/machine/profile-md.h>
175 * general rounding functions.
178 #define ROUNDDOWN(x,y) (((x)/(y))*(y))
179 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
182 * Linked list of pages allocated for a particular allocation context block.
186 void *first
; /* pointer to first byte available */
187 void *ptr
; /* pointer to next available byte */
188 struct page_list
*next
; /* next page allocated */
189 size_t bytes_free
; /* # bytes available */
190 size_t bytes_allocated
; /* # bytes allocates so far */
191 size_t num_allocations
; /* # of allocations */
195 * Allocation context block.
198 struct alloc_context
{
199 struct alloc_context
*next
; /* next allocation context block */
200 struct page_list
*plist
; /* head of page list */
201 prof_lock_t lock
; /* lock field available to asm */
206 * Callback structure that records information for one record in the
213 void *sec_ptr
; /* callback user data */
214 /* callback function */
215 size_t (*callback
)(struct profile_vars
*, struct callback
*);
216 long sec_val1
; /* section specific value */
217 long sec_val2
; /* section specific value */
218 size_t sec_recsize
; /* record size */
219 size_t sec_length
; /* total length */
220 char sec_name
[STR_MAX
]; /* section name */
224 * Basic profil information (except for the profil buffer).
227 struct profile_profil
{
228 prof_uptrint_t lowpc
; /* lowest address */
229 prof_uptrint_t highpc
; /* highest address */
230 size_t text_len
; /* highpc-lowpc */
231 size_t profil_len
; /* length of the profil buffer */
232 size_t counter_size
; /* size of indivual counters (HISTCOUNTER) */
233 unsigned long scale
; /* scaling factor (65536 / scale) */
234 unsigned long profil_unused
[8]; /* currently unused */
238 * Profiling internal variables. This structure is intended to be machine independent.
241 struct profile_vars
{
242 int major_version
; /* major version number */
243 int minor_version
; /* minor version number */
244 size_t vars_size
; /* size of profile_vars structure */
245 size_t plist_size
; /* size of page_list structure */
246 size_t acontext_size
; /* size of allocation context struct */
247 size_t callback_size
; /* size of callback structure */
248 profile_type_t type
; /* profile type */
249 const char *error_msg
; /* error message for perror */
250 const char *filename
; /* filename to write to */
251 char *str_ptr
; /* string table */
253 #if !defined(MACH_KERNEL) && !defined(_KERNEL)
254 FILE *stream
; /* stdio stream to write to */
255 FILE *diag_stream
; /* stdio stream to write diagnostics to */
256 /* function to write out some bytes */
257 size_t (*fwrite_func
)(const void *, size_t, size_t, FILE *);
259 void *stream
; /* pointer passed to fwrite_func */
260 void *diag_stream
; /* stdio stream to write diagnostics to */
261 /* function to write out some bytes */
262 size_t (*fwrite_func
)(const void *, size_t, size_t, void *);
265 size_t page_size
; /* machine pagesize */
266 size_t str_bytes
; /* # bytes in string table */
267 size_t str_total
; /* # bytes allocated total for string table */
268 long clock_ticks
; /* # clock ticks per second */
270 /* profil related variables */
271 struct profile_profil profil_info
; /* profil information */
272 HISTCOUNTER
*profil_buf
; /* profil buffer */
274 /* Profiling output selection */
275 void (*output_init
)(struct profile_vars
*); /* output init function */
276 void (*output
)(struct profile_vars
*); /* output function */
277 void *output_ptr
; /* output specific info */
279 /* allocation contexts */
280 struct alloc_context
*acontext
[(int)ACONTEXT_MAX
];
282 void (*bogus_func
)(void); /* Function to use if address out of bounds */
283 prof_uptrint_t vars_unused
[63]; /* future growth */
286 prof_flag_t init
; /* != 0 if initialized */
287 prof_flag_t active
; /* != 0 if profiling is active */
288 prof_flag_t do_profile
; /* != 0 if profiling is being done */
289 prof_flag_t use_dci
; /* != 0 if using DCI */
291 prof_flag_t use_profil
; /* != 0 if using profil */
292 prof_flag_t recursive_alloc
; /* != 0 if alloc taking place */
293 prof_flag_t output_uarea
; /* != 0 if output the uarea */
294 prof_flag_t output_stats
; /* != 0 if output the stats */
296 prof_flag_t output_clock
; /* != 0 if output the clock ticks */
297 prof_flag_t multiple_sections
; /* != 0 if output allows multiple sections */
298 prof_flag_t have_bb
; /* != 0 if we have basic block data */
299 prof_flag_t init_format
; /* != 0 if output format has been chosen */
301 prof_flag_t debug
; /* != 0 if debugging */
302 prof_flag_t check_funcs
; /* != 0 if check gprof arcs for being in range */
303 prof_flag_t flag_unused
[62]; /* space for more flags */
305 struct profile_stats stats
; /* profiling statistics */
306 struct profile_md md
; /* machine dependent info */
310 * Profiling static data.
313 extern struct profile_vars _profile_vars
;
316 * Functions called by the machine dependent routines, and provided by
317 * specific routines to the kernel, server, and user space library.
320 #if (__GNUC__ < 2) || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || defined(lint)
321 #define __attribute__(arg)
324 #if defined(_KERNEL) || defined(MACH_KERNEL)
325 #define _profile_printf printf
327 extern int _profile_printf(const char *, ...) __attribute__((format(printf
,1,2)));
330 extern void *_profile_alloc_pages (size_t);
331 extern void _profile_free_pages (void *, size_t);
332 extern void _profile_error(struct profile_vars
*);
335 * Functions provided by the machine dependent files.
338 extern void _profile_md_init(struct profile_vars
*, profile_type_t
, profile_alloc_mem_t
);
339 extern int _profile_md_start(void);
340 extern int _profile_md_stop(void);
341 extern void *_profile_alloc(struct profile_vars
*, size_t, acontext_type_t
);
342 extern size_t _gprof_write(struct profile_vars
*, struct callback
*);
343 extern size_t _prof_write(struct profile_vars
*, struct callback
*);
344 extern void _profile_update_stats(struct profile_vars
*);
345 extern void _profile_reset(struct profile_vars
*);
347 #if !defined(_KERNEL) && !defined(MACH_KERNEL)
348 extern void _profile_print_stats(FILE *, const struct profile_stats
*, const struct profile_profil
*);
349 extern void _profile_merge_stats(struct profile_stats
*, const struct profile_stats
*);
353 * Functions defined in profile-kgmon.c
356 extern long _profile_kgmon(int,
361 void (*)(kgmon_control_t
));
363 extern void kgmon_server_control(kgmon_control_t
);
366 #endif /* _KERNEL or MACH_KERNEL */
368 #endif /* _PROFILE_INTERNAL_H */