关于服务器核心数和CPU占比的信息

关于服务器核心数和CPU占比的信息,第1张

查看系统CPU的相关信息

cat /proc/cpuinfo

然后对照网址中的这个表

>

使用wmi

类“Win32_Processor”中LoadPercentage属性为当前的cpu使用率

示例代码: Private Sub Timer1_Timer()

Dim WMI服务 As Object

Dim 对象 As Object

Dim 子对象 As Object

Dim 电脑名 As String

Dim 刷新 As Long

刷新 = 0

电脑名 = "" '表示本地计算机

Set WMI服务 = GetObject("winmgmts://" & 电脑名 & "/root/cimv2")

Set 对象 = WMI服务InstancesOf("Win32_Processor")

MeCurrentX = 0

MeCurrentY = 0

For Each 子对象 In 对象

If 刷新 = 0 Then

刷新 = 1

MeCls

End If

MePrint 子对象Name & "[" & 子对象CurrentClockSpeed & "Hz] 使用率:" & _

子对象LoadPercentage & "%"

Next

End Sub

内存情况比较简单

MEMORYSTATUSEX mstx;

mstxdwLength = sizeof (mstx);

GlobalMemoryStatusEx( &mstx );

int iMemeryUsePercentage = mstxdwMemoryLoad;

int iTotalPhysMB = mstxullTotalPhys/1024/1024;

int iAvailPhysMB = mstxullAvailPhys/1024/1024;

int iTotalPageFileMB = mstxullTotalPageFile/1024/1024;

int iAvailPageFileMB = mstxullAvailPageFile/1024/1024;

char LogBuff[128];

memset( LogBuff , 0 , 128 );

sprintf( LogBuff , "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d" , 100 - iMemeryUsePercentage , iAvailPhysMB , iTotalPhysMB , iAvailPageFileMB , iTotalPageFileMB );

printf("%s\n",LogBuff);

以上程序分别输出可用百分比,可以用物理内存/总物理内存,可用页面文件/总页面文件

获取CPU的比较复杂,我这边只有获取单个进程CPU占用的方法,不过可以遍历所有进程分别获取再求和就是整个cpu占用率了。

#include <stdioh>

#include <Windowsh>

typedef long long int64_t;

typedef unsigned long long uint64_t;

/// 时间转换

static uint64_t file_time_2_utc(const FILETIME ftime)

{

LARGE_INTEGER li;

liLowPart = ftime->dwLowDateTime;

liHighPart = ftime->dwHighDateTime;

return liQuadPart;

}

/// 获得CPU的核数

static int get_processor_number()

{

SYSTEM_INFO info;

GetSystemInfo(&info);

return (int)infodwNumberOfProcessors;

}

int get_cpu_usage(int pid)

{

//cpu数量

static int processor_count_ = -1;

//上一次的时间

static int64_t last_time_ = 0;

static int64_t last_system_time_ = 0;

FILETIME now;

FILETIME creation_time;

FILETIME exit_time;

FILETIME kernel_time;

FILETIME user_time;

int64_t system_time;

int64_t time;

int64_t system_time_delta;

int64_t time_delta;

int cpu = -1;

if(processor_count_ == -1)

{

processor_count_ = get_processor_number();

}

GetSystemTimeAsFileTime(&now);

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))

{

return -1;

}

system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))

/ processor_count_;

time = file_time_2_utc(&now);

if ((last_system_time_ == 0) || (last_time_ == 0))

{

last_system_time_ = system_time;

last_time_ = time;

return -1;

}

system_time_delta = system_time - last_system_time_;

time_delta = time - last_time_;

if (time_delta == 0)

return -1;

cpu = (int)((system_time_delta 100 + time_delta / 2) / time_delta);

last_system_time_ = system_time;

last_time_ = time;

return cpu;

}

int main()

{

while(1)

{

int cpu;

// 参数为进程id

cpu = get_cpu_usage(5160);

printf("CPU使用率: %d%%\n",cpu);

Sleep(1000);

}

return 0;

}

可以直接用命令prtconf查看,得到的输出有类似下面的行,其中number of processors 就是这个aix系统所拥有的核心数:

$ prtconf

System Model: IBM,8284-22A

Machine Serial Number:

Processor Type: PowerPC_POWER8

Processor Implementation Mode: POWER 8

Processor Version: PV_8_Compat

Number Of Processors: 2

Processor Clock Speed: 4157 MHz

CPU Type: 64-bit

Kernel Type: 64-bit

LPAR Info:

Memory Size: 16384 MB

Good Memory Size: 16384 MB

以上就是关于关于服务器核心数和CPU占比的信息全部的内容,包括:关于服务器核心数和CPU占比的信息、docker读不到cpu数量、vb.net如何获取当前进程的cpu和内存使用率等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9641200.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存