Use size_t type for the buffer_size parameter to formatter functions, static two...
[Rockbox.git] / firmware / profile.c
blob71fe3438792bd4271baebe5bb8cc7103f4c5ed97
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Profiling routines counts ticks and calls to each profiled function.
12 * Copyright (C) 2005 by Brandon Low
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************
22 * profile_func_enter() based on mcount found in gmon.c:
24 ***************************************************************************
25 * Copyright (c) 1991, 1998 The Regents of the University of California.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. [rescinded 22 July 1999]
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 * @(#)gmon.c 5.3 (Berkeley) 5/22/91
55 #include <file.h>
56 #include <sprintf.h>
57 #include <system.h>
58 #include <string.h>
59 #include <timer.h>
60 #include "profile.h"
62 static unsigned short profiling = PROF_OFF;
63 static size_t recursion_level;
64 static unsigned short indices[INDEX_SIZE];
65 static struct pfd_struct pfds[NUMPFDS];
66 /* This holds a pointer to the last pfd effected for time tracking */
67 static struct pfd_struct *last_pfd;
68 /* These are used to track the time when we've lost the CPU so it doesn't count
69 * against any of the profiled functions */
70 static int profiling_thread = -1;
72 /* internal function prototypes */
73 static void profile_timer_tick(void);
74 static void profile_timer_unregister(void);
76 static void write_function_recursive(int fd, struct pfd_struct *pfd, int depth);
78 /* Be careful to use the right one for the size of your variable */
79 #ifdef CPU_COLDFIRE
80 #define ADDQI_L(_var,_value) \
81 asm ("addq.l %[value],%[var];" \
82 : [var] "+g" (_var) \
83 : [value] "I" (_value) )
84 #else
85 #define ADDQI_L(var, value) var += value
86 #endif
88 void profile_thread_stopped(int current_thread) {
89 if (current_thread == profiling_thread) {
90 /* If profiling is busy or idle */
91 if (profiling < PROF_ERROR) {
92 /* Unregister the timer so that other threads aren't interrupted */
93 timer_unregister();
95 /* Make sure we don't waste time profiling when we're running the
96 * wrong thread */
97 profiling |= PROF_OFF_THREAD;
101 void profile_thread_started(int current_thread) {
102 if (current_thread == profiling_thread) {
103 /* Now we are allowed to profile again */
104 profiling &= PROF_ON_THREAD;
105 /* if profiling was busy or idle */
106 if (profiling < PROF_ERROR) {
107 /* After we de-mask, if profiling is active, reactivate the timer */
108 timer_register(0, profile_timer_unregister,
109 TIMER_FREQ/10000, 0, profile_timer_tick);
114 static void profile_timer_tick(void) {
115 if (!profiling) {
116 register struct pfd_struct *my_last_pfd = last_pfd;
117 if (my_last_pfd) {
118 ADDQI_L(my_last_pfd->time,1);
123 static void profile_timer_unregister(void) {
124 profiling = PROF_ERROR;
125 profstop();
128 /* This function clears the links on top level linkers, and clears the needed
129 * parts of memory in the index array */
130 void profstart(int current_thread) {
131 recursion_level = 0;
132 profiling_thread = current_thread;
133 last_pfd = (struct pfd_struct*)0;
134 pfds[0].link = 0;
135 pfds[0].self_pc = 0;
136 memset(&indices,0,INDEX_SIZE * sizeof(unsigned short));
137 timer_register(
138 0, profile_timer_unregister, TIMER_FREQ/10000, 0, profile_timer_tick);
139 profiling = PROF_ON;
142 static void write_function_recursive(int fd, struct pfd_struct *pfd, int depth){
143 unsigned short link = pfd->link;
144 fdprintf(fd,"0x%08lX\t%08ld\t%08ld\t%04d\n", (size_t)pfd->self_pc,
145 pfd->count, pfd->time, depth);
146 if (link > 0 && link < NUMPFDS) {
147 write_function_recursive(fd, &pfds[link], depth++);
151 void profstop() {
152 int profiling_exit = profiling;
153 int fd = 0;
154 int i;
155 unsigned short current_index;
156 timer_unregister();
157 profiling = PROF_OFF;
158 fd = open("/profile.out", O_WRONLY|O_CREAT|O_TRUNC);
159 if (profiling_exit == PROF_ERROR) {
160 fdprintf(fd,"Profiling exited with an error.\n");
161 fdprintf(fd,"Overflow or timer stolen most likely.\n");
163 fdprintf(fd,"PROFILE_THREAD\tPFDS_USED\n");
164 fdprintf(fd,"%08d\t%08d\n", profiling_thread,
165 pfds[0].link);
166 fdprintf(fd,"FUNCTION_PC\tCALL_COUNT\tTICKS\t\tDEPTH\n");
167 for (i = 0; i < INDEX_SIZE; i++) {
168 current_index = indices[i];
169 if (current_index != 0) {
170 write_function_recursive(fd, &pfds[current_index], 0);
173 fdprintf(fd,"DEBUG PROFILE DATA FOLLOWS\n");
174 fdprintf(fd,"INDEX\tLOCATION\tSELF_PC\t\tCOUNT\t\tTIME\t\tLINK\tCALLER\n");
175 for (i = 0; i < NUMPFDS; i++) {
176 struct pfd_struct *my_last_pfd = &pfds[i];
177 if (my_last_pfd->self_pc != 0) {
178 fdprintf(fd,
179 "%04d\t0x%08lX\t0x%08lX\t0x%08lX\t0x%08lX\t%04d\t0x%08lX\n",
180 i, (size_t)my_last_pfd, (size_t)my_last_pfd->self_pc,
181 my_last_pfd->count, my_last_pfd->time, my_last_pfd->link,
182 (size_t)my_last_pfd->caller);
185 fdprintf(fd,"INDEX_ADDRESS=INDEX\n");
186 for (i=0; i < INDEX_SIZE; i++) {
187 fdprintf(fd,"%08lX=%04d\n",(size_t)&indices[i],indices[i]);
189 close(fd);
192 void profile_func_exit(void *self_pc, void *call_site) {
193 (void)call_site;
194 (void)self_pc;
195 /* When we started timing, we set the time to the tick at that time
196 * less the time already used in function */
197 if (profiling) {
198 return;
200 profiling = PROF_BUSY;
202 register unsigned short my_recursion_level = recursion_level;
203 if (my_recursion_level) {
204 my_recursion_level--;
205 recursion_level = my_recursion_level;
206 } else {
207 /* This shouldn't be necessary, maybe exit could be called first */
208 register struct pfd_struct *my_last_pfd = last_pfd;
209 if (my_last_pfd) {
210 last_pfd = my_last_pfd->caller;
214 profiling = PROF_ON;
217 #define ALLOCATE_PFD(temp) \
218 temp = ++pfds[0].link;\
219 if (temp >= NUMPFDS) goto overflow; \
220 pfd = &pfds[temp];\
221 pfd->self_pc = self_pc; pfd->count = 1; pfd->time = 0
223 void profile_func_enter(void *self_pc, void *from_pc) {
224 struct pfd_struct *pfd;
225 struct pfd_struct *prev_pfd;
226 unsigned short *pfd_index_pointer;
227 unsigned short pfd_index;
229 /* check that we are profiling and that we aren't recursively invoked
230 * this is equivalent to 'if (profiling != PROF_ON)' but it's faster */
231 if (profiling) {
232 return;
234 profiling = PROF_BUSY;
235 /* A check that the PC is in the code range here wouldn't hurt, but this is
236 * logically guaranteed to be a valid address unless the constants are
237 * breaking the rules. */
238 pfd_index_pointer = &indices[((size_t)from_pc)&INDEX_MASK];
239 pfd_index = *pfd_index_pointer;
240 if (pfd_index == 0) {
241 /* new caller, allocate new storage */
242 ALLOCATE_PFD(pfd_index);
243 pfd->link = 0;
244 *pfd_index_pointer = pfd_index;
245 goto done;
247 pfd = &pfds[pfd_index];
248 if (pfd->self_pc == self_pc) {
249 /* only / most recent function called by this caller, usual case */
250 /* increment count, start timing and exit */
251 goto found;
253 /* collision, bad for performance, look down the list of functions called by
254 * colliding PCs */
255 for (; /* goto done */; ) {
256 pfd_index = pfd->link;
257 if (pfd_index == 0) {
258 /* no more previously called functions, allocate a new one */
259 ALLOCATE_PFD(pfd_index);
260 /* this function becomes the new head, link to the old head */
261 pfd->link = *pfd_index_pointer;
262 /* and set the index to point to this function */
263 *pfd_index_pointer = pfd_index;
264 /* start timing and exit */
265 goto done;
267 /* move along the chain */
268 prev_pfd = pfd;
269 pfd = &pfds[pfd_index];
270 if (pfd->self_pc == self_pc) {
271 /* found ourself */
272 /* Remove me from my old spot */
273 prev_pfd->link = pfd->link;
274 /* Link to the old head */
275 pfd->link = *pfd_index_pointer;
276 /* Make me head */
277 *pfd_index_pointer = pfd_index;
278 /* increment count, start timing and exit */
279 goto found;
283 /* We've found a pfd, increment it */
284 found:
285 ADDQI_L(pfd->count,1);
286 /* We've (found or created) and updated our pfd, save it and start timing */
287 done:
289 register struct pfd_struct *my_last_pfd = last_pfd;
290 if (pfd != my_last_pfd) {
291 /* If we are not recursing */
292 pfd->caller = my_last_pfd;
293 last_pfd = pfd;
294 } else {
295 ADDQI_L(recursion_level,1);
298 /* Start timing this function */
299 profiling = PROF_ON;
300 return; /* normal return restores saved registers */
302 overflow:
303 /* this is the same as 'profiling = PROF_ERROR' */
304 profiling = PROF_ERROR;
305 return;