/** * Test main function */ int main(int argc, char *argv[]) { odph_linux_pthread_t thread_tbl[MAX_WORKERS]; int num_workers; odp_queue_t queue; uint64_t tick, ns; odp_queue_param_t param; odp_pool_param_t params; odp_timer_pool_param_t tparams; odp_timer_pool_info_t tpinfo; odp_cpumask_t cpumask; char cpumaskstr[ODP_CPUMASK_STR_SIZE]; odp_shm_t shm = ODP_SHM_INVALID; test_globals_t *gbls = NULL; int err = 0; printf("\nODP timer example starts\n"); if (odp_init_global(NULL, NULL)) { err = 1; printf("ODP global init failed.\n"); goto err_global; } /* Init this thread. */ if (odp_init_local(ODP_THREAD_CONTROL)) { err = 1; printf("ODP local init failed.\n"); goto err_local; } printf("\n"); printf("ODP system info\n"); printf("---------------\n"); printf("ODP API version: %s\n", odp_version_api_str()); printf("CPU model: %s\n", odp_cpu_model_str()); printf("CPU freq (hz): %"PRIu64"\n", odp_cpu_hz_max()); printf("Cache line size: %i\n", odp_sys_cache_line_size()); printf("Max CPU count: %i\n", odp_cpu_count()); printf("\n"); /* Reserve memory for test_globals_t from shared mem */ shm = odp_shm_reserve("shm_test_globals", sizeof(test_globals_t), ODP_CACHE_LINE_SIZE, 0); if (ODP_SHM_INVALID == shm) { err = 1; EXAMPLE_ERR("Error: shared mem reserve failed.\n"); goto err; } gbls = odp_shm_addr(shm); if (NULL == gbls) { err = 1; EXAMPLE_ERR("Error: shared mem alloc failed.\n"); goto err; } memset(gbls, 0, sizeof(test_globals_t)); gbls->pool = ODP_POOL_INVALID; gbls->tp = ODP_TIMER_POOL_INVALID; parse_args(argc, argv, &gbls->args); memset(thread_tbl, 0, sizeof(thread_tbl)); /* Default to system CPU count unless user specified */ num_workers = MAX_WORKERS; if (gbls->args.cpu_count) num_workers = gbls->args.cpu_count; /* Get default worker cpumask */ num_workers = odp_cpumask_default_worker(&cpumask, num_workers); (void)odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); printf("cpu mask: %s\n", cpumaskstr); printf("resolution: %i usec\n", gbls->args.resolution_us); printf("min timeout: %i usec\n", gbls->args.min_us); printf("max timeout: %i usec\n", gbls->args.max_us); printf("period: %i usec\n", gbls->args.period_us); printf("timeouts: %i\n", gbls->args.tmo_count); /* * Create pool for timeouts */ odp_pool_param_init(¶ms); params.tmo.num = NUM_TMOS; params.type = ODP_POOL_TIMEOUT; gbls->pool = odp_pool_create("msg_pool", ¶ms); if (gbls->pool == ODP_POOL_INVALID) { err = 1; EXAMPLE_ERR("Pool create failed.\n"); goto err; } tparams.res_ns = gbls->args.resolution_us * ODP_TIME_USEC_IN_NS; tparams.min_tmo = gbls->args.min_us * ODP_TIME_USEC_IN_NS; tparams.max_tmo = gbls->args.max_us * ODP_TIME_USEC_IN_NS; tparams.num_timers = num_workers; /* One timer per worker */ tparams.priv = 0; /* Shared */ tparams.clk_src = ODP_CLOCK_CPU; gbls->tp = odp_timer_pool_create("timer_pool", &tparams); if (gbls->tp == ODP_TIMER_POOL_INVALID) { err = 1; EXAMPLE_ERR("Timer pool create failed.\n"); goto err; } odp_timer_pool_start(); odp_shm_print_all(); (void)odp_timer_pool_info(gbls->tp, &tpinfo); printf("Timer pool\n"); printf("----------\n"); printf(" name: %s\n", tpinfo.name); printf(" resolution: %"PRIu64" ns\n", tpinfo.param.res_ns); printf(" min tmo: %"PRIu64" ticks\n", tpinfo.param.min_tmo); printf(" max tmo: %"PRIu64" ticks\n", tpinfo.param.max_tmo); printf("\n"); /* * Create a queue for timer test */ odp_queue_param_init(¶m); param.type = ODP_QUEUE_TYPE_SCHED; param.sched.prio = ODP_SCHED_PRIO_DEFAULT; param.sched.sync = ODP_SCHED_SYNC_PARALLEL; param.sched.group = ODP_SCHED_GROUP_ALL; queue = odp_queue_create("timer_queue", ¶m); if (queue == ODP_QUEUE_INVALID) { err = 1; EXAMPLE_ERR("Timer queue create failed.\n"); goto err; } printf("CPU freq %"PRIu64" Hz\n", odp_cpu_hz_max()); printf("Timer ticks vs nanoseconds:\n"); ns = 0; tick = odp_timer_ns_to_tick(gbls->tp, ns); printf(" %12" PRIu64 " ns -> %12" PRIu64 " ticks\n", ns, tick); printf(" %12" PRIu64 " ticks -> %12" PRIu64 " ns\n", tick, odp_timer_tick_to_ns(gbls->tp, tick)); for (ns = 1; ns <= 100 * ODP_TIME_SEC_IN_NS; ns *= 10) { tick = odp_timer_ns_to_tick(gbls->tp, ns); printf(" %12" PRIu64 " ns -> %12" PRIu64 " ticks\n", ns, tick); printf(" %12" PRIu64 " ticks -> %12" PRIu64 " ns\n", tick, odp_timer_tick_to_ns(gbls->tp, tick)); } printf("\n"); gbls->num_workers = num_workers; /* Initialize number of timeouts to receive */ odp_atomic_init_u32(&gbls->remain, gbls->args.tmo_count * num_workers); /* Barrier to sync test case execution */ odp_barrier_init(&gbls->test_barrier, num_workers); /* Create and launch worker threads */ odph_linux_pthread_create(thread_tbl, &cpumask, run_thread, gbls, ODP_THREAD_WORKER); /* Wait for worker threads to exit */ odph_linux_pthread_join(thread_tbl, num_workers); /* free resources */ if (odp_queue_destroy(queue)) err = 1; err: if (gbls != NULL && gbls->tp != ODP_TIMER_POOL_INVALID) odp_timer_pool_destroy(gbls->tp); if (gbls != NULL && gbls->pool != ODP_TIMER_POOL_INVALID) if (odp_pool_destroy(gbls->pool)) err = 1; if (shm != ODP_SHM_INVALID) if (odp_shm_free(shm)) err = 1; if (odp_term_local()) err = 1; err_local: if (odp_term_global()) err = 1; err_global: if (err) { printf("Err: ODP timer test failed\n\n"); return -1; } printf("ODP timer test complete\n\n"); return 0; }
static odp_cos_t build_cos_w_queue(const char *name) { odp_cos_t cos; odp_queue_t queue_cos; odp_queue_param_t qparam; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } memset(&qparam, 0, sizeof(odp_queue_param_t)); qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; qparam.sched.sync = ODP_SCHED_SYNC_ATOMIC; qparam.sched.group = ODP_SCHED_GROUP_ALL; queue_cos = odp_queue_create(name, ODP_QUEUE_TYPE_SCHED, &qparam); if (queue_cos == ODP_QUEUE_INVALID) { OFP_ERR("Failed to create queue\n"); odp_cos_destroy(cos); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); odp_queue_destroy(queue_cos); return ODP_COS_INVALID; } return cos; } static odp_cos_t build_cos_set_queue(const char *name, odp_queue_t queue_cos) { odp_cos_t cos; cos = odp_cos_create(name); if (cos == ODP_COS_INVALID) { OFP_ERR("Failed to create COS"); return ODP_COS_INVALID; } #if ODP_VERSION < 104 if (odp_cos_set_queue(cos, queue_cos) < 0) { #else if (odp_cos_queue_set(cos, queue_cos) < 0) { #endif OFP_ERR("Failed to set queue on COS"); odp_cos_destroy(cos); return ODP_COS_INVALID; } return cos; } static odp_pmr_t build_udp_prm(void) { uint32_t pmr_udp_val = TEST_PORT; uint32_t pmr_udp_mask = 0xffffffff; #if ODP_VERSION < 104 return odp_pmr_create(ODP_PMR_UDP_DPORT, &pmr_udp_val, &pmr_udp_mask, 1); #else const odp_pmr_match_t match = { .term = ODP_PMR_UDP_DPORT, .val = &pmr_udp_val, .mask = &pmr_udp_mask, .val_sz = 1 }; return odp_pmr_create(&match); #endif } static void app_processing(void) { int fd_rcv = -1; char buf[1500]; int len = sizeof(buf); do { struct ofp_sockaddr_in addr = {0}; fd_rcv = ofp_socket(OFP_AF_INET, OFP_SOCK_DGRAM, OFP_IPPROTO_UDP); if (fd_rcv == -1) { OFP_ERR("Faild to create RCV socket (errno = %d)\n", ofp_errno); break; } addr.sin_len = sizeof(struct ofp_sockaddr_in); addr.sin_family = OFP_AF_INET; addr.sin_port = odp_cpu_to_be_16(TEST_PORT); addr.sin_addr.s_addr = IP4(192, 168, 100, 1); if (ofp_bind(fd_rcv, (const struct ofp_sockaddr *)&addr, sizeof(struct ofp_sockaddr_in)) == -1) { OFP_ERR("Faild to bind socket (errno = %d)\n", ofp_errno); break; } len = ofp_recv(fd_rcv, buf, len, 0); if (len == -1) OFP_ERR("Faild to receive data (errno = %d)\n", ofp_errno); else OFP_INFO("Data received: length = %d.\n", len); } while (0); if (fd_rcv != -1) { ofp_close(fd_rcv); fd_rcv = -1; } OFP_INFO("Test ended.\n"); }
int odp_schedule_init_global(void) { odp_shm_t shm; odp_pool_t pool; int i, j; odp_pool_param_t params; shm = odp_shm_reserve("odp_scheduler", sizeof(sched_t), ODP_CACHE_LINE_SIZE, 0); sched = odp_shm_addr(shm); if (sched == NULL) { ODP_ERR("Schedule init: Shm reserve failed.\n"); return -1; } memset(sched, 0, sizeof(sched_t)); params.buf.size = sizeof(sched_cmd_t); params.buf.align = 0; params.buf.num = NUM_SCHED_CMD; params.type = ODP_POOL_BUFFER; pool = odp_pool_create("odp_sched_pool", ¶ms); if (pool == ODP_POOL_INVALID) { ODP_ERR("Schedule init: Pool create failed.\n"); return -1; } sched->pool = pool; sched->shm = shm; odp_spinlock_init(&sched->mask_lock); for (i = 0; i < ODP_CONFIG_SCHED_PRIOS; i++) { odp_queue_t queue; char name[] = "odp_priXX_YY"; name[7] = '0' + i / 10; name[8] = '0' + i - 10*(i / 10); for (j = 0; j < QUEUES_PER_PRIO; j++) { name[10] = '0' + j / 10; name[11] = '0' + j - 10*(j / 10); queue = odp_queue_create(name, ODP_QUEUE_TYPE_POLL, NULL); if (queue == ODP_QUEUE_INVALID) { ODP_ERR("Sched init: Queue create failed.\n"); return -1; } sched->pri_queue[i][j] = queue; sched->pri_mask[i] = 0; } } return 0; }