Hello golang experts,

I am curious how does goroutine scheduler picks what goroutine to run, 
among several runnable. Does it optimize for fairness in any way?

I ran a quick experiment and found out that goroutines that run for longer 
intervals between yield points receive proportionally larger CPU share.
In the following code:
package main

import (
    "fmt"
"runtime"
    "time"
)

func run(n int) int {
    ret := 1
    for i := 0; i < n; i++ {
        ret *= i
    }
    return ret
}

func foo(ch chan float64, n, m int) {
    f := 0
var run_time, wait_time, start_time, end_time int64
for i := 0; i < m; i++ {
start_time = time.Now().UnixNano()
if end_time != 0 {
wait_time += start_time - end_time;
}
                f += run(n)
end_time = time.Now().UnixNano();
run_time += end_time - start_time;
runtime.Gosched()
}
    ch <- float64(run_time) / float64(run_time + wait_time)
}

func main() {
    ch := make(chan float64)
    go foo(ch, 100000, 100)
    go foo(ch, 1000000, 100)

    v := <-ch
    fmt.Printf("first: %f\n", v)
    v = <-ch
    fmt.Printf("second: %f\n", v)
}

Here, one goroutine gets 10x cpu time compared to the other.
$ GOMAXPROCS=1 ./ss 
first: 0.911957
second: 0.091656

Does this test expose the scheduler's cpu policy correctly, or it is 
biased? What is the best reading about scheduler's policies?

Thank you!
Dmitry.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to