获得系统性能:cpu、内存
来源:互联网 发布:后现代主义 知乎 编辑:程序博客网 时间:2024/05/18 03:49
Arcemu code:
cpp:
/* * ArcEmu MMORPG Server * Copyright (C) 2005-2007 Ascent Team <http://www.ascentemu.com/> * Copyright (C) 2008-2011 <http://www.ArcEmu.org/> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */#include "SysInfo.hpp"#ifdef WIN32#include <Windows.h>#include <psapi.h>#pragma comment(lib, "Psapi")#endif#if defined(linux) || defined(__linux)#include <sys/time.h>#include <sys/resource.h>#include <unistd.h>#include <fstream>#include <sstream>#include <cstdlib>#endif#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)#include <sys/time.h>#include <sys/resource.h>#include <sys/types.h>#include <sys/sysctl.h>#ifndef NULL#define NULL 0#endif#endif#ifdef __APPLE__#include <sys/time.h>#include <sys/resource.h>#include <sys/types.h>#include <sys/sysctl.h>#include <mach/task.h>#include <mach/mach.h>#endifnamespace Arcemu{#ifdef WIN32long SysInfo::GetCPUCount(){SYSTEM_INFO info;GetSystemInfo(&info);return info.dwNumberOfProcessors;}unsigned long long SysInfo::GetCPUUsage(){FILETIME dummy;FILETIME kerneltime;FILETIME usertime;BOOL success = GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, &kerneltime, &usertime);if(success == 0)return 0;ULARGE_INTEGER ktime;ULARGE_INTEGER utime;ktime.HighPart = kerneltime.dwHighDateTime;ktime.LowPart = kerneltime.dwLowDateTime;utime.HighPart = usertime.dwHighDateTime;utime.LowPart = usertime.dwLowDateTime;double totaltime = static_cast< double >(ktime.QuadPart + utime.QuadPart);// GetProcessTimes() returns the value in 100 nanosecond units and we need it in microsecondstotaltime /= 10;return static_cast< unsigned long long >(totaltime);}unsigned long long SysInfo::GetRAMUsage(){PROCESS_MEMORY_COUNTERS pmcex;GetProcessMemoryInfo(GetCurrentProcess(), &pmcex, sizeof(pmcex));return pmcex.PagefileUsage;}unsigned long long SysInfo::GetTickCount(){return ::GetTickCount();}#endif#if defined(linux) || defined(__linux)long SysInfo::GetCPUCount(){return sysconf(_SC_NPROCESSORS_ONLN);}unsigned long long SysInfo::GetCPUUsage(){rusage usage;if(getrusage(RUSAGE_SELF, &usage) != 0)return 0;unsigned long long k = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec;unsigned long long u = usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;return (k + u);}unsigned long long SysInfo::GetRAMUsage(){std::ifstream memf;std::string line;std::string value;std::stringstream fname;char line2[100];float memusage;unsigned long pos = 0;unsigned long pid = getpid();fname << "/proc/" << pid << "/status";memf.open(fname.str().c_str());do{pos = std::string::npos;memf.getline(line2, 100);line.assign(line2);pos = line.find("VmRSS");}while(pos == std::string::npos);pos = line.find(" ");++pos;value = line.substr(pos, std::string::npos);memusage = static_cast<float>(atof(value.c_str()));memf.close();memusage *= 1024.0f;return memusage;}unsigned long long SysInfo::GetTickCount(){timeval Time;if(gettimeofday(&Time, NULL) != 0)return 0;return (Time.tv_sec * 1000) + (Time.tv_usec / 1000);}#endif#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)long SysInfo::GetCPUCount(){int mib[4];int ncpu;size_t len = 4;sysctlnametomib("hw.ncpu", mib, &len);size_t len2 = sizeof(ncpu);sysctl(mib, len, &ncpu, &len2, NULL, 0);return ncpu;}unsigned long long SysInfo::GetCPUUsage(){rusage usage;if(getrusage(RUSAGE_SELF, &usage) != 0)return 0;unsigned long long k = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec;unsigned long long u = usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;return (k + u);}unsigned long long SysInfo::GetRAMUsage(){return 0;}unsigned long long SysInfo::GetTickCount(){timeval Time;if(gettimeofday(&Time, NULL) != 0)return 0;return (Time.tv_sec * 1000) + (Time.tv_usec / 1000);}#endif#ifdef __APPLE__long SysInfo::GetCPUCount(){int mib[4];int ncpu;size_t len = 4;sysctlnametomib("hw.ncpu", mib, &len);size_t len2 = sizeof(ncpu);sysctl(mib, len, &ncpu, &len2, NULL, 0);return ncpu;}unsigned long long SysInfo::GetCPUUsage(){rusage usage;if(getrusage(RUSAGE_SELF, &usage) != 0)return 0;unsigned long long k = usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec;unsigned long long u = usage.ru_stime.tv_sec * 1000000 + usage.ru_stime.tv_usec;return (k + u);}unsigned long long SysInfo::GetRAMUsage(){task_t task = MACH_PORT_NULL;task_basic_info t_info;mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;if(KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))return 0;return t_info.resident_size;}unsigned long long SysInfo::GetTickCount(){timeval Time;if(gettimeofday(&Time, NULL) != 0)return 0;return (Time.tv_sec * 1000) + (Time.tv_usec / 1000);}#endif}
hpp:
/* * ArcEmu MMORPG Server * Copyright (C) 2005-2007 Ascent Team <http://www.ascentemu.com/> * Copyright (C) 2008-2011 <http://www.ArcEmu.org/> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */#ifndef ARCEMU_SYSINFO_HPP#define ARCEMU_SYSINFO_HPPnamespace Arcemu{//////////////////////////////////////////////////////////class SysInfo// Class with static methods capable of retrieving// some basic system information/////////////////////////////////////////////////////////class SysInfo{public://////////////////////////////////////////////////////////////////////static int GetCPUCount()// Tells the number of CPUs installed in the system////Parameter(s)// None////Return Value// Returns the number of CPUs////////////////////////////////////////////////////////////////////////static long GetCPUCount();//////////////////////////////////////////////////////////////////////static unsigned long long GetCPUUsage()// Tells the CPU time ( kernel and user )spent on executing// the caller process.//////Parameter(s)// None////Return Value// Returns the used CPU time in microseconds.////////////////////////////////////////////////////////////////////////static unsigned long long GetCPUUsage();//////////////////////////////////////////////////////////////////////static unsigned long GetRAMUsage()// Tells the RAM usage of the caller process.////Parameter(s)// None////Return Value// Returns the number of bytes used by the caller process////////////////////////////////////////////////////////////////////////static unsigned long long GetRAMUsage();//////////////////////////////////////////////////////////////////////static unsigned long long GetTickCount()// Returns the time elapsed since some starting point, with// milliseconds precision.////Parameter(s)// None////Return Value// Returns the milliseconds elapsed since some point in the past////////////////////////////////////////////////////////////////////////static unsigned long long GetTickCount();};}#endif
- 获得系统性能:cpu、内存
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟…
- Windows系统CPU内存网络性能统计第一篇 内存
- Windows系统CPU内存网络性能统计第一篇 内存
- Windows系统CPU内存网络性能统计第一篇 内存
- Windows系统CPU内存网络性能统计第一篇 内存
- Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率
- Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率C#
- Windows系统CPU内存网络性能统计第四篇 CPU 多核CPU各核使用率C++
- Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率
- ARM平台上蓝牙协议栈Bluez的移植使用和配置
- Java数据库连接池实现
- union+和+union+all+的区别
- 底层之旅——Android蓝牙系统分析
- 认识(大端-小端)端模式
- 获得系统性能:cpu、内存
- boost中关于正则表达式在vs2008不能匹配中文的问题
- 搜索引擎工作原理
- JVM工作原理和特点
- 基于网络环境的程序设计综合实验报告
- GDI利用CImage读取磁盘文件
- 【WebRTC基础系列】Mid-call features
- java swing jInternalFrame组件例子
- [iOS创业经验分享] 苹果AppStore应用商店生存之道