mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-24 13:48:56 +08:00
[mem][linux]: add ExLinux on mem package and move VirtualMemoryEx info on it
This commit is contained in:
parent
989328f334
commit
930a873984
40
mem/ex_linux.go
Normal file
40
mem/ex_linux.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
//go:build linux
|
||||||
|
|
||||||
|
package mem
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExVirtualMemory struct {
|
||||||
|
ActiveFile uint64 `json:"activefile"`
|
||||||
|
InactiveFile uint64 `json:"inactivefile"`
|
||||||
|
ActiveAnon uint64 `json:"activeanon"`
|
||||||
|
InactiveAnon uint64 `json:"inactiveanon"`
|
||||||
|
Unevictable uint64 `json:"unevictable"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v ExVirtualMemory) String() string {
|
||||||
|
s, _ := json.Marshal(v)
|
||||||
|
return string(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExLinux struct{}
|
||||||
|
|
||||||
|
func NewExLinux() *ExLinux {
|
||||||
|
return &ExLinux{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex *ExLinux) VirtualMemory() (*ExVirtualMemory, error) {
|
||||||
|
return ex.VirtualMemoryWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ex *ExLinux) VirtualMemoryWithContext(ctx context.Context) (*ExVirtualMemory, error) {
|
||||||
|
_, vmEx, err := fillFromMeminfoWithContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return vmEx, nil
|
||||||
|
}
|
@ -6,7 +6,6 @@ package mem
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
@ -19,19 +18,6 @@ import (
|
|||||||
"github.com/shirou/gopsutil/v4/internal/common"
|
"github.com/shirou/gopsutil/v4/internal/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VirtualMemoryExStat struct {
|
|
||||||
ActiveFile uint64 `json:"activefile"`
|
|
||||||
InactiveFile uint64 `json:"inactivefile"`
|
|
||||||
ActiveAnon uint64 `json:"activeanon"`
|
|
||||||
InactiveAnon uint64 `json:"inactiveanon"`
|
|
||||||
Unevictable uint64 `json:"unevictable"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v VirtualMemoryExStat) String() string {
|
|
||||||
s, _ := json.Marshal(v)
|
|
||||||
return string(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func VirtualMemory() (*VirtualMemoryStat, error) {
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
||||||
return VirtualMemoryWithContext(context.Background())
|
return VirtualMemoryWithContext(context.Background())
|
||||||
}
|
}
|
||||||
@ -44,19 +30,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
|||||||
return vm, nil
|
return vm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func VirtualMemoryEx() (*VirtualMemoryExStat, error) {
|
func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *ExVirtualMemory, error) {
|
||||||
return VirtualMemoryExWithContext(context.Background())
|
|
||||||
}
|
|
||||||
|
|
||||||
func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) {
|
|
||||||
_, vmEx, err := fillFromMeminfoWithContext(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return vmEx, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) {
|
|
||||||
filename := common.HostProcWithContext(ctx, "meminfo")
|
filename := common.HostProcWithContext(ctx, "meminfo")
|
||||||
lines, _ := common.ReadLines(filename)
|
lines, _ := common.ReadLines(filename)
|
||||||
|
|
||||||
@ -67,7 +41,7 @@ func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *Virtu
|
|||||||
sReclaimable := false // "Sreclaimable:" not available: 2.6.19 / Nov 2006
|
sReclaimable := false // "Sreclaimable:" not available: 2.6.19 / Nov 2006
|
||||||
|
|
||||||
ret := &VirtualMemoryStat{}
|
ret := &VirtualMemoryStat{}
|
||||||
retEx := &VirtualMemoryExStat{}
|
retEx := &ExVirtualMemory{}
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
fields := strings.Split(line, ":")
|
fields := strings.Split(line, ":")
|
||||||
@ -409,7 +383,7 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
|||||||
// calculateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
|
// calculateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
|
||||||
// "MemAvailable:" column. It reimplements an algorithm from the link below
|
// "MemAvailable:" column. It reimplements an algorithm from the link below
|
||||||
// https://github.com/giampaolo/psutil/pull/890
|
// https://github.com/giampaolo/psutil/pull/890
|
||||||
func calculateAvailVmem(ctx context.Context, ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 {
|
func calculateAvailVmem(ctx context.Context, ret *VirtualMemoryStat, retEx *ExVirtualMemory) uint64 {
|
||||||
var watermarkLow uint64
|
var watermarkLow uint64
|
||||||
|
|
||||||
fn := common.HostProcWithContext(ctx, "zoneinfo")
|
fn := common.HostProcWithContext(ctx, "zoneinfo")
|
||||||
|
@ -13,8 +13,10 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestVirtualMemoryEx(t *testing.T) {
|
func TestExVirtualMemory(t *testing.T) {
|
||||||
v, err := VirtualMemoryEx()
|
ex := NewExLinux()
|
||||||
|
|
||||||
|
v, err := ex.VirtualMemory()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user