Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/test/signal/CMakeLists.txt b/test/signal/CMakeLists.txt
- new file mode 100644
- index 0000000..dd847ad
- --- /dev/null
- +++ b/test/signal/CMakeLists.txt
- @@ -0,0 +1,12 @@
- +INCLUDE(${CMAKE_HOME_DIRECTORY}/cmake/env.cmake)
- +SET(SRCS "")
- +LIST(APPEND SRCS main.c)
- +
- +# user gcc flags
- +ADD_DEFINITIONS(-O0 -g)
- +
- +SET(APP signal)
- +ADD_EXECUTABLE(${APP} ${SRCS})
- +TARGET_LINK_LIBRARIES(${APP} -Wl,--start-group ${STS_LIBS} -Wl,--end-group)
- +link_rte_libs_all(${APP})
- +TARGET_LINK_LIBRARIES(${APP} ${OTHER_LIB})
- diff --git a/test/signal/main.c b/test/signal/main.c
- new file mode 100644
- index 0000000..f20da2e
- --- /dev/null
- +++ b/test/signal/main.c
- @@ -0,0 +1,204 @@
- +#include <stdlib.h>
- +#include <string.h>
- +#include <ev.h>
- +#include <getopt.h>
- +#include <unistd.h>
- +
- +#include <rte_common.h>
- +#include <rte_eal.h>
- +#include <rte_lcore.h>
- +#include <rte_cycles.h>
- +
- +#include <sts_common.h>
- +#include <sts_lcore.h>
- +#include <sts_evloop.h>
- +#include <sts_buffer.h>
- +#include <sts_tcp_frame.h>
- +#include <sts_rpc_client.h>
- +
- +#define LCORE_SIGNUM SIGHUP
- +
- +
- +/* Application side */
- +struct lcore_app_params {
- + int8_t exit;
- +};
- +
- +static struct lcore_app_params lcore_params[RTE_MAX_LCORE] = { { 0 } };
- +
- +/* get params */
- +static struct lcore_app_params *
- +get_params(sts_lcore_t core)
- +{
- + return lcore_params + core;
- +}
- +
- +/* get my params */
- +static struct lcore_app_params *
- +get_my_params(void)
- +{
- + return get_params(rte_lcore_id());
- +}
- +
- +/* lcore function */
- +static int
- +lcore_loop(__attribute__((unused)) void *arg)
- +{
- + uint64_t cnt;
- + struct lcore_app_params *p = get_my_params();
- +
- + for (cnt = 0; p->exit == 0; cnt++) {
- + rte_pause();
- + }
- +
- + printf("C%03u: cycles count = %lu\n", rte_lcore_id(), cnt);
- + return 0;
- +}
- +
- +static void
- +lcore_app_shutdown(void *arg)
- +{
- + struct lcore_app_params *p = arg;
- + p->exit = 1;
- +}
- +
- +
- +
- +
- +
- +
- +
- +
- +/* library side */
- +struct lcore_cell {
- + /* thread id */
- + volatile pthread_t tid;
- +
- + /* lcore function */
- + lcore_function_t *func;
- + void *description;
- +
- + /* Shutdown and setup hooks with their arg */
- + void (*shutdown)(void *);
- + void (*setup)(void *);
- + void *opaque_arg;
- +};
- +
- +static struct lcore_cell lcores[RTE_MAX_LCORE] = { { .shutdown = NULL } };
- +
- +/* get remote cell */
- +static struct lcore_cell *
- +get_cell(sts_lcore_t core)
- +{
- + return lcores + core;
- +}
- +
- +/* get my cell */
- +static struct lcore_cell *
- +get_my_cell(void)
- +{
- + return get_cell(rte_lcore_id());
- +}
- +
- +static void
- +lcore_sig_action(int signum, siginfo_t *si, __attribute__((unused)) void *ctx)
- +{
- + if (signum != LCORE_SIGNUM) {
- + printf("wrong signal %d\n", signum);
- + abort();
- + }
- +
- + printf("caught signal %d on lcore %u from pid %d\n", signum, rte_lcore_id(), si->si_pid);
- + struct lcore_cell *c = get_my_cell();
- + c->shutdown(c->opaque_arg);
- +}
- +
- +/* set up each thread */
- +static int
- +lcore_get_tid(__attribute__((unused)) void *arg)
- +{
- + printf("set up lcore %u\n", rte_lcore_id());
- + struct lcore_cell *c = get_my_cell();
- + sigset_t set;
- + int ret;
- +
- + /* set our thread id */
- + c->tid = pthread_self();
- +
- + /* shutdown hook */
- + c->func = lcore_loop;
- + c->shutdown = lcore_app_shutdown;
- + c->opaque_arg = get_my_params();
- +
- + /* setting signal mask */
- + sigemptyset(&set);
- + sigfillset(&set);
- + sigdelset(&set, LCORE_SIGNUM);
- +
- + if ((ret = pthread_sigmask(SIG_SETMASK, &set, NULL)) != 0) {
- + return ret;
- + }
- +
- + struct sigaction newact = {
- + .sa_sigaction = lcore_sig_action,
- + .sa_flags = SA_SIGINFO | SA_RESTART,
- + };
- +
- + if ((ret = sigaction(LCORE_SIGNUM, &newact, NULL)) != 0) {
- + return ret;
- + }
- +
- + return 0;
- +}
- +
- +static void
- +invoke_core_shutdown(sts_lcore_t core)
- +{
- + struct lcore_cell *c = get_cell(core);
- +#ifndef USE_SIGNAL
- + c->shutdown(c->opaque_arg);
- +#else
- + pthread_kill(c->tid, LCORE_SIGNUM);
- +#endif
- +}
- +
- +
- +
- +
- +
- +
- +int
- +main(
- + __attribute__((unused)) int argc,
- + __attribute__((unused)) char **argv)
- +{
- + int ret = rte_eal_init(argc, argv);
- + if (ret < 0) {
- + return ret;
- + }
- + argc -= ret;
- + argv += ret;
- +
- + if (rte_eal_mp_remote_launch(lcore_get_tid, NULL, SKIP_MASTER) != 0) {
- + return 1;
- + }
- +
- + printf("process pid %u\n", getpid());
- + rte_eal_mp_wait_lcore();
- +
- + if (rte_eal_mp_remote_launch(lcore_loop, NULL, SKIP_MASTER) != 0) {
- + return 1;
- + }
- +
- + sts_lcore_t core;
- + RTE_LCORE_FOREACH_SLAVE(core) {
- + /* dramatic pause */
- + sleep(1);
- + /* kill a core */
- + invoke_core_shutdown(core);
- + /* wait for a core to finish */
- + rte_eal_wait_lcore(core);
- + }
- +
- + return 0;
- +}
Advertisement
Add Comment
Please, Sign In to add comment