Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
Travis Geiselbrecht | 671cb79 | 2009-06-28 11:27:48 -0700 | [diff] [blame] | 2 | * Copyright (c) 2008-2009 Travis Geiselbrecht |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 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 Geiselbrecht | 671cb79 | 2009-06-28 11:27:48 -0700 | [diff] [blame] | 29 | #if WITH_LIB_CONSOLE |
Travis Geiselbrecht | 39deded | 2009-01-24 20:12:27 -0800 | [diff] [blame] | 30 | #include <lib/console.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 31 | |
| 32 | static int cmd_threads(int argc, const cmd_args *argv); |
| 33 | static int cmd_threadstats(int argc, const cmd_args *argv); |
| 34 | static int cmd_threadload(int argc, const cmd_args *argv); |
| 35 | |
| 36 | STATIC_COMMAND_START |
| 37 | #if DEBUGLEVEL > 1 |
Travis Geiselbrecht | 671cb79 | 2009-06-28 11:27:48 -0700 | [diff] [blame] | 38 | STATIC_COMMAND("threads", "list kernel threads", &cmd_threads) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 39 | #endif |
| 40 | #if THREAD_STATS |
Travis Geiselbrecht | 671cb79 | 2009-06-28 11:27:48 -0700 | [diff] [blame] | 41 | STATIC_COMMAND("threadstats", "thread level statistics", &cmd_threadstats) |
| 42 | STATIC_COMMAND("threadload", "toggle thread load display", &cmd_threadload) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 43 | #endif |
| 44 | STATIC_COMMAND_END(kernel); |
| 45 | |
| 46 | #if DEBUGLEVEL > 1 |
| 47 | static 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 |
| 57 | static 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 | |
| 73 | static 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 Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 78 | 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 | |
| 99 | static 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 Geiselbrecht | 671cb79 | 2009-06-28 11:27:48 -0700 | [diff] [blame] | 109 | timer_set_periodic(&tltimer, 1000, &threadload, NULL); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 110 | 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 | |