summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/mongotop/command.go
blob: 595668d68a09215ca7cfa7ba64eca74e6cae0c7a (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package mongotop

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/mongodb/mongo-tools/common/text"
	"sort"
	"time"
)

// FormattableDiff represents a diff of two samples taken by mongotop,
// which can be printed to output in various formats.
type FormattableDiff interface {
	// Generate a JSON representation of the diff
	JSON() string
	// Generate a table-like representation which can be printed to a terminal
	Grid() string
}

// ServerStatus represents the results of the "serverStatus" command.
type ServerStatus struct {
	Locks map[string]LockStats `bson:"locks,omitempty"`
}

// LockStats contains information on time spent acquiring and holding a lock.
type LockStats struct {
	AcquireCount        *ReadWriteLockTimes `bson:"acquireCount"`
	TimeLockedMicros    ReadWriteLockTimes  `bson:"timeLockedMicros"`
	TimeAcquiringMicros ReadWriteLockTimes  `bson:"timeAcquiringMicros"`
}

// ReadWriteLockTimes contains read/write lock times on a database.
type ReadWriteLockTimes struct {
	Read       int64 `bson:"R"`
	Write      int64 `bson:"W"`
	ReadLower  int64 `bson:"r"`
	WriteLower int64 `bson:"w"`
}

// ServerStatusDiff contains a map of the lock time differences for each database.
type ServerStatusDiff struct {
	// namespace -> lock times
	Totals map[string]LockDelta `json:"totals"`
	Time   time.Time            `json:"time"`
}

// LockDelta represents the differences in read/write lock times between two samples.
type LockDelta struct {
	Read  int64 `json:"read"`
	Write int64 `json:"write"`
}

// TopDiff contains a map of the differences between top samples for each namespace.
type TopDiff struct {
	// namespace -> totals
	Totals map[string]NSTopInfo `json:"totals"`
	Time   time.Time            `json:"time"`
}

// Top holds raw output of the "top" command.
type Top struct {
	Totals map[string]NSTopInfo `bson:"totals"`
}

// NSTopInfo holds information about a single namespace.
type NSTopInfo struct {
	Total TopField `bson:"total" json:"total"`
	Read  TopField `bson:"readLock" json:"read"`
	Write TopField `bson:"writeLock" json:"write"`
}

// TopField contains the timing and counts for a single lock statistic within the "top" command.
type TopField struct {
	Time  int `bson:"time" json:"time"`
	Count int `bson:"count" json:"count"`
}

// struct to enable sorting of namespaces by lock time with the sort package
type sortableTotal struct {
	Name  string
	Total int64
}

type sortableTotals []sortableTotal

func (a sortableTotals) Less(i, j int) bool {
	if a[i].Total == a[j].Total {
		return a[i].Name > a[j].Name
	}
	return a[i].Total < a[j].Total
}
func (a sortableTotals) Len() int      { return len(a) }
func (a sortableTotals) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// Diff takes an older Top sample, and produces a TopDiff
// representing the deltas of each metric between the two samples.
func (top Top) Diff(previous Top) TopDiff {
	// The diff to eventually return
	diff := TopDiff{
		Totals: map[string]NSTopInfo{},
		Time:   time.Now(),
	}

	// For each namespace we are tracking, subtract the times and counts
	// for total/read/write and build a new map containing the diffs.
	prevTotals := previous.Totals
	curTotals := top.Totals
	for ns, prevNSInfo := range prevTotals {
		if curNSInfo, ok := curTotals[ns]; ok {
			diff.Totals[ns] = NSTopInfo{
				Total: TopField{
					Time:  (curNSInfo.Total.Time - prevNSInfo.Total.Time) / 1000,
					Count: curNSInfo.Total.Count - prevNSInfo.Total.Count,
				},
				Read: TopField{
					Time:  (curNSInfo.Read.Time - prevNSInfo.Read.Time) / 1000,
					Count: curNSInfo.Read.Count - prevNSInfo.Read.Count,
				},
				Write: TopField{
					Time:  (curNSInfo.Write.Time - prevNSInfo.Write.Time) / 1000,
					Count: curNSInfo.Write.Count - prevNSInfo.Write.Count,
				},
			}
		}
	}
	return diff
}

