Skip to content

time.Sleep() on amd64 linux can't sleep <10ms #260

@gopherbot

Description

@gopherbot

by wmundt42:

What steps will reproduce the problem?
1. Run the following program with -duration=5000000

package main

import "time"
import "fmt"
import "flag"

var Duration = flag.Int64("duration", 10000, "time to sleep for")
var Count = flag.Int64("count", 100, "times to sleep")

func main() {
        flag.Parse();

        t1 := time.Nanoseconds();
        for i := int64(0); i < *Count; i++ {
                time.Sleep(*Duration)
        }
        t2 := time.Nanoseconds();

        delta := (t2 - t1) / *Count;
        off := delta - *Duration;
        offpct := float(off) / float(*Duration) * 100;

        fmt.Println("asked to sleep for", *Duration, "-- actually slept
for", t2-t1);
        fmt.Println("off by ", off, "ns, or", offpct, "percent");
}

What is the expected output? What do you see instead?

Expected, more oe less: 
asked to sleep for 5000000 -- actually slept for 500000000
off by 0 ns, or 0.000 percent

Actual:
asked to sleep for 5000000 -- actually slept for 1022879000
off by  5228790 ns, or 104.5758 percent

What is your $GOOS?  $GOARCH?

linux amd64

Which revision are you sync'ed to?  (hg log -l 1)

4130:e1069e80ef83

Please provide any additional information below.

It appears that time.Sleep rounds up to the nearest 10ms, at least when
called repeatedly.  Since time.Ticker is implemented in terms of Sleep,
this means Tickers -- at least on this platform -- can't seem to tick more
often than every 10ms.

It may be relevant that the machine in question is a VM running under
VMWare ESXI; uname -a says "Linux wmtest2 2.6.24-24-server #1 SMP Tue Jun
30 20:24:57 UTC 2009 x86_64 GNU/Linux"

If this is expected behavior, these limitations should be documented on
both time.Sleep and time.Ticker.

Workaround for ticker use case: it is possible to process ticks more often
by setting up multiple 10ms Tickers and a goroutine to mux the channels.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions