Repository navigation
Expand file tree
/
Copy pathmemory-stats.cpp
More file actions
172 lines (141 loc) · 5.14 KB
/
Copy pathmemory-stats.cpp
File metadata and controls
172 lines (141 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/server/memory-stats.h"
#include "hphp/runtime/base/static-string-table.h"
#include "hphp/runtime/vm/jit/tc.h"
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <ios>
#include <iostream>
#include <sstream>
#include <string>
#include <folly/portability/Unistd.h>
namespace HPHP {
MemoryStats g_memoryStats;
void MemoryStats::ReportMemory(std::string &output, Writer::Format format) {
std::ostringstream out;
Writer *w;
if (format == Writer::Format::XML) {
w = new XMLWriter(out);
} else if (format == Writer::Format::HTML) {
w = new HTMLWriter(out);
} else {
assert(format == Writer::Format::JSON);
w = new JSONWriter(out);
}
w->writeFileHeader();
// read and fill StatM structure
StatM procStatM;
if (!FillProcessStatM(&procStatM)) {
// failed to read statm file
w->writeEntry("Success", false);
w->writeFileFooter();
return;
}
// successfully read statm file
w->writeEntry("Success", true);
// Calculate unknown size
size_t totalBytes = GetStaticStringSize();
totalBytes += procStatM.m_text;
w->beginObject("Memory");
// process memory statistics
w->beginObject("Process Stats (bytes)");
w->writeEntry("VmSize", procStatM.m_vmSize);
w->writeEntry("VmRss", procStatM.m_vmRss);
w->writeEntry("Shared", procStatM.m_share);
w->writeEntry("Text(Code)", procStatM.m_text);
w->writeEntry("Data", procStatM.m_data);
w->endObject("Process Stats");
// Start HHVM internal memory buckets
w->beginObject("Breakdown");
// static string stats
w->beginObject("Static Strings");
w->writeEntry("Bytes", GetStaticStringSize());
w->beginObject("Details");
w->writeEntry("Count", makeStaticStringCount());
w->endObject("Details");
w->endObject("Static Strings");
// code segment stats
w->beginObject("Code");
w->beginObject("Details");
w->writeEntry("Bytes", procStatM.m_text);
w->endObject("Details");
w->endObject("Code");
// TC/Jit
w->beginObject("TC/Jit");
{
size_t globalTCUsed = 0;
size_t globalTCSize = 0;
w->beginObject("Details");
for (auto const& blockUsageInfo : jit::tc::getUsageInfo()) {
if (blockUsageInfo.global) {
globalTCSize += blockUsageInfo.capacity;
globalTCUsed += blockUsageInfo.used;
w->beginObject(blockUsageInfo.name.c_str());
w->writeEntry("Used", blockUsageInfo.used);
w->writeEntry("Capacity", blockUsageInfo.capacity);
w->endObject(blockUsageInfo.name.c_str());
}
};
w->writeEntry("Total Used", globalTCUsed);
w->writeEntry("Total Capacity", globalTCSize);
w->endObject("Details");
w->writeEntry("Bytes", globalTCSize);
totalBytes += globalTCSize;
}
w->endObject("TC/Jit");
// currently unknown portion of vmSize
w->writeEntry("Unknown", procStatM.m_vmSize - totalBytes);
w->endObject("Breakdown");
// End HHVM internal memory buckets
w->endObject("Memory");
w->writeFileFooter();
delete w;
output = out.str();
return;
}
MemoryStats* MemoryStats::GetInstance() {
static MemoryStats s_memoryStats;
return &s_memoryStats;
}
void MemoryStats::ResetStaticStringSize() {
m_staticStringSize.store(0);
}
void MemoryStats::LogStaticStringAlloc(size_t bytes) {
m_staticStringSize += bytes;
}
size_t MemoryStats::GetStaticStringSize() {
return m_staticStringSize.load();
}
bool MemoryStats::FillProcessStatM(MemoryStats::StatM* pStatM) {
std::ifstream statm_stream("/proc/self/statm", std::ios_base::in);
if (!statm_stream.good()){
return false;
}
uint64_t pageSizeBytes = sysconf(_SC_PAGE_SIZE);
size_t deprecatedField;
statm_stream >> pStatM->m_vmSize >> pStatM->m_vmRss
>> pStatM->m_share >> pStatM->m_text
>> deprecatedField >> pStatM->m_data;
pStatM->m_vmSize *= pageSizeBytes;
pStatM->m_vmRss *= pageSizeBytes;
pStatM->m_share *= pageSizeBytes;
pStatM->m_text *= pageSizeBytes;
pStatM->m_data *= pageSizeBytes;
return true;
}
}