yerden

signal test app

Jan 30th, 2016
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.58 KB | None | 0 0
  1. diff --git a/test/signal/CMakeLists.txt b/test/signal/CMakeLists.txt
  2. new file mode 100644
  3. index 0000000..dd847ad
  4. --- /dev/null
  5. +++ b/test/signal/CMakeLists.txt
  6. @@ -0,0 +1,12 @@
  7. +INCLUDE(${CMAKE_HOME_DIRECTORY}/cmake/env.cmake)
  8. +SET(SRCS "")
  9. +LIST(APPEND SRCS main.c)
  10. +
  11. +# user gcc flags
  12. +ADD_DEFINITIONS(-O0 -g)
  13. +
  14. +SET(APP signal)
  15. +ADD_EXECUTABLE(${APP} ${SRCS})
  16. +TARGET_LINK_LIBRARIES(${APP} -Wl,--start-group ${STS_LIBS} -Wl,--end-group)
  17. +link_rte_libs_all(${APP})
  18. +TARGET_LINK_LIBRARIES(${APP} ${OTHER_LIB})
  19. diff --git a/test/signal/main.c b/test/signal/main.c
  20. new file mode 100644
  21. index 0000000..f20da2e
  22. --- /dev/null
  23. +++ b/test/signal/main.c
  24. @@ -0,0 +1,204 @@
  25. +#include <stdlib.h>
  26. +#include <string.h>
  27. +#include <ev.h>
  28. +#include <getopt.h>
  29. +#include <unistd.h>
  30. +
  31. +#include <rte_common.h>
  32. +#include <rte_eal.h>
  33. +#include <rte_lcore.h>
  34. +#include <rte_cycles.h>
  35. +
  36. +#include <sts_common.h>
  37. +#include <sts_lcore.h>
  38. +#include <sts_evloop.h>
  39. +#include <sts_buffer.h>
  40. +#include <sts_tcp_frame.h>
  41. +#include <sts_rpc_client.h>
  42. +
  43. +#define LCORE_SIGNUM SIGHUP
  44. +
  45. +
  46. +/* Application side */
  47. +struct lcore_app_params {
  48. +   int8_t exit;
  49. +};
  50. +
  51. +static struct lcore_app_params lcore_params[RTE_MAX_LCORE] = { { 0 } };
  52. +
  53. +/* get params */
  54. +static struct lcore_app_params *
  55. +get_params(sts_lcore_t core)
  56. +{
  57. +   return lcore_params + core;
  58. +}
  59. +
  60. +/* get my params */
  61. +static struct lcore_app_params *
  62. +get_my_params(void)
  63. +{
  64. +   return get_params(rte_lcore_id());
  65. +}
  66. +
  67. +/* lcore function */
  68. +static int
  69. +lcore_loop(__attribute__((unused)) void *arg)
  70. +{
  71. +   uint64_t cnt;
  72. +   struct lcore_app_params *p = get_my_params();
  73. +
  74. +   for (cnt = 0; p->exit == 0; cnt++) {
  75. +       rte_pause();
  76. +   }
  77. +
  78. +   printf("C%03u: cycles count = %lu\n", rte_lcore_id(), cnt);
  79. +   return 0;
  80. +}
  81. +
  82. +static void
  83. +lcore_app_shutdown(void *arg)
  84. +{
  85. +   struct lcore_app_params *p = arg;
  86. +   p->exit = 1;
  87. +}
  88. +
  89. +
  90. +
  91. +
  92. +
  93. +
  94. +
  95. +
  96. +/* library side */
  97. +struct lcore_cell {
  98. +   /* thread id */
  99. +   volatile pthread_t tid;
  100. +
  101. +   /* lcore function */
  102. +   lcore_function_t *func;
  103. +   void *description;
  104. +
  105. +   /* Shutdown and setup hooks with their arg */
  106. +   void (*shutdown)(void *);
  107. +   void (*setup)(void *);
  108. +   void *opaque_arg;
  109. +};
  110. +
  111. +static struct lcore_cell lcores[RTE_MAX_LCORE] = { { .shutdown = NULL } };
  112. +
  113. +/* get remote cell */
  114. +static struct lcore_cell *
  115. +get_cell(sts_lcore_t core)
  116. +{
  117. +   return lcores + core;
  118. +}
  119. +
  120. +/* get my cell */
  121. +static struct lcore_cell *
  122. +get_my_cell(void)
  123. +{
  124. +   return get_cell(rte_lcore_id());
  125. +}
  126. +
  127. +static void
  128. +lcore_sig_action(int signum, siginfo_t *si, __attribute__((unused)) void *ctx)
  129. +{
  130. +   if (signum != LCORE_SIGNUM) {
  131. +       printf("wrong signal %d\n", signum);
  132. +       abort();
  133. +   }
  134. +
  135. +   printf("caught signal %d on lcore %u from pid %d\n", signum, rte_lcore_id(), si->si_pid);
  136. +   struct lcore_cell *c = get_my_cell();
  137. +   c->shutdown(c->opaque_arg);
  138. +}
  139. +
  140. +/* set up each thread */
  141. +static int
  142. +lcore_get_tid(__attribute__((unused)) void *arg)
  143. +{
  144. +   printf("set up lcore %u\n", rte_lcore_id());
  145. +   struct lcore_cell *c = get_my_cell();
  146. +   sigset_t set;
  147. +   int ret;
  148. +
  149. +   /* set our thread id */
  150. +   c->tid = pthread_self();
  151. +
  152. +   /* shutdown hook */
  153. +   c->func = lcore_loop;
  154. +   c->shutdown = lcore_app_shutdown;
  155. +   c->opaque_arg = get_my_params();
  156. +
  157. +   /* setting signal mask */
  158. +   sigemptyset(&set);
  159. +   sigfillset(&set);
  160. +   sigdelset(&set, LCORE_SIGNUM);
  161. +
  162. +   if ((ret = pthread_sigmask(SIG_SETMASK, &set, NULL)) != 0) {
  163. +       return ret;
  164. +   }
  165. +
  166. +   struct sigaction newact = {
  167. +       .sa_sigaction = lcore_sig_action,
  168. +       .sa_flags = SA_SIGINFO | SA_RESTART,
  169. +   };
  170. +
  171. +   if ((ret = sigaction(LCORE_SIGNUM, &newact, NULL)) != 0) {
  172. +       return ret;
  173. +   }
  174. +
  175. +   return 0;
  176. +}
  177. +
  178. +static void
  179. +invoke_core_shutdown(sts_lcore_t core)
  180. +{
  181. +   struct lcore_cell *c = get_cell(core);
  182. +#ifndef USE_SIGNAL
  183. +   c->shutdown(c->opaque_arg);
  184. +#else
  185. +   pthread_kill(c->tid, LCORE_SIGNUM);
  186. +#endif
  187. +}
  188. +
  189. +
  190. +
  191. +
  192. +
  193. +
  194. +int
  195. +main(
  196. +       __attribute__((unused)) int argc,
  197. +       __attribute__((unused)) char **argv)
  198. +{
  199. +   int ret = rte_eal_init(argc, argv);
  200. +   if (ret < 0) {
  201. +       return ret;
  202. +   }
  203. +   argc -= ret;
  204. +   argv += ret;
  205. +
  206. +   if (rte_eal_mp_remote_launch(lcore_get_tid, NULL, SKIP_MASTER) != 0) {
  207. +       return 1;
  208. +   }
  209. +
  210. +   printf("process pid %u\n", getpid());
  211. +   rte_eal_mp_wait_lcore();
  212. +
  213. +   if (rte_eal_mp_remote_launch(lcore_loop, NULL, SKIP_MASTER) != 0) {
  214. +       return 1;
  215. +   }
  216. +
  217. +   sts_lcore_t core;
  218. +   RTE_LCORE_FOREACH_SLAVE(core) {
  219. +       /* dramatic pause */
  220. +       sleep(1);
  221. +       /* kill a core */
  222. +       invoke_core_shutdown(core);
  223. +       /* wait for a core to finish */
  224. +       rte_eal_wait_lcore(core);
  225. +   }
  226. +
  227. +   return 0;
  228. +}
Advertisement
Add Comment
Please, Sign In to add comment