blob: 7b7a64878252f6878cd2e0992ed092273d43fd19 [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
Travis Geiselbrecht671cb792009-06-28 11:27:48 -07002 * Copyright (c) 2008-2009 Travis Geiselbrecht
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07003 *
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 Geiselbrecht671cb792009-06-28 11:27:48 -070029#if WITH_LIB_CONSOLE
Travis Geiselbrecht39deded2009-01-24 20:12:27 -080030#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
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070038STATIC_COMMAND("threads", "list kernel threads", &cmd_threads)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070039#endif
40#if THREAD_STATS
Travis Geiselbrecht671cb792009-06-28 11:27:48 -070041STATIC_COMMAND("threadstats", "thread level statistics", &cmd_threadstats)
42STATIC_COMMAND("threadload", "toggle thread load display", &cmd_threadload)
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070043#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
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -070078 bigtime_t idle_time = thread_stats.idle_time;
79 if (current_thread == idle_thread) {
80 idle_time += current_time_hires() - thread_stats.last_idle_timestamp;
81 }
82 bigtime_t busy_time = 1000000ULL - (idle_time - last_idle_time);
83
84 uint busypercent = (busy_time * 10000) / (1000000);
85
86// printf("idle_time %lld, busytime %lld\n", idle_time - last_idle_time, busy_time);
87 printf("LOAD: %d.%02d%%, cs %d, ints %d, timer ints %d, timers %d\n", busypercent / 100, busypercent % 100,
88 thread_stats.context_switches - old_stats.context_switches,
89 thread_stats.interrupts - old_stats.interrupts,
90 thread_stats.timer_ints - old_stats.timer_ints,
91 thread_stats.timers - old_stats.timers);
92
93 old_stats = thread_stats;
94 last_idle_time = idle_time;
95
96 return INT_NO_RESCHEDULE;
97}
98
99static int cmd_threadload(int argc, const cmd_args *argv)
100{
101 static bool showthreadload = false;
102 static timer_t tltimer;
103
104 enter_critical_section();
105
106 if (showthreadload == false) {
107 // start the display
108 timer_initialize(&tltimer);
Travis Geiselbrecht671cb792009-06-28 11:27:48 -0700109 timer_set_periodic(&tltimer, 1000, &threadload, NULL);
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -0700110 showthreadload = true;
111 } else {
112 timer_cancel(&tltimer);
113 showthreadload = false;
114 }
115
116 exit_critical_section();
117
118 return 0;
119}
120
121#endif
122
123#endif
124