cpuusage.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <rtthread.h>
  2. #include <rthw.h>
  3. #include "cpuusage.h"
  4. #include "lcd.h"
  5. #ifdef RT_USING_RTGUI
  6. #include <rtgui/event.h>
  7. #include <rtgui/rtgui_server.h>
  8. #include <rtgui/rtgui_system.h>
  9. #endif
  10. #define CPU_USAGE_CALC_TICK 10
  11. #define CPU_USAGE_LOOP 100
  12. static rt_uint8_t cpu_usage_major = 0, cpu_usage_minor= 0;
  13. static rt_uint32_t total_count = 0;
  14. static void cpu_usage_idle_hook()
  15. {
  16. rt_tick_t tick;
  17. rt_uint32_t count;
  18. volatile rt_uint32_t loop;
  19. if (total_count == 0)
  20. {
  21. loop = 0;
  22. /* get total count */
  23. rt_enter_critical();
  24. tick = rt_tick_get();
  25. while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
  26. {
  27. total_count ++;
  28. while (loop < CPU_USAGE_LOOP) loop ++;
  29. }
  30. rt_exit_critical();
  31. }
  32. count = 0;
  33. loop = 0;
  34. /* get CPU usage */
  35. tick = rt_tick_get();
  36. while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
  37. {
  38. count ++;
  39. while (loop < CPU_USAGE_LOOP) loop ++;
  40. }
  41. /* calculate major and minor */
  42. if (count < total_count)
  43. {
  44. count = total_count - count;
  45. cpu_usage_major = (count * 100) / total_count;
  46. cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
  47. }
  48. else
  49. {
  50. total_count = count;
  51. /* no CPU usage */
  52. cpu_usage_major = 0;
  53. cpu_usage_minor = 0;
  54. }
  55. }
  56. void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
  57. {
  58. RT_ASSERT(major != RT_NULL);
  59. RT_ASSERT(minor != RT_NULL);
  60. *major = cpu_usage_major;
  61. *minor = cpu_usage_minor;
  62. }
  63. void cpu_usage_init()
  64. {
  65. /* set idle thread hook */
  66. rt_thread_idle_sethook(cpu_usage_idle_hook);
  67. }
  68. extern struct rt_messagequeue mq;
  69. extern rt_thread_t info_tid;
  70. static void cpu_thread_entry(void *parameter)
  71. {
  72. #ifdef RT_USING_RTGUI
  73. struct rtgui_event_command ecmd;
  74. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  75. ecmd.type = RTGUI_CMD_USER_INT;
  76. ecmd.command_id = CPU_UPDATE;
  77. #else
  78. struct lcd_msg msg;
  79. #endif
  80. while (1)
  81. {
  82. #ifdef RT_USING_RTGUI
  83. rtgui_thread_send(info_tid, &ecmd.parent, sizeof(ecmd));
  84. #else
  85. msg.type = CPU_MSG;
  86. msg.major = cpu_usage_major;
  87. msg.minor = cpu_usage_minor;
  88. rt_mq_send(&mq, &msg, sizeof(msg));
  89. #endif
  90. rt_thread_delay(20);
  91. }
  92. }
  93. static rt_thread_t cpu_thread;
  94. void rt_hw_cpu_init(void)
  95. {
  96. cpu_usage_init();
  97. cpu_thread = rt_thread_create("cpu", cpu_thread_entry, RT_NULL, 384, 27, 5);
  98. if(cpu_thread != RT_NULL)
  99. rt_thread_startup(cpu_thread);
  100. }