Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cfd887

Browse files
committedJun 27, 2020
added the ability to spawn a process to sample
1 parent faed421 commit 2cfd887

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go get -u -v github.com/0x0f0f0f/memplot/cmd
1414

1515
```
1616
Usage of memplot:
17+
Any argument following the options will be interpreted as the command to spawn and sample
1718
-dur duration
1819
total profiling time (default 10s)
1920
-height string

‎cmd/memplot.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package main
22

33
import (
4-
"errors"
54
"flag"
65
"fmt"
76
"github.com/0x0f0f0f/memplot"
87
"gonum.org/v1/plot/vg"
8+
"log"
99
"os"
10+
"os/exec"
1011
"time"
1112
)
1213

@@ -23,6 +24,12 @@ func main() {
2324
PlotVsz: false,
2425
}
2526

27+
flag.Usage = func() {
28+
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
29+
fmt.Fprintf(os.Stderr, "Any argument following the options will be"+
30+
" interpreted as the command to spawn and sample\n")
31+
flag.PrintDefaults()
32+
}
2633
// Default sample duration time
2734
defaultSd, err := time.ParseDuration("5ms")
2835
check(err)
@@ -45,10 +52,26 @@ func main() {
4552

4653
flag.Parse()
4754

48-
// Checks for valid flags
55+
// Run the PID passed with the -pid flag or the arguments following options
4956
if *pidPtr <= 0 {
50-
panic(errors.New("Invalid PID. Please specify a PID using -pid flag"))
57+
args := flag.Args()
58+
if len(args) == 0 {
59+
fmt.Fprintf(os.Stderr,
60+
"Invalid PID. Please specify PID using -pid flag"+
61+
" or specify a command to exec and sample\n")
62+
flag.Usage()
63+
os.Exit(1)
64+
}
65+
cmd := exec.Command(args[0], args[1:]...)
66+
err := cmd.Start()
67+
check(err)
68+
*pidPtr = cmd.Process.Pid
69+
} else {
70+
if len(flag.Args()) > 0 {
71+
log.Println("A pid was specified. Ignoring arguments")
72+
}
5173
}
74+
5275
widthImage, err := vg.ParseLength(*widthStr)
5376
check(err)
5477
heightImage, err := vg.ParseLength(*heightStr)

‎memplot.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func NewMemoryCollection(pid int32, sd, duration time.Duration) (*MemoryCollecti
3737
}
3838

3939
start := time.Now()
40-
elapsed := time.Since(start)
4140
var mem *process.MemoryInfoStat
4241
coll := &MemoryCollection{
4342
Pid: pid,
@@ -46,7 +45,7 @@ func NewMemoryCollection(pid int32, sd, duration time.Duration) (*MemoryCollecti
4645
Samples: make([]MemoryInstant, 0),
4746
}
4847

49-
for elapsed <= duration {
48+
for elapsed := time.Since(start); elapsed <= duration; elapsed = time.Since(start) {
5049
elapsed = time.Since(start)
5150
mem, err = proc.MemoryInfo()
5251
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.