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