Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
1d09f7a4
G
Gopsutil
项目概览
Canread
/
Gopsutil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gopsutil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1d09f7a4
编写于
2月 22, 2016
作者:
S
shirou
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #160 from walles/walles/vmtest
Document + add tests for for mem.VirtualMemory()
上级
e04a02d2
dd875d49
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
96 addition
and
35 deletion
+96
-35
mem/mem.go
mem/mem.go
+36
-10
mem/mem_darwin.go
mem/mem_darwin.go
+17
-0
mem/mem_darwin_cgo.go
mem/mem_darwin_cgo.go
+8
-7
mem/mem_darwin_nocgo.go
mem/mem_darwin_nocgo.go
+3
-9
mem/mem_darwin_test.go
mem/mem_darwin_test.go
+11
-5
mem/mem_freebsd.go
mem/mem_freebsd.go
+2
-2
mem/mem_linux.go
mem/mem_linux.go
+1
-1
mem/mem_test.go
mem/mem_test.go
+18
-1
未找到文件。
mem/mem.go
浏览文件 @
1d09f7a4
...
...
@@ -4,18 +4,44 @@ import (
"encoding/json"
)
// Memory usage statistics. Total, Available and Used contain numbers of bytes
// for human consumption.
//
// The other fields in this struct contain kernel specific values.
type
VirtualMemoryStat
struct
{
Total
uint64
`json:"total"`
Available
uint64
`json:"available"`
Used
uint64
`json:"used"`
// Total amount of RAM on this system
Total
uint64
`json:"total"`
// RAM available for programs to allocate
//
// This value is computed from the kernel specific values.
Available
uint64
`json:"available"`
// RAM used by programs
//
// This value is computed from the kernel specific values.
Used
uint64
`json:"used"`
// Percentage of RAM used by programs
//
// This value is computed from the kernel specific values.
UsedPercent
float64
`json:"used_percent"`
Free
uint64
`json:"free"`
Active
uint64
`json:"active"`
Inactive
uint64
`json:"inactive"`
Buffers
uint64
`json:"buffers"`
Cached
uint64
`json:"cached"`
Wired
uint64
`json:"wired"`
Shared
uint64
`json:"shared"`
// This is the kernel's notion of free memory; RAM chips whose bits nobody
// cares about the value of right now. For a human consumable number,
// Available is what you really want.
Free
uint64
`json:"free"`
// OS X / BSD specific numbers:
// http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/
Active
uint64
`json:"active"`
Inactive
uint64
`json:"inactive"`
Wired
uint64
`json:"wired"`
// Linux specific numbers
// https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
Buffers
uint64
`json:"buffers"`
Cached
uint64
`json:"cached"`
}
type
SwapMemoryStat
struct
{
...
...
mem/mem_darwin.go
浏览文件 @
1d09f7a4
...
...
@@ -3,12 +3,29 @@
package
mem
import
(
"encoding/binary"
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
func
getHwMemsize
()
(
uint64
,
error
)
{
totalString
,
err
:=
syscall
.
Sysctl
(
"hw.memsize"
)
if
err
!=
nil
{
return
0
,
err
}
// syscall.sysctl() helpfully assumes the result is a null-terminated string and
// removes the last byte of the result if it's 0 :/
totalString
+=
"
\x00
"
total
:=
uint64
(
binary
.
LittleEndian
.
Uint64
([]
byte
(
totalString
)))
return
total
,
nil
}
// SwapMemory returns swapinfo.
func
SwapMemory
()
(
*
SwapMemoryStat
,
error
)
{
var
ret
*
SwapMemoryStat
...
...
mem/mem_darwin_cgo.go
浏览文件 @
1d09f7a4
...
...
@@ -28,19 +28,20 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
return
nil
,
fmt
.
Errorf
(
"host_statistics error=%d"
,
status
)
}
totalCount
:=
vmstat
.
wire_count
+
vmstat
.
active_count
+
vmstat
.
inactive_count
+
vmstat
.
free_count
pageSize
:=
uint64
(
syscall
.
Getpagesize
())
total
,
err
:=
getHwMemsize
()
if
err
!=
nil
{
return
nil
,
err
}
totalCount
:=
C
.
natural_t
(
total
/
pageSize
)
availableCount
:=
vmstat
.
inactive_count
+
vmstat
.
free_count
usedPercent
:=
100
*
float64
(
totalCount
-
availableCount
)
/
float64
(
totalCount
)
usedCount
:=
totalCount
-
vmstat
.
free_c
ount
usedCount
:=
totalCount
-
availableC
ount
pageSize
:=
uint64
(
syscall
.
Getpagesize
())
return
&
VirtualMemoryStat
{
Total
:
pageSize
*
uint64
(
totalCount
)
,
Total
:
total
,
Available
:
pageSize
*
uint64
(
availableCount
),
Used
:
pageSize
*
uint64
(
usedCount
),
UsedPercent
:
usedPercent
,
...
...
mem/mem_darwin_nocgo.go
浏览文件 @
1d09f7a4
...
...
@@ -8,8 +8,6 @@ import (
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
// Runs vm_stat and returns Free and inactive pages
...
...
@@ -67,11 +65,7 @@ func parseVMStat(out string, vms *VirtualMemoryStat) error {
func
VirtualMemory
()
(
*
VirtualMemoryStat
,
error
)
{
ret
:=
&
VirtualMemoryStat
{}
t
,
err
:=
common
.
DoSysctrl
(
"hw.memsize"
)
if
err
!=
nil
{
return
nil
,
err
}
total
,
err
:=
strconv
.
ParseUint
(
t
[
0
],
10
,
64
)
total
,
err
:=
getHwMemsize
()
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -83,8 +77,8 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
ret
.
Available
=
ret
.
Free
+
ret
.
Inactive
ret
.
Total
=
total
ret
.
Used
=
ret
.
Total
-
ret
.
Fre
e
ret
.
UsedPercent
=
float64
(
ret
.
Total
-
ret
.
Available
)
/
float64
(
ret
.
Total
)
*
100.0
ret
.
Used
=
ret
.
Total
-
ret
.
Availabl
e
ret
.
UsedPercent
=
100
*
float64
(
ret
.
Used
)
/
float64
(
ret
.
Total
)
return
ret
,
nil
}
mem/mem_darwin_test.go
浏览文件 @
1d09f7a4
...
...
@@ -3,6 +3,9 @@
package
mem
import
(
"os/exec"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
...
...
@@ -12,13 +15,16 @@ func TestVirtualMemoryDarwin(t *testing.T) {
v
,
err
:=
VirtualMemory
()
assert
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
v
.
Total
>
0
)
assert
.
Equal
(
t
,
v
.
Total
,
v
.
Active
+
v
.
Inactive
+
v
.
Free
+
v
.
Wired
)
outBytes
,
err
:=
exec
.
Command
(
"/usr/sbin/sysctl"
,
"hw.memsize"
)
.
Output
()
assert
.
Nil
(
t
,
err
)
outString
:=
string
(
outBytes
)
outString
=
strings
.
TrimSpace
(
outString
)
outParts
:=
strings
.
Split
(
outString
,
" "
)
actualTotal
,
err
:=
strconv
.
ParseInt
(
outParts
[
1
],
10
,
64
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
uint64
(
actualTotal
),
v
.
Total
)
assert
.
True
(
t
,
v
.
Available
>
0
)
assert
.
True
(
t
,
v
.
Available
<
v
.
Total
)
assert
.
Equal
(
t
,
v
.
Available
,
v
.
Total
-
v
.
Wired
-
v
.
Active
,
"%v"
,
v
)
assert
.
Equal
(
t
,
v
.
Available
,
v
.
Free
+
v
.
Inactive
,
"%v"
,
v
)
assert
.
True
(
t
,
v
.
Used
>
0
)
...
...
mem/mem_freebsd.go
浏览文件 @
1d09f7a4
...
...
@@ -79,8 +79,8 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
}
ret
.
Available
=
ret
.
Inactive
+
ret
.
Cached
+
ret
.
Free
ret
.
Used
=
ret
.
Active
+
ret
.
Wired
+
ret
.
Cached
ret
.
UsedPercent
=
float64
(
ret
.
Total
-
ret
.
Available
)
/
float64
(
ret
.
Total
)
*
100.0
ret
.
Used
=
ret
.
Total
-
ret
.
Available
ret
.
UsedPercent
=
float64
(
ret
.
Used
)
/
float64
(
ret
.
Total
)
*
100.0
return
ret
,
nil
}
...
...
mem/mem_linux.go
浏览文件 @
1d09f7a4
...
...
@@ -51,7 +51,7 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
if
!
memavail
{
ret
.
Available
=
ret
.
Free
+
ret
.
Buffers
+
ret
.
Cached
}
ret
.
Used
=
ret
.
Total
-
ret
.
Fre
e
ret
.
Used
=
ret
.
Total
-
ret
.
Availabl
e
ret
.
UsedPercent
=
float64
(
ret
.
Total
-
ret
.
Available
)
/
float64
(
ret
.
Total
)
*
100.0
return
ret
,
nil
...
...
mem/mem_test.go
浏览文件 @
1d09f7a4
...
...
@@ -3,6 +3,8 @@ package mem
import
(
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func
TestVirtual_memory
(
t
*
testing
.
T
)
{
...
...
@@ -14,6 +16,21 @@ func TestVirtual_memory(t *testing.T) {
if
v
==
empty
{
t
.
Errorf
(
"error %v"
,
v
)
}
assert
.
True
(
t
,
v
.
Total
>
0
)
assert
.
True
(
t
,
v
.
Available
>
0
)
assert
.
True
(
t
,
v
.
Used
>
0
)
assert
.
Equal
(
t
,
v
.
Total
,
v
.
Available
+
v
.
Used
,
"Total should be computable from available + used: %v"
,
v
)
assert
.
True
(
t
,
v
.
Free
>
0
)
assert
.
True
(
t
,
v
.
Available
>
v
.
Free
,
"Free should be a subset of Available: %v"
,
v
)
assert
.
InDelta
(
t
,
v
.
UsedPercent
,
100
*
float64
(
v
.
Used
)
/
float64
(
v
.
Total
),
0.1
,
"UsedPercent should be how many percent of Total is Used: %v"
,
v
)
}
func
TestSwap_memory
(
t
*
testing
.
T
)
{
...
...
@@ -35,7 +52,7 @@ func TestVirtualMemoryStat_String(t *testing.T) {
UsedPercent
:
30.1
,
Free
:
40
,
}
e
:=
`{"total":10,"available":20,"used":30,"used_percent":30.1,"free":40,"active":0,"inactive":0,"
buffers":0,"cached":0,"wired":0,"shar
ed":0}`
e
:=
`{"total":10,"available":20,"used":30,"used_percent":30.1,"free":40,"active":0,"inactive":0,"
wired":0,"buffers":0,"cach
ed":0}`
if
e
!=
fmt
.
Sprintf
(
"%v"
,
v
)
{
t
.
Errorf
(
"VirtualMemoryStat string is invalid: %v"
,
v
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录