syswatch_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * File : syswatch_test.c
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2020-01-10 qiyongzhong first version
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <rtthread.h>
  11. #include <syswatch_config.h>
  12. #ifdef SYSWATCH_USING_TEST
  13. #define DBG_TAG "syswatch_test"
  14. #define DBG_LVL DBG_LOG
  15. #include <rtdbg.h>
  16. static rt_uint32_t thread_cnt = 0;
  17. static rt_uint32_t thread_sign = 0;
  18. static void syswatch_test_thread_entry(void *parameter)
  19. {
  20. rt_uint32_t delay_s = (rt_uint32_t)parameter;
  21. rt_thread_t thread = rt_thread_self();
  22. #if (SYSWATCH_EXCEPT_RESOLVE_MODE == 2)
  23. rt_uint32_t idx;
  24. idx = atoi(thread->name+6);
  25. idx %= 32;
  26. if ((thread_sign & (1<<idx)) == 0)//first entry
  27. {
  28. thread_sign |= (1<<idx);
  29. LOG_I("system watch test thread startup, name = %.*s, priority = %d", RT_NAME_MAX, thread->name, thread->current_priority);
  30. LOG_I("system watch thread exception timeout = %d s, delay = %d s", SYSWATCH_EXCEPT_TIMEOUT, delay_s);
  31. LOG_I("system watch exception resolve mode = %d", SYSWATCH_EXCEPT_RESOLVE_MODE);
  32. rt_thread_mdelay(delay_s * 1000);
  33. LOG_I("%.*s thread exception begin. priority = %d", RT_NAME_MAX, thread->name ,thread->current_priority);
  34. while(1);
  35. }
  36. else//second entry
  37. {
  38. thread_sign &= ~(1<<idx);
  39. LOG_I("%.*s thread test completion, it will be closed after 10s .", RT_NAME_MAX, thread->name);
  40. rt_thread_mdelay(10 * 1000);
  41. LOG_I("%.*s thread closed.", RT_NAME_MAX, thread->name);
  42. }
  43. #else
  44. LOG_I("system watch test thread startup, name = %.*s, priority = %d", RT_NAME_MAX, thread->name, thread->current_priority);
  45. LOG_I("system watch thread exception timeout = %d s, delay = %d s", SYSWATCH_EXCEPT_TIMEOUT, delay_s);
  46. LOG_I("system watch exception resolve mode = %d", SYSWATCH_EXCEPT_RESOLVE_MODE);
  47. rt_thread_mdelay(delay_s * 1000);
  48. LOG_I("%.*s thread exception begin. priority = %d", RT_NAME_MAX, thread->name ,thread->current_priority);
  49. while(1);
  50. #endif
  51. }
  52. static void syswatch_test_startup(rt_uint8_t prio, rt_uint32_t except_delay_s)
  53. {
  54. rt_thread_t tid;
  55. char name[RT_NAME_MAX+1];
  56. thread_cnt++;
  57. rt_sprintf(name, "swtst_%02d", thread_cnt);
  58. tid = rt_thread_create(name, syswatch_test_thread_entry, (void *)(except_delay_s),
  59. 512, prio, 20);
  60. if ( ! tid)
  61. {
  62. LOG_E("create syswatch test thread failed.");
  63. return;
  64. }
  65. rt_thread_startup(tid);
  66. }
  67. static void syswatch_test(int argc, char **argv)
  68. {
  69. rt_uint8_t prio = 0;
  70. rt_uint32_t delay_s = 0;
  71. if(argc < 2)
  72. {
  73. rt_kprintf("Usage: syswatch_test <priority 0-%d> [delay_s]\n", RT_THREAD_PRIORITY_MAX-2);
  74. return ;
  75. }
  76. prio = atoi(argv[1]);
  77. if (prio > RT_THREAD_PRIORITY_MAX-2)
  78. {
  79. prio = RT_THREAD_PRIORITY_MAX-2;
  80. }
  81. if (argc == 3)
  82. {
  83. delay_s = atoi(argv[2]);
  84. }
  85. syswatch_test_startup(prio, delay_s);
  86. }
  87. MSH_CMD_EXPORT(syswatch_test, system watch test);
  88. #endif //SYSWATCH_TEST_USING