Skip to content

Commit

Permalink
add virtual mem
Browse files Browse the repository at this point in the history
  • Loading branch information
exapsy committed Jan 20, 2024
1 parent 24496ab commit 2eec5d6
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/peekprof.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type App struct {
server *http.Server
noProfilerOutput bool
pretty bool
showConsole bool
}

type AppOptions struct {
Expand All @@ -47,6 +48,7 @@ type AppOptions struct {
ChartLiveUpdates bool
NoProfilerOutput bool
Pretty bool
ShowConsole bool
}

func NewApp(opts *AppOptions) *App {
Expand All @@ -66,7 +68,7 @@ func NewApp(opts *AppOptions) *App {
opts.Host = "localhost:8089"
}

exts := []interface{}{}
var exts []interface{}
if opts.CsvFilename != "" {
csvExtractorOpts := extractors.NewCsvExtractorOptions(opts.CsvFilename)
exts = append(exts, csvExtractorOpts)
Expand Down Expand Up @@ -107,6 +109,7 @@ func NewApp(opts *AppOptions) *App {
server: server,
noProfilerOutput: opts.NoProfilerOutput,
pretty: opts.Pretty,
showConsole: opts.ShowConsole,
}
}

Expand Down Expand Up @@ -155,8 +158,8 @@ func (a *App) watchMemoryUsage(wg *sync.WaitGroup) {
go func() {
defer wg.Done()
defer a.cancel()
if !a.pretty {
fmt.Printf("timestamp, rss kb, %%cpu\n")
if a.showConsole && !a.pretty {
fmt.Printf("timestamp, rss kb, virtual kb, %%cpu\n")
}
ch := a.process.WatchStats(a.ctx, a.refreshInterval)
LOOP:
Expand All @@ -167,25 +170,40 @@ func (a *App) watchMemoryUsage(wg *sync.WaitGroup) {
break LOOP
}

// TODO make console an extractor instead to skip this goto ~~logic~~ atrocity
if !a.showConsole {
goto skipConsole
}

if !a.noProfilerOutput {
if !a.pretty {
fmt.Printf("%s,%d,%.1f\n", pstats.Timestamp, pstats.MemoryUsage.Rss, pstats.CpuUsage.Percentage)
fmt.Printf(
"%s,%d,%d,%.1f\n",
pstats.Timestamp,
pstats.MemoryUsage.Rss,
pstats.MemoryUsage.Virtual,
pstats.CpuUsage.Percentage,
)
} else {
fmt.Printf(
"%02d:%02d:%02d\tmemory usage: %d mb\tcpu usage: %.1f%%\n",
"%02d:%02d:%02d\tmemory usage: %d mb\tvirtual: %d mb\tcpu usage: %.1f%%\n",
pstats.Timestamp.Hour(),
pstats.Timestamp.Minute(),
pstats.Timestamp.Second(),
pstats.MemoryUsage.Rss/1024,
pstats.MemoryUsage.Virtual/1024,
pstats.CpuUsage.Percentage,
)
}
}

skipConsole:

err := a.extractor.Add(extractors.ProcessStatsData{
MemoryUsage: extractors.MemoryUsageData{
Rss: pstats.MemoryUsage.Rss,
RssSwap: pstats.MemoryUsage.RssSwap,
Virtual: pstats.MemoryUsage.Virtual,
},
CpuUsage: extractors.CpuUsageData{
Percentage: pstats.CpuUsage.Percentage,
Expand Down
11 changes: 11 additions & 0 deletions internal/extractors/chartExtractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func (m *ChartExtractor) generateMemoryUsageChart(withLiveUpdatesListener bool)
line.AddSeries("RSS+Swap", rssSwapLine, charts.WithLabelOpts(opts.Label{Show: true, Position: "top"}))
}

virtualMemLine := m.getVirtualMemLineData()
line.AddSeries("Virtual", virtualMemLine, charts.WithLabelOpts(opts.Label{Show: true, Position: "top"}))

if m.UpdateLiveListenWSHost != "" && withLiveUpdatesListener {
m.AddMemoryLineLiveUpdateJSFuncs(line)
}
Expand Down Expand Up @@ -322,6 +325,14 @@ func (m *ChartExtractor) getRssSwapLineData() []opts.LineData {
return items
}

func (m *ChartExtractor) getVirtualMemLineData() []opts.LineData {
items := make([]opts.LineData, len(m.Data))
for i := 0; i < len(m.Data); i++ {
items[i] = opts.LineData{Value: m.Data[i].MemoryUsage.Virtual / 1024}
}
return items
}

// DivideTimeIntoParts returns a string formatted time that is divided into parts
func (m *ChartExtractor) DivideTimeIntoParts(parts int) []string {
if parts == 0 {
Expand Down
9 changes: 5 additions & 4 deletions internal/extractors/csvExtractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ func (c *CsvMemoryUsage) dataToCsvRecord(data ProcessStatsData) []string {
timestamp := data.Timestamp.Local().Format(time.RFC3339)
rss := fmt.Sprintf("%d", data.MemoryUsage.Rss)
rssSwap := fmt.Sprintf("%d", data.MemoryUsage.RssSwap)
virt := fmt.Sprintf("%d", data.MemoryUsage.Virtual)
cpuPercent := fmt.Sprintf("%.1f", data.CpuUsage.Percentage)

if runtime.GOOS != "darwin" {
r = []string{timestamp, rss, rssSwap, cpuPercent}
r = []string{timestamp, rss, rssSwap, virt, cpuPercent}
} else {
r = []string{timestamp, rss, cpuPercent}
r = []string{timestamp, rss, virt, cpuPercent}
}

return r
Expand All @@ -63,9 +64,9 @@ func (c *CsvMemoryUsage) dataToCsvRecord(data ProcessStatsData) []string {
func (c *CsvMemoryUsage) headers() []string {
var headers []string
if runtime.GOOS != "darwin" {
headers = []string{"timestamp", "rss kb", "rss+swap kb", "cpu%"}
headers = []string{"timestamp", "rss kb", "rss+swap kb", "virtual kb", "cpu%"}
} else {
headers = []string{"timestamp", "rss kb", "cpu%"}
headers = []string{"timestamp", "rss kb", "virtual kb", "cpu%"}
}
return headers
}
Expand Down
1 change: 1 addition & 0 deletions internal/extractors/extractors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type MemoryUsageData struct {
Rss int64
RssSwap int64
Virtual int64
}

type CpuUsageData struct {
Expand Down
27 changes: 27 additions & 0 deletions internal/process/linuxProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ func (p *LinuxProcess) GetCpuUsage() (CpuUsage, error) {
}, nil
}

func (p *LinuxProcess) GetVirtualMem() (int64, error) {
cmd := fmt.Sprintf(`grep VmSize /proc/%d/status | awk '{print $2}'`, p.Pid)
output, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
if err.Error() != "signal: interrupt" {
return 0, fmt.Errorf("failed executing command %s: %s", cmd, err)
}
}

outputStr := strings.Trim(string(output), "\n ")
if len(output) == 0 {
return 0, nil
}

memUsage, err := strconv.ParseInt(outputStr, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to convert output %q to int: %w", outputStr, err)
}

return memUsage, nil
}

func (p *LinuxProcess) GetMemoryUsage() (MemoryUsage, error) {
emptymu := MemoryUsage{}

Expand All @@ -219,10 +241,15 @@ func (p *LinuxProcess) GetMemoryUsage() (MemoryUsage, error) {
return emptymu, fmt.Errorf("failed getting process rss with swap: %w", err)
}
rssSwap := rss + swap
virtMem, err := p.GetVirtualMem()
if err != nil {
return emptymu, fmt.Errorf("failed getting process virtual memory: %w", err)
}

return MemoryUsage{
Rss: rss,
RssSwap: rssSwap,
Virtual: virtMem,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type MemoryUsage struct {
Rss int64 `json:"rss"`
RssSwap int64 `json:"rssSwap"`
Virtual int64 `json:"virtual"`
}

type CpuUsage struct {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Flags
This is used with -live and -html. The profiler automatically opens the file in your browser.
`)
pretty := flag.Bool("pretty", false, "Print in a more human-friendly - non-csv format, and print the pid of the running process.")
showConsole := flag.Bool("console", true, "Show the console output of the process")

flag.Parse()

Expand Down Expand Up @@ -153,6 +154,7 @@ Flags
ChartLiveUpdates: *live,
NoProfilerOutput: *noOutput,
Pretty: *pretty,
ShowConsole: *showConsole,
})
a.Start()
}
Expand Down

0 comments on commit 2eec5d6

Please sign in to comment.