OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [runtime/] [sema_test.go] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package runtime_test
6
 
7
import (
8
        "runtime"
9
        "sync/atomic"
10
        "testing"
11
)
12
 
13
func BenchmarkSemaUncontended(b *testing.B) {
14
        type PaddedSem struct {
15
                sem uint32
16
                pad [32]uint32
17
        }
18
        const CallsPerSched = 1000
19
        procs := runtime.GOMAXPROCS(-1)
20
        N := int32(b.N / CallsPerSched)
21
        c := make(chan bool, procs)
22
        for p := 0; p < procs; p++ {
23
                go func() {
24
                        sem := new(PaddedSem)
25
                        for atomic.AddInt32(&N, -1) >= 0 {
26
                                runtime.Gosched()
27
                                for g := 0; g < CallsPerSched; g++ {
28
                                        runtime.Semrelease(&sem.sem)
29
                                        runtime.Semacquire(&sem.sem)
30
                                }
31
                        }
32
                        c <- true
33
                }()
34
        }
35
        for p := 0; p < procs; p++ {
36
                <-c
37
        }
38
}
39
 
40
func benchmarkSema(b *testing.B, block, work bool) {
41
        const CallsPerSched = 1000
42
        const LocalWork = 100
43
        procs := runtime.GOMAXPROCS(-1)
44
        N := int32(b.N / CallsPerSched)
45
        c := make(chan bool, procs)
46
        c2 := make(chan bool, procs/2)
47
        sem := uint32(0)
48
        if block {
49
                for p := 0; p < procs/2; p++ {
50
                        go func() {
51
                                runtime.Semacquire(&sem)
52
                                c2 <- true
53
                        }()
54
                }
55
        }
56
        for p := 0; p < procs; p++ {
57
                go func() {
58
                        foo := 0
59
                        for atomic.AddInt32(&N, -1) >= 0 {
60
                                runtime.Gosched()
61
                                for g := 0; g < CallsPerSched; g++ {
62
                                        runtime.Semrelease(&sem)
63
                                        if work {
64
                                                for i := 0; i < LocalWork; i++ {
65
                                                        foo *= 2
66
                                                        foo /= 2
67
                                                }
68
                                        }
69
                                        runtime.Semacquire(&sem)
70
                                }
71
                        }
72
                        c <- foo == 42
73
                        runtime.Semrelease(&sem)
74
                }()
75
        }
76
        if block {
77
                for p := 0; p < procs/2; p++ {
78
                        <-c2
79
                }
80
        }
81
        for p := 0; p < procs; p++ {
82
                <-c
83
        }
84
}
85
 
86
func BenchmarkSemaSyntNonblock(b *testing.B) {
87
        benchmarkSema(b, false, false)
88
}
89
 
90
func BenchmarkSemaSyntBlock(b *testing.B) {
91
        benchmarkSema(b, true, false)
92
}
93
 
94
func BenchmarkSemaWorkNonblock(b *testing.B) {
95
        benchmarkSema(b, false, true)
96
}
97
 
98
func BenchmarkSemaWorkBlock(b *testing.B) {
99
        benchmarkSema(b, true, true)
100
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.