Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
ead83931
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,发现更多精彩内容 >>
提交
ead83931
编写于
3月 11, 2015
作者:
W
WAKAYAMA Shirou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cpu: use wmic to get CPU usage on Windows.
上级
1851e63d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
119 addition
and
55 deletion
+119
-55
cpu/cpu.go
cpu/cpu.go
+2
-54
cpu/cpu_test.go
cpu/cpu_test.go
+4
-1
cpu/cpu_unix.go
cpu/cpu_unix.go
+90
-0
cpu/cpu_windows.go
cpu/cpu_windows.go
+23
-0
未找到文件。
cpu/cpu.go
浏览文件 @
ead83931
...
...
@@ -5,7 +5,6 @@ import (
"runtime"
"strconv"
"strings"
"time"
)
type
CPUTimesStat
struct
{
...
...
@@ -38,62 +37,11 @@ type CPUInfoStat struct {
Flags
[]
string
`json:"flags"`
}
func
CPUCounts
(
logical
bool
)
(
int
,
error
)
{
return
runtime
.
NumCPU
(),
nil
}
var
lastCPUTimes
[]
CPUTimesStat
var
lastPerCPUTimes
[]
CPUTimesStat
func
CPUPercent
(
interval
time
.
Duration
,
percpu
bool
)
([]
float64
,
error
)
{
getAllBusy
:=
func
(
t
CPUTimesStat
)
(
float64
,
float64
)
{
busy
:=
t
.
User
+
t
.
System
+
t
.
Nice
+
t
.
Iowait
+
t
.
Irq
+
t
.
Softirq
+
t
.
Steal
+
t
.
Guest
+
t
.
GuestNice
+
t
.
Stolen
return
busy
+
t
.
Idle
,
busy
}
calculate
:=
func
(
t1
,
t2
CPUTimesStat
)
float64
{
t1All
,
t1Busy
:=
getAllBusy
(
t1
)
t2All
,
t2Busy
:=
getAllBusy
(
t2
)
if
t2Busy
<=
t1Busy
{
return
0
}
if
t2All
<=
t1All
{
return
1
}
return
(
t2Busy
-
t1Busy
)
/
(
t2All
-
t1All
)
*
100
}
cpuTimes
,
err
:=
CPUTimes
(
percpu
)
if
err
!=
nil
{
return
nil
,
err
}
if
interval
>
0
{
if
!
percpu
{
lastCPUTimes
=
cpuTimes
}
else
{
lastPerCPUTimes
=
cpuTimes
}
time
.
Sleep
(
interval
)
cpuTimes
,
err
=
CPUTimes
(
percpu
)
if
err
!=
nil
{
return
nil
,
err
}
}
ret
:=
make
([]
float64
,
len
(
cpuTimes
))
if
!
percpu
{
ret
[
0
]
=
calculate
(
lastCPUTimes
[
0
],
cpuTimes
[
0
])
lastCPUTimes
=
cpuTimes
}
else
{
for
i
,
t
:=
range
cpuTimes
{
ret
[
i
]
=
calculate
(
lastPerCPUTimes
[
i
],
t
)
}
lastPerCPUTimes
=
cpuTimes
}
return
ret
,
nil
func
CPUCounts
(
logical
bool
)
(
int
,
error
)
{
return
runtime
.
NumCPU
(),
nil
}
func
(
c
CPUTimesStat
)
String
()
string
{
...
...
cpu/cpu_test.go
浏览文件 @
ead83931
...
...
@@ -63,7 +63,10 @@ func TestCpuInfo(t *testing.T) {
func
testCPUPercent
(
t
*
testing
.
T
,
percpu
bool
)
{
numcpu
:=
runtime
.
NumCPU
()
testCount
:=
3
if
runtime
.
GOOS
!=
"windows"
{
testCount
=
100
v
,
err
:=
CPUPercent
(
time
.
Millisecond
,
percpu
)
if
err
!=
nil
{
t
.
Errorf
(
"error %v"
,
err
)
...
...
@@ -72,7 +75,7 @@ func testCPUPercent(t *testing.T, percpu bool) {
t
.
Fatalf
(
"wrong number of entries from CPUPercent: %v"
,
v
)
}
}
for
i
:=
0
;
i
<
100
;
i
++
{
for
i
:=
0
;
i
<
testCount
;
i
++
{
duration
:=
time
.
Duration
(
10
)
*
time
.
Microsecond
v
,
err
:=
CPUPercent
(
duration
,
percpu
)
if
err
!=
nil
{
...
...
cpu/cpu_unix.go
0 → 100644
浏览文件 @
ead83931
// +build linux,freebsd,darwin
package
cpu
import
(
"encoding/json"
"strconv"
"strings"
"time"
)
func
CPUPercent
(
interval
time
.
Duration
,
percpu
bool
)
([]
float64
,
error
)
{
getAllBusy
:=
func
(
t
CPUTimesStat
)
(
float64
,
float64
)
{
busy
:=
t
.
User
+
t
.
System
+
t
.
Nice
+
t
.
Iowait
+
t
.
Irq
+
t
.
Softirq
+
t
.
Steal
+
t
.
Guest
+
t
.
GuestNice
+
t
.
Stolen
return
busy
+
t
.
Idle
,
busy
}
calculate
:=
func
(
t1
,
t2
CPUTimesStat
)
float64
{
t1All
,
t1Busy
:=
getAllBusy
(
t1
)
t2All
,
t2Busy
:=
getAllBusy
(
t2
)
if
t2Busy
<=
t1Busy
{
return
0
}
if
t2All
<=
t1All
{
return
1
}
return
(
t2Busy
-
t1Busy
)
/
(
t2All
-
t1All
)
*
100
}
cpuTimes
,
err
:=
CPUTimes
(
percpu
)
if
err
!=
nil
{
return
nil
,
err
}
if
interval
>
0
{
if
!
percpu
{
lastCPUTimes
=
cpuTimes
}
else
{
lastPerCPUTimes
=
cpuTimes
}
time
.
Sleep
(
interval
)
cpuTimes
,
err
=
CPUTimes
(
percpu
)
if
err
!=
nil
{
return
nil
,
err
}
}
ret
:=
make
([]
float64
,
len
(
cpuTimes
))
if
!
percpu
{
ret
[
0
]
=
calculate
(
lastCPUTimes
[
0
],
cpuTimes
[
0
])
lastCPUTimes
=
cpuTimes
}
else
{
for
i
,
t
:=
range
cpuTimes
{
ret
[
i
]
=
calculate
(
lastPerCPUTimes
[
i
],
t
)
}
lastPerCPUTimes
=
cpuTimes
}
return
ret
,
nil
}
func
(
c
CPUTimesStat
)
String
()
string
{
v
:=
[]
string
{
`"cpu":"`
+
c
.
CPU
+
`"`
,
`"user":`
+
strconv
.
FormatFloat
(
c
.
User
,
'f'
,
1
,
64
),
`"system":`
+
strconv
.
FormatFloat
(
c
.
System
,
'f'
,
1
,
64
),
`"idle":`
+
strconv
.
FormatFloat
(
c
.
Idle
,
'f'
,
1
,
64
),
`"nice":`
+
strconv
.
FormatFloat
(
c
.
Nice
,
'f'
,
1
,
64
),
`"iowait":`
+
strconv
.
FormatFloat
(
c
.
Iowait
,
'f'
,
1
,
64
),
`"irq":`
+
strconv
.
FormatFloat
(
c
.
Irq
,
'f'
,
1
,
64
),
`"softirq":`
+
strconv
.
FormatFloat
(
c
.
Softirq
,
'f'
,
1
,
64
),
`"steal":`
+
strconv
.
FormatFloat
(
c
.
Steal
,
'f'
,
1
,
64
),
`"guest":`
+
strconv
.
FormatFloat
(
c
.
Guest
,
'f'
,
1
,
64
),
`"guest_nice":`
+
strconv
.
FormatFloat
(
c
.
GuestNice
,
'f'
,
1
,
64
),
`"stolen":`
+
strconv
.
FormatFloat
(
c
.
Stolen
,
'f'
,
1
,
64
),
}
return
`{`
+
strings
.
Join
(
v
,
","
)
+
`}`
}
func
(
c
CPUInfoStat
)
String
()
string
{
s
,
_
:=
json
.
Marshal
(
c
)
return
string
(
s
)
}
func
init
()
{
lastCPUTimes
,
_
=
CPUTimes
(
false
)
lastPerCPUTimes
,
_
=
CPUTimes
(
true
)
}
cpu/cpu_windows.go
浏览文件 @
ead83931
...
...
@@ -6,6 +6,7 @@ import (
"strconv"
"strings"
"syscall"
"time"
"unsafe"
common
"github.com/shirou/gopsutil/common"
...
...
@@ -79,3 +80,25 @@ func CPUInfo() ([]CPUInfoStat, error) {
}
return
ret
,
nil
}
func
CPUPercent
(
interval
time
.
Duration
,
percpu
bool
)
([]
float64
,
error
)
{
ret
:=
[]
float64
{}
lines
,
err
:=
common
.
GetWmic
(
"cpu"
,
"loadpercentage"
)
if
err
!=
nil
{
return
ret
,
err
}
for
_
,
l
:=
range
lines
{
t
:=
strings
.
Split
(
l
,
","
)
if
len
(
t
)
<
2
{
continue
}
p
,
err
:=
strconv
.
Atoi
(
t
[
1
])
if
err
!=
nil
{
p
=
0
}
ret
=
append
(
ret
,
float64
(
p
)
/
100.0
)
}
return
ret
,
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录