-
Notifications
You must be signed in to change notification settings - Fork 128
/
commands.go
172 lines (150 loc) · 3.83 KB
/
commands.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
package main
import (
"encoding/json"
"fmt"
)
type Commands struct {
cmds []*Command
gd map[string]int
}
func NewCommands() Commands {
return Commands{
gd: make(map[string]int),
}
}
const (
CmdCreate = "create goroutine"
CmdStop = "stop goroutine"
CmdSend = "send to channel"
CmdBlock = "block goroutine"
CmdUnblock = "unblock goroutine"
CmdSleep = "sleep goroutine"
)
// Command is a common structure for all
// types of supported events (aka 'commands').
// It's main purpose to handle JSON marshalling.
type Command struct {
Time int64 "json:\"t\""
Command string "json:\"command\""
Name string "json:\"name,omitempty\""
Parent string "json:\"parent,omitempty\""
Channels []string "json:\"channels,omitempty\""
From string "json:\"from,omitempty\""
To string "json:\"to,omitempty\""
Channel string "json:\"ch,omitempty\""
Value interface{} "json:\"value,omitempty\""
EventID string "json:\"eid,omitempty\""
Duration int64 "json:\"duration,omitempty\""
Depth int "json:\"depth,omitempty\""
}
func (c *Commands) toJSON() []byte {
data, err := json.MarshalIndent(c.cmds, "", " ")
if err != nil {
panic(err)
}
return data
}
func (c *Commands) StartGoroutine(ts int64, gname string, gid, pid uint64) {
// TODO: use gname
name := fmt.Sprintf("#%d", gid)
parent := fmt.Sprintf("#%d", pid)
// ignore parent for 'main()' which has pid 0
if pid == 0 {
parent = ""
}
c.gd[name] = 0
if parent != "" {
c.gd[name] = c.gd[parent] + 1
}
cmd := &Command{
Time: ts,
Command: CmdCreate,
Name: name,
Parent: parent,
Depth: c.gd[name],
}
c.cmds = append(c.cmds, cmd)
}
func (c *Commands) StopGoroutine(ts int64, name string, gid uint64) {
cmd := &Command{
Time: ts,
Command: CmdStop,
Name: fmt.Sprintf("#%d", gid),
}
c.cmds = append(c.cmds, cmd)
}
func (c *Commands) ChanSend(send_ts, recv_ts int64, cid, fgid, tgid, val uint64) {
cmd := &Command{
Time: recv_ts,
Command: CmdSend,
From: fmt.Sprintf("#%d", fgid),
To: fmt.Sprintf("#%d", tgid),
Channel: fmt.Sprintf("#%d", cid),
Value: fmt.Sprintf("%d", val),
Duration: recv_ts - send_ts,
}
c.cmds = append(c.cmds, cmd)
}
func (c *Commands) BlockGoroutine(ts int64, gid uint64) {
cmd := &Command{
Time: ts,
Command: CmdBlock,
Name: fmt.Sprintf("#%d", gid),
}
c.cmds = append(c.cmds, cmd)
}
func (c *Commands) UnblockGoroutine(ts int64, gid uint64) {
cmd := &Command{
Time: ts,
Command: CmdUnblock,
Name: fmt.Sprintf("#%d", gid),
}
c.cmds = append(c.cmds, cmd)
}
func (c *Commands) SleepGoroutine(ts int64, gid uint64) {
cmd := &Command{
Time: ts,
Command: CmdSleep,
Name: fmt.Sprintf("#%d", gid),
}
c.cmds = append(c.cmds, cmd)
}
//ByTimestamp implements sort.Interface for sorting command by timestamp.
type ByTimestamp []*Command
func (a ByTimestamp) Len() int { return len(a) }
func (a ByTimestamp) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByTimestamp) Less(i, j int) bool { return a[i].Time < a[j].Time }
// Count counts total number of commands
func (c Commands) Count() int {
return len(c.cmds)
}
// CountCreateGoroutine counts total number of CreateGoroutine commands.
func (c Commands) CountCreateGoroutine() int {
var count int
for _, cmd := range c.cmds {
if cmd.Command == CmdCreate {
count++
}
}
return count
}
// CountStopGoroutine counts total number of StopGoroutine commands.
func (c Commands) CountStopGoroutine() int {
var count int
for _, cmd := range c.cmds {
if cmd.Command == CmdStop {
count++
}
}
return count
}
// CountSendToChannel counts total number of SendToChannel commands.
func (c Commands) CountSendToChannel() int {
var count int
for _, cmd := range c.cmds {
if cmd.Command == CmdSend {
count++
}
}
return count
}