// Grid returns a tabular representation of the TopDiff.
func (td TopDiff) Grid() string {
	buf := &bytes.Buffer{}
	out := &text.GridWriter{ColumnPadding: 4}
	out.WriteCells("ns", "total", "read", "write", time.Now().Format("2006-01-02T15:04:05Z07:00"))
	out.EndRow()

	//Sort by total time
	totals := make(sortableTotals, 0, len(td.Totals))
	for ns, diff := range td.Totals {
		totals = append(totals, sortableTotal{ns, int64(diff.Total.Time)})
	}

	sort.Sort(sort.Reverse(totals))
	for i, st := range totals {
		diff := td.Totals[st.Name]
		out.WriteCells(st.Name,
			fmt.Sprintf("%vms", diff.Total.Time),
			fmt.Sprintf("%vms", diff.Read.Time),
			fmt.Sprintf("%vms", diff.Write.Time),
			"")
		out.EndRow()
		if i >= 9 {
			break
		}
	}
	out.Flush(buf)
	return buf.String()
}

// JSON returns a JSON representation of the TopDiff.
func (td TopDiff) JSON() string {
	bytes, err := json.Marshal(td)
	if err != nil {
		panic(err)
	}
	return string(bytes)
}

// JSON returns a JSON representation of the ServerStatusDiff.
func (ssd ServerStatusDiff) JSON() string {
	bytes, err := json.Marshal(ssd)
	if err != nil {
		panic(err)
	}
	return string(bytes)
}

// Grid returns a tabular representation of the ServerStatusDiff.
func (ssd ServerStatusDiff) Grid() string {
	buf := &bytes.Buffer{}
	out := &text.GridWriter{ColumnPadding: 4}
	out.WriteCells("db", "total", "read", "write", time.Now().Format("2006-01-02T15:04:05Z07:00"))
	out.EndRow()

	//Sort by total time
	totals := make(sortableTotals, 0, len(ssd.Totals))
	for ns, diff := range ssd.Totals {
		totals = append(totals, sortableTotal{ns, diff.Read + diff.Write})
	}

	sort.Sort(sort.Reverse(totals))
	for i, st := range totals {
		diff := ssd.Totals[st.Name]
		out.WriteCells(st.Name,
			fmt.Sprintf("%vms", diff.Read+diff.Write),
			fmt.Sprintf("%vms", diff.Read),
			fmt.Sprintf("%vms", diff.Write),
			"")
		out.EndRow()
		if i >= 9 {
			break
		}
	}

	out.Flush(buf)
	return buf.String()
}

// Diff takes an older ServerStatus sample, and produces a ServerStatusDiff
// representing the deltas of each metric between the two samples.
func (ss ServerStatus) Diff(previous ServerStatus) ServerStatusDiff {
	// the diff to eventually return
	diff := ServerStatusDiff{
		Totals: map[string]LockDelta{},
		Time:   time.Now(),
	}

	prevLocks := previous.Locks
	curLocks := ss.Locks
	for ns, prevNSInfo := range prevLocks {
		if curNSInfo, ok := curLocks[ns]; ok {
			prevTimeLocked := prevNSInfo.TimeLockedMicros
			curTimeLocked := curNSInfo.TimeLockedMicros

			diff.Totals[ns] = LockDelta{
				Read: (curTimeLocked.Read + curTimeLocked.ReadLower -
					(prevTimeLocked.Read + prevTimeLocked.ReadLower)) / 1000,
				Write: (curTimeLocked.Write + curTimeLocked.WriteLower -
					(prevTimeLocked.Write + prevTimeLocked.WriteLower)) / 1000,
			}
		}
	}

	return diff
}