-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
482 lines (415 loc) · 10.5 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright 2021 Chris Thunes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"math/bits"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
"github.com/c2nes/jtopthreads/internal/proc"
)
// Combined output of jstack with data collected from /proc.
type StackDump struct {
Text string
ProcStats map[int]string
Uptime time.Duration
}
type Thread struct {
Header string
Name string
CPU time.Duration
Elapsed time.Duration
TID string
NID int
Stack string
}
func getHeaderField(line string, name string) string {
startMarker := name + "="
startIdx := strings.Index(line, startMarker)
if startIdx < 0 {
return ""
}
suffix := line[startIdx+len(startMarker):]
endIdx := strings.Index(suffix, " ")
if endIdx < 0 {
endIdx = len(suffix)
}
return suffix[:endIdx]
}
func (dump *StackDump) parseThread(lines []string) (*Thread, error) {
header := lines[0]
stack := strings.Join(lines[1:], "\n")
// Extract name and remove quotes
startName := 1
endName := strings.LastIndexByte(header, '"')
if endName <= 0 {
return nil, errors.New("invalid thread name (no closing quote)")
}
name := header[startName:endName]
tid := getHeaderField(header, "tid")
if tid == "" {
return nil, errors.New("tid= field missing from header")
}
nidString := getHeaderField(header, "nid")
if nidString == "" {
return nil, errors.New("nid= field missing from header")
}
nid64, err := strconv.ParseInt(nidString, 0, bits.UintSize)
if err != nil {
return nil, fmt.Errorf("unable to parse nid: %w", err)
}
nid := int(nid64)
// Parse prop data for thread if we have it
var stat *proc.ProcStat
if procString, ok := dump.ProcStats[nid]; ok {
stat, err = proc.Parse(procString)
if err != nil {
return nil, fmt.Errorf("error parsing /proc/[pid]/task/[tid]/stat: %w", err)
}
}
// In recent Java versions stack dumps include CPU and elapsed time. We use
// this data if it is available, but will fall back to using data from /proc
// (again, if available).
var cpu, elapsed time.Duration
cpuString := getHeaderField(header, "cpu")
if cpuString != "" {
cpu, err = time.ParseDuration(cpuString)
if err != nil {
return nil, err
}
} else if stat != nil {
cpu = proc.Duration(stat.Utime + stat.Stime)
} else {
cpu = 0 * time.Second
}
elapsedString := getHeaderField(header, "elapsed")
if elapsedString != "" {
elapsed, err = time.ParseDuration(elapsedString)
if err != nil {
return nil, err
}
} else if stat != nil {
elapsed = dump.Uptime - proc.Duration(stat.Starttime)
} else {
elapsed = 0 * time.Second
}
thread := &Thread{
Header: header,
Name: name,
CPU: cpu,
Elapsed: elapsed,
TID: tid,
NID: nid,
Stack: stack,
}
return thread, nil
}
func (dump *StackDump) ParseThreads() (map[string]*Thread, error) {
threads := make(map[string]*Thread)
var thread []string
for _, l := range strings.Split(dump.Text, "\n") {
if len(l) > 0 && (l[0] == ' ' || l[0] == '\t') {
if len(thread) > 0 {
thread = append(thread, l)
}
} else {
if len(thread) > 0 {
parsed, err := dump.parseThread(thread)
if err != nil {
return nil, err
}
threads[parsed.TID] = parsed
thread = nil
}
if len(l) > 0 && l[0] == '"' && strings.Contains(l, "nid=") {
thread = append(thread, l)
}
}
}
if len(thread) > 0 {
parsed, err := dump.parseThread(thread)
if err != nil {
return nil, err
}
threads[parsed.TID] = parsed
}
return threads, nil
}
func printHeader(cpuFrac float64, header string) {
fi, err := os.Stdout.Stat()
if err != nil || fi.Mode()&os.ModeCharDevice == 0 {
// Error checking status or non-TTY
fmt.Printf("%.6f\t%s\n", cpuFrac, header)
} else {
fmt.Printf("[%6.2f%%] %s\n", 100*cpuFrac, header)
}
}
func printTopThreads(dump0, dump1 *StackDump, n int, summary bool) error {
threads0, err := dump0.ParseThreads()
if err != nil {
panic(err)
}
threads1, err := dump1.ParseThreads()
if err != nil {
panic(err)
}
type withCPUFrac struct {
frac float64
thread *Thread
cpu time.Duration
elapsed time.Duration
}
var totalCPU time.Duration
var maxElapsed time.Duration
var top []*withCPUFrac
for tid, t1 := range threads1 {
var cpu time.Duration
var elapsed time.Duration
if t0, ok := threads0[tid]; ok {
cpu = t1.CPU - t0.CPU
elapsed = t1.Elapsed - t0.Elapsed
} else {
cpu = t1.CPU
elapsed = t1.Elapsed
}
totalCPU += cpu
if elapsed > maxElapsed {
maxElapsed = elapsed
}
frac := float64(cpu) / float64(elapsed)
top = append(top, &withCPUFrac{frac, t1, cpu, elapsed})
}
// Sort by CPU time in descending order
sort.Slice(top, func(i, j int) bool {
if top[i].frac == top[j].frac {
return top[i].thread.TID < top[j].thread.TID
}
return top[i].frac > top[j].frac
})
if n <= 0 {
n = len(top)
}
for _, t := range top[:n] {
printHeader(t.frac, t.thread.Header)
if !summary {
if len(t.thread.Stack) > 0 {
fmt.Println(t.thread.Stack)
}
fmt.Println()
}
}
totalFrac := float64(totalCPU) / float64(maxElapsed)
printHeader(totalFrac, fmt.Sprintf("Total (elapsed %s)", maxElapsed))
return nil
}
func parseJavaPID(s string) (int, error) {
// Try as pid and then search with JPS
pid, err := strconv.Atoi(s)
if err == nil {
return pid, nil
}
cmd := exec.Command("jps", "-l")
out, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
for _, line := range strings.Split(string(out), "\n") {
parts := strings.SplitN(line, " ", 2)
if len(parts) == 2 && parts[1] == s {
return strconv.Atoi(parts[0])
}
}
return 0, fmt.Errorf("no process found matching \"%s\"", s)
}
func collectProcStats(pid int) (map[int]string, error) {
bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
tasks, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/task", pid))
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
res := make(map[int]string)
res[pid] = string(bytes)
// From proc(5), on /proc/[pid]/task
//
// "This is a directory that contains one subdirectory for each thread in
// the process. The name of each subdirectory is the numerical thread ID
// ([tid]) of the thread (see gettid(2))."
for _, task := range tasks {
tid, err := strconv.Atoi(task.Name())
// Ignore any invalid directory entries
if err != nil {
continue
}
// We already read this one
if tid == pid {
continue
}
bytes, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/task/%d/stat", pid, tid))
if err != nil {
if os.IsNotExist(err) {
continue
} else {
return nil, err
}
}
res[tid] = string(bytes)
}
return res, nil
}
func readProcUptime() (time.Duration, error) {
v, err := ioutil.ReadFile("/proc/uptime")
if err != nil {
return 0, err
}
var up, idle float64
_, err = fmt.Sscanf(string(v), "%f %f", &up, &idle)
if err != nil {
return 0, err
}
return time.Duration(up * float64(time.Second)), nil
}
func jstack(pid int) (*StackDump, error) {
// Read /proc/uptime so we can calculate elapsed process time
uptime, err := readProcUptime()
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
}
// Collect process stats
procResCh := make(chan map[int]string, 1)
procErrCh := make(chan error, 1)
go func() {
res, err := collectProcStats(pid)
procResCh <- res
procErrCh <- err
}()
cmd := exec.Command("jstack", strconv.Itoa(pid))
out, err := cmd.CombinedOutput()
// Wait for go routine to complete before returning any errors
procErr := <-procErrCh
if err != nil {
return nil, err
}
if procErr != nil {
return nil, procErr
}
return &StackDump{string(out), <-procResCh, uptime}, nil
}
func main() {
flag.Usage = func() {
out := flag.CommandLine.Output()
usage := "usage: %s [options] <stack-file> [stack-file]\n"
usage += " or: %s [options] [-sample <duration>] <pid | main-class>\n\n"
fmt.Fprintf(out, usage, os.Args[0], os.Args[0])
flag.PrintDefaults()
}
usageError := func(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, "error: "+format, a...)
fmt.Fprint(os.Stderr, "\n\n")
flag.Usage()
os.Exit(1)
}
topN := 0
duration := time.Duration(0)
summary := false
flag.IntVar(&topN, "n", topN, "limit output to the top `N` threads")
flag.DurationVar(&duration, "sample", duration, "sample process for `duration`")
flag.BoolVar(&summary, "summary", summary, "omit stacks")
flag.Parse()
var dump0, dump1 *StackDump
if flag.NArg() == 2 {
if duration > 0 {
usageError("-sample not supported with file arguments")
}
bytes0, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
bytes1, err := ioutil.ReadFile(flag.Arg(1))
if err != nil {
log.Fatal(err)
}
dump0 = &StackDump{Text: string(bytes0)}
dump1 = &StackDump{Text: string(bytes1)}
} else if flag.NArg() == 1 {
arg := flag.Arg(0)
// A single argument can be a file, pid or main-class. Process as a file
// if a matching file exists, otherwise assume the arg is a pid/main-class.
if _, err := os.Stat(arg); err == nil {
if duration > 0 {
usageError("-sample not supported with file argument")
}
bytes1, err := ioutil.ReadFile(arg)
if err != nil {
log.Fatal(err)
}
dump0 = &StackDump{}
dump1 = &StackDump{Text: string(bytes1)}
} else {
pid, err := parseJavaPID(arg)
if err != nil {
log.Fatal(err)
}
ch0 := make(chan *StackDump)
go func() {
dump, err := jstack(pid)
if err != nil {
log.Fatal(err)
}
ch0 <- dump
}()
if duration > 0 {
ch1 := make(chan *StackDump)
go func() {
time.Sleep(duration)
dump, err := jstack(pid)
if err != nil {
log.Fatal(err)
}
ch1 <- dump
}()
dump0 = <-ch0
dump1 = <-ch1
} else {
dump0 = &StackDump{}
dump1 = <-ch0
}
}
} else if flag.NArg() < 1 {
usageError("argument missing")
} else {
usageError("too many arguments")
}
if err := printTopThreads(dump0, dump1, topN, summary); err != nil {
log.Fatal(err)
}
}