cpuusage.c 2.6 KB

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