Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
12843632
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,发现更多精彩内容 >>
提交
12843632
编写于
7月 24, 2015
作者:
S
Shirou WAKAYAMA
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cpu[darwin]: experimental implemtation to get CPU times on darwin.
上级
61cb1210
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
81 addition
and
31 deletion
+81
-31
cpu/cpu_darwin.go
cpu/cpu_darwin.go
+81
-31
未找到文件。
cpu/cpu_darwin.go
浏览文件 @
12843632
...
...
@@ -4,6 +4,7 @@ package cpu
import
(
"fmt"
"io"
"os/exec"
"strconv"
"strings"
...
...
@@ -11,15 +12,7 @@ import (
common
"github.com/shirou/gopsutil/common"
)
// sys/resource.h
const
(
CPUser
=
0
CPNice
=
1
CPSys
=
2
CPIntr
=
3
CPIdle
=
4
CPUStates
=
5
)
const
HELPER_PATH
=
"/tmp/gopsutil_cpu_helper"
var
ClocksPerSec
=
float64
(
128
)
...
...
@@ -32,65 +25,74 @@ func init() {
ClocksPerSec
=
float64
(
i
)
}
}
// adhoc compile on the host. Errors will be ignored.
// gcc is required to compile.
if
!
common
.
PathExists
(
HELPER_PATH
)
{
cmd
:=
exec
.
Command
(
"gcc"
,
"-o"
,
HELPER_PATH
,
"-x"
,
"c"
,
"-"
)
stdin
,
err
:=
cmd
.
StdinPipe
()
if
err
!=
nil
{
return
}
io
.
WriteString
(
stdin
,
cpu_helper_src
)
stdin
.
Close
()
cmd
.
Output
()
}
}
func
CPUTimes
(
percpu
bool
)
([]
CPUTimesStat
,
error
)
{
var
ret
[]
CPUTimesStat
var
sysctlCall
string
var
ncpu
int
if
percpu
{
sysctlCall
=
"kern.cp_times"
ncpu
,
_
=
CPUCounts
(
true
)
}
else
{
sysctlCall
=
"kern.cp_time"
ncpu
=
1
if
!
common
.
PathExists
(
HELPER_PATH
)
{
return
nil
,
fmt
.
Errorf
(
"gopsutil helper(%s) does not exists. gcc required."
,
HELPER_PATH
)
}
cpuTimes
,
err
:=
common
.
DoSysctrl
(
sysctlCall
)
out
,
err
:=
exec
.
Command
(
HELPER_PATH
)
.
Output
(
)
if
err
!=
nil
{
return
ret
,
err
}
for
i
:=
0
;
i
<
ncpu
;
i
++
{
offset
:=
CPUStates
*
i
user
,
err
:=
strconv
.
ParseFloat
(
cpuTimes
[
CPUser
+
offset
],
64
)
for
_
,
line
:=
range
strings
.
Split
(
string
(
out
),
"
\n
"
)
{
f
:=
strings
.
Split
(
string
(
line
),
","
)
if
len
(
f
)
!=
5
{
continue
}
cpu
,
err
:=
strconv
.
ParseFloat
(
f
[
0
],
64
)
if
err
!=
nil
{
return
ret
,
err
}
nice
,
err
:=
strconv
.
ParseFloat
(
cpuTimes
[
CPNice
+
offset
],
64
)
// cpu:99 means total, so just ignore if percpu
if
(
percpu
&&
cpu
==
99
)
||
(
!
percpu
&&
cpu
!=
99
)
{
continue
}
user
,
err
:=
strconv
.
ParseFloat
(
f
[
1
],
64
)
if
err
!=
nil
{
return
ret
,
err
}
sys
,
err
:=
strconv
.
ParseFloat
(
cpuTimes
[
CPSys
+
offset
],
64
)
sys
,
err
:=
strconv
.
ParseFloat
(
f
[
2
],
64
)
if
err
!=
nil
{
return
ret
,
err
}
idle
,
err
:=
strconv
.
ParseFloat
(
cpuTimes
[
CPIdle
+
offset
],
64
)
idle
,
err
:=
strconv
.
ParseFloat
(
f
[
3
],
64
)
if
err
!=
nil
{
return
ret
,
err
}
intr
,
err
:=
strconv
.
ParseFloat
(
cpuTimes
[
CPIntr
+
offset
],
64
)
nice
,
err
:=
strconv
.
ParseFloat
(
f
[
4
],
64
)
if
err
!=
nil
{
return
ret
,
err
}
c
:=
CPUTimesStat
{
User
:
float64
(
user
/
ClocksPerSec
),
Nice
:
float64
(
nice
/
ClocksPerSec
),
System
:
float64
(
sys
/
ClocksPerSec
),
Idle
:
float64
(
idle
/
ClocksPerSec
),
Irq
:
float64
(
intr
/
ClocksPerSec
),
}
if
!
percpu
{
c
.
CPU
=
"cpu-total"
}
else
{
c
.
CPU
=
fmt
.
Sprintf
(
"cpu%d"
,
i
)
c
.
CPU
=
fmt
.
Sprintf
(
"cpu%d"
,
uint16
(
cpu
)
)
}
ret
=
append
(
ret
,
c
)
}
return
ret
,
nil
}
...
...
@@ -155,3 +157,51 @@ func CPUInfo() ([]CPUInfoStat, error) {
return
append
(
ret
,
c
),
nil
}
const
cpu_helper_src
=
`
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <stdio.h>
int main() {
natural_t cpuCount;
processor_info_array_t ia;
mach_msg_type_number_t ic;
kern_return_t error = host_processor_info(mach_host_self(),
PROCESSOR_CPU_LOAD_INFO, &cpuCount, &ia, &ic);
if (error) {
return error;
}
processor_cpu_load_info_data_t* cpuLoadInfo =
(processor_cpu_load_info_data_t*) ia;
unsigned int all[4];
// cpu_no,user,system,idle,nice
for (int cpu=0; cpu<cpuCount; cpu++){
printf("%d,", cpu);
for (int i=0; i<4; i++){
printf("%d", cpuLoadInfo[cpu].cpu_ticks[i]);
all[i] = all[i] + cpuLoadInfo[cpu].cpu_ticks[i];
if (i != 3){
printf(",");
}else{
printf("\n");
}
}
}
printf("99,");
for (int i=0; i<4; i++){
printf("%d", all[i]);
if (i != 3){
printf(",");
}else{
printf("\n");
}
}
vm_deallocate(mach_task_self(), (vm_address_t)ia, ic);
return error;
}
`
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录