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_);//查询当前程序的内存使用量 }; } }