]> git.saurik.com Git - apple/xnu.git/blob - osfmk/profiling/profile-mk.c
xnu-344.49.tar.gz
[apple/xnu.git] / osfmk / profiling / profile-mk.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Microkernel interface to common profiling.
30 */
31
32 #include <profiling/profile-mk.h>
33 #include <string.h>
34 #include <kern/cpu_number.h>
35 #include <kern/processor.h>
36 #include <kern/spl.h>
37 #include <kern/misc_protos.h>
38 #include <vm/vm_kern.h>
39 #include <mach/vm_param.h>
40
41 #include <device/ds_routines.h>
42 #include <device/io_req.h>
43 #include <device/buf.h>
44
45 extern char etext[], pstart[];
46
47 #if NCPUS > 1
48 struct profile_vars *_profile_vars_cpus[NCPUS] = { &_profile_vars };
49 struct profile_vars _profile_vars_aux[NCPUS-1];
50 #endif
51
52 void *
53 _profile_alloc_pages (size_t size)
54 {
55 vm_offset_t addr;
56
57 /*
58 * For the MK, we can't support allocating pages at runtime, because we
59 * might be at interrupt level, so abort if we didn't size the table
60 * properly.
61 */
62
63 if (PROFILE_VARS(0)->active) {
64 panic("Call to _profile_alloc_pages while profiling is running.");
65 }
66
67 if (kmem_alloc(kernel_map, &addr, size)) {
68 panic("Could not allocate memory for profiling");
69 }
70
71 memset((void *)addr, '\0', size);
72 if (PROFILE_VARS(0)->debug) {
73 printf("Allocated %d bytes for profiling, address 0x%x\n", (int)size, (int)addr);
74 }
75
76 return((caddr_t)addr);
77 }
78
79 void
80 _profile_free_pages(void *addr, size_t size)
81 {
82 if (PROFILE_VARS(0)->debug) {
83 printf("Freed %d bytes for profiling, address 0x%x\n", (int)size, (int)addr);
84 }
85
86 kmem_free(kernel_map, (vm_offset_t)addr, size);
87 return;
88 }
89
90 void _profile_error(struct profile_vars *pv)
91 {
92 panic("Fatal error in profiling");
93 }
94
95 void
96 kmstartup(void)
97 {
98 prof_uptrint_t textsize;
99 prof_uptrint_t monsize;
100 prof_uptrint_t lowpc;
101 prof_uptrint_t highpc;
102 int i;
103 struct profile_vars *pv;
104
105 /*
106 * round lowpc and highpc to multiples of the density we're using
107 * so the rest of the scaling (here and in gprof) stays in ints.
108 */
109
110 lowpc = ROUNDDOWN((prof_uptrint_t)&pstart[0], HISTFRACTION*sizeof(LHISTCOUNTER));
111 highpc = ROUNDUP((prof_uptrint_t)&etext[0], HISTFRACTION*sizeof(LHISTCOUNTER));
112 textsize = highpc - lowpc;
113 monsize = (textsize / HISTFRACTION) * sizeof(LHISTCOUNTER);
114
115 for (i = 0; i < NCPUS; i++) {
116 pv = PROFILE_VARS(i);
117
118 #if NCPUS > 1
119 if (!pv) {
120 _profile_vars_cpus[i] = pv = &_profile_vars_aux[i-i];
121 }
122 #endif
123
124 #ifdef DEBUG_PROFILE
125 pv->debug = 1;
126 #endif
127 pv->page_size = PAGE_SIZE;
128 _profile_md_init(pv, PROFILE_GPROF, PROFILE_ALLOC_MEM_YES);
129
130 /* Profil related variables */
131 pv->profil_buf = _profile_alloc (pv, monsize, ACONTEXT_PROFIL);
132 pv->profil_info.highpc = highpc;
133 pv->profil_info.lowpc = lowpc;
134 pv->profil_info.text_len = textsize;
135 pv->profil_info.profil_len = monsize;
136 pv->profil_info.counter_size = sizeof(LHISTCOUNTER);
137 pv->profil_info.scale = 0x10000 / HISTFRACTION;
138 pv->stats.profil_buckets = monsize / sizeof(LHISTCOUNTER);
139
140 /* Other gprof variables */
141 pv->stats.my_cpu = i;
142 pv->stats.max_cpu = NCPUS;
143 pv->init = 1;
144 pv->active = 1;
145 pv->use_dci = 0;
146 pv->use_profil = 1;
147 pv->check_funcs = 1; /* for now */
148
149 if (pv->debug) {
150 printf("Profiling kernel, s_textsize=%ld, monsize=%ld [0x%lx..0x%lx], cpu = %d\n",
151 (long)textsize,
152 (long)monsize,
153 (long)lowpc,
154 (long)highpc,
155 i);
156 }
157 }
158
159 _profile_md_start();
160 }
161
162 /* driver component */
163
164 int
165 gprofprobe(caddr_t port, void *ctlr)
166 {
167 return(1);
168 }
169
170 void
171 gprofattach(void)
172 {
173 kmstartup();
174 return;
175 }
176
177 /* struct bus_device *gprofinfo[NGPROF]; */
178 struct bus_device *gprofinfo[1];
179
180 struct bus_driver gprof_driver = {
181 gprofprobe, 0, gprofattach, 0, 0, "gprof", gprofinfo, "gprofc", 0, 0};
182
183
184 io_return_t
185 gprofopen(dev_t dev,
186 int flags,
187 io_req_t ior)
188 {
189 ior->io_error = D_SUCCESS;
190 return(0);
191 }
192
193 void
194 gprofclose(dev_t dev)
195 {
196 return;
197 }
198
199 void
200 gprofstrategy(io_req_t ior)
201 {
202 void *sys_ptr = (void *)0;
203
204 long count = _profile_kgmon(!(ior->io_op & IO_READ),
205 ior->io_count,
206 ior->io_recnum,
207 NCPUS,
208 &sys_ptr,
209 (void (*)(kgmon_control_t))0);
210
211 if (count < 0) {
212 ior->io_error = D_INVALID_RECNUM;
213
214 } else {
215 if (count > 0 && sys_ptr != (void *)0) {
216 if (ior->io_op & IO_READ) {
217 memcpy((void *)ior->io_data, sys_ptr, count);
218 } else {
219 memcpy(sys_ptr, (void *)ior->io_data, count);
220 }
221 }
222
223 ior->io_error = D_SUCCESS;
224 ior->io_residual = ior->io_count - count;
225 }
226
227 iodone(ior);
228 }
229
230 io_return_t
231 gprofread(dev_t dev,
232 io_req_t ior)
233 {
234 return(block_io(gprofstrategy, minphys, ior));
235 }
236
237 io_return_t
238 gprofwrite(dev_t dev,
239 io_req_t ior)
240 {
241 return (block_io(gprofstrategy, minphys, ior));
242 }