Show
Ignore:
Timestamp:
04/09/08 15:37:35 (9 months ago)
Author:
aturner
Message:

switch to timespec (nanosecond precision) for sleeping. OS X's AbsoluteTime?
methods are really damn accurate! refs #41

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • features/performance/src/send_packets.c

    r1970 r1971  
    327327    static int adjuster_flipper = 0; 
    328328#ifdef HAVE_TCPREPLAY 
    329     struct timeval adjuster = { 0, OPT_VALUE_SLEEP_ACCEL } 
     329    struct timespec adjuster = { 0, OPT_VALUE_SLEEP_ACCEL * 1000 } 
    330330#else 
    331     struct timeval adjuster = { 0, ADJUSTER_OFFSET }; 
    332 #endif 
    333     static struct timeval nap = { 0, 0 }; 
     331    struct timespec adjuster = { 0, ADJUSTER_OFFSET * 1000 }; 
     332#endif 
     333    static struct timespec nap = { 0, 0 }; 
    334334    static u_int32_t double_up = 0; 
    335     struct timeval now, sleep_until, nap_this_time; 
    336     struct timespec ignore, sleep; 
     335    struct timeval nap_for, now, sleep_until; 
     336    struct timespec ignore, sleep, nap_this_time; 
    337337    float n; 
    338338    static u_int32_t send = 0;      /* accellerator.   # of packets to send w/o sleeping */ 
    339     u_int32_t ppusec; /* packets per usec */ 
     339    u_int64_t ppnsec; /* packets per usec */ 
    340340     
    341341    /* just return if topspeed */ 
     
    373373         */ 
    374374        if (timerisset(last) && timercmp(time, last, >)) { 
    375             timersub(time, last, &nap); 
    376             timerdiv(&nap, options.speed.speed); 
     375            timersub(time, last, &nap_for); 
     376            TIMEVAL_TO_TIMESPEC(&nap_for, &nap); 
     377            timesdiv(&nap, options.speed.speed); 
    377378        } 
    378379        else { 
     
    382383             * last packet. 
    383384             */ 
    384             timerclear(&nap); 
     385            timesclear(&nap); 
    385386        } 
    386387        break; 
     
    394395            n = (float)len / (options.speed.speed * 1024 * 1024 / 8); /* convert Mbps to bps */ 
    395396            nap.tv_sec = n; 
    396             nap.tv_usec = (n - nap.tv_sec)  * 1000000; 
     397            nap.tv_nsec = (n - nap.tv_sec)  * 1000000000; 
    397398            dbgx(3, "packet size %d\t\tequals %f bps\t\tnap " TIMEVAL_FORMAT, len, n,  
    398                 nap.tv_sec, nap.tv_usec); 
     399                nap.tv_sec, nap.tv_nsec); 
    399400        } 
    400401        else { 
    401402            /* don't sleep at all for the first packet */ 
    402             timerclear(&nap); 
     403            timesclear(&nap); 
    403404        } 
    404405        break; 
     
    406407    case SPEED_PACKETRATE: 
    407408        /* only need to calculate this the first time */ 
    408         if (! timerisset(&nap)) { 
     409        if (! timesisset(&nap)) { 
    409410            /* run in packets/sec */ 
    410             ppusec = 1000000 / options.speed.speed; 
    411             MICROSEC_TO_TIMEVAL(ppusec, &nap); 
    412             dbgx(1, "sending 1 packet per %lu usec", nap.tv_usec); 
     411            ppnsec = 1000000000 / options.speed.speed; 
     412            NANOSEC_TO_TIMESPEC(ppnsec, &nap); 
     413            dbgx(1, "sending 1 packet per %lu nsec", nap.tv_nsec); 
    413414        } 
    414415        break; 
     
    439440     */ 
    440441    nap_this_time.tv_sec = nap.tv_sec; 
    441     nap_this_time.tv_usec = nap.tv_usec; 
     442    nap_this_time.tv_nsec = nap.tv_nsec; 
    442443 
    443444    /* apply the adjuster... */ 
    444     if (timerisset(&adjuster)) { 
    445         if (timercmp(&nap_this_time, &adjuster, >)) { 
    446             timersub(&nap_this_time, &adjuster, &nap_this_time); 
    447             dbgx(1, "adjusting nap_this_time by %lu usec", adjuster.tv_usec); 
     445    if (timesisset(&adjuster)) { 
     446        if (timescmp(&nap_this_time, &adjuster, >)) { 
     447            timessub(&nap_this_time, &adjuster, &nap_this_time); 
     448            dbgx(1, "adjusting nap_this_time by %lu nsec", adjuster.tv_nsec); 
    448449        } else {  
    449450            dbg(1, "resetting nap_this_time to 0"); 
    450             timerclear(&nap_this_time); 
     451            timesclear(&nap_this_time); 
    451452        } 
    452453    } 
    453454     
    454455    /* don't sleep if nap = { 0, 0} */ 
    455     if (!timerisset(&nap_this_time)) 
     456    if (!timesisset(&nap_this_time)) 
    456457        return; 
    457458