Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
c3899894
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,发现更多精彩内容 >>
提交
c3899894
编写于
6月 01, 2016
作者:
B
Ben Aldrich
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
calculate cpu percentage compared to last call if 0 interval is given
上级
bae75faa
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
117 addition
and
30 deletion
+117
-30
cpu/cpu.go
cpu/cpu.go
+16
-2
cpu/cpu_test.go
cpu/cpu_test.go
+42
-0
cpu/cpu_unix.go
cpu/cpu_unix.go
+59
-28
未找到文件。
cpu/cpu.go
浏览文件 @
c3899894
...
...
@@ -5,6 +5,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
)
type
TimesStat
struct
{
...
...
@@ -37,8 +38,21 @@ type InfoStat struct {
Flags
[]
string
`json:"flags"`
}
var
lastCPUTimes
[]
TimesStat
var
lastPerCPUTimes
[]
TimesStat
//CPUPercent
type
cpuPercent
struct
{
sync
.
Mutex
lastCPUTimes
[]
TimesStat
lastPerCPUTimes
[]
TimesStat
}
var
lastCPUPercent
cpuPercent
func
init
()
{
lastCPUPercent
.
Lock
()
lastCPUPercent
.
lastCPUTimes
,
_
=
Times
(
false
)
lastCPUPercent
.
lastPerCPUTimes
,
_
=
Times
(
true
)
lastCPUPercent
.
Unlock
()
}
func
Counts
(
logical
bool
)
(
int
,
error
)
{
return
runtime
.
NumCPU
(),
nil
...
...
cpu/cpu_test.go
浏览文件 @
c3899894
...
...
@@ -94,6 +94,40 @@ func testCPUPercent(t *testing.T, percpu bool) {
}
}
func
testCPUPercentLastUsed
(
t
*
testing
.
T
,
percpu
bool
)
{
numcpu
:=
runtime
.
NumCPU
()
testCount
:=
10
if
runtime
.
GOOS
!=
"windows"
{
testCount
=
2
v
,
err
:=
Percent
(
time
.
Millisecond
,
percpu
)
if
err
!=
nil
{
t
.
Errorf
(
"error %v"
,
err
)
}
// Skip CircleCI which CPU num is different
if
os
.
Getenv
(
"CIRCLECI"
)
!=
"true"
{
if
(
percpu
&&
len
(
v
)
!=
numcpu
)
||
(
!
percpu
&&
len
(
v
)
!=
1
)
{
t
.
Fatalf
(
"wrong number of entries from CPUPercent: %v"
,
v
)
}
}
}
for
i
:=
0
;
i
<
testCount
;
i
++
{
v
,
err
:=
Percent
(
0
,
percpu
)
if
err
!=
nil
{
t
.
Errorf
(
"error %v"
,
err
)
}
time
.
Sleep
(
1
*
time
.
Millisecond
)
for
_
,
percent
:=
range
v
{
// Check for slightly greater then 100% to account for any rounding issues.
if
percent
<
0.0
||
percent
>
100.0001
*
float64
(
numcpu
)
{
t
.
Fatalf
(
"CPUPercent value is invalid: %f"
,
percent
)
}
}
}
}
func
TestCPUPercent
(
t
*
testing
.
T
)
{
testCPUPercent
(
t
,
false
)
}
...
...
@@ -101,3 +135,11 @@ func TestCPUPercent(t *testing.T) {
func
TestCPUPercentPerCpu
(
t
*
testing
.
T
)
{
testCPUPercent
(
t
,
true
)
}
func
TestCPUPercentIntervalZero
(
t
*
testing
.
T
)
{
testCPUPercentLastUsed
(
t
,
false
)
}
func
TestCPUPercentIntervalZeroPerCPU
(
t
*
testing
.
T
)
{
testCPUPercentLastUsed
(
t
,
true
)
}
cpu/cpu_unix.go
浏览文件 @
c3899894
...
...
@@ -7,24 +7,46 @@ import (
"time"
)
func
Percent
(
interval
time
.
Duration
,
percpu
bool
)
([]
float64
,
error
)
{
getAllBusy
:=
func
(
t
TimesStat
)
(
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
func
getAllBusy
(
t
TimesStat
)
(
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
}
func
calculateBusy
(
t1
,
t2
TimesStat
)
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
}
func
calcualteALLBusy
(
t1
,
t2
[]
TimesStat
)
([]
float64
,
error
)
{
// Make sure the CPU measurements have the same length.
if
len
(
t1
)
!=
len
(
t2
)
{
return
nil
,
fmt
.
Errorf
(
"received two CPU counts: %d != %d"
,
len
(
t1
),
len
(
t2
),
)
}
calculate
:=
func
(
t1
,
t2
TimesStat
)
float64
{
t1All
,
t1Busy
:=
getAllBusy
(
t1
)
t2All
,
t2Busy
:=
getAllBusy
(
t2
)
ret
:=
make
([]
float64
,
len
(
t1
))
for
i
,
t
:=
range
t2
{
ret
[
i
]
=
calculateBusy
(
t1
[
i
],
t
)
}
return
ret
,
nil
}
if
t2Busy
<=
t1Busy
{
return
0
}
if
t2All
<=
t1All
{
return
1
}
return
(
t2Busy
-
t1Busy
)
/
(
t2All
-
t1All
)
*
100
//Percent calculates the percentage of cpu used either per CPU or combined.
//If an interval of 0 is given it will compare the current cpu times against the last call.
func
Percent
(
interval
time
.
Duration
,
percpu
bool
)
([]
float64
,
error
)
{
if
interval
<=
0
{
return
percentUsedFromLastCall
(
percpu
)
}
// Get CPU usage at the start of the interval.
...
...
@@ -33,9 +55,7 @@ func Percent(interval time.Duration, percpu bool) ([]float64, error) {
return
nil
,
err
}
if
interval
>
0
{
time
.
Sleep
(
interval
)
}
time
.
Sleep
(
interval
)
// And at the end of the interval.
cpuTimes2
,
err
:=
Times
(
percpu
)
...
...
@@ -43,17 +63,28 @@ func Percent(interval time.Duration, percpu bool) ([]float64, error) {
return
nil
,
err
}
// Make sure the CPU measurements have the same length.
if
len
(
cpuTimes1
)
!=
len
(
cpuTimes2
)
{
return
nil
,
fmt
.
Errorf
(
"received two CPU counts: %d != %d"
,
len
(
cpuTimes1
),
len
(
cpuTimes2
),
)
return
calcualteALLBusy
(
cpuTimes1
,
cpuTimes2
)
}
func
percentUsedFromLastCall
(
percpu
bool
)
([]
float64
,
error
)
{
cpuTimes
,
err
:=
Times
(
percpu
)
if
err
!=
nil
{
return
nil
,
err
}
lastCPUPercent
.
Lock
()
defer
lastCPUPercent
.
Unlock
()
var
lastTimes
[]
TimesStat
if
percpu
{
lastTimes
=
lastCPUPercent
.
lastPerCPUTimes
lastCPUPercent
.
lastPerCPUTimes
=
cpuTimes
}
else
{
lastTimes
=
lastCPUPercent
.
lastCPUTimes
lastCPUPercent
.
lastCPUTimes
=
cpuTimes
}
ret
:=
make
([]
float64
,
len
(
cpuTimes1
))
for
i
,
t
:=
range
cpuTimes2
{
ret
[
i
]
=
calculate
(
cpuTimes1
[
i
],
t
)
if
lastTimes
==
nil
{
return
nil
,
fmt
.
Errorf
(
"Error getting times for cpu percent. LastTimes was nil"
)
}
return
ret
,
nil
return
calcualteALLBusy
(
lastTimes
,
cpuTimes
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录