2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_sample_profile.h>
22 #ifndef AO_SAMPLE_PROFILE_LOW_PC
23 #define AO_SAMPLE_PROFILE_LOW_PC 0x08002000
26 #ifndef AO_SAMPLE_PROFILE_HIGH_PC
27 #define AO_SAMPLE_PROFILE_HIGH_PC 0x0800f000
30 #ifndef AO_SAMPLE_PROFILE_SHIFT
31 #define AO_SAMPLE_PROFILE_SHIFT 6
34 #define AO_SAMPLE_PROFILE_RANGE (AO_SAMPLE_PROFILE_HIGH_PC - AO_SAMPLE_PROFILE_LOW_PC)
35 #define AO_SAMPLE_PROFILE_NUM (AO_SAMPLE_PROFILE_RANGE >> AO_SAMPLE_PROFILE_SHIFT)
37 static uint16_t prev_tick;
38 static uint16_t samples[AO_SAMPLE_PROFILE_NUM];
39 static uint8_t missed[AO_SAMPLE_PROFILE_NUM/8];
40 static uint16_t max_miss;
41 static uint32_t task, isr, os, idle;
43 extern uint8_t ao_idle_loc;
46 ao_sample_profile_point(uint32_t pc, uint16_t tick, uint8_t in_isr)
48 uint16_t delta = tick - prev_tick;
50 if (pc < AO_SAMPLE_PROFILE_LOW_PC)
52 if (pc >= AO_SAMPLE_PROFILE_HIGH_PC)
58 asm("mov %0,sp" : "=&r" (sp));
59 sp_delta = sp - (uint8_t *) ao_cur_task->stack;
60 if (-96 < sp_delta && sp_delta < 16)
61 ao_panic(AO_PANIC_STACK);
66 else if (ao_cur_task) {
67 ao_cur_task->ticks += delta;
69 } else if (pc == (uint32_t) &ao_idle_loc)
74 pc -= AO_SAMPLE_PROFILE_LOW_PC;
75 pc >>= AO_SAMPLE_PROFILE_SHIFT;
79 missed[pc >> 3] |= (1 << (pc & 7));
86 ao_sample_profile_start(void)
88 prev_tick = ao_sample_profile_timer_start();
92 ao_sample_profile_stop(void)
94 ao_sample_profile_timer_stop();
98 ao_sample_profile_dump(void)
103 printf ("task %6d\n", task);
104 printf ("isr %6d\n", isr);
105 printf ("os %6d\n", os);
106 printf ("idle %6d\n", idle);
107 printf ("irq blocked %d\n", max_miss);
108 for (t = 0; t < ao_num_tasks; t++)
109 printf ("task %6d %6d %6d %s\n",
112 ao_tasks[t]->max_run,
114 for (a = 0; a < AO_SAMPLE_PROFILE_NUM; a++) {
116 printf ("%04x %c %u\n",
117 (a << AO_SAMPLE_PROFILE_SHIFT) + AO_SAMPLE_PROFILE_LOW_PC,
118 missed[a >> 3] & (1 << (a & 7)) ? '*' : ' ',
124 ao_sample_profile_clear(void)
128 task = isr = os = idle = 0;
130 memset(samples, '\0', sizeof (samples));
131 memset(missed, '\0', sizeof (missed));
132 for (t = 0; t < ao_num_tasks; t++) {
133 ao_tasks[t]->ticks = 0;
134 ao_tasks[t]->yields = 0;
135 ao_tasks[t]->max_run = 0;
140 ao_sample_profile_cmd(void)
143 switch (ao_cmd_lex_c) {
145 ao_sample_profile_start();
148 ao_sample_profile_stop();
151 ao_sample_profile_dump();
154 ao_sample_profile_clear();
157 ao_cmd_status = ao_cmd_syntax_error;
162 static __code struct ao_cmds ao_sample_profile_cmds[] = {
163 { ao_sample_profile_cmd, "S <1 start,0 stop, d dump,c clear>\0Sample profile" },
168 ao_sample_profile_init(void)
170 ao_sample_profile_timer_init();
171 ao_cmd_register(&ao_sample_profile_cmds[0]);
172 ao_sample_profile_clear();