root/nano-RK/projects/basic_task_stats/main.c

Revision 476, 6.6 kB (checked in by agr, 5 months ago)

kernel task stats api added

Line 
1 /******************************************************************************
2 *  Nano-RK, a real-time operating system for sensor networks.
3 *  Copyright (C) 2007, Real-Time and Multimedia Lab, Carnegie Mellon University
4 *  All rights reserved.
5 *
6 *  This is the Open Source Version of Nano-RK included as part of a Dual
7 *  Licensing Model. If you are unsure which license to use please refer to:
8 *  http://www.nanork.org/nano-RK/wiki/Licensing
9 *
10 *  This program is free software: you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation, version 2.0 of the License.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 *
22 *******************************************************************************/
23
24 #include <nrk.h>
25 #include <include.h>
26 #include <ulib.h>
27 #include <stdio.h>
28 #include <hal.h>
29 #include <nrk_error.h>
30 #include <nrk_timer.h>
31 #include <nrk_stack_check.h>
32 #include <nrk_stats.h>
33
34
35 NRK_STK Stack1[NRK_APP_STACKSIZE];
36 nrk_task_type TaskOne;
37 void Task1(void);
38
39 NRK_STK Stack2[NRK_APP_STACKSIZE];
40 nrk_task_type TaskTwo;
41 void Task2 (void);
42
43 NRK_STK Stack3[NRK_APP_STACKSIZE];
44 nrk_task_type TaskThree;
45 void Task3 (void);
46
47
48 NRK_STK Stack4[NRK_APP_STACKSIZE];
49 nrk_task_type TaskFour;
50 void Task4 (void);
51
52 void nrk_create_taskset();
53 uint8_t kill_stack(uint8_t val);
54
55 int
56 main ()
57 {
58   nrk_setup_ports();
59   nrk_setup_uart(UART_BAUDRATE_115K2);
60
61   nrk_init();
62
63   nrk_led_clr(ORANGE_LED);
64   nrk_led_clr(BLUE_LED);
65   nrk_led_clr(GREEN_LED);
66   nrk_led_clr(RED_LED);
67  
68   nrk_time_set(0,0);
69   nrk_create_taskset ();
70   nrk_start();
71  
72   return 0;
73 }
74
75 // This structure should be global since it is quite large!
76 nrk_task_stat_t my_stats;
77
78 void Task1()
79 {
80 nrk_time_t t;
81 uint16_t cnt;
82 cnt=0;
83 nrk_kprintf( PSTR("Nano-RK Version ") );
84 printf( "%d\r\n",NRK_VERSION );
85
86 printf( "My node's address is %u\r\n",NODE_ADDR );
87  
88 printf( "Task1 PID=%u\r\n",nrk_get_pid());
89
90   while(1) {
91         nrk_led_toggle(ORANGE_LED);
92         nrk_gpio_toggle(NRK_DEBUG_0);
93         printf( "Task1 cnt=%u\r\n",cnt );
94         nrk_wait_until_next_period();
95         // Uncomment this line to cause a stack overflow
96         // if(cnt>20) kill_stack(10);
97
98         // At time 50, the OS will halt and print statistics
99         // This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
100          if(cnt==50)  {
101                 nrk_stats_display_all();
102                 // This will induce a kernel panic on purpose   
103                 nrk_halt();
104                 }
105
106         // This is an example of how to access the task execution data
107         if( cnt==10 ) {
108         nrk_stats_get(nrk_get_pid(), &my_stats);
109         nrk_kprintf( PSTR( "\r\n   Total CPU: "));
110         t=_nrk_ticks_to_time(my_stats.total_ticks);
111         printf( "%lu secs %lu ms", t.secs, t.nano_secs/NANOS_PER_MS );
112         nrk_kprintf( PSTR( "\r\n   Time [Min,Last,Max]: "));
113         t=_nrk_ticks_to_time(my_stats.min_exec_ticks);
114         printf( "%lu secs %lu ms, ", t.secs, t.nano_secs/NANOS_PER_MS );
115         t=_nrk_ticks_to_time(my_stats.last_exec_ticks);
116         printf( "%lu secs %lu ms, ", t.secs, t.nano_secs/NANOS_PER_MS );
117         t=_nrk_ticks_to_time(my_stats.max_exec_ticks);
118         printf( "%lu secs %lu ms", t.secs, t.nano_secs/NANOS_PER_MS );
119         nrk_kprintf( PSTR( "\r\n   Swap-ins: "));
120         printf( "%lu",my_stats.swapped_in );
121         nrk_kprintf( PSTR( "\r\n   Preemptions: "));
122         printf( "%lu",my_stats.preempted);
123         nrk_kprintf( PSTR( "\r\n   Kernel Violations: "));
124         printf( "%u",my_stats.violations);
125         nrk_kprintf( PSTR( "\r\n   Overflow Error Status: "));
126         printf( "%u",my_stats.overflow);
127         nrk_kprintf( PSTR("\r\n") );
128         }
129
130         cnt++;
131         }
132 }
133
134 void Task2()
135 {
136   int16_t cnt;
137   printf( "Task2 PID=%u\r\n",nrk_get_pid());
138   cnt=0;
139   while(1) {
140         nrk_led_toggle(BLUE_LED);
141         nrk_gpio_toggle(NRK_DEBUG_1);
142         printf( "Task2 signed cnt=%d\r\n",cnt );
143         nrk_wait_until_next_period();
144         //nrk_stats_display_pid(nrk_get_pid());
145         cnt--;
146         }
147 }
148
149 void Task3()
150 {
151 uint16_t cnt;
152   printf( "Task3 PID=%u\r\n",nrk_get_pid());
153   cnt=0;
154   while(1) {
155         nrk_led_toggle(GREEN_LED);
156         nrk_gpio_toggle(NRK_DEBUG_2);
157         printf( "Task3 cnt=%u\r\n",cnt );
158         nrk_wait_until_next_period();
159         cnt++;
160         }
161 }
162
163 void Task4()
164 {
165 uint16_t cnt;
166
167   printf( "Task4 PID=%u\r\n",nrk_get_pid());
168   cnt=0;
169   while(1) {
170         nrk_led_toggle(RED_LED);
171         nrk_gpio_toggle(NRK_DEBUG_3);
172         printf( "Task4 cnt=%u\r\n",cnt );
173         nrk_wait_until_next_period();
174         cnt++;
175         }
176 }
177
178 void
179 nrk_create_taskset()
180 {
181   nrk_task_set_entry_function( &TaskOne, Task1);
182   nrk_task_set_stk( &TaskOne, Stack1, NRK_APP_STACKSIZE);
183   TaskOne.prio = 1;
184   TaskOne.FirstActivation = TRUE;
185   TaskOne.Type = BASIC_TASK;
186   TaskOne.SchType = PREEMPTIVE;
187   TaskOne.period.secs = 0;
188   TaskOne.period.nano_secs = 250*NANOS_PER_MS;
189   TaskOne.cpu_reserve.secs = 1;
190   TaskOne.cpu_reserve.nano_secs = 50*NANOS_PER_MS;
191   TaskOne.offset.secs = 0;
192   TaskOne.offset.nano_secs= 0;
193   nrk_activate_task (&TaskOne);
194
195   nrk_task_set_entry_function( &TaskTwo, Task2);
196   nrk_task_set_stk( &TaskTwo, Stack2, NRK_APP_STACKSIZE);
197   TaskTwo.prio = 2;
198   TaskTwo.FirstActivation = TRUE;
199   TaskTwo.Type = BASIC_TASK;
200   TaskTwo.SchType = PREEMPTIVE;
201   TaskTwo.period.secs = 0;
202   TaskTwo.period.nano_secs = 500*NANOS_PER_MS;
203   TaskTwo.cpu_reserve.secs = 0;
204   TaskTwo.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
205   TaskTwo.offset.secs = 0;
206   TaskTwo.offset.nano_secs= 0;
207   nrk_activate_task (&TaskTwo);
208
209
210   nrk_task_set_entry_function( &TaskThree, Task3);
211   nrk_task_set_stk( &TaskThree, Stack3, NRK_APP_STACKSIZE);
212   TaskThree.prio = 3;
213   TaskThree.FirstActivation = TRUE;
214   TaskThree.Type = BASIC_TASK;
215   TaskThree.SchType = PREEMPTIVE;
216   TaskThree.period.secs = 1;
217   TaskThree.period.nano_secs = 0;
218   TaskThree.cpu_reserve.secs = 0;
219   TaskThree.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
220   TaskThree.offset.secs = 0;
221   TaskThree.offset.nano_secs= 0;
222   nrk_activate_task (&TaskThree);
223
224
225   nrk_task_set_entry_function( &TaskFour, Task4);
226   nrk_task_set_stk( &TaskFour, Stack4, NRK_APP_STACKSIZE);
227   TaskFour.prio = 4;
228   TaskFour.FirstActivation = TRUE;
229   TaskFour.Type = BASIC_TASK;
230   TaskFour.SchType = PREEMPTIVE;
231   TaskFour.period.secs = 2;
232   TaskFour.period.nano_secs = 0;
233   TaskFour.cpu_reserve.secs = 0;
234   TaskFour.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
235   TaskFour.offset.secs = 0;
236   TaskFour.offset.nano_secs= 0;
237   nrk_activate_task (&TaskFour);
238
239
240 }
241
242 uint8_t kill_stack(uint8_t val)
243 {
244 char bad_memory[10];
245 uint8_t i;
246 for(i=0; i<10; i++ ) bad_memory[i]=i;
247 for(i=0; i<10; i++ ) printf( "%d ", bad_memory[i]);
248    printf( "Die Stack %d\r\n",val );
249 if(val>1) kill_stack(val-1);
250 return 0;
251 }
252
253
Note: See TracBrowser for help on using the browser.