/* * Set up the given timer. The value in pt->pt_time.it_value is taken * to be an absolute time for CLOCK_REALTIME/CLOCK_MONOTONIC timers and * a relative time for CLOCK_VIRTUAL/CLOCK_PROF timers. */ void timer_settime(struct ptimer *pt) { struct ptimer *ptn, *pptn; struct ptlist *ptl; KASSERT(mutex_owned(&timer_lock)); if (!CLOCK_VIRTUAL_P(pt->pt_type)) { callout_halt(&pt->pt_ch, &timer_lock); if (timespecisset(&pt->pt_time.it_value)) { /* * Don't need to check tshzto() return value, here. * callout_reset() does it for us. */ callout_reset(&pt->pt_ch, pt->pt_type == CLOCK_MONOTONIC ? tshztoup(&pt->pt_time.it_value) : tshzto(&pt->pt_time.it_value), realtimerexpire, pt); } } else { if (pt->pt_active) { ptn = LIST_NEXT(pt, pt_list); LIST_REMOVE(pt, pt_list); for ( ; ptn; ptn = LIST_NEXT(ptn, pt_list)) timespecadd(&pt->pt_time.it_value, &ptn->pt_time.it_value, &ptn->pt_time.it_value); } if (timespecisset(&pt->pt_time.it_value)) { if (pt->pt_type == CLOCK_VIRTUAL) ptl = &pt->pt_proc->p_timers->pts_virtual; else ptl = &pt->pt_proc->p_timers->pts_prof; for (ptn = LIST_FIRST(ptl), pptn = NULL; ptn && timespeccmp(&pt->pt_time.it_value, &ptn->pt_time.it_value, >); pptn = ptn, ptn = LIST_NEXT(ptn, pt_list)) timespecsub(&pt->pt_time.it_value, &ptn->pt_time.it_value, &pt->pt_time.it_value); if (pptn) LIST_INSERT_AFTER(pptn, pt, pt_list); else LIST_INSERT_HEAD(ptl, pt, pt_list); for ( ; ptn ; ptn = LIST_NEXT(ptn, pt_list)) timespecsub(&ptn->pt_time.it_value, &pt->pt_time.it_value, &ptn->pt_time.it_value); pt->pt_active = 1; } else pt->pt_active = 0; }
int nanosleep1(struct lwp *l, clockid_t clock_id, int flags, struct timespec *rqt, struct timespec *rmt) { struct timespec rmtstart; int error, timo; if ((error = ts2timo(clock_id, flags, rqt, &timo, &rmtstart)) != 0) { if (error == ETIMEDOUT) { error = 0; if (rmt != NULL) rmt->tv_sec = rmt->tv_nsec = 0; } return error; } /* * Avoid inadvertently sleeping forever */ if (timo == 0) timo = 1; again: error = kpause("nanoslp", true, timo, NULL); if (rmt != NULL || error == 0) { struct timespec rmtend; struct timespec t0; struct timespec *t; (void)clock_gettime1(clock_id, &rmtend); t = (rmt != NULL) ? rmt : &t0; if (flags & TIMER_ABSTIME) { timespecsub(rqt, &rmtend, t); } else { timespecsub(&rmtend, &rmtstart, t); timespecsub(rqt, t, t); } if (t->tv_sec < 0) timespecclear(t); if (error == 0) { timo = tstohz(t); if (timo > 0) goto again; } } if (error == ERESTART) error = EINTR; if (error == EWOULDBLOCK) error = 0; return error; }
/* * Like cv_timedwait_sig(), but takes an absolute hires future time * rather than a future time in clock ticks. Will not return showing * that a timeout occurred until the future time is passed. * If 'when' is a NULL pointer, no timeout will occur. * Returns: * Function result in order of presidence: * 0 if a signal was received * -1 if timeout occured * >0 if awakened via cv_signal() or cv_broadcast() * or by a spurious wakeup. * (might return time remaining) * As a special test, if someone abruptly resets the system time * (but not through adjtime(2); drifting of the clock is allowed and * expected [see timespectohz_adj()]), then we force a return of -1 * so the caller can return a premature timeout to the calling process * so it can reevaluate the situation in light of the new system time. * (The system clock has been reset if timecheck != timechanged.) */ int cv_waituntil_sig(kcondvar_t *cvp, kmutex_t *mp, timestruc_t *when, int timecheck) { timestruc_t now; timestruc_t delta; int rval; if (when == NULL) return (cv_wait_sig_swap(cvp, mp)); gethrestime(&now); delta = *when; timespecsub(&delta, &now); if (delta.tv_sec < 0 || (delta.tv_sec == 0 && delta.tv_nsec == 0)) { /* * We have already reached the absolute future time. * Call cv_timedwait_sig() just to check for signals. * We will return immediately with either 0 or -1. */ rval = cv_timedwait_sig(cvp, mp, lbolt); } else { if (timecheck == timechanged) { rval = cv_timedwait_sig(cvp, mp, lbolt + timespectohz_adj(when, now)); } else { /* * Someone reset the system time; * just force an immediate timeout. */ rval = -1; } if (rval == -1 && timecheck == timechanged) { /* * Even though cv_timedwait_sig() returned showing a * timeout, the future time may not have passed yet. * If not, change rval to indicate a normal wakeup. */ gethrestime(&now); delta = *when; timespecsub(&delta, &now); if (delta.tv_sec > 0 || (delta.tv_sec == 0 && delta.tv_nsec > 0)) rval = 1; } } return (rval); }
/* This function is used by clock_settime and settimeofday */ static int settime1(struct proc *p, const struct timespec *ts, bool check_kauth) { struct timespec delta, now; int s; /* WHAT DO WE DO ABOUT PENDING REAL-TIME TIMEOUTS??? */ s = splclock(); nanotime(&now); timespecsub(ts, &now, &delta); if (check_kauth && kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_TIME, KAUTH_REQ_SYSTEM_TIME_SYSTEM, __UNCONST(ts), &delta, KAUTH_ARG(check_kauth ? false : true)) != 0) { splx(s); return (EPERM); } #ifdef notyet if ((delta.tv_sec < 86400) && securelevel > 0) { /* XXX elad - notyet */ splx(s); return (EPERM); } #endif tc_setclock(ts); timespecadd(&boottime, &delta, &boottime); resettodr(); splx(s); return (0); }
static int runone(struct timespec *t) { size_t i; struct pipe *p; struct timespec ts, te; int result; writes = nwrites; fired = good = 0; for (i = 0, p = pipes; i < nactive; i++, p++) { if (write(p->fd[1], "e", 1) != 1) err(EXIT_FAILURE, "send"); writes--; fired++; } if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) err(EXIT_FAILURE, "clock_gettime"); result = eloop_start(e, NULL); if (clock_gettime(CLOCK_MONOTONIC, &te) == -1) err(EXIT_FAILURE, "clock_gettime"); timespecsub(&te, &ts, t); return result; }
static int timedwait(semid_t id, u_int msec, u_int *delta, int error) { struct timespec start, end; if (clock_gettime(CLOCK_REALTIME, &start) < 0) { fail_errno("clock_gettime(CLOCK_REALTIME)"); return (-1); } end.tv_sec = msec / 1000; end.tv_nsec = msec % 1000 * 1000000; timespecadd(&end, &start); if (ksem_timedwait(id, &end) < 0) { if (errno != error) { fail_errno("ksem_timedwait"); return (-1); } } else if (error != 0) { fail_err("ksem_timedwait() didn't fail"); return (-1); } if (clock_gettime(CLOCK_REALTIME, &end) < 0) { fail_errno("clock_gettime(CLOCK_REALTIME)"); return (-1); } timespecsub(&end, &start); *delta = end.tv_nsec / 1000000; *delta += end.tv_sec * 1000; return (0); }
int lwp_timer_copyout(lwp_timer_t *lwptp, int error) { timespec_t rmtime; timespec_t now; if (lwptp->lwpt_tsp == NULL) /* nothing to do */ return (error); rmtime.tv_sec = rmtime.tv_nsec = 0; if (error != ETIME) { gethrestime(&now); if ((now.tv_sec < lwptp->lwpt_rqtime.tv_sec) || ((now.tv_sec == lwptp->lwpt_rqtime.tv_sec) && (now.tv_nsec < lwptp->lwpt_rqtime.tv_nsec))) { rmtime = lwptp->lwpt_rqtime; timespecsub(&rmtime, &now); } } if (curproc->p_model == DATAMODEL_NATIVE) { if (copyout(&rmtime, lwptp->lwpt_tsp, sizeof (timespec_t))) error = EFAULT; } else { timespec32_t rmtime32; TIMESPEC_TO_TIMESPEC32(&rmtime32, &rmtime); if (copyout(&rmtime32, lwptp->lwpt_tsp, sizeof (timespec32_t))) error = EFAULT; } return (error); }
static int cpufreq_latency(void) { struct cpufreq *cf = cf_backend; struct timespec nta, ntb; const uint32_t n = 10; uint32_t i, j, l, m; uint64_t s; l = cpufreq_get_min(); m = cpufreq_get_max(); /* * For each state, sample the average transition * latency required to set the state for all CPUs. */ for (i = 0; i < cf->cf_state_count; i++) { for (s = 0, j = 0; j < n; j++) { /* * Attempt to exclude possible * caching done by the backend. */ if (i == 0) cpufreq_set_all_raw(l); else { cpufreq_set_all_raw(m); } nanotime(&nta); cpufreq_set_all_raw(cf->cf_state[i].cfs_freq); nanotime(&ntb); timespecsub(&ntb, &nta, &ntb); if (ntb.tv_sec != 0 || ntb.tv_nsec > CPUFREQ_LATENCY_MAX) continue; if (s >= UINT64_MAX - CPUFREQ_LATENCY_MAX) break; /* Convert to microseconds to prevent overflow */ s += ntb.tv_nsec / 1000; } /* * Consider the backend unsuitable if * the transition latency was too high. */ if (s == 0) return EMSGSIZE; cf->cf_state[i].cfs_latency = s / n; } return 0; }
static void cyclic_test_002_func(void *arg) { struct timespec ts; nanotime(&ts); timespecsub(&ts,&test_002_start); printf("%s: called after %lu.%09lu on curcpu %d\n",__func__,(u_long) ts.tv_sec,(u_long) ts.tv_nsec, curcpu); }
static void cyclic_test_003_func(void *arg) { struct timespec ts; nanotime(&ts); timespecsub(&ts,&test_003_start); printf("%s: called after %lu.%09lu on curcpu %d id %ju\n",__func__,(u_long) ts.tv_sec,(u_long) ts.tv_nsec, curcpu, (uintmax_t)(uintptr_t) arg); }
int lwp_park(struct timespec *ts, const void *hint) { struct timespec tsx; sleepq_t *sq; kmutex_t *mp; wchan_t wchan; int timo, error; lwp_t *l; /* Fix up the given timeout value. */ if (ts != NULL) { getnanotime(&tsx); timespecsub(ts, &tsx, &tsx); if (tsx.tv_sec < 0 || (tsx.tv_sec == 0 && tsx.tv_nsec <= 0)) return ETIMEDOUT; if ((error = itimespecfix(&tsx)) != 0) return error; timo = tstohz(&tsx); KASSERT(timo != 0); } else timo = 0; /* Find and lock the sleep queue. */ l = curlwp; wchan = lwp_park_wchan(l->l_proc, hint); sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp); /* * Before going the full route and blocking, check to see if an * unpark op is pending. */ lwp_lock(l); if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) { l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED); lwp_unlock(l); mutex_spin_exit(mp); return EALREADY; } lwp_unlock_to(l, mp); l->l_biglocks = 0; sleepq_enqueue(sq, wchan, "parked", &lwp_park_sobj); error = sleepq_block(timo, true); switch (error) { case EWOULDBLOCK: error = ETIMEDOUT; break; case ERESTART: error = EINTR; break; default: /* nothing */ break; } return error; }
static void * run(void *param) { struct timespec ts, to, te; clockid_t clck; pthread_condattr_t attr; pthread_cond_t cond; pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; int ret = 0; clck = *(clockid_t *)param; pthread_condattr_init(&attr); pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */ pthread_cond_init(&cond, &attr); ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0); ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0); to = ts; if (debug) printf("started: %lld.%09ld sec\n", (long long)to.tv_sec, to.tv_nsec); ts.tv_sec += WAITTIME; /* Timeout wait */ switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) { case ETIMEDOUT: /* Timeout */ ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); timespecsub(&te, &to, &to); if (debug) { printf("timeout: %lld.%09ld sec\n", (long long)te.tv_sec, te.tv_nsec); printf("elapsed: %lld.%09ld sec\n", (long long)to.tv_sec, to.tv_nsec); } if (isQEMU()) { double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec; ATF_REQUIRE(to_seconds >= WAITTIME * 0.9); /* Loose upper limit because of qemu timing bugs */ ATF_REQUIRE(to_seconds < WAITTIME * 2.5); } else { ATF_REQUIRE_EQ(to.tv_sec, WAITTIME); } break; default: ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret)); } ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)), "pthread_mutex_unlock: %s", strerror(ret)); pthread_exit(&ret); }
static inline void profile_leave(void) { struct timespec t1, dt; if ((profiling & PROFILE_QUEUE) == 0) return; clock_gettime(CLOCK_MONOTONIC, &t1); timespecsub(&t1, &profile.t0, &dt); log_debug("profile-queue: %s %lld.%09ld", profile.name, (long long)dt.tv_sec, dt.tv_nsec); }
static int has_battery(void) { struct timespec s, e; size_t len; int val; clock_gettime(CLOCK_MONOTONIC_FAST, &s); BatLifePrevT = s; len = sizeof(val); if (sysctlbyname("hw.acpi.acline", &val, &len, NULL, 0) < 0) { /* No AC line information */ return 0; } clock_gettime(CLOCK_MONOTONIC_FAST, &e); timespecsub(&e, &s); if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) { /* hw.acpi.acline takes to long to be useful */ syslog(LOG_NOTICE, "hw.acpi.acline takes too long"); return 0; } clock_gettime(CLOCK_MONOTONIC_FAST, &s); len = sizeof(val); if (sysctlbyname("hw.acpi.battery.life", &val, &len, NULL, 0) < 0) { /* No battery life */ return 0; } clock_gettime(CLOCK_MONOTONIC_FAST, &e); timespecsub(&e, &s); if (e.tv_sec > 0 || e.tv_nsec > BAT_SYSCTL_TIME_MAX) { /* hw.acpi.battery.life takes to long to be useful */ syslog(LOG_NOTICE, "hw.acpi.battery.life takes too long"); return 0; } return 1; }
static uint64_t timespec_to_pmtmr(const struct timespec *tsnew, const struct timespec *tsold) { struct timespec tsdiff; int64_t nsecs; tsdiff = *tsnew; timespecsub(&tsdiff, tsold); nsecs = tsdiff.tv_sec * 1000000000 + tsdiff.tv_nsec; assert(nsecs >= 0); return (nsecs * PMTMR_FREQ / 1000000000 + pmtmr_old); }
static struct timespec thread_juggle(int fd1, int fd2, int pipeline) { struct timespec tstart, tfinish; pthread_t thread; int i, j; threaded_pipeline = pipeline; if (pthread_mutex_init(&threaded_mtx, NULL) != 0) err(-1, "thread_juggle: pthread_mutex_init"); if (pthread_create(&thread, NULL, juggling_thread, &fd2) != 0) err(-1, "thread_juggle: pthread_create"); if (pthread_mutex_lock(&threaded_mtx) != 0) err(-1, "thread_juggle: pthread_mutex_lock"); while (!threaded_child_ready) { if (pthread_cond_wait(&threaded_cond, &threaded_mtx) != 0) err(-1, "thread_juggle: pthread_cond_wait"); } if (pthread_mutex_unlock(&threaded_mtx) != 0) err(-1, "thread_juggle: pthread_mutex_unlock"); if (clock_gettime(CLOCK_REALTIME, &tstart) < 0) err(-1, "thread_juggle: clock_gettime"); for (i = 0; i < NUMCYCLES; i++) { for (j = 0; j < pipeline; j++) { if (message_send(fd1) < 0) err(-1, "message_send fd1"); } for (j = 0; j < pipeline; j++) { if (message_recv(fd1) < 0) err(-1, "message_recv fd1"); } } if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0) err(-1, "thread_juggle: clock_gettime"); if (pthread_join(thread, NULL) != 0) err(-1, "thread_juggle: pthread_join"); timespecsub(&tfinish, &tstart); return (tfinish); }
uint64_t yr_stopwatch_elapsed_us( YR_STOPWATCH* stopwatch) { struct timespec ts_stop; struct timespec ts_elapsed; #if defined(HAVE_CLOCK_GETTIME) clock_gettime(CLOCK_MONOTONIC, &ts_stop); timespecsub(&ts_stop, &stopwatch->ts_start, &ts_elapsed); #else #endif return ts_elapsed.tv_sec * 1000000L + ts_elapsed.tv_nsec / 1000; }
static int futex_copyin_timeout(int op, struct l_timespec *luts, int clockrt, struct timespec *ts) { struct l_timespec lts; struct timespec kts; int error; error = copyin(luts, <s, sizeof(lts)); if (error) return (error); error = linux_to_native_timespec(ts, <s); if (error) return (error); if (clockrt) { nanotime(&kts); timespecsub(ts, &kts); } else if (op == LINUX_FUTEX_WAIT_BITSET) { nanouptime(&kts); timespecsub(ts, &kts); } return (error); }
void Chew(struct timespec *tsa, struct timespec *tsc, unsigned sa, unsigned sc) { static int idx; struct timespec ts; printf("%d.%09d ", tsa->tv_sec, tsa->tv_nsec); printf("%d.%09d ", tsc->tv_sec, tsc->tv_nsec); printf("%u %u ", sa, sc); ts = *tsc; timespecsub(&ts,tsa); printf("%.9f ", ts.tv_sec + ts.tv_nsec / 1e9); printf("\n"); fflush(stdout); }
static int jzsmb_transfer_read(device_t dev, struct iic_msg *msg) { struct jzsmb_softc *sc; struct timespec start, diff; uint16_t con, resid; int timeo; sc = device_get_softc(dev); timeo = JZSMB_TIMEOUT * msg->len; SMB_ASSERT_LOCKED(sc); con = SMB_READ(sc, SMBCON); con |= SMBCON_STPHLD; SMB_WRITE(sc, SMBCON, con); getnanouptime(&start); for (resid = msg->len; resid > 0; resid--) { for (int i = 0; i < min(resid, 8); i++) SMB_WRITE(sc, SMBDC, SMBDC_CMD); for (;;) { getnanouptime(&diff); timespecsub(&diff, &start); if ((SMB_READ(sc, SMBST) & SMBST_RFNE) != 0) { msg->buf[msg->len - resid] = SMB_READ(sc, SMBDC) & SMBDC_DAT; break; } else DELAY(1000); if (tstohz(&diff) >= timeo) { device_printf(dev, "read timeout (status=0x%02x)\n", SMB_READ(sc, SMBST)); return (EIO); } } } con = SMB_READ(sc, SMBCON); con &= ~SMBCON_STPHLD; SMB_WRITE(sc, SMBCON, con); return (0); }
static int jzsmb_transfer_write(device_t dev, struct iic_msg *msg, int stop_hold) { struct jzsmb_softc *sc; struct timespec start, diff; uint16_t con, resid; int timeo; sc = device_get_softc(dev); timeo = JZSMB_TIMEOUT * msg->len; SMB_ASSERT_LOCKED(sc); con = SMB_READ(sc, SMBCON); con |= SMBCON_STPHLD; SMB_WRITE(sc, SMBCON, con); getnanouptime(&start); for (resid = msg->len; resid > 0; resid--) { for (;;) { getnanouptime(&diff); timespecsub(&diff, &start); if ((SMB_READ(sc, SMBST) & SMBST_TFNF) != 0) { SMB_WRITE(sc, SMBDC, msg->buf[msg->len - resid]); break; } else DELAY((1000 * hz) / JZSMB_TIMEOUT); if (tstohz(&diff) >= timeo) { device_printf(dev, "write timeout (status=0x%02x)\n", SMB_READ(sc, SMBST)); return (EIO); } } } if (!stop_hold) { con = SMB_READ(sc, SMBCON); con &= ~SMBCON_STPHLD; SMB_WRITE(sc, SMBCON, con); } return (0); }
static __inline int acpi_cmbat_info_expired(struct timespec *lastupdated) { struct timespec curtime; if (lastupdated == NULL) { return (1); } if (!timespecisset(lastupdated)) { return (1); } getnanotime(&curtime); timespecsub(&curtime, lastupdated); return ((curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire())); }
static int acpi_cmbat_info_expired(struct timespec *lastupdated) { struct timespec curtime; ACPI_SERIAL_ASSERT(cmbat); if (lastupdated == NULL) return (TRUE); if (!timespecisset(lastupdated)) return (TRUE); getnanotime(&curtime); timespecsub(&curtime, lastupdated); return (curtime.tv_sec < 0 || curtime.tv_sec > acpi_battery_get_info_expire()); }
ATF_TC_BODY(sigtimedwait_all0timeout, tc) { sigset_t block; struct timespec ts, before, after, len; siginfo_t info; int r; sigemptyset(&block); ts.tv_sec = 0; ts.tv_nsec = 0; clock_gettime(CLOCK_MONOTONIC, &before); r = sigtimedwait(&block, &info, &ts); clock_gettime(CLOCK_MONOTONIC, &after); ATF_REQUIRE(r == -1); ATF_REQUIRE_ERRNO(EAGAIN, errno); timespecsub(&after, &before, &len); ATF_REQUIRE(len.tv_sec < 1); }
static void * reader(void *cookie) { struct timespec end; char buf[1]; long val; for (;;) { (void)pthread_barrier_wait(&barrier); if (read(fds[0], buf, sizeof(buf)) == -1) { err(1, "read"); } clock_gettime(CLOCK_MONOTONIC, &end); timespecsub(&end, &start, &end); val = (long)end.tv_sec * 1000000000 + (long)end.tv_nsec; printf("%ld\n", val); if (val > max) max = val; if (val < min) min = val; } }
static int testwait(semid_t id, u_int *delta) { struct timespec start, end; if (clock_gettime(CLOCK_REALTIME, &start) < 0) { fail_errno("clock_gettime(CLOCK_REALTIME)"); return (-1); } if (ksem_wait(id) < 0) { fail_errno("ksem_wait"); return (-1); } if (clock_gettime(CLOCK_REALTIME, &end) < 0) { fail_errno("clock_gettime(CLOCK_REALTIME)"); return (-1); } timespecsub(&end, &start); *delta = end.tv_nsec / 1000000; *delta += end.tv_sec * 1000; return (0); }
/* * Same as above, but run in a loop that handles the fd conditions result. */ int asr_run_sync(struct asr_query *as, struct asr_result *ar) { struct pollfd fds[1]; struct timespec pollstart, pollend, elapsed; int timeout, r, p, saved_errno = errno; while ((r = asr_run(as, ar)) == ASYNC_COND) { fds[0].fd = ar->ar_fd; fds[0].events = (ar->ar_cond == ASR_WANT_READ) ? POLLIN:POLLOUT; timeout = ar->ar_timeout; again: if (clock_gettime(CLOCK_MONOTONIC, &pollstart)) break; p = poll(fds, 1, timeout); if (p == -1 && errno == EINTR) { if (clock_gettime(CLOCK_MONOTONIC, &pollend)) break; timespecsub(&pollend, &pollstart, &elapsed); timeout -= (elapsed.tv_sec * 1000) + (elapsed.tv_nsec / 1000000); if (timeout < 1) break; goto again; } /* * Otherwise, just ignore the error and let asr_run() * catch the failure. */ } errno = saved_errno; return (r); }
/* * Juggle messages between two file descriptors in a single thread/process, * so simply a measure of IPC performance. */ static struct timespec juggle(int fd1, int fd2, int pipeline) { struct timespec tstart, tfinish; int i, j; if (clock_gettime(CLOCK_REALTIME, &tstart) < 0) err(-1, "juggle: clock_gettime"); for (i = 0; i < NUMCYCLES; i++) { for (j = 0; j < pipeline; j++) { if (message_send(fd1) < 0) err(-1, "message_send fd1"); } for (j = 0; j < pipeline; j++) { if (message_recv(fd2) < 0) err(-1, "message_recv fd2"); if (message_send(fd2) < 0) err(-1, "message_send fd2"); } for (j = 0; j < pipeline; j++) { if (message_recv(fd1) < 0) err(-1, "message_recv fd1"); } } if (clock_gettime(CLOCK_REALTIME, &tfinish) < 0) err(-1, "juggle: clock_gettime"); timespecsub(&tfinish, &tstart); return (tfinish); }
void process_a_child(int sd, struct sockaddr_in *sin, int use_sctp) { int cnt, sz; int *p; char buffer[MAX_SINGLE_MSG]; struct timespec tvs, tve; struct incast_msg_req inrec; int no_clock_s=1, no_clock_e=1, i; socklen_t optlen; int optval; ssize_t readin, sendout, tot_out=0; if (verbose) { if(clock_gettime(CLOCK_MONOTONIC_PRECISE, &tvs)) no_clock_s = 1; else no_clock_s = 0; } optval = 1; optlen = sizeof(optval); if (use_sctp) { if(setsockopt(sd, IPPROTO_SCTP, SCTP_NODELAY, &optval, optlen)) { printf("Warning - can't turn on nodelay for sctp err:%d\n", errno); } } else { if(setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, &optval, optlen)) { printf("Warning - can't turn on nodelay for tcp err:%d\n", errno); } } /* Find out how much we must send */ errno = 0; readin = recv(sd, &inrec, sizeof(inrec), 0); if(readin != sizeof(inrec)) { if (readin) printf("Did not get %ld bytes got:%ld err:%d\n", (long int)sizeof(inrec), (long int)readin, errno); goto out; } cnt = ntohl(inrec.number_of_packets); sz = ntohl(inrec.size); /* How big must the socket buffer be? */ optlen = sizeof(optval); optval = (cnt * sz) + 1; if(setsockopt(sd, SOL_SOCKET, SO_SNDBUF, &optval, optlen) != 0){ printf("Warning - could not grow sock buf to %d will block err:%d\n", optval, errno); } /* protect against bad buffer sizes */ if (sz > MAX_SINGLE_MSG) { cnt = (optval / MAX_SINGLE_MSG) + 1; sz = MAX_SINGLE_MSG; } memset(buffer, 0, sizeof(buffer)); p = (int *)&buffer; *p = 1; for(i=0; i<cnt; i++) { sendout = send(sd, buffer, sz, 0); if (sendout < sz) { printf("Error sending %d sz:%d sendout:%ld\n", errno, sz, (long int)sendout); goto out; } tot_out += sendout; *p = *p + 1; } if (verbose) { if(clock_gettime(CLOCK_MONOTONIC_PRECISE, &tve)) no_clock_e = 1; else no_clock_e = 0; if ((no_clock_e == 0) && (no_clock_s == 0)) { timespecsub(&tve, &tvs); printf("%d rec of %d in %ld.%9.9ld (tot_out:%ld)\n", cnt, sz, (long int)tve.tv_sec, tve.tv_nsec, (long int)tot_out); } } else if (tot_out < (cnt * sz)) { printf("--tot_out was %ld but cnt:%d * sz:%d == %d\n", (long int)tot_out, cnt, sz, (cnt * sz)); } out: if (nap_time) { struct timespec nap; nap.tv_sec = 0; nap.tv_nsec = nap_time; nanosleep(&nap, NULL); } close(sd); }
static int mon_battery(void) { struct timespec cur, ts; int acline, life; size_t len; clock_gettime(CLOCK_MONOTONIC_FAST, &cur); ts = cur; timespecsub(&ts, &BatLifePrevT); if (ts.tv_sec < BatLifePollIntvl) return 1; BatLifePrevT = cur; len = sizeof(acline); if (sysctlbyname("hw.acpi.acline", &acline, &len, NULL, 0) < 0) return 1; if (acline) { BatShutdownLinger = -1; BatShutdownLingerCnt = 0; restore_backlight(); return 1; } if (!BackLightDown && BackLightPct != 100) { int backlight_max, backlight; len = sizeof(backlight_max); if (sysctlbyname("hw.backlight_max", &backlight_max, &len, NULL, 0) < 0) { /* No more backlight adjustment */ BackLightPct = 100; goto after_backlight; } len = sizeof(OldBackLightLevel); if (sysctlbyname("hw.backlight_level", &OldBackLightLevel, &len, NULL, 0) < 0) { /* No more backlight adjustment */ BackLightPct = 100; goto after_backlight; } backlight = (backlight_max * BackLightPct) / 100; if (backlight >= OldBackLightLevel) { /* No more backlight adjustment */ BackLightPct = 100; goto after_backlight; } if (sysctlbyname("hw.backlight_level", NULL, NULL, &backlight, sizeof(backlight)) < 0) { /* No more backlight adjustment */ BackLightPct = 100; goto after_backlight; } BackLightDown = 1; } after_backlight: len = sizeof(life); if (sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0) < 0) return 1; if (BatShutdownLinger > 0) { ts = cur; timespecsub(&ts, &BatShutdownStartT); if (ts.tv_sec > BatShutdownLinger) BatShutdownLinger = 0; } if (life <= BatLifeMin) { if (BatShutdownLinger == 0 || BatShutdownLingerSet == 0) { syslog(LOG_ALERT, "low battery life %d%%, " "shutting down", life); if (vfork() == 0) execlp("poweroff", "poweroff", NULL); return 0; } else if (BatShutdownLinger < 0) { BatShutdownLinger = BatShutdownLingerSet; BatShutdownStartT = cur; } low_battery_alert(life); } return 1; }