mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 07:22:20 +00:00
utils: Add memory unit abstraction
Add MemUnit to help to manage memory, this will handle memory units internally and provide proper methods to convert to different units. Signed-off-by: Jose Carlos Venegas Munoz <jose.carlos.venegas.munoz@intel.com>
This commit is contained in:
parent
5e7d253859
commit
b6a7d8d63a
@ -240,3 +240,38 @@ func SupportsVsocks() bool {
|
||||
var StartCmd = func(c *exec.Cmd) error {
|
||||
return c.Start()
|
||||
}
|
||||
|
||||
// AlignMem align memory provided to a block size
|
||||
func (m MemUnit) AlignMem(blockSize MemUnit) MemUnit {
|
||||
memSize := m
|
||||
if m < blockSize {
|
||||
memSize = blockSize
|
||||
|
||||
}
|
||||
|
||||
remainder := memSize % blockSize
|
||||
|
||||
if remainder != 0 {
|
||||
// Align memory to memoryBlockSizeMB
|
||||
memSize += blockSize - remainder
|
||||
|
||||
}
|
||||
return memSize
|
||||
}
|
||||
|
||||
type MemUnit uint64
|
||||
|
||||
func (m MemUnit) ToMiB() uint64 {
|
||||
return m.ToBytes() / (1 * MiB).ToBytes()
|
||||
}
|
||||
|
||||
func (m MemUnit) ToBytes() uint64 {
|
||||
return uint64(m)
|
||||
}
|
||||
|
||||
const (
|
||||
Byte MemUnit = 1
|
||||
KiB = Byte << 10
|
||||
MiB = KiB << 10
|
||||
GiB = MiB << 10
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user