blob: c3d72204104e68ad52a90a196d0387bd65c6bebb [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <debug.h>
25#include <kernel/thread.h>
26#include <kernel/timer.h>
27#include <platform.h>
28
Travis Geiselbrecht39deded2009-01-24 20:12:27 -080029#if defined(WITH_LIB_CONSOLE)
30#include <lib/console.h>
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070031
32static int cmd_threads(int argc, const cmd_args *argv);
33static int cmd_threadstats(int argc, const cmd_args *argv);
34static int cmd_threadload(int argc, const cmd_args *argv);
35
36STATIC_COMMAND_START
37#if DEBUGLEVEL > 1
38 { "threads", "list kernel threads", &cmd_threads },
39#endif
40#if THREAD_STATS
41 { "threadstats", "thread level statistics", &cmd_threadstats },
42 { "threadload", "toggle thread load display", &cmd_threadload },
43#endif
44STATIC_COMMAND_END(kernel);
45
46#if DEBUGLEVEL > 1
47static int cmd_threads(int argc, const cmd_args *argv)
48{
49 printf("thread list:\n");
50 dump_all_threads();
51
52 return 0;
53}
54#endif
55
56#if THREAD_STATS
57static int cmd_threadstats(int argc, const cmd_args *argv)
58{
59 printf("thread stats:\n");
60 printf("\ttotal idle time: %lld\n", thread_stats.idle_time);
61 printf("\ttotal busy time: %lld\n", current_time_hires() - thread_stats.idle_time);
62 printf("\treschedules: %d\n", thread_stats.reschedules);
63 printf("\tcontext_switches: %d\n", thread_stats.context_switches);
64 printf("\tpreempts: %d\n", thread_stats.preempts);
65 printf("\tyields: %d\n", thread_stats.yields);
66 printf("\tinterrupts: %d\n", thread_stats.interrupts);
67 printf("\ttimer interrupts: %d\n", thread_stats.timer_ints);
68 printf("\ttimers: %d\n", thread_stats.timers);
69
70 return 0;
71}
72
73static enum handler_return threadload(struct timer *t, time_t now, void *arg)
74{
75 static struct thread_stats old_stats;
76 static bigtime_t last_idle_time;
77
78 timer_set_oneshot(t, 1000, &threadload, NULL);
79
80 bigtime_t idle_time = thread_stats.idle_time;
81 if (current_thread == idle_thread) {
82 idle_time += current_time_hires() - thread_stats.last_idle_timestamp;
83 }
84 bigtime_t busy_time = 1000000ULL - (idle_time - last_idle_time);
85
86 uint busypercent = (busy_time * 10000) / (1000000);
87
88// printf("idle_time %lld, busytime %lld\n", idle_time - last_idle_time, busy_time);
89 printf("LOAD: %d.%02d%%, cs %d, ints %d, timer ints %d, timers %d\n", busypercent / 100, busypercent % 100,
90 thread_stats.context_switches - old_stats.context_switches,
91 thread_stats.interrupts - old_stats.interrupts,
92 thread_stats.timer_ints - old_stats.timer_ints,
93 thread_stats.timers - old_stats.timers);
94
95 old_stats = thread_stats;
96 last_idle_time = idle_time;
97
98 return INT_NO_RESCHEDULE;
99}
100
101static int cmd_threadload(int argc, const cmd_args *argv)
102{
103 static bool showthreadload = false;
104 static timer_t tltimer;
105
106 enter_critical_section();
107
108 if (showthreadload == false) {
109 // start the display
110 timer_initialize(&tltimer);
111 timer_set_oneshot(&tltimer, 1000, &threadload, NULL);
112 showthreadload = true;
113 } else {
114 timer_cancel(&tltimer);
115 showthreadload = false;
116 }
117
118 exit_critical_section();
119
120 return 0;
121}
122
123#endif
124
125#endif
126