syswatch_test.c 3.2 KB

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