Skip to content

Commit

Permalink
Show memory usage in 'status' command output
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored and praveenkumar committed Oct 17, 2022
1 parent 7109cde commit 25d3886
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cmd/crc/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type status struct {
DiskSize int64 `json:"diskSize,omitempty"`
CacheUsage int64 `json:"cacheUsage,omitempty"`
CacheDir string `json:"cacheDir,omitempty"`
RAMSize int64 `json:"ramSize,omitempty"`
RAMUsage int64 `json:"ramUsage,omitempty"`
Preset preset.Preset `json:"preset"`
}

Expand Down Expand Up @@ -80,6 +82,8 @@ func getStatus(client *daemonclient.Client, cacheDir string) *status {
PodmanVersion: clusterStatus.PodmanVersion,
DiskUsage: clusterStatus.DiskUse,
DiskSize: clusterStatus.DiskSize,
RAMSize: clusterStatus.RAMSize,
RAMUsage: clusterStatus.RAMUse,
CacheUsage: size,
CacheDir: cacheDir,
Preset: clusterStatus.Preset,
Expand Down Expand Up @@ -108,6 +112,10 @@ func (s *status) prettyPrintTo(writer io.Writer) error {
}

lines = append(lines,
line{"RAM Usage", fmt.Sprintf(
"%s of %s",
units.HumanSize(float64(s.RAMUsage)),
units.HumanSize(float64(s.RAMSize)))},
line{"Disk Usage", fmt.Sprintf(
"%s of %s (Inside the CRC VM)",
units.HumanSize(float64(s.DiskUsage)),
Expand Down
36 changes: 36 additions & 0 deletions cmd/crc/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestPlainStatus(t *testing.T) {
expected := `CRC VM: Running
OpenShift: Running (v4.5.1)
Podman: 3.3.1
RAM Usage: 0B of 0B
Disk Usage: 10GB of 20GB (Inside the CRC VM)
Cache Usage: 10kB
Cache Directory: %s
Expand Down Expand Up @@ -95,6 +96,7 @@ func TestStatusWithoutPodman(t *testing.T) {

expected := `CRC VM: Running
OpenShift: Running (v4.5.1)
RAM Usage: 0B of 0B
Disk Usage: 10GB of 20GB (Inside the CRC VM)
Cache Usage: 10kB
Cache Directory: %s
Expand Down Expand Up @@ -170,3 +172,37 @@ func TestJsonStatusWithError(t *testing.T) {
`
assert.Equal(t, expected, out.String())
}

func TestStatusWithMemoryPodman(t *testing.T) {
cacheDir, err := ioutil.TempDir("", "cache")
require.NoError(t, err)
defer os.RemoveAll(cacheDir)

client := mocks.NewClient(t)
require.NoError(t, ioutil.WriteFile(filepath.Join(cacheDir, "crc.qcow2"), make([]byte, 10000), 0600))

client.On("Status").Return(apiClient.ClusterStatusResult{
CrcStatus: string(state.Running),
OpenshiftStatus: string(types.OpenshiftRunning),
OpenshiftVersion: "4.5.1",
DiskUse: 10_000_000_000,
DiskSize: 20_000_000_000,
RAMSize: 1_000_000,
RAMUse: 900_000,
Preset: preset.OpenShift,
}, nil)

out := new(bytes.Buffer)
assert.NoError(t, runStatus(out, &daemonclient.Client{
APIClient: client,
}, cacheDir, ""))

expected := `CRC VM: Running
OpenShift: Running (v4.5.1)
RAM Usage: 900kB of 1MB
Disk Usage: 10GB of 20GB (Inside the CRC VM)
Cache Usage: 10kB
Cache Directory: %s
`
assert.Equal(t, fmt.Sprintf(expected, cacheDir), out.String())
}
2 changes: 2 additions & 0 deletions pkg/crc/api/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func TestStatus(t *testing.T) {
PodmanVersion: "3.3.1",
DiskUse: int64(10000000000),
DiskSize: int64(20000000000),
RAMUse: int64(1000),
RAMSize: int64(2000),
Preset: preset.OpenShift,
},
statusResult,
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/api/api_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var testCases = []testCase{
// status
{
request: get("status"),
response: jSon(`{"CrcStatus":"Running","OpenshiftStatus":"Running","OpenshiftVersion":"4.5.1","PodmanVersion":"3.3.1","DiskUse":10000000000,"DiskSize":20000000000,"Preset":"openshift"}`),
response: jSon(`{"CrcStatus":"Running","OpenshiftStatus":"Running","OpenshiftVersion":"4.5.1","PodmanVersion":"3.3.1","DiskUse":10000000000,"DiskSize":20000000000,"RAMUse":1000,"RAMSize":2000,"Preset":"openshift"}`),
},

// status with failure
Expand Down
6 changes: 4 additions & 2 deletions pkg/crc/api/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ type ClusterStatusResult struct {
PodmanVersion string `json:"PodmanVersion,omitempty"`
DiskUse int64
DiskSize int64
RAMUse int64
RAMSize int64
Preset preset.Preset
}

type ConsoleResult struct {
ClusterConfig types.ClusterConfig
}

// setOrUnsetConfigResult struct is used to return the result of
// SetOrUnsetConfigResult struct is used to return the result of
// setconfig/unsetconfig command
type SetOrUnsetConfigResult struct {
Properties []string
}

// getConfigResult struct is used to return the result of getconfig command
// GetConfigResult struct is used to return the result of getconfig command
type GetConfigResult struct {
Configs map[string]interface{}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (h *Handler) Status(c *context) error {
PodmanVersion: res.PodmanVersion,
DiskUse: res.DiskUse,
DiskSize: res.DiskSize,
RAMSize: res.RAMSize,
RAMUse: res.RAMUse,
Preset: res.Preset,
})
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/crc/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ func GetRootPartitionUsage(sshRunner *ssh.Runner) (int64, int64, error) {
return diskSize, diskUsage, nil
}

// GetRAMUsage return RAM size and RAM usage in bytes
func GetRAMUsage(sshRunner *ssh.Runner) (int64, int64, error) {
cmd := "awk '/^Mem/ {print $2,$3}' <(free -b)"
out, _, err := sshRunner.Run(cmd)

if err != nil {
return 0, 0, err
}

ramDetails := strings.Split(strings.TrimSpace(out), " ")
ramSize, err := strconv.ParseInt(ramDetails[0], 10, 64)
if err != nil {
return 0, 0, err
}
ramUsage, err := strconv.ParseInt(ramDetails[1], 10, 64)
if err != nil {
return 0, 0, err
}

return ramSize, ramUsage, nil
}

func EnsureSSHKeyPresentInTheCluster(ctx context.Context, ocConfig oc.Config, sshPublicKeyPath string) error {
sshPublicKeyByte, err := ioutil.ReadFile(sshPublicKeyPath)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type client struct {
config crcConfig.Storage

diskDetails *memoize.Memoizer
ramDetails *memoize.Memoizer
}

func NewClient(name string, debug bool, config crcConfig.Storage) Client {
Expand All @@ -42,6 +43,7 @@ func NewClient(name string, debug bool, config crcConfig.Storage) Client {
debug: debug,
config: config,
diskDetails: memoize.NewMemoizer(time.Minute, 5*time.Minute),
ramDetails: memoize.NewMemoizer(30*time.Second, 2*time.Minute),
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/crc/machine/fakemachine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func (c *Client) Status() (*types.ClusterStatusResult, error) {
PodmanVersion: "3.3.1",
DiskUse: 10_000_000_000,
DiskSize: 20_000_000_000,
RAMSize: 2_000,
RAMUse: 1_000,
Preset: preset.OpenShift,
}, nil
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/crc/machine/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
clusterStatusResult.PodmanVersion = vm.bundle.GetPodmanVersion()
clusterStatusResult.Preset = preset.Podman
}

ramSize, ramUse := client.getRAMStatus(vm)
clusterStatusResult.RAMSize = ramSize
clusterStatusResult.RAMUse = ramUse

return clusterStatusResult, nil
}

Expand Down Expand Up @@ -104,3 +109,25 @@ func getOpenShiftStatus(ctx context.Context, ip string) types.OpenshiftStatus {
}
return types.OpenshiftStopped
}

func (client *client) getRAMStatus(vm *virtualMachine) (int64, int64) {
ram, err, _ := client.ramDetails.Memoize("ram", func() (interface{}, error) {
sshRunner, err := vm.SSHRunner()
if err != nil {
return nil, errors.Wrap(err, "Error creating the ssh client")
}
defer sshRunner.Close()
ramSize, ramUse, err := cluster.GetRAMUsage(sshRunner)
if err != nil {
return nil, err
}
return []int64{ramSize, ramUse}, nil
})

if err != nil {
logging.Debugf("Cannot get RAM usage: %v", err)
return 0, 0
}

return ram.([]int64)[0], ram.([]int64)[1]
}
2 changes: 2 additions & 0 deletions pkg/crc/machine/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type ClusterStatusResult struct {
PodmanVersion string
DiskUse int64
DiskSize int64
RAMUse int64
RAMSize int64
Preset crcpreset.Preset
}

Expand Down

0 comments on commit 25d3886

Please sign in to comment.