ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/proc_mem.cpp
(Generate patch)
# Line 1 | Line 1
1 + #include "proc_mem.h"
2 + #ifdef __APPLE__
3 + #include<mach/mach.h>
4 + void get_mem_usage(double& vm_usage, double& resident_set) {
5 +  vm_usage=0;
6 +  resident_set=0;
7 +  struct task_basic_info t_info;
8 +  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
9 +
10 +  if (KERN_SUCCESS == task_info(mach_task_self(),
11 +         TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count))
12 +
13 +    {
14 +    vm_usage=double(t_info.virtual_size)/1024;
15 +    resident_set=double(t_info.resident_size)/1024;
16 +    }
17 + // resident size is in t_info.resident_size;
18 + // virtual size is in t_info.virtual_size;
19 + }
20 +
21 + #elif defined(_WIN32) || defined(_WIN64)
22 + #include "windows.h"
23 + #include "psapi.h"
24 + void get_mem_usage(double& vm_usage, double& resident_set) {
25 +        PROCESS_MEMORY_COUNTERS_EX pmc;
26 +        GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS)&pmc, sizeof(pmc));
27 +        //SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
28 +    //SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
29 +    vm_usage=(double)pmc.PrivateUsage;
30 +    resident_set=(double)pmc.WorkingSetSize;
31 + }
32 +
33 + #else //assume Linux
34   #include <unistd.h>
35   #include <string>
36   #include <ios>
37   #include <fstream>
38 < //////////////////////////////////////////////////////////////////////////////
6 < // process_mem_usage(double &, double &) - takes two doubles by reference,
7 < // attempts to read the system-dependent data for a process' virtual memory
8 < // size and resident set size, and return the results in KB.
9 < //
10 < // On failure, returns 0.0, 0.0
11 <
12 < void process_mem_usage(double& vm_usage, double& resident_set) {
38 > void get_mem_usage(double& vm_usage, double& resident_set) {
39     using std::ios_base;
40     using std::ifstream;
41     using std::string;
# Line 35 | Line 61
61  
62     stat_stream.close();
63  
38
64     long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
65     vm_usage     = vsize / 1024.0;
66     resident_set = rss * page_size_kb;
67   }
68 + #endif
69 +
70 + //////////////////////////////////////////////////////////////////////////////
71 + // get_mem_usage(double &, double &) - takes two doubles by reference,
72 + // attempts to read the system-dependent data for a process' virtual memory
73 + // size and resident set size, and return the results in KB.
74 + //
75 + // On failure, returns 0.0, 0.0
76 +
77  
78 < void print_mem_usage() {
78 > void print_mem_usage(FILE* fout) {
79    double vs, rs;
80 <  process_mem_usage(vs,rs);
80 >  get_mem_usage(vs,rs);
81    vs/=1024;
82    rs/=1024;
83 <  fprintf(stderr, "VMSize: %6.1fMB\tRSize: %6.1fMB\n", vs, rs);
83 >  fprintf(fout, "VMSize: %6.1fMB\tRSize: %6.1fMB\n", vs, rs);
84    }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines