ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/proc_mem.cpp
Revision: 310
Committed: Fri Mar 22 20:06:27 2013 UTC (6 years, 7 months ago) by gpertea
File size: 2775 byte(s)
Log Message:
sync with igm repo

Line User Rev File contents
1 gpertea 310 #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 gpertea 18 #include <unistd.h>
35     #include <string>
36     #include <ios>
37     #include <fstream>
38 gpertea 310 void get_mem_usage(double& vm_usage, double& resident_set) {
39 gpertea 18 using std::ios_base;
40     using std::ifstream;
41     using std::string;
42     vm_usage = 0.0;
43     resident_set = 0.0;
44     // 'file' stat seems to give the most reliable results
45     ifstream stat_stream("/proc/self/stat",ios_base::in);
46     // dummy vars for leading entries in stat that we don't care about
47     string pid, comm, state, ppid, pgrp, session, tty_nr;
48     string tpgid, flags, minflt, cminflt, majflt, cmajflt;
49     string utime, stime, cutime, cstime, priority, nice;
50     string O, itrealvalue, starttime;
51    
52     // the two fields we want
53     //
54     unsigned long vsize;
55     long rss;
56    
57     stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
58     >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
59     >> utime >> stime >> cutime >> cstime >> priority >> nice
60     >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
61    
62     stat_stream.close();
63    
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 gpertea 310 #endif
69 gpertea 18
70 gpertea 310 //////////////////////////////////////////////////////////////////////////////
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(FILE* fout) {
79 gpertea 18 double vs, rs;
80 gpertea 310 get_mem_usage(vs,rs);
81 gpertea 18 vs/=1024;
82     rs/=1024;
83 gpertea 310 fprintf(fout, "VMSize: %6.1fMB\tRSize: %6.1fMB\n", vs, rs);
84 gpertea 18 }