-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
214 lines (171 loc) · 4.81 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bytes"
"fmt"
"io"
"math"
"os"
"os/exec"
"strconv"
"sync"
"time"
"github.com/jbreitbart/coBench/commands"
"github.com/jbreitbart/coBench/stats"
mstats "github.com/montanaflynn/stats"
)
func setupCmd(c string, cpuID int, logFilename string) (*exec.Cmd, *os.File, error) {
var commandName string
commandStr := make([]string, 0)
env := os.Environ()
if *hermitcore {
commandName = "numactl"
commandStr = append(commandStr, "numactl", "--physcpubind", cpus[cpuID], "/bin/sh")
env = append(env, "HERMIT_CPUS="+*threads, "HERMIT_MEM=4G", "HERMIT_ISLE=uhyve")
} else {
commandName = "/bin/sh"
env = append(env, "GOMP_CPU_AFFINITY="+cpus[cpuID], "OMP_NUM_THREADS="+*threads)
}
commandStr = append(commandStr, "-c")
if *perfStat != "" {
c = "perf stat -e " + *perfStat + " " + c
}
commandStr = append(commandStr, c)
cmd := exec.Command(commandName, commandStr...)
cmd.Env = env
// try to avoid duplicate filenames; TODO not perfect
if _, err := os.Stat(logFilename + ".log"); err == nil {
for i := 0; i < 100; i++ {
if _, err := os.Stat(logFilename + "--" + strconv.Itoa(i) + ".log"); err != nil {
logFilename = logFilename + "-" + strconv.Itoa(i)
break
}
}
}
logFilename += ".log"
outfile, err := os.Create(logFilename)
if err != nil {
return nil, nil, fmt.Errorf("Error while creating file: %v", err)
}
cmd.Stdout = outfile
cmd.Stderr = outfile
return cmd, outfile, nil
}
func runSingle(c string, catConfig [2]uint64) ([]stats.DataPerRun, error) {
if catConfig[0] != 0 && catConfig[1] != 0 {
if err := writeCATConfig(catConfig); err != nil {
return nil, fmt.Errorf("Error while writting CAT config: %v", err)
}
}
filename := commands.Pretty(c)
if catConfig[0] != 0 && catConfig[1] != 0 {
filename += fmt.Sprintf("-%x", catConfig[0])
}
cmd, outFile, err := setupCmd(c, 0, filename)
if err != nil {
return nil, err
}
defer outFile.Close()
// used to count how many apps have reached their min limit
done := make(chan int, 1)
done <- 1
// used to return an error from the go-routines
errs := make(chan error, 1)
// used to return the app runtimes
var runtimes []stats.DataPerRun
// used to wait for the following 2 goroutines
var wg sync.WaitGroup
wg.Add(1)
go runCmdMinTimes(cmd, *runs, &wg, &runtimes, done, errs)
wg.Wait()
if len(errs) != 0 {
return nil, <-errs
}
return runtimes, nil
}
func runPair(cPair [2]string, catConfig [2]uint64) ([][]stats.DataPerRun, error) {
if catConfig[0] != 0 && catConfig[1] != 0 {
if err := writeCATConfig(catConfig); err != nil {
return nil, fmt.Errorf("Error while writting CAT config: %v", err)
}
}
var cmds [len(cpus)]*exec.Cmd
// setup commands
for i := range cmds {
filename := fmt.Sprintf("%v-%v", commands.Pretty(cPair[i]), commands.Pretty(cPair[(i+1)%2]))
if catConfig[0] != 0 && catConfig[1] != 0 {
filename += fmt.Sprintf("-%x", catConfig[0])
}
var outFile *os.File
var err error
cmds[i], outFile, err = setupCmd(cPair[i], i, filename)
if err != nil {
return nil, err
}
defer outFile.Close()
}
// used to count how many apps have reached their min limit
done := make(chan int, 1)
done <- 0
// used to return an error from the go-routines
errs := make(chan error, len(cmds))
// used to return the app runtimes
runtimes := make([][]stats.DataPerRun, len(cmds))
// used to wait for the following 2 goroutines
var wg sync.WaitGroup
wg.Add(len(cmds))
for i, c := range cmds {
go runCmdMinTimes(c, *runs, &wg, &runtimes[i], done, errs)
}
wg.Wait()
if len(errs) != 0 {
return nil, <-errs
}
return runtimes, nil
}
func runCmdMinTimes(cmd *exec.Cmd, min int, wg *sync.WaitGroup, runtime *[]stats.DataPerRun, done chan int, errs chan error) {
defer wg.Done()
oldVariance := 0.0
var runtimeInSeconds []float64
completed := false
for i := 1; ; i++ {
// create a copy of the command
cmd := *cmd
var buf bytes.Buffer
cmd.Stdout = io.MultiWriter(cmd.Stdout, &buf)
cmd.Stderr = io.MultiWriter(cmd.Stderr, &buf)
start := time.Now()
err := cmd.Run()
var data stats.DataPerRun
data.Runtime = time.Since(start)
data.Output = buf.String()
if err != nil {
errs <- fmt.Errorf("Error running %v: %v", cmd.Args, err)
return
}
// did the other cmd result in an error?
if len(errs) != 0 {
return
}
d := <-done
// check if the other application was running the whole time
if d != len(cpus) {
// yes
*runtime = append(*runtime, data)
runtimeInSeconds = append(runtimeInSeconds, data.Runtime.Seconds())
}
// did we run min times?
if !completed && i >= min {
vari, _ := mstats.Variance(runtimeInSeconds)
if math.IsNaN(*varianceDiff) || math.Abs(vari-oldVariance) > *varianceDiff {
d++
completed = true
}
oldVariance = vari
}
done <- d
// both applications are done
if d == len(cpus) {
return
}
}
}