232.【go 语言】PProf 的使用——CPU和内存占用分析(二)

Python036

232.【go 语言】PProf 的使用——CPU和内存占用分析(二),第1张

项目更目录下执行 go tool pprof http://127.0.0.1:2022/debug/pprof/profile?seconds=30 ,结束之后会默认进入 PProf 的命令行交互模式,接着输入 top 10 ,如下图,

项目根目录下执行 go tool pprof http://127.0.0.1:2022/debug/pprof/heap ,结束之后会默认进入 PProf 的命令行交互模式,接着输入 top ,如图所示,

上面可以看到, main.main.fun1 的 cum 大小正好等于自身的 flat 大小加上 main.Add 大小的 flat 大小

Windows7系统获取CPU使用率有两种方法:

1.通过任务管理器。

2.通过电脑软件鲁大师。

方法一:

1.按Ctrl+Alt+Delete热启,选择启动任务管理器(此处无法截图)。

2.切换到性能面板,观察CPU使用率。

方法二:

1.找到电脑桌面上的鲁大师图标,点击鼠标右键,选择以管理员身份运行(电脑上必须事先安装好鲁大师才能选择)。

2.切换到温度管理面板,观察CPU使用。

得到cpu占有率的API函数:

GetSystemTimes

得到内存使用情况的API函数:

GlobalMemoryStatusEx Function

Retrieves information about the system's current usage of both physical and virtual memory.

GetPerformanceInfo Function

Retrieves the performance values contained in the PERFORMANCE_INFORMATION structure

获取特定程序的内存使用情况用:

GetProcessMemoryInfo Function

Retrieves information about the memory usage of the specified process.

#define _WIN32_WINNT   0x0501

#include <Windows.h>

#include <iostream>

using   namespace   std

__int64 CompareFileTime ( FILETIME time1, FILETIME time2 )

{

__int64 a = time1.dwHighDateTime <<32 | time1.dwLowDateTime

__int64 b = time2.dwHighDateTime <<32 | time2.dwLowDateTime

return   (b - a)

}

void main()

{

HANDLE hEvent

BOOL res

FILETIME preidleTime

FILETIME prekernelTime

FILETIME preuserTime

FILETIME idleTime

FILETIME kernelTime

FILETIME userTime

res = GetSystemTimes( &idleTime, &kernelTime, &userTime )

preidleTime = idleTime

prekernelTime = kernelTime

preuserTime = userTime

hEvent = CreateEvent (NULL,FALSE,FALSE,NULL)// 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled

while (1){

WaitForSingleObject( hEvent,1000 )//等待500毫秒

res = GetSystemTimes( &idleTime, &kernelTime, &userTime )

int idle = CompareFileTime( preidleTime,idleTime)

int kernel = CompareFileTime( prekernelTime, kernelTime)

int user = CompareFileTime(preuserTime, userTime)

int cpu = (kernel +user - idle) *100/(kernel+user)

int cpuidle = ( idle) *100/(kernel+user)

cout <<"CPU利用率:" <<cpu <<"%" <<"      CPU空闲率:" <<cpuidle <<"%" <<endl

preidleTime = idleTime

prekernelTime = kernelTime

preuserTime = userTime

}

}