]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | #include "MemoryModel.h" |
31f18b77 | 2 | #include "include/compat.h" |
7c673cae FG |
3 | #include "debug.h" |
4 | #if defined(__linux__) | |
5 | #include <malloc.h> | |
6 | #endif | |
7 | ||
8 | #include <fstream> | |
9 | ||
10 | #define dout_subsys ceph_subsys_ | |
11 | ||
20effc67 TL |
12 | using namespace std; |
13 | ||
7c673cae FG |
14 | MemoryModel::MemoryModel(CephContext *cct_) |
15 | : cct(cct_) | |
16 | { | |
17 | } | |
18 | ||
19 | void MemoryModel::_sample(snap *psnap) | |
20 | { | |
21 | ifstream f; | |
22 | ||
23 | f.open(PROCPREFIX "/proc/self/status"); | |
24 | if (!f.is_open()) { | |
25 | ldout(cct, 0) << "check_memory_usage unable to open " PROCPREFIX "/proc/self/status" << dendl; | |
26 | return; | |
27 | } | |
28 | while (!f.eof()) { | |
29 | string line; | |
30 | getline(f, line); | |
31 | ||
32 | if (strncmp(line.c_str(), "VmSize:", 7) == 0) | |
33 | psnap->size = atol(line.c_str() + 7); | |
34 | else if (strncmp(line.c_str(), "VmRSS:", 6) == 0) | |
35 | psnap->rss = atol(line.c_str() + 7); | |
36 | else if (strncmp(line.c_str(), "VmHWM:", 6) == 0) | |
37 | psnap->hwm = atol(line.c_str() + 7); | |
38 | else if (strncmp(line.c_str(), "VmLib:", 6) == 0) | |
39 | psnap->lib = atol(line.c_str() + 7); | |
40 | else if (strncmp(line.c_str(), "VmPeak:", 7) == 0) | |
41 | psnap->peak = atol(line.c_str() + 7); | |
42 | else if (strncmp(line.c_str(), "VmData:", 7) == 0) | |
43 | psnap->data = atol(line.c_str() + 7); | |
44 | } | |
45 | f.close(); | |
46 | ||
47 | f.open(PROCPREFIX "/proc/self/maps"); | |
48 | if (!f.is_open()) { | |
49 | ldout(cct, 0) << "check_memory_usage unable to open " PROCPREFIX "/proc/self/maps" << dendl; | |
50 | return; | |
51 | } | |
52 | ||
53 | long heap = 0; | |
54 | while (f.is_open() && !f.eof()) { | |
55 | string line; | |
56 | getline(f, line); | |
57 | //ldout(cct, 0) << "line is " << line << dendl; | |
58 | ||
59 | const char *start = line.c_str(); | |
60 | const char *dash = start; | |
61 | while (*dash && *dash != '-') dash++; | |
62 | if (!*dash) | |
63 | continue; | |
64 | const char *end = dash + 1; | |
65 | while (*end && *end != ' ') end++; | |
66 | if (!*end) | |
67 | continue; | |
68 | unsigned long long as = strtoll(start, 0, 16); | |
69 | unsigned long long ae = strtoll(dash+1, 0, 16); | |
70 | ||
71 | //ldout(cct, 0) << std::hex << as << " to " << ae << std::dec << dendl; | |
72 | ||
73 | end++; | |
74 | const char *mode = end; | |
75 | ||
76 | int skip = 4; | |
77 | while (skip--) { | |
78 | end++; | |
79 | while (*end && *end != ' ') end++; | |
80 | } | |
81 | if (*end) | |
82 | end++; | |
83 | ||
84 | long size = ae - as; | |
85 | //ldout(cct, 0) << "size " << size << " mode is '" << mode << "' end is '" << end << "'" << dendl; | |
86 | ||
87 | /* | |
88 | * anything 'rw' and anon is assumed to be heap. | |
89 | */ | |
90 | if (mode[0] == 'r' && mode[1] == 'w' && !*end) | |
91 | heap += size; | |
92 | } | |
93 | ||
94 | psnap->heap = heap >> 10; | |
95 | ||
96 | } |