void PrecisionRecallM::addStopTime() { struct timeval end, elapsed; gettimeofday(&end, NULL); TIMEVAL_SUB(&end, &begin, &elapsed); t += elapsed.tv_sec * 1000000 + elapsed.tv_usec; }
struct timeval * rtadvd_timer_rest(struct rtadvd_timer *rat) { static struct timeval returnval, now; gettimeofday(&now, NULL); if (TIMEVAL_LEQ(&rat->rat_tm, &now)) { syslog(LOG_DEBUG, "<%s> a timer must be expired, but not yet", __func__); returnval.tv_sec = returnval.tv_usec = 0; } else TIMEVAL_SUB(&rat->rat_tm, &now, &returnval); return (&returnval); }
int main(int argc, char *argv[]) { int fd; struct sockaddr_in locAddr; struct sockaddr_in remAddr; socklen_t remAddrLen; ssize_t nBytes; static char buf[PRIORITY_MESSAGE_SIZE_BYTES]; int msgCnt; parse_options(argc, argv); if (opt_help) usage(); if (opt_port == 0) opt_port = PRIORITY_PORT_DEFAULT; if (opt_prio != 0) { struct sched_param param; memset(¶m, 0, sizeof(param)); param.sched_priority = opt_prio; /* SCHED_RR introduces lots of jitter */ if (sched_setscheduler(0, SCHED_FIFO, ¶m) != 0) { perror("sched_setscheduler"); exit(1); } } fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd == -1) { perror("socket"); exit(1); } memset(&locAddr, 0, sizeof(locAddr)); locAddr.sin_family = AF_INET; locAddr.sin_addr.s_addr = htonl(INADDR_ANY); locAddr.sin_port = htons(opt_port); if (bind(fd, (struct sockaddr *)&locAddr, sizeof(locAddr)) != 0) { perror("bind"); close(fd); exit(1); } for (msgCnt = 0; msgCnt < MESSAGE_CNT; msgCnt++) { memset(&remAddr, 0, sizeof(remAddr)); remAddrLen = sizeof(remAddr); nBytes = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&remAddr, &remAddrLen); if (nBytes < 0) { perror("recv"); close(fd); exit(1); } /* message arrival time */ if (gettimeofday(&msgTimevals[msgCnt], NULL) != 0) { perror("gettimeofday"); close(fd); exit(1); } if (opt_verbose) printf("message received\n"); } /* for (msgCnt...) */ /* convert all times to useconds after the first message */ for (msgCnt = 0; msgCnt < MESSAGE_CNT; msgCnt++) TIMEVAL_SUB(msgTimes[msgCnt], msgTimevals[msgCnt], msgTimevals[0]); /* fit line to samples 1..(MESSAGE_CNT-1) * http://faculty.cs.niu.edu/~hutchins/csci230/best-fit.htm */ { double sumX = 0.0; double sumY = 0.0; double sumX2 = 0.0; double sumXY = 0.0; double xMean, yMean, slope, yInt; double jLo, jHi, jSum; for (msgCnt = 1; msgCnt < MESSAGE_CNT; msgCnt++) { sumX += msgCnt; sumY += msgTimes[msgCnt]; sumX2 += (msgCnt * msgCnt); sumXY += ((double)msgCnt * (double)msgTimes[msgCnt]); } xMean = sumX / (MESSAGE_CNT - 1); yMean = sumY / (MESSAGE_CNT - 1); slope = (sumXY - sumX * yMean) / (sumX2 - sumX * xMean); yInt = yMean - slope * xMean; /* find max jitter */ jLo = 0.0; jHi = 0.0; jSum = 0.0; for (msgCnt = 1; msgCnt < MESSAGE_CNT; msgCnt++) { double j = msgTimes[msgCnt] - (slope * msgCnt + yInt); if (jLo > j) jLo = j; if (jHi < j) jHi = j; if (j < 0) j = -j; jSum += j; } printf("jitter: %0.1f...%0.1f usec\n", jLo, jHi); printf(" %0.1f avg\n", jSum / (MESSAGE_CNT - 1)); } exit(0); } /* main */
static struct timeval * rtsol_check_timer(void) { static struct timeval returnval; struct timeval now, rtsol_timer; struct ifinfo *ifinfo; int flags; gettimeofday(&now, NULL); rtsol_timer = tm_max; for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { if (TIMEVAL_LEQ(ifinfo->expire, now)) { if (dflag > 1) warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, " "state = %d", ifinfo->ifname, ifinfo->state); switch (ifinfo->state) { case IFS_DOWN: case IFS_TENTATIVE: /* interface_up returns 0 on success */ flags = interface_up(ifinfo->ifname); if (flags == 0) ifinfo->state = IFS_DELAY; else if (flags == IFS_TENTATIVE) ifinfo->state = IFS_TENTATIVE; else ifinfo->state = IFS_DOWN; break; case IFS_IDLE: { int oldstatus = ifinfo->active; int probe = 0; ifinfo->active = interface_status(ifinfo); if (oldstatus != ifinfo->active) { warnmsg(LOG_DEBUG, __func__, "%s status is changed" " from %d to %d", ifinfo->ifname, oldstatus, ifinfo->active); probe = 1; ifinfo->state = IFS_DELAY; } else if (ifinfo->probeinterval && (ifinfo->probetimer -= ifinfo->timer.tv_sec) <= 0) { /* probe timer expired */ ifinfo->probetimer = ifinfo->probeinterval; probe = 1; ifinfo->state = IFS_PROBE; } if (probe && mobile_node) defrouter_probe(ifinfo->sdl->sdl_index); break; } case IFS_DELAY: ifinfo->state = IFS_PROBE; sendpacket(ifinfo); break; case IFS_PROBE: if (ifinfo->probes < MAX_RTR_SOLICITATIONS) sendpacket(ifinfo); else { warnmsg(LOG_INFO, __func__, "No answer " "after sending %d RSs", ifinfo->probes); ifinfo->probes = 0; ifinfo->state = IFS_IDLE; } break; } rtsol_timer_update(ifinfo); } if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) rtsol_timer = ifinfo->expire; } if (TIMEVAL_EQ(rtsol_timer, tm_max)) { warnmsg(LOG_DEBUG, __func__, "there is no timer"); return(NULL); } else if (TIMEVAL_LT(rtsol_timer, now)) /* this may occur when the interval is too small */ returnval.tv_sec = returnval.tv_usec = 0; else TIMEVAL_SUB(&rtsol_timer, &now, &returnval); if (dflag > 1) warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld", (long)returnval.tv_sec, (long)returnval.tv_usec); return(&returnval); }