mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-29 20:24:31 +00:00
Merge pull request #457 from liubin/feature/405-add-memory-info-for-kata-env
runtime: add CPU cores and memory basic info for `kata-env` sub-command
This commit is contained in:
commit
6fc7d4b238
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user