debug.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2008-2009 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. * @defgroup debug Debug
  25. * @{
  26. */
  27. /**
  28. * @file
  29. * @brief Debug console functions.
  30. */
  31. #include <debug.h>
  32. #include <kernel/thread.h>
  33. #include <kernel/timer.h>
  34. #include <platform.h>
  35. #if WITH_LIB_CONSOLE
  36. #include <lib/console.h>
  37. static int cmd_threads(int argc, const cmd_args *argv);
  38. static int cmd_threadstats(int argc, const cmd_args *argv);
  39. static int cmd_threadload(int argc, const cmd_args *argv);
  40. static int cmd_exitDebug(int argc, const cmd_args *argv);
  41. STATIC_COMMAND_START
  42. #if DEBUGLEVEL > 1
  43. STATIC_COMMAND("threads", "list kernel threads", &cmd_threads)
  44. STATIC_COMMAND("exit", "exit debug", &cmd_exitDebug)
  45. #endif
  46. #if THREAD_STATS
  47. STATIC_COMMAND("threadstats", "thread level statistics", &cmd_threadstats)
  48. STATIC_COMMAND("threadload", "toggle thread load display", &cmd_threadload)
  49. #endif
  50. STATIC_COMMAND_END(kernel);
  51. #if DEBUGLEVEL > 1
  52. static int cmd_threads(int argc, const cmd_args *argv)
  53. {
  54. printf("thread list:\n");
  55. dump_all_threads();
  56. return 0;
  57. }
  58. #endif
  59. static int cmd_exitDebug(int argc, const cmd_args *argv)
  60. {
  61. printf("cmd_exitDebug:\n");
  62. mt_boot_init(NULL);
  63. return 0;
  64. }
  65. #if THREAD_STATS
  66. static int cmd_threadstats(int argc, const cmd_args *argv)
  67. {
  68. printf("thread stats:\n");
  69. printf("\ttotal idle time: %lld\n", thread_stats.idle_time);
  70. printf("\ttotal busy time: %lld\n", current_time_hires() - thread_stats.idle_time);
  71. printf("\treschedules: %d\n", thread_stats.reschedules);
  72. printf("\tcontext_switches: %d\n", thread_stats.context_switches);
  73. printf("\tpreempts: %d\n", thread_stats.preempts);
  74. printf("\tyields: %d\n", thread_stats.yields);
  75. printf("\tinterrupts: %d\n", thread_stats.interrupts);
  76. printf("\ttimer interrupts: %d\n", thread_stats.timer_ints);
  77. printf("\ttimers: %d\n", thread_stats.timers);
  78. return 0;
  79. }
  80. static enum handler_return threadload(struct timer *t, time_t now, void *arg)
  81. {
  82. static struct thread_stats old_stats;
  83. static bigtime_t last_idle_time;
  84. bigtime_t idle_time = thread_stats.idle_time;
  85. if (current_thread == idle_thread) {
  86. idle_time += current_time_hires() - thread_stats.last_idle_timestamp;
  87. }
  88. bigtime_t busy_time = 1000000ULL - (idle_time - last_idle_time);
  89. uint busypercent = (busy_time * 10000) / (1000000);
  90. // printf("idle_time %lld, busytime %lld\n", idle_time - last_idle_time, busy_time);
  91. printf("LOAD: %d.%02d%%, cs %d, ints %d, timer ints %d, timers %d\n", busypercent / 100, busypercent % 100,
  92. thread_stats.context_switches - old_stats.context_switches,
  93. thread_stats.interrupts - old_stats.interrupts,
  94. thread_stats.timer_ints - old_stats.timer_ints,
  95. thread_stats.timers - old_stats.timers);
  96. old_stats = thread_stats;
  97. last_idle_time = idle_time;
  98. return INT_NO_RESCHEDULE;
  99. }
  100. static int cmd_threadload(int argc, const cmd_args *argv)
  101. {
  102. static bool showthreadload = false;
  103. static timer_t tltimer;
  104. enter_critical_section();
  105. if (showthreadload == false) {
  106. // start the display
  107. timer_initialize(&tltimer);
  108. timer_set_periodic(&tltimer, 1000, &threadload, NULL);
  109. showthreadload = true;
  110. } else {
  111. timer_cancel(&tltimer);
  112. showthreadload = false;
  113. }
  114. exit_critical_section();
  115. return 0;
  116. }
  117. #endif
  118. #endif