diff --git a/src/runtime/cli/kata-env.go b/src/runtime/cli/kata-env.go index 6c8ce35e63..0c555ed311 100644 --- a/src/runtime/cli/kata-env.go +++ b/src/runtime/cli/kata-env.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "os" + "runtime" "strings" "github.com/BurntSushi/toml" @@ -18,6 +19,7 @@ import ( "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci" vcUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" specs "github.com/opencontainers/runtime-spec/specs-go" + "github.com/prometheus/procfs" "github.com/urfave/cli" ) @@ -25,7 +27,7 @@ import ( // // XXX: Increment for every change to the output format // (meaning any change to the EnvInfo type). -const formatVersion = "1.0.24" +const formatVersion = "1.0.25" // MetaInfo stores information on the format of the output itself type MetaInfo struct { @@ -53,6 +55,14 @@ type ImageInfo struct { type CPUInfo struct { Vendor string Model string + CPUs int +} + +// MemoryInfo stores host memory details +type MemoryInfo struct { + Total uint64 + Free uint64 + Available uint64 } // RuntimeConfigInfo stores runtime config details. @@ -123,6 +133,7 @@ type HostInfo struct { Architecture string Distro DistroInfo CPU CPUInfo + Memory MemoryInfo VMContainerCapable bool SupportVSocks bool } @@ -222,15 +233,19 @@ func getHostInfo() (HostInfo, error) { hostCPU := CPUInfo{ Vendor: cpuVendor, Model: cpuModel, + CPUs: runtime.NumCPU(), } supportVSocks, _ := vcUtils.SupportsVsocks() + memoryInfo := getMemoryInfo() + host := HostInfo{ Kernel: hostKernelVersion, Architecture: arch, Distro: hostDistro, CPU: hostCPU, + Memory: memoryInfo, VMContainerCapable: hostVMContainerCapable, SupportVSocks: supportVSocks, } @@ -238,6 +253,24 @@ func getHostInfo() (HostInfo, error) { return host, nil } +func getMemoryInfo() MemoryInfo { + fs, err := procfs.NewDefaultFS() + if err != nil { + return MemoryInfo{} + } + + mi, err := fs.Meminfo() + if err != nil { + return MemoryInfo{} + } + + return MemoryInfo{ + Total: mi.MemTotal, + Free: mi.MemFree, + Available: mi.MemAvailable, + } +} + func getNetmonInfo(config oci.RuntimeConfig) NetmonInfo { netmonConfig := config.NetmonConfig diff --git a/src/runtime/cli/kata-env_amd64_test.go b/src/runtime/cli/kata-env_amd64_test.go index e58763be60..43b9e5c4b2 100644 --- a/src/runtime/cli/kata-env_amd64_test.go +++ b/src/runtime/cli/kata-env_amd64_test.go @@ -50,6 +50,9 @@ func TestEnvGetEnvInfoSetsCPUType(t *testing.T) { env, err := getEnvInfo(configFile, config) assert.NoError(err) + // Free/Available are changing + expectedEnv.Host.Memory = env.Host.Memory + assert.Equal(expectedEnv, env) assert.NotEmpty(archRequiredCPUFlags) diff --git a/src/runtime/cli/kata-env_generic_test.go b/src/runtime/cli/kata-env_generic_test.go index 3077be61b3..df7a98d268 100644 --- a/src/runtime/cli/kata-env_generic_test.go +++ b/src/runtime/cli/kata-env_generic_test.go @@ -45,6 +45,9 @@ func testEnvGetEnvInfoSetsCPUTypeGeneric(t *testing.T) { env, err := getEnvInfo(configFile, config) assert.NoError(err) + // Free/Available are changing + expectedEnv.Host.Memory = env.Host.Memory + assert.Equal(expectedEnv, env) assert.Equal(archRequiredCPUFlags, savedArchRequiredCPUFlags) diff --git a/src/runtime/cli/kata-env_test.go b/src/runtime/cli/kata-env_test.go index 159b105b45..97260f58bb 100644 --- a/src/runtime/cli/kata-env_test.go +++ b/src/runtime/cli/kata-env_test.go @@ -14,6 +14,7 @@ import ( "os" "path" "path/filepath" + "runtime" goruntime "runtime" "strings" "testing" @@ -272,6 +273,10 @@ VERSION_ID="%s" expectedHostDetails.CPU.Model = "v8" } + // set CPU num. + // will not set memory info, because memory may be changed. + expectedHostDetails.CPU.CPUs = runtime.NumCPU() + return expectedHostDetails, nil } @@ -391,7 +396,16 @@ func TestEnvGetHostInfo(t *testing.T) { host, err := getHostInfo() assert.NoError(t, err) + // Free/Available are changing + expectedHostDetails.Memory = host.Memory + assert.Equal(t, expectedHostDetails, host) + + // check CPU cores and memory info + assert.Equal(t, true, host.CPU.CPUs > 0) + assert.Equal(t, true, host.Memory.Total > 0) + assert.Equal(t, true, host.Memory.Free > 0) + assert.Equal(t, true, host.Memory.Available > 0) } func TestEnvGetHostInfoNoProcCPUInfo(t *testing.T) { @@ -470,6 +484,9 @@ func TestEnvGetEnvInfo(t *testing.T) { env, err := getEnvInfo(configFile, config) assert.NoError(t, err) + // Free/Available are changing + expectedEnv.Host.Memory = env.Host.Memory + assert.Equal(t, expectedEnv, env) } } @@ -495,6 +512,9 @@ func TestEnvGetEnvInfoNoHypervisorVersion(t *testing.T) { env, err := getEnvInfo(configFile, config) assert.NoError(err) + // Free/Available are changing + expectedEnv.Host.Memory = env.Host.Memory + assert.Equal(expectedEnv, env) }