From 995d7230b296ad427b2ed3750df1f7348a941b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Thu, 23 Oct 2014 13:31:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=A8linux=E4=B8=8B?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=A8=8B=E5=BA=8F=E5=86=85=E5=AD=98=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Profiler/Profiler.cpp | 12 ++++++++---- TinySTL/Profiler/Profiler.h | 14 ++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/TinySTL/Profiler/Profiler.cpp b/TinySTL/Profiler/Profiler.cpp index a2d0fb3..d6d749e 100644 --- a/TinySTL/Profiler/Profiler.cpp +++ b/TinySTL/Profiler/Profiler.cpp @@ -29,12 +29,19 @@ namespace TinySTL{ } size_t ProfilerInstance::memory(MemoryUnit mu){ + size_t memory = 0; #ifdef WIN32 PROCESS_MEMORY_COUNTERS pmc; HANDLE hProcess = GetCurrentProcess(); if (!GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) throw std::runtime_error("GetProcessMemoryInfo failed"); - size_t memory = 0; + CloseHandle(hProcess); + #else + struct rusage usage; + if(getrusage(RUSAGE_SELF, &usage) == -1) + throw std::runtime_error("getrusage failed"); + memory = usage.ru_maxrss / 1024;//如果某些linux平台不完全支持getrusage则ru_maxrss总是返回0 + #endif switch (mu){ case MemoryUnit::KB_: memory = pmc.WorkingSetSize KB; @@ -46,10 +53,7 @@ namespace TinySTL{ memory = pmc.WorkingSetSize GB; break; } - CloseHandle(hProcess); return memory; - #else - #endif } } } \ No newline at end of file diff --git a/TinySTL/Profiler/Profiler.h b/TinySTL/Profiler/Profiler.h index 952c0bd..0b366cc 100644 --- a/TinySTL/Profiler/Profiler.h +++ b/TinySTL/Profiler/Profiler.h @@ -14,6 +14,8 @@ #include #pragma comment(lib, "psapi.lib") #else +#include +#include #endif namespace TinySTL{ @@ -34,14 +36,14 @@ namespace TinySTL{ static TimePoint startTime; static TimePoint finishTime; public: - static void start(); - static void finish(); - static void dumpDuringTime(std::ostream& os = std::cout); + static void start();//开始计时 + static void finish();//结束计时 + static void dumpDuringTime(std::ostream& os = std::cout);//打印时间 - static double second(); - static double millisecond(); + static double second();//以秒为单位返回时间 + static double millisecond();//以毫秒为单位返回时间 - static size_t memory(MemoryUnit mu = MemoryUnit::KB_); + static size_t memory(MemoryUnit mu = MemoryUnit::KB_);//查询当前程序的内存使用量 }; } }