forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
142 lines (124 loc) · 3.13 KB
/
progress.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
package main
import (
"fmt"
"sync/atomic"
"time"
"github.com/dgraph-io/dgraph/x"
)
type phase int32
const (
nothing phase = iota
mapPhase
reducePhase
)
type progress struct {
rdfCount int64
mapEdgeCount int64
reduceEdgeCount int64
reduceKeyCount int64
start time.Time
startReduce time.Time
// shutdown is a bidirectional channel used to manage the stopping of the
// report goroutine. It handles both the request to stop the report
// goroutine, as well as the message back to say that the goroutine has
// stopped. The channel MUST be unbuffered for this to work.
shutdown chan struct{}
phase phase
}
func newProgress() *progress {
return &progress{
start: time.Now(),
shutdown: make(chan struct{}),
}
}
func (p *progress) setPhase(ph phase) {
atomic.StoreInt32((*int32)(&p.phase), int32(ph))
}
func (p *progress) report() {
for {
select {
case <-time.After(time.Second):
p.reportOnce()
case <-p.shutdown:
p.shutdown <- struct{}{}
return
}
}
}
func (p *progress) reportOnce() {
mapEdgeCount := atomic.LoadInt64(&p.mapEdgeCount)
switch phase(atomic.LoadInt32((*int32)(&p.phase))) {
case nothing:
case mapPhase:
rdfCount := atomic.LoadInt64(&p.rdfCount)
elapsed := time.Since(p.start)
fmt.Printf("MAP %s rdf_count:%s rdf_speed:%s/sec edge_count:%s edge_speed:%s/sec\n",
fixedDuration(elapsed),
niceFloat(float64(rdfCount)),
niceFloat(float64(rdfCount)/elapsed.Seconds()),
niceFloat(float64(mapEdgeCount)),
niceFloat(float64(mapEdgeCount)/elapsed.Seconds()),
)
case reducePhase:
now := time.Now()
elapsed := time.Since(p.startReduce)
if p.startReduce.IsZero() {
p.startReduce = time.Now()
elapsed = time.Second
}
reduceKeyCount := atomic.LoadInt64(&p.reduceKeyCount)
reduceEdgeCount := atomic.LoadInt64(&p.reduceEdgeCount)
pct := ""
if mapEdgeCount != 0 {
pct = fmt.Sprintf("[%.2f%%] ", 100*float64(reduceEdgeCount)/float64(mapEdgeCount))
}
fmt.Printf("REDUCE %s %sedge_count:%s edge_speed:%s/sec "+
"plist_count:%s plist_speed:%s/sec\n",
fixedDuration(now.Sub(p.start)),
pct,
niceFloat(float64(reduceEdgeCount)),
niceFloat(float64(reduceEdgeCount)/elapsed.Seconds()),
niceFloat(float64(reduceKeyCount)),
niceFloat(float64(reduceKeyCount)/elapsed.Seconds()),
)
default:
x.AssertTruef(false, "invalid phase")
}
}
func (p *progress) endSummary() {
p.shutdown <- struct{}{}
<-p.shutdown
p.reportOnce()
total := fixedDuration(time.Since(p.start))
fmt.Printf("Total: %v\n", total)
}
func fixedDuration(d time.Duration) string {
str := fmt.Sprintf("%02ds", int(d.Seconds())%60)
if d >= time.Minute {
str = fmt.Sprintf("%02dm", int(d.Minutes())%60) + str
}
if d >= time.Hour {
str = fmt.Sprintf("%02dh", int(d.Hours())) + str
}
return str
}
var suffixes = [...]string{"", "k", "M", "G", "T"}
func niceFloat(f float64) string {
idx := 0
for f >= 1000 {
f /= 1000
idx++
}
if idx >= len(suffixes) {
return fmt.Sprintf("%f", f)
}
suf := suffixes[idx]
switch {
case f >= 100:
return fmt.Sprintf("%.1f%s", f, suf)
case f >= 10:
return fmt.Sprintf("%.2f%s", f, suf)
default:
return fmt.Sprintf("%.3f%s", f, suf)
}
}