Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const (
)

var (
showCPUInfo *bool
showCPUUsage *bool
showRAM *bool
showDisk *bool
showLocalIP *bool
showPublicIP *bool
showAll *bool
show5TopRAM *bool
showCPUInfo *bool
showCPUUsage *bool
showRAM *bool
showDisk *bool
showLocalIP *bool
showPublicIP *bool
showAll *bool
showNTopRAM *bool
numberOfProcessesToShow *int
)

func main() {
Expand All @@ -33,7 +34,8 @@ func main() {
showDisk = flag.Bool("disk", false, "Show disk usage")
showLocalIP = flag.Bool("local-ip", false, "Show local IP address")
showPublicIP = flag.Bool("public-ip", false, "Show public IP address")
show5TopRAM = flag.Bool("top5-ram", false, "Show top 5 process that consume the most memory")
showNTopRAM = flag.Bool("top-ram", false, "Show top n process that consume the most memory")
numberOfProcessesToShow = flag.Int("n-proc", 5, "number of processes to show")
showAll = flag.Bool("all", false, "Show all stats")
flag.Parse()

Expand All @@ -52,7 +54,7 @@ func main() {
{*showDisk, GetDiskUsage},
{*showLocalIP, GetLocalIPAddress},
{*showPublicIP, GetPublicIPAddress},
{*show5TopRAM, GetTopProcesses},
{*showNTopRAM, func() { GetTopProcesses(*numberOfProcessesToShow) }},
}
for _, pair := range functionsWithConditions {
if *showAll || pair.condition {
Expand Down
27 changes: 22 additions & 5 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/shirou/gopsutil/process"
"runtime"
"sort"
"strings"
)

func getReadableSize(sizeInBytes uint64) (readableSizeString string) {
Expand Down Expand Up @@ -42,8 +43,7 @@ func GetRamUsage() {
}

// GetTopProcesses print out the top 5 process that are consuming most RAM
func GetTopProcesses() {
strOutput := ""
func GetTopProcesses(numberOfRequestedProcesses int) {
processes, err := process.Processes()
if err != nil {
StandardPrinter(ErrorRedColor, "Could not retrieve running process list.")
Expand Down Expand Up @@ -86,15 +86,32 @@ func GetTopProcesses() {
return memoryPercentOfIthProcess > memoryPercentOfJthProcess
})

for i := 0; i < 5; i++ {
howManyProcessesToShow := numberOfRequestedProcesses
if len(processes) < howManyProcessesToShow { howManyProcessesToShow = len(processes) }

maxIndexAsStringLength := len(fmt.Sprintf("%d", howManyProcessesToShow))
topProcessesHeader := fmt.Sprintf("Top %d processes by memory usage:", howManyProcessesToShow)
ResultPrinter(topProcessesHeader, "")

for i := 0; i < howManyProcessesToShow; i++ {
memoryPercentOfIthProcess, err := processes[i].MemoryPercent()
if err != nil {
StandardPrinter(ErrorRedColor, "Process memory usage access denied.") //More descriptive error
panic(err)
}
Comment on lines +96 to 101

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to handle out of range cases in this loop

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks for testing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added:

howManyProcessesToShow := numberOfRequestedProcessesif len(processes) < howManyProcessesToShow { howManyProcessesToShow = len(processes) }

strOutput += fmt.Sprintf("PID: %5d, memory %%: %2.1f\n", processes[i].Pid, memoryPercentOfIthProcess)

indexAsString := fmt.Sprintf("%d", i + 1)
indexWithSpaces := fmt.Sprintf("%s%s",
strings.Repeat(" ", maxIndexAsStringLength - len(indexAsString)),
indexAsString,
)
fmt.Printf("\t%s. %s %5d, %s %2.1f%%\n",
indexWithSpaces,
fmt.Sprintf(BoldWhite, "PID:"),
processes[i].Pid,
fmt.Sprintf(BoldWhite, "memory:"),
memoryPercentOfIthProcess)
}
ResultPrinter("Top 5 processes by memory usage: \n", strOutput)
}

func GetDiskUsage() {
Expand Down