-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from leosunmo/heap-stats
Heap stats
- Loading branch information
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(cmdNodeHeap) | ||
} | ||
|
||
var cmdNodeHeap = &cobra.Command{ | ||
Use: "heap", | ||
Short: "Display the node heap stats.", | ||
Long: `Show node heap stats and settings.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
v := getClient() | ||
|
||
nodeStats, err := v.GetNodeJVMStats() | ||
|
||
if err != nil { | ||
fmt.Printf("Error getting node JVM stats: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
var header []string | ||
var rows [][]string | ||
header = []string{"Name", "Role", "Heap Max", "Heap Used", "Heap %", "Non-Heap Committed", "Non-Heap Used"} | ||
rows = [][]string{} | ||
sort.Slice(nodeStats, func(i, j int) bool { | ||
return nodeStats[i].Name < nodeStats[j].Name | ||
}) | ||
for _, node := range nodeStats { | ||
row := []string{ | ||
node.Name, | ||
node.Role, | ||
byteCountSI(int64(node.JVMStats.HeapMaxBytes)), | ||
byteCountSI(int64(node.JVMStats.HeapUsedBytes)), | ||
fmt.Sprintf("%d %%", node.JVMStats.HeapUsedPercentage), | ||
byteCountSI(int64(node.JVMStats.NonHeapCommittedBytes)), | ||
byteCountSI(int64(node.JVMStats.NonHeapUsedBytes)), | ||
} | ||
|
||
rows = append(rows, row) | ||
} | ||
|
||
table := renderTable(rows, header) | ||
fmt.Println(table) | ||
}, | ||
} | ||
|
||
func byteCountSI(b int64) string { | ||
const unit = 1000 | ||
if b < unit { | ||
return fmt.Sprintf("%d B", b) | ||
} | ||
div, exp := int64(unit), 0 | ||
for n := b / unit; n >= unit; n /= unit { | ||
div *= unit | ||
exp++ | ||
} | ||
return fmt.Sprintf("%.1f %cB", | ||
float64(b)/float64(div), "kMGTPE"[exp]) | ||
} |