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