source: benchmark/readyQ/churn.go@ 07997cd

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 07997cd was 160ee4c, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fix churn.go to use channels instead of semaphores.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1package main
2
3import (
4 "flag"
5 "fmt"
6 "math/rand"
7 "runtime"
8 "sync"
9 "sync/atomic"
10 "time"
11 "golang.org/x/text/language"
12 "golang.org/x/text/message"
13)
14
15func churner(result chan uint64, start *sync.WaitGroup, spots [] chan struct {}) {
16 s := rand.NewSource(time.Now().UnixNano())
17 rng := rand.New(s)
18
19 count := uint64(0)
20 start.Wait()
21 for true {
22
23 sem := spots[ rng.Intn(100) % len(spots) ];
24 sem <- (struct {}{})
25 <- sem;
26
27 count += 1
28 if clock_mode && atomic.LoadInt32(&stop) == 1 { break }
29 if !clock_mode && count >= stop_count { break }
30 }
31
32 atomic.AddInt64(&threads_left, -1);
33 result <- count
34}
35
36func main() {
37 var spot_cnt int
38
39 spot_cntOpt := flag.Int("s", 1, "Number of spots in the system")
40
41 bench_init()
42
43 spot_cnt = *spot_cntOpt
44
45 threads_left = int64(nthreads)
46
47 result := make(chan uint64)
48 var wg sync.WaitGroup
49 wg.Add(1)
50
51 spots := make([] chan struct {}, spot_cnt)
52 for i := range spots {
53 spots[i] = make(chan struct {}, 1)
54 }
55
56 for i := 0; i < nthreads; i++ {
57 go churner(result, &wg, spots)
58 }
59 fmt.Printf("Starting\n");
60 atomic.StoreInt32(&stop, 0)
61 start := time.Now()
62 wg.Done();
63 wait(start, true);
64
65 atomic.StoreInt32(&stop, 1)
66 end := time.Now()
67 duration := end.Sub(start)
68
69 fmt.Printf("\nDone\n")
70
71 for atomic.LoadInt64(&threads_left) != 0 {
72 for i := range spots {
73 select {
74 case spots[i] <- (struct {}{}):
75 default:
76 }
77 runtime.Gosched()
78 }
79 }
80
81 global_counter := uint64(0)
82 for i := 0; i < nthreads; i++ {
83 global_counter += <- result
84 }
85
86 p := message.NewPrinter(language.English)
87 p.Printf("Duration (ms) : %d\n", duration.Milliseconds())
88 p.Printf("Number of processors : %d\n", nprocs);
89 p.Printf("Number of threads : %d\n", nthreads);
90 p.Printf("Number of spots : %d\n", spot_cnt);
91 p.Printf("Total Operations(ops): %15d\n", global_counter);
92 // p.Printf("Total blocks : %15d\n", global_blocks);
93 p.Printf("Ops per second : %18.2f\n", float64(global_counter) / duration.Seconds());
94 p.Printf("ns per ops : %18.2f\n", float64(duration.Nanoseconds()) / float64(global_counter))
95 p.Printf("Ops per threads : %15d\n", global_counter / uint64(nthreads))
96 p.Printf("Ops per procs : %15d\n", global_counter / uint64(nprocs))
97 p.Printf("Ops/sec/procs : %18.2f\n", (float64(global_counter) / float64(nprocs)) / duration.Seconds())
98 p.Printf("ns per ops/procs : %18.2f\n", float64(duration.Nanoseconds()) / (float64(global_counter) / float64(nprocs)))
99}
Note: See TracBrowser for help on using the repository browser.