void test_cancel2(void) { gallus_result_t r; shutdown_grace_level_t l; null_thread_t nt = NULL; global_state_reset(); r = global_state_set(GLOBAL_STATE_INITIALIZING); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = s_null_create(&nt, SHUTDOWN_GRACEFULLY); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = gallus_thread_start((gallus_thread_t *)&nt, false); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = global_state_set(GLOBAL_STATE_STARTED); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); gallus_msg_debug(1, "waiting for shutdown request ...\n"); while ((r = global_state_wait_for_shutdown_request( &l, 1000LL * 1000LL * 100LL)) == GALLUS_RESULT_TIMEDOUT) { gallus_msg_debug(1, "still waiting for shutdown request ...\n"); } if (r != GALLUS_RESULT_OK) { gallus_perror(r); } TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); TEST_ASSERT_EQUAL(l, SHUTDOWN_GRACEFULLY); gallus_thread_destroy((gallus_thread_t *)&nt); }
static bool s_start_ofp_handler(void) { lagopus_result_t res = LAGOPUS_RESULT_ANY_FAILURES; /* set gstate started */ res = global_state_set(GLOBAL_STATE_STARTED); if (res != LAGOPUS_RESULT_OK) { lagopus_perror(res); TEST_FAIL_MESSAGE("handler_test_utils.c: gstate start error"); return false; } /* create ofp_handler */ res = ofp_handler_initialize(NULL, &th); if (res != LAGOPUS_RESULT_OK) { lagopus_perror(res); TEST_FAIL_MESSAGE("handler_test_utils.c: handler creation error"); return false; } /* start ofp_handler */ res = ofp_handler_start(); if (res != LAGOPUS_RESULT_OK) { lagopus_perror(res); TEST_FAIL_MESSAGE("handler_test_utils.c: handler start error"); return false; } return true; }
static void pipeline_stage_create_start(lagopus_pipeline_stage_t *sptr, lagopus_pipeline_stage_pre_pause_proc_t pre_pause_proc, lagopus_pipeline_stage_sched_proc_t sched_proc, lagopus_pipeline_stage_setup_proc_t setup_proc, lagopus_pipeline_stage_fetch_proc_t fetch_proc, lagopus_pipeline_stage_main_proc_t main_proc, lagopus_pipeline_stage_throw_proc_t throw_proc, lagopus_pipeline_stage_shutdown_proc_t shutdown_proc, lagopus_pipeline_stage_finalize_proc_t finalize_proc, lagopus_pipeline_stage_freeup_proc_t freeup_proc) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; pipeline_stage_create(sptr, pre_pause_proc, sched_proc, setup_proc, fetch_proc, main_proc, throw_proc, shutdown_proc, finalize_proc, freeup_proc); ret = lagopus_pipeline_stage_start(sptr); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "lagopus_pipeline_stage_start error."); SLEEP; ret = global_state_set(GLOBAL_STATE_STARTED); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "global_state_set error."); SLEEP; }
void setUp(void) { if (dpmgr != NULL) { return; } /* Datapath manager alloc. */ dpmgr = dpmgr_alloc(); if (dpmgr == NULL) { fprintf(stderr, "Datapath manager allocation failed\n"); exit(-1); } (void)global_state_set(GLOBAL_STATE_STARTED); }
void test_cancel1(void) { gallus_result_t r; null_thread_t nt = NULL; global_state_reset(); r = global_state_set(GLOBAL_STATE_INITIALIZING); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = s_null_create(&nt, SHUTDOWN_GRACEFULLY); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = gallus_thread_start((gallus_thread_t *)&nt, false); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); sleep(1); gallus_thread_destroy((gallus_thread_t *)&nt); }
void test_set_get(void) { gallus_result_t r; global_state_t s; int i; global_state_reset(); for (i = (int)GLOBAL_STATE_INITIALIZING; i <= (int)GLOBAL_STATE_FINALIZED; i++) { r = global_state_set((global_state_t)i); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); r = global_state_get(&s); TEST_ASSERT_EQUAL(r, GALLUS_RESULT_OK); TEST_ASSERT_EQUAL(s, (global_state_t)i); } }
int main(int argc, const char *const argv[]) { lagopus_result_t st = LAGOPUS_RESULT_ANY_FAILURES; lagopus_pipeline_stage_t s = NULL; size_t nthd = 1; (void)argc; if (IS_VALID_STRING(argv[1]) == true) { size_t tmp; if (lagopus_str_parse_uint64(argv[1], &tmp) == LAGOPUS_RESULT_OK && tmp > 0LL) { nthd = tmp; } } fprintf(stdout, "Creating... "); st = lagopus_pipeline_stage_create(&s, 0, "a_test", nthd, sizeof(void *), 1024, s_pre_pause, s_sched, s_setup, s_fetch, s_main, s_throw, s_shutdown, s_finalize, s_freeup); if (st == LAGOPUS_RESULT_OK) { fprintf(stdout, "Created.\n"); fprintf(stdout, "Setting up... "); st = lagopus_pipeline_stage_setup(&s); if (st == LAGOPUS_RESULT_OK) { fprintf(stdout, "Set up.\n"); fprintf(stdout, "Starting... "); st = lagopus_pipeline_stage_start(&s); if (st == LAGOPUS_RESULT_OK) { fprintf(stdout, "Started.\n"); fprintf(stdout, "Opening the front door... "); st = global_state_set(GLOBAL_STATE_STARTED); if (st == LAGOPUS_RESULT_OK) { char buf[1024]; char *cmd = NULL; fprintf(stdout, "The front door is open.\n"); fprintf(stdout, "> "); while (fgets(buf, sizeof(buf), stdin) != NULL && st == LAGOPUS_RESULT_OK) { (void)lagopus_str_trim_right(buf, "\r\n\t ", &cmd); if (strcasecmp(cmd, "pause") == 0 || strcasecmp(cmd, "spause") == 0) { fprintf(stdout, "Pausing... "); if ((st = lagopus_pipeline_stage_pause(&s, -1LL)) == LAGOPUS_RESULT_OK) { if (strcasecmp(cmd, "spause") == 0) { s_set(0LL); } fprintf(stdout, "Paused " PF64(u) "\n", s_get()); } else { fprintf(stdout, "Failure.\n"); } } else if (strcasecmp(cmd, "resume") == 0) { fprintf(stdout, "Resuming... "); if ((st = lagopus_pipeline_stage_resume(&s)) == LAGOPUS_RESULT_OK) { fprintf(stdout, "Resumed.\n"); } else { fprintf(stdout, "Failure.\n"); } } else if (strcasecmp(cmd, "get") == 0) { fprintf(stdout, PF64(u) "\n", s_get()); } free((void *)cmd); cmd = NULL; fprintf(stdout, "> "); } fprintf(stdout, "\nDone.\n"); fprintf(stdout, "Shutting down... "); st = lagopus_pipeline_stage_shutdown(&s, SHUTDOWN_GRACEFULLY); if (st == LAGOPUS_RESULT_OK) { fprintf(stdout, "Shutdown accepted... "); sleep(1); s_do_stop = true; fprintf(stdout, "Waiting shutdown... "); st = lagopus_pipeline_stage_wait(&s, -1LL); if (st == LAGOPUS_RESULT_OK) { fprintf(stdout, "OK, Shutdown.\n"); } } } } } } fflush(stdout); if (st != LAGOPUS_RESULT_OK) { lagopus_perror(st); } fprintf(stdout, "Destroying... "); lagopus_pipeline_stage_destroy(&s); fprintf(stdout, "Destroyed.\n"); return (st == LAGOPUS_RESULT_OK) ? 0 : 1; }
static inline int s_do_main(int argc, const char *const argv[], int ipcfd) { lagopus_result_t st = LAGOPUS_RESULT_ANY_FAILURES; lagopus_msg_info("Initializing all the modules.\n"); (void)global_state_set(GLOBAL_STATE_INITIALIZING); if ((st = lagopus_module_initialize_all(argc, (const char *const *)argv)) == LAGOPUS_RESULT_OK && s_got_term_sig == false) { lagopus_msg_info("All the modules are initialized.\n"); lagopus_msg_info("Starting all the modules.\n"); (void)global_state_set(GLOBAL_STATE_STARTING); if ((st = lagopus_module_start_all()) == LAGOPUS_RESULT_OK && s_got_term_sig == false) { shutdown_grace_level_t l = SHUTDOWN_UNKNOWN; lagopus_msg_info("All the modules are started and ready to go.\n"); config_propagate_lagopus_conf(); (void)global_state_set(GLOBAL_STATE_STARTED); if (ipcfd >= 0) { int zero = 0; (void)write(ipcfd, (void *)&zero, sizeof(int)); (void)close(ipcfd); ipcfd = -1; } lagopus_msg_info("The Lagopus is a go.\n"); s_gen_pidfile(); while ((st = global_state_wait_for_shutdown_request(&l, REQ_TIMEDOUT)) == LAGOPUS_RESULT_TIMEDOUT) { lagopus_msg_debug(5, "Waiting for the shutdown request...\n"); } if (st == LAGOPUS_RESULT_OK) { (void)global_state_set(GLOBAL_STATE_ACCEPT_SHUTDOWN); if ((st = lagopus_module_shutdown_all(l)) == LAGOPUS_RESULT_OK) { if ((st = lagopus_module_wait_all(s_to)) == LAGOPUS_RESULT_OK) { lagopus_msg_info("Shutdown succeeded.\n"); } else if (st == LAGOPUS_RESULT_TIMEDOUT) { do_cancel: lagopus_msg_warning("Trying to stop forcibly...\n"); if ((st = lagopus_module_stop_all()) == LAGOPUS_RESULT_OK) { if ((st = lagopus_module_wait_all(s_to)) == LAGOPUS_RESULT_OK) { lagopus_msg_warning("Stopped forcibly.\n"); } } } } else if (st == LAGOPUS_RESULT_TIMEDOUT) { goto do_cancel; } } } } lagopus_module_finalize_all(); if (st != LAGOPUS_RESULT_OK) { lagopus_msg_warning("Bailed out, anyway. The latest result status is:" "%s\n", lagopus_error_get_string(st)); if (ipcfd >= 0) { int one = 1; (void)write(ipcfd, (void *)&one, sizeof(int)); (void)close(ipcfd); } } s_del_pidfile(); return (st == LAGOPUS_RESULT_OK) ? 0 : 1; }
int main(int argc, const char *const argv[]) { lagopus_result_t st = LAGOPUS_RESULT_ANY_FAILURES; test_stage_t t = NULL; size_t tmp; size_t nthd = 1; size_t c_max = 1000LL * 1000LL * 100LL; size_t mod = 0; lagopus_chrono_t t_begin; lagopus_chrono_t t_end; lagopus_chrono_t t_total; (void)argc; WHAT_TIME_IS_IT_NOW_IN_NSEC(t_begin); if (IS_VALID_STRING(argv[1]) == true) { if (lagopus_str_parse_uint64(argv[1], &tmp) == LAGOPUS_RESULT_OK && tmp > 0LL) { nthd = tmp; } if (IS_VALID_STRING(argv[2]) == true) { if (lagopus_str_parse_uint64(argv[2], &tmp) == LAGOPUS_RESULT_OK && tmp > 0LL) { c_max = tmp; } if (IS_VALID_STRING(argv[3]) == true) { if (lagopus_str_parse_uint64(argv[3], &tmp) == LAGOPUS_RESULT_OK) { mod = tmp; } } } } st = lagopus_pipeline_stage_create((lagopus_pipeline_stage_t *)&t, sizeof(*t), "a_test", nthd, sizeof(void *), 1024, s_pre_pause, s_sched, s_setup, /* s_fetch, */ NULL, s_main, /* s_throw, */ NULL, s_shutdown, s_finalize, s_freeup); if (st == LAGOPUS_RESULT_OK) { t->m_n_max_count = c_max; t->m_mod = mod; st = lagopus_pipeline_stage_setup((lagopus_pipeline_stage_t *)&t); if (st == LAGOPUS_RESULT_OK) { st = lagopus_pipeline_stage_start((lagopus_pipeline_stage_t *)&t); if (st == LAGOPUS_RESULT_OK) { WHAT_TIME_IS_IT_NOW_IN_NSEC(t_begin); st = global_state_set(GLOBAL_STATE_STARTED); if (st == LAGOPUS_RESULT_OK) { sleep(1); st = lagopus_pipeline_stage_shutdown((lagopus_pipeline_stage_t *)&t, SHUTDOWN_GRACEFULLY); if (st == LAGOPUS_RESULT_OK) { st = lagopus_pipeline_stage_wait((lagopus_pipeline_stage_t *)&t, -1LL); } } } } } WHAT_TIME_IS_IT_NOW_IN_NSEC(t_end); t_total = t_end - t_begin; if (st != LAGOPUS_RESULT_OK) { lagopus_perror(st); } fprintf(stdout, "total %f sec.\n\n" "single thd throughput: %f Mops/s\n" "total throuput: %f Mops/s\n\n" "%f nsec/op\n", (double)t_total / 1000.0 / 1000.0 / 1000.0, (double)c_max / (double)t_total * 1000.0, (double)c_max * (double)nthd / (double)t_total * 1000.0, (double)t_total / (double)c_max); return (st == LAGOPUS_RESULT_OK) ? 0 : 1; }