irq_storm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file irq_storm.c
  15. * @brief IRQ Storm stress test code.
  16. *
  17. * @addtogroup IRQ_STORM
  18. * @{
  19. */
  20. #include <stdlib.h>
  21. #include "ch.h"
  22. #include "hal.h"
  23. #include "chprintf.h"
  24. #include "irq_storm.h"
  25. /*===========================================================================*/
  26. /* Module local definitions. */
  27. /*===========================================================================*/
  28. #define MSG_SEND_LEFT (msg_t)0
  29. #define MSG_SEND_RIGHT (msg_t)1
  30. /*===========================================================================*/
  31. /* Module exported variables. */
  32. /*===========================================================================*/
  33. /*===========================================================================*/
  34. /* Module local types. */
  35. /*===========================================================================*/
  36. /*===========================================================================*/
  37. /* Module local variables. */
  38. /*===========================================================================*/
  39. static const irq_storm_config_t *config;
  40. static bool saturated;
  41. /*
  42. * Mailboxes and buffers.
  43. */
  44. static mailbox_t mb[IRQ_STORM_CFG_NUM_THREADS];
  45. static msg_t b[IRQ_STORM_CFG_NUM_THREADS][IRQ_STORM_CFG_MAILBOX_SIZE];
  46. /*
  47. * Threads working areas.
  48. */
  49. static THD_WORKING_AREA(irq_storm_thread_wa[IRQ_STORM_CFG_NUM_THREADS],
  50. IRQ_STORM_CFG_STACK_SIZE);
  51. /*
  52. * Pointers to threads.
  53. */
  54. static thread_t *threads[IRQ_STORM_CFG_NUM_THREADS];
  55. /*===========================================================================*/
  56. /* Module local functions. */
  57. /*===========================================================================*/
  58. /*===========================================================================*/
  59. /* Module exported functions. */
  60. /*===========================================================================*/
  61. /*
  62. * Test worker threads.
  63. */
  64. static THD_FUNCTION(irq_storm_thread, arg) {
  65. static volatile unsigned x = 0;
  66. static unsigned cnt = 0;
  67. unsigned me = (unsigned)arg;
  68. unsigned target;
  69. unsigned r;
  70. msg_t msg;
  71. chRegSetThreadName("irq_storm");
  72. /* Thread loop, until terminated.*/
  73. while (chThdShouldTerminateX() == false) {
  74. /* Waiting for a message.*/
  75. chMBFetchTimeout(&mb[me], &msg, TIME_INFINITE);
  76. #if IRQ_STORM_CFG_RANDOMIZE != FALSE
  77. /* Pseudo-random delay.*/
  78. {
  79. chSysLock();
  80. r = rand() & 15;
  81. chSysUnlock();
  82. while (r--)
  83. x++;
  84. }
  85. #else /* IRQ_STORM_CFG_RANDOMIZE == FALSE */
  86. /* Fixed delay.*/
  87. {
  88. r = me >> 4;
  89. while (r--)
  90. x++;
  91. }
  92. #endif /* IRQ_STORM_CFG_RANDOMIZE == FALSE */
  93. /* Deciding in which direction to re-send the message.*/
  94. if (msg == MSG_SEND_LEFT)
  95. target = me - 1;
  96. else
  97. target = me + 1;
  98. if (target < IRQ_STORM_CFG_NUM_THREADS) {
  99. /* If this thread is not at the end of a chain re-sending the message,
  100. note this check works because the variable target is unsigned.*/
  101. msg = chMBPostTimeout(&mb[target], msg, TIME_IMMEDIATE);
  102. if (msg != MSG_OK)
  103. saturated = TRUE;
  104. }
  105. else {
  106. /* Provides a visual feedback about the system.*/
  107. if (++cnt >= 500) {
  108. cnt = 0;
  109. palTogglePad(config->port, config->pad);
  110. }
  111. }
  112. }
  113. }
  114. /**
  115. * @brief GPT1 callback.
  116. */
  117. void irq_storm_gpt1_cb(GPTDriver *gptp) {
  118. msg_t msg;
  119. (void)gptp;
  120. chSysLockFromISR();
  121. msg = chMBPostI(&mb[0], MSG_SEND_RIGHT);
  122. if (msg != MSG_OK)
  123. saturated = true;
  124. chSysUnlockFromISR();
  125. }
  126. /**
  127. * @brief GPT2 callback.
  128. */
  129. void irq_storm_gpt2_cb(GPTDriver *gptp) {
  130. msg_t msg;
  131. (void)gptp;
  132. chSysLockFromISR();
  133. msg = chMBPostI(&mb[IRQ_STORM_CFG_NUM_THREADS - 1], MSG_SEND_LEFT);
  134. if (msg != MSG_OK)
  135. saturated = true;
  136. chSysUnlockFromISR();
  137. }
  138. /**
  139. * @brief IRQ storm execution.
  140. *
  141. * @param[in] cfg pointer to the test configuration structure
  142. *
  143. * @api
  144. */
  145. void irq_storm_execute(const irq_storm_config_t *cfg) {
  146. unsigned i;
  147. gptcnt_t interval, threshold, worst;
  148. /* Global configuration pointer.*/
  149. config = cfg;
  150. /* Starting timers using the stored configurations.*/
  151. gptStart(cfg->gpt1p, cfg->gptcfg1p);
  152. gptStart(cfg->gpt2p, cfg->gptcfg2p);
  153. /*
  154. * Initializes the mailboxes and creates the worker threads.
  155. */
  156. for (i = 0; i < IRQ_STORM_CFG_NUM_THREADS; i++) {
  157. chMBObjectInit(&mb[i], b[i], IRQ_STORM_CFG_MAILBOX_SIZE);
  158. threads[i] = chThdCreateStatic(irq_storm_thread_wa[i],
  159. sizeof irq_storm_thread_wa[i],
  160. IRQ_STORM_CFG_THREADS_PRIORITY,
  161. irq_storm_thread,
  162. (void *)i);
  163. }
  164. /* Printing environment information.*/
  165. chprintf(cfg->out, "");
  166. chprintf(cfg->out, "\r\n*** ChibiOS/RT IRQ-STORM long duration test\r\n***\r\n");
  167. chprintf(cfg->out, "*** Kernel: %s\r\n", CH_KERNEL_VERSION);
  168. chprintf(cfg->out, "*** Compiled: %s\r\n", __DATE__ " - " __TIME__);
  169. #ifdef PORT_COMPILER_NAME
  170. chprintf(cfg->out, "*** Compiler: %s\r\n", PORT_COMPILER_NAME);
  171. #endif
  172. chprintf(cfg->out, "*** Architecture: %s\r\n", PORT_ARCHITECTURE_NAME);
  173. #ifdef PORT_CORE_VARIANT_NAME
  174. chprintf(cfg->out, "*** Core Variant: %s\r\n", PORT_CORE_VARIANT_NAME);
  175. #endif
  176. chprintf(cfg->out, "*** System Clock: %d\r\n", cfg->sysclk);
  177. #ifdef PORT_INFO
  178. chprintf(cfg->out, "*** Port Info: %s\r\n", PORT_INFO);
  179. #endif
  180. #ifdef PLATFORM_NAME
  181. chprintf(cfg->out, "*** Platform: %s\r\n", PLATFORM_NAME);
  182. #endif
  183. #ifdef BOARD_NAME
  184. chprintf(cfg->out, "*** Test Board: %s\r\n", BOARD_NAME);
  185. #endif
  186. chprintf(cfg->out, "***\r\n");
  187. chprintf(cfg->out, "*** Iterations: %d\r\n", IRQ_STORM_CFG_ITERATIONS);
  188. chprintf(cfg->out, "*** Randomize: %d\r\n", IRQ_STORM_CFG_RANDOMIZE);
  189. chprintf(cfg->out, "*** Threads: %d\r\n", IRQ_STORM_CFG_NUM_THREADS);
  190. chprintf(cfg->out, "*** Mailbox size: %d\r\n\r\n", IRQ_STORM_CFG_MAILBOX_SIZE);
  191. /* Test loop.*/
  192. worst = 0;
  193. for (i = 1; i <= IRQ_STORM_CFG_ITERATIONS; i++){
  194. chprintf(cfg->out, "Iteration %d\r\n", i);
  195. saturated = false;
  196. threshold = 0;
  197. /* Timer intervals starting at 2mS then decreased by 10% after each
  198. cycle.*/
  199. for (interval = 2000; interval >= 2; interval -= (interval + 9) / 10) {
  200. /* Timers programmed slightly out of phase each other.*/
  201. gptStartContinuous(cfg->gpt1p, interval - 1); /* Slightly out of phase.*/
  202. gptStartContinuous(cfg->gpt2p, interval + 1); /* Slightly out of phase.*/
  203. /* Storming for one second.*/
  204. chThdSleepMilliseconds(1000);
  205. /* Timers stopped.*/
  206. gptStopTimer(cfg->gpt1p);
  207. gptStopTimer(cfg->gpt2p);
  208. /* Did the storm saturate the threads chain?*/
  209. if (!saturated)
  210. chprintf(cfg->out, ".");
  211. else {
  212. chprintf(cfg->out, "#");
  213. if (threshold == 0)
  214. threshold = interval;
  215. break;
  216. }
  217. }
  218. /* Gives threads a chance to empty the mailboxes before next cycle.*/
  219. chThdSleepMilliseconds(20);
  220. chprintf(cfg->out, "\r\nSaturated at %d uS\r\n\r\n", threshold);
  221. if (threshold > worst)
  222. worst = threshold;
  223. }
  224. gptStopTimer(cfg->gpt1p);
  225. gptStopTimer(cfg->gpt2p);
  226. chprintf(cfg->out, "Worst case at %d uS\r\n", worst);
  227. chprintf(cfg->out, "\r\nTest Complete\r\n");
  228. /* Terminating threads and cleaning up.*/
  229. for (i = 0; i < IRQ_STORM_CFG_NUM_THREADS; i++) {
  230. chThdTerminate(threads[i]);
  231. chThdWait(threads[i]);
  232. threads[i] = NULL;
  233. }
  234. }
  235. /** @} */