static int lw_ethtool_load_options(struct teamd_context *ctx, struct teamd_port *tdport, struct lw_ethtool_port_priv *ethtool_ppriv) { struct teamd_config_path_cookie *cpcookie = ethtool_ppriv->common.cpcookie; int err; int tmp; err = teamd_config_int_get(ctx, &tmp, "@.queue_id", cpcookie); if (!err) { if (tmp < 0) { teamd_log_err("\"delay_up\" must not be negative number."); return -EINVAL; } teamd_log_dbg("delay_up \"%d\".", tmp); ms_to_timespec(ðtool_ppriv->delay_up, tmp); } err = teamd_config_int_get(ctx, &tmp, "@.delay_down", cpcookie); if (!err) { if (tmp < 0) { teamd_log_err("\"delay_down\" must not be negative number."); return -EINVAL; } teamd_log_dbg("delay_down \"%d\".", tmp); ms_to_timespec(ðtool_ppriv->delay_down, tmp); } return 0; }
int main(int argc, char* argv[]) { int i; unsigned long prio; struct timespec period, runtime, timeout; struct timeval tv1, tv2, tv3; if (argc != 3) { printf("Error: invalid option\n"); } prio = atoi(argv[1]); /* priority. */ period = ms_to_timespec(atoi(argv[2])); /* period. */ runtime = ms_to_timespec(1000); /* execution time. */ timeout = ms_to_timespec(1000); /* timeout. */ /* bannar. */ printf("sample program\n"); rt_init(); rt_set_period(period); rt_set_runtime(runtime); rt_set_scheduler(SCHED_FP); /* you can also set SCHED_EDF. */ rt_set_priority(prio); rt_reserve_start(runtime, NULL); /* QoS is guaranteed. */ rt_run(timeout); for (i = 0; i < 20; i++) { gettimeofday(&tv1, NULL); printf("start %lu:%06lu\n", tv1.tv_sec, tv1.tv_usec); fflush(stdout); do { gettimeofday(&tv2, NULL); /* tv2 - tv1 = tv3 */ tvsub(&tv2, &tv1, &tv3); } while (tv3.tv_sec < 2); printf("finish %lu:%06lu\n", tv2.tv_sec, tv2.tv_usec); fflush(stdout); if (!rt_wait_period()) { printf("deadline is missed!\n"); } } rt_reserve_stop(); rt_exit(); return 0; }
void TimerFD::arm(int timeout) { if (timeout >= 0) m_timeout = timeout; itimerspec its {{0,0},{0,0}}; ms_to_timespec(m_timeout, &its.it_value); if (! m_one_shot) { its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; } ::timerfd_settime(fd, 0, &its, nullptr); }
static int lw_psr_load_options(struct teamd_context *ctx, struct teamd_port *tdport, struct lw_psr_port_priv *psr_ppriv) { struct teamd_config_path_cookie *cpcookie = psr_ppriv->common.cpcookie; int err; int tmp; err = teamd_config_int_get(ctx, &tmp, "@.interval", cpcookie); if (err) { teamd_log_err("Failed to get \"interval\" link-watch option."); return -EINVAL; } teamd_log_dbg("interval \"%d\".", tmp); ms_to_timespec(&psr_ppriv->interval, tmp); err = teamd_config_int_get(ctx, &tmp, "@.init_wait", cpcookie); if (!err) ms_to_timespec(&psr_ppriv->init_wait, tmp); /* if init_wait is set to 0, use default_init_wait */ if (err || !tmp) psr_ppriv->init_wait = lw_psr_default_init_wait; teamd_log_dbg("init_wait \"%d\".", timespec_to_ms(&psr_ppriv->init_wait)); err = teamd_config_int_get(ctx, &tmp, "@.missed_max", cpcookie); if (err) { teamd_log_err("Failed to get \"missed_max\" link-watch option."); return -EINVAL; } if (tmp < 0) { teamd_log_err("\"missed_max\" must not be negative number."); return -EINVAL; } teamd_log_dbg("missed_max \"%d\".", tmp); psr_ppriv->missed_max = tmp; return 0; }
static u32_t cond_wait(XaxMachinery *xm, ZCond *zcond, ZMutex *zmutex, u32_t timeout_ms, bool verbose) { int result; ZCondWaitRecord zcwr; zcondwaitrecord_init_auto(&zcwr, xm->zdt, zcond, zmutex); ZAS *zasses[2]; zasses[0] = &zcwr.zas; int zascount = 1; LegacyZTimer *ztimer; bool using_ztimer = false; struct timespec start_time; if (timeout_ms > 0) { (xm->zclock->methods->read)(xm->zclock, &start_time); struct timespec timeout_offset; ms_to_timespec(&timeout_offset, timeout_ms); struct timespec timeout_absolute; timespec_add(&timeout_absolute, &start_time, &timeout_offset); ztimer = (xm->zclock->methods->new_timer)(xm->zclock, &timeout_absolute); zasses[1] = (xm->zclock->methods->get_zas)(ztimer); zascount += 1; using_ztimer = true; } if (verbose) { fprintf(stderr, "xax_arch cond_wait zcond %08x START\n", (uint32_t) zcond); } int idx = zas_wait_any(xm->zdt, zasses, zascount); if (verbose && idx==0) { fprintf(stderr, "xax_arch cond_wait zcond %08x SUCCEED\n", (uint32_t) zcond); } if (idx==1) { result = SYS_ARCH_TIMEOUT; } else if (timeout_ms > 0) { /* Calculate for how long we waited for the cond. */ struct timespec end_time; (xm->zclock->methods->read)(xm->zclock, &end_time); struct timespec delay; timespec_subtract(&delay, &end_time, &start_time); result = timespec_to_ms(&delay); } else { result = SYS_ARCH_TIMEOUT; } if (using_ztimer) { (xm->zclock->methods->free_timer)(ztimer); } zcondwaitrecord_free(xm->zdt, &zcwr); return result; }
int main(int argc, char* argv[]) { int i, j, task_num; struct timeval tv1, tv2, tv3; int dmiss_count = 0; pthread_t th[100]; if (argc != 5) { printf("Error: invalid option\n"); printf("usage: rt_task period runtime deadline task_num\n"); exit(EXIT_FAILURE); } #ifdef USE_SCHED_CPU prio = 0; /* priority. */ //printf("---- period :%d ms ----\n", atoi(argv[1])); period = ms_to_timespec(atoi(argv[1])); /* period. */ //printf("---- runtime :%d ms ----\n", atoi(argv[2])); runtime = ms_to_timespec(atoi(argv[2])); /* execution time. */ //printf("---- deadline:%d ms ----\n", atoi(argv[3])); deadline = ms_to_timespec( atoi(argv[3]) ); /* execution time. */ //printf("---- timeout:%d ms ----\n", 30000); timeout = ms_to_timespec(3000); /* timeout. */ #endif prio = getpid(); struct timespec ts1,ts2,ts3; task_num = atoi(argv[4]); int cpu; cpu_set_t set; cpu = sched_getcpu(); CPU_ZERO(&set); CPU_SET(cpu,&set); gettime(&ts1); #if 0 for(j=0; j<task_num; j++) pthread_create(&th[j], NULL, thread, (void *)NULL); pthread_setaffinity_np(pthread_self() ,sizeof(cpu_set_t), &set); for(j=0; j<task_num; j++) pthread_join(th[j],NULL); #else /* generate each job by using fork */ pid_t pid; for(j=0; j<task_num; j++){ pid= fork(); prio = DEFAULT_PRIO; if( -1 == pid) { fprintf(stderr,"can not fork\n"); break; }else if( 0 == pid){ fprintf(stderr,"fork! pid=%d\n",getpid()); thread(NULL); exit(EXIT_SUCCESS); } } pthread_setaffinity_np(pthread_self() ,sizeof(cpu_set_t), &set); int status = 0; while(1){ pid = wait(&status); if(pid == -1){ if(ECHILD == errno){ break; } else if (EINTR == errno) { fprintf(stderr,"synchronize\n"); continue; } err (EXIT_FAILURE, "wait error"); } fprintf (stderr,"parenet: child = %d, status=%d\n", pid, status); } #endif gettime(&ts2); fprintf(stderr,"total_time = %llu\n",timespec_to_ns_sub(&ts1,&ts2)); fprintf(stdout,"%llu\n",timespec_to_ns_sub(&ts1,&ts2)); return 0; }