《编程实现获取linux服务器cpu、内存和磁盘使用率》
废话不多说了,直接上代码:
#pragma once
#include <inttypes.h>
#include <iostream>
#if defined(_WIN64) || (_WIN32)
#include <windows.h>
#else
#include <sys/sysinfo.h>
#endif
using namespace std;
namespace robin
{
class Usage
{
public:
static void init()
{
}
static uint32_t getMemoryUsgae();
static uint32_t getCpuUsage();
};
/*
winBase.h
windows.h
MEMORYSTATUS ms;
*/
#if (_WIN64) || (_WIN32)
uint32_t Usage::getMemoryUsgae()
{
MEMORYSTATUS ms;
::GlobalMemoryStatus(&ms);
return ms.dwMemoryLoad;
}
//CPU使用率;使用方法:直接调用getCpuUsage()函数
//原理:记录一定时间内CPU的空闲时间和繁忙时间,然后计算得出
__int64 CompareFileTime(FILETIME time1, FILETIME time2)
{
__int64 a = (__int64)time1.dwHighDateTime << 32 | time1.dwLowDateTime;
__int64 b = (__int64)time2.dwHighDateTime << 32 | time2.dwLowDateTime;
return (b - a);
}
uint32_t Usage::getCpuUsage()
{
static bool bInit = false;
static FILETIME pre_idleTime;
static FILETIME pre_kernelTime;
static FILETIME pre_userTime;
FILETIME idleTime;//空闲时间
FILETIME kernelTime;//核心态时间
FILETIME userTime;//用户态时间
bool res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
__int64 idle = CompareFileTime(pre_idleTime, idleTime);
__int64 kernel = CompareFileTime(pre_kernelTime, kernelTime);
__int64 user = CompareFileTime(pre_userTime, userTime);
pre_userTime = userTime;
pre_kernelTime = kernelTime;
pre_idleTime = idleTime;
if (bInit == false)
{
bInit = true;
return 0;
}
if ((kernel + user) == 0)
return 0;
int cpu_occupancy_rate = double(kernel + user - idle) * 100.0 / double(kernel + user);
//(总的时间 - 空闲时间)/ 总的时间 = 占用CPU时间的比率,即占用率
//int cpu_idle_rate = idle * 100.0 / (kernel + user);
//空闲时间 / 总的时间 = 闲置CPU时间的比率,即闲置率
//int cpu_kernel_rate = kernel * 100 / (kernel + user);
//核心态时间 / 总的时间 = 核心态占用的比率
//int cpu_user_rate = user * 100 / (kernel + user);
//用户态时间 / 总的时间 = 用户态占用的比率
/*cout << "CPU占用率:" << cpu_occupancy_rate << "%" << endl
<< "CPU闲置率:" << cpu_idle_rate << "%" << endl
<< "核心态占比率:" << cpu_kernel_rate << "%" << endl
<< "用户态占比率:" << cpu_user_rate << "%" << endl << endl;*/
return cpu_occupancy_rate;
}
#else // linux
uint32_t Usage::getMemoryUsgae()
{
struct sysinfo tmp;
int ret = sysinfo(&tmp);
uint32_t percent;
if (ret == 0)
{
double t = tmp.freeram;
t = t * 100.0 / tmp.totalram;
return 100 - t;
}
return 0;
}
typedef struct _SysCPUInfo
{
char name[260];
uint64_t user;
uint64_t nic;
uint64_t system;
uint64_t idle;
}SysCPUInfo;
void GetHostCPUInfo(SysCPUInfo *cpuinfo)
{
FILE *fd;
char buff[256];
memset(buff, '\0', 256);
fd = fopen("/proc/stat", "r");
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu %lu %lu %lu",
(char *)&cpuinfo->name,
&cpuinfo->user,
&cpuinfo->nic,
&cpuinfo->system,
&cpuinfo->idle);
fclose(fd);
}
uint32_t Usage::getCpuUsage()
{
static uint64_t old_user;
static uint64_t old_system;
static uint64_t old_nic;
static uint64_t old_idle;
static bool bInit = false;
SysCPUInfo curInfo;
GetHostCPUInfo(&curInfo);
if (bInit == false)
{
old_user = curInfo.user;
old_system = curInfo.system;
old_idle = curInfo.idle;
old_nic = curInfo.nic;
bInit = true;
return 0;
}
float cpu_use = 0.0;
uint64_t old_CPU_Time = (uint64_t)(old_user + old_nic + old_system + old_idle);
uint64_t new_CPU_Time = (uint64_t)(curInfo.user + curInfo.nic + curInfo.system + curInfo.idle);
uint64_t usr_Time_Diff = (uint64_t)(curInfo.user - old_user);
uint64_t sys_Time_Diff = (uint64_t)(curInfo.system - old_system);
uint64_t nic_Time_Diff = (uint64_t)(curInfo.nic - old_nic);
if ((new_CPU_Time - old_CPU_Time) != 0)
cpu_use = (double)100.0 * (usr_Time_Diff + sys_Time_Diff + nic_Time_Diff) / (new_CPU_Time - old_CPU_Time);
else
cpu_use = 0.0;
old_user = curInfo.user;
old_system = curInfo.system;
old_idle = curInfo.idle;
old_nic = curInfo.nic;
return cpu_use;
}
/*
void testLinux()
{
struct timespec req;
req.tv_sec = 1;
req.tv_nsec = 0;
struct timespec rem;
int cpuPer = getCpuUsage();
for (int i=0; i<100; i++)
{
int ret = nanosleep(&req, &rem);
cpuPer = getCpuUsage();
printf("cpu = %lu\n", cpuPer);
}
}
*/
#endif
}
调用时候,在线程中设置定时器,或者 sleep一段时间,调用函数,返回百分比;
备注:CPU的占用率,首次调用会返回0。
1.Font Awesome 是一个非常方便的图标库。这些图标都是矢量图形,被保存在 .svg 的文件格式中。这些图标就和字体一样,你可以通过像素单位指定它们的大小,它们将会继承其父HTML元素的字体大小。2.你可以将 Font Awesome 图标库增添至任何一个应用中,方法很简单,只需要在你的 HTML 头部增加下列代码即可:<link rel="style...
作为一个注册中心,为了确保微服务能够正常的注册和发现,单机的eureka server肯定不能满足这样的需求,如果注册中心挂掉,那么线上的所有服务都会有影响,那么需要有一个高可用的eureka server来支撑,本文主要介绍高可用的eureka server的搭建。 在eureka的服务治理设计中,所有的节点既是服务提供方,也是服务消费方,服务注册中心也是,那么eureka server_eureka server https
1. 与鼠标相关的事件函数OnMouseXXX:事件检测:2. 数学函数Mathf里的静态变量:3. Mathf里的静态方法:实现cube从1移动到3:输出移动:4. Mathf常用方法:floor(向下取整,cell是向上取整):其他:5. 游戏开发中的插值运算(lerp)先快后慢运动:6. 使用MoveTowards做匀速..._通过getaxis获取按键,用来控制立方体的书平方向的移动关键代码
【代码】WPF控件 ~ GroupBox、TextBlock 、TextBox。_wpf groupbox里加控件
写一个存为csv文件的代码 with open(outputFile, 'w', newline='') as csvfile: writer = csv.writer(csvfile) for item in sortsim: writer.writerow([item[0], item[1], item[2]])出现问题了Traceback (mo..._newline' is an inva
最近有个需求,就是要在一个图像插件上面,需要弹出一个窗口,在弹出的窗口之上又的弹出一个模式窗口。然后我用到了window.showmodaldialog 方法1.参数showModalDialog("子页面地址","向子页面传递参数","子页面外观设置"),比如说var data = new Date().getTime();var timeValue = window.
转自:http://blog.csdn.net/yuyongpeng/article/details/1818717目录(?)[-]Linux压缩文件的读取Compress压缩文件Gzip压缩文件和zcatBzip2压缩文件和bzcatTar压缩文件Zip和unzip压缩文件Rar压缩文件Linux压缩文件的读取· *.Z comp
今天给大家分享几个Python使用的小技巧,原文来自于Python 技巧总结 | Taotao&#x27;s ZonePython 技巧总结 | Taotao&#x27;s Zone,进行了细微的调整,感谢作者!1. 枚举 - enumerate 可以有参数哦之前我们这样操作:i = 0for item in iterable:p
网上有很多网站搭建教程,今天搭建好之后,也来发表一下自己的搭建步骤:第一步,买域名:一直想搭建一个个人的网站,最近在弄关于服务器的东西,说弄就弄,百度了很多东西,说国内的域名需要备案,并且.com的域名要比国外的贵一些,所以去了godaddy上买了一个.com的域名,链接地址https://sg.godaddy.com/zh?isc=gennbacn29&countrview=1¤_快速建站技术
问题:什么是分页,为什么使用分页? 分页就是将数据以多页去展示,使用分页可以提高客户的感受。 分页分类: 1.物理分页 只从数据库中查询出当前页的数据。 优点:不占用很多内存 缺点:效率比较低 2.逻辑分页 从数据库中将所有记录查询出业,存储到内存中,要想展示当前页 数据,直接从内存中获取。 优点:效率高 缺点:占用内存比较高 ...
class PubEventArgs : EventArgs ...{ private readonly string m_magazineName; private readonly DateTime m_pubDate; public PubEventArgs(string magazineName, DateTime pubDate)_event设计
采用因子图模型,针对不含有初始信息的用户推荐一定主题下的相关度最大的k个文献。_文献因子分析模型图