mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-05-01 13:14:33 +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"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
@ -18,6 +19,7 @@ import (
|
|||||||
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
|
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
|
||||||
vcUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
vcUtils "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
"github.com/prometheus/procfs"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,7 +27,7 @@ import (
|
|||||||
//
|
//
|
||||||
// XXX: Increment for every change to the output format
|
// XXX: Increment for every change to the output format
|
||||||
// (meaning any change to the EnvInfo type).
|
// (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
|
// MetaInfo stores information on the format of the output itself
|
||||||
type MetaInfo struct {
|
type MetaInfo struct {
|
||||||
@ -53,6 +55,14 @@ type ImageInfo struct {
|
|||||||
type CPUInfo struct {
|
type CPUInfo struct {
|
||||||
Vendor string
|
Vendor string
|
||||||
Model string
|
Model string
|
||||||
|
CPUs int
|
||||||
|
}
|
||||||
|
|
||||||
|
// MemoryInfo stores host memory details
|
||||||
|
type MemoryInfo struct {
|
||||||
|
Total uint64
|
||||||
|
Free uint64
|
||||||
|
Available uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
// RuntimeConfigInfo stores runtime config details.
|
// RuntimeConfigInfo stores runtime config details.
|
||||||
@ -123,6 +133,7 @@ type HostInfo struct {
|
|||||||
Architecture string
|
Architecture string
|
||||||
Distro DistroInfo
|
Distro DistroInfo
|
||||||
CPU CPUInfo
|
CPU CPUInfo
|
||||||
|
Memory MemoryInfo
|
||||||
VMContainerCapable bool
|
VMContainerCapable bool
|
||||||
SupportVSocks bool
|
SupportVSocks bool
|
||||||
}
|
}
|
||||||
@ -222,15 +233,19 @@ func getHostInfo() (HostInfo, error) {
|
|||||||
hostCPU := CPUInfo{
|
hostCPU := CPUInfo{
|
||||||
Vendor: cpuVendor,
|
Vendor: cpuVendor,
|
||||||
Model: cpuModel,
|
Model: cpuModel,
|
||||||
|
CPUs: runtime.NumCPU(),
|
||||||
}
|
}
|
||||||
|
|
||||||
supportVSocks, _ := vcUtils.SupportsVsocks()
|
supportVSocks, _ := vcUtils.SupportsVsocks()
|
||||||
|
|
||||||
|
memoryInfo := getMemoryInfo()
|
||||||
|
|
||||||
host := HostInfo{
|
host := HostInfo{
|
||||||
Kernel: hostKernelVersion,
|
Kernel: hostKernelVersion,
|
||||||
Architecture: arch,
|
Architecture: arch,
|
||||||
Distro: hostDistro,
|
Distro: hostDistro,
|
||||||
CPU: hostCPU,
|
CPU: hostCPU,
|
||||||
|
Memory: memoryInfo,
|
||||||
VMContainerCapable: hostVMContainerCapable,
|
VMContainerCapable: hostVMContainerCapable,
|
||||||
SupportVSocks: supportVSocks,
|
SupportVSocks: supportVSocks,
|
||||||
}
|
}
|
||||||
@ -238,6 +253,24 @@ func getHostInfo() (HostInfo, error) {
|
|||||||
return host, nil
|
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 {
|
func getNetmonInfo(config oci.RuntimeConfig) NetmonInfo {
|
||||||
netmonConfig := config.NetmonConfig
|
netmonConfig := config.NetmonConfig
|
||||||
|
|
||||||
|
@ -50,6 +50,9 @@ func TestEnvGetEnvInfoSetsCPUType(t *testing.T) {
|
|||||||
env, err := getEnvInfo(configFile, config)
|
env, err := getEnvInfo(configFile, config)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
// Free/Available are changing
|
||||||
|
expectedEnv.Host.Memory = env.Host.Memory
|
||||||
|
|
||||||
assert.Equal(expectedEnv, env)
|
assert.Equal(expectedEnv, env)
|
||||||
|
|
||||||
assert.NotEmpty(archRequiredCPUFlags)
|
assert.NotEmpty(archRequiredCPUFlags)
|
||||||
|
@ -45,6 +45,9 @@ func testEnvGetEnvInfoSetsCPUTypeGeneric(t *testing.T) {
|
|||||||
env, err := getEnvInfo(configFile, config)
|
env, err := getEnvInfo(configFile, config)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
// Free/Available are changing
|
||||||
|
expectedEnv.Host.Memory = env.Host.Memory
|
||||||
|
|
||||||
assert.Equal(expectedEnv, env)
|
assert.Equal(expectedEnv, env)
|
||||||
|
|
||||||
assert.Equal(archRequiredCPUFlags, savedArchRequiredCPUFlags)
|
assert.Equal(archRequiredCPUFlags, savedArchRequiredCPUFlags)
|
||||||
|
@ -14,6 +14,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
goruntime "runtime"
|
goruntime "runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -272,6 +273,10 @@ VERSION_ID="%s"
|
|||||||
expectedHostDetails.CPU.Model = "v8"
|
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
|
return expectedHostDetails, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,7 +396,16 @@ func TestEnvGetHostInfo(t *testing.T) {
|
|||||||
host, err := getHostInfo()
|
host, err := getHostInfo()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Free/Available are changing
|
||||||
|
expectedHostDetails.Memory = host.Memory
|
||||||
|
|
||||||
assert.Equal(t, expectedHostDetails, host)
|
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) {
|
func TestEnvGetHostInfoNoProcCPUInfo(t *testing.T) {
|
||||||
@ -470,6 +484,9 @@ func TestEnvGetEnvInfo(t *testing.T) {
|
|||||||
env, err := getEnvInfo(configFile, config)
|
env, err := getEnvInfo(configFile, config)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Free/Available are changing
|
||||||
|
expectedEnv.Host.Memory = env.Host.Memory
|
||||||
|
|
||||||
assert.Equal(t, expectedEnv, env)
|
assert.Equal(t, expectedEnv, env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -495,6 +512,9 @@ func TestEnvGetEnvInfoNoHypervisorVersion(t *testing.T) {
|
|||||||
env, err := getEnvInfo(configFile, config)
|
env, err := getEnvInfo(configFile, config)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
|
// Free/Available are changing
|
||||||
|
expectedEnv.Host.Memory = env.Host.Memory
|
||||||
|
|
||||||
assert.Equal(expectedEnv, env)
|
assert.Equal(expectedEnv, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user