-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtiming.go
91 lines (78 loc) · 1.83 KB
/
mtiming.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
package main
//
// a MapReduce pseudo-application to test that workers
// execute map tasks in parallel.
//
// go build -buildmode=plugin mtiming.go
//
import "../mr"
import "strings"
import "fmt"
import "os"
import "syscall"
import "time"
import "sort"
import "io/ioutil"
func nparallel(phase string) int {
// create a file so that other workers will see that
// we're running at the same time as them.
pid := os.Getpid()
myfilename := fmt.Sprintf("mr-worker-%s-%d", phase, pid)
err := ioutil.WriteFile(myfilename, []byte("x"), 0666)
if err != nil {
panic(err)
}
// are any other workers running?
// find their PIDs by scanning directory for mr-worker-XXX files.
dd, err := os.Open(".")
if err != nil {
panic(err)
}
names, err := dd.Readdirnames(1000000)
if err != nil {
panic(err)
}
ret := 0
for _, name := range names {
var xpid int
pat := fmt.Sprintf("mr-worker-%s-%%d", phase)
n, err := fmt.Sscanf(name, pat, &xpid)
if n == 1 && err == nil {
err := syscall.Kill(xpid, 0)
if err == nil {
// if err == nil, xpid is alive.
ret += 1
}
}
}
dd.Close()
time.Sleep(1 * time.Second)
err = os.Remove(myfilename)
if err != nil {
panic(err)
}
return ret
}
func Map(filename string, contents string) []mr.KeyValue {
t0 := time.Now()
ts := float64(t0.Unix()) + (float64(t0.Nanosecond()) / 1000000000.0)
pid := os.Getpid()
n := nparallel("map")
kva := []mr.KeyValue{}
kva = append(kva, mr.KeyValue{
fmt.Sprintf("times-%v", pid),
fmt.Sprintf("%.1f", ts)})
kva = append(kva, mr.KeyValue{
fmt.Sprintf("parallel-%v", pid),
fmt.Sprintf("%d", n)})
return kva
}
func Reduce(key string, values []string) string {
//n := nparallel("reduce")
// sort values to ensure deterministic output.
vv := make([]string, len(values))
copy(vv, values)
sort.Strings(vv)
val := strings.Join(vv, " ")
return val
}