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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/process_metrics.h"
#include <windows.h> // Must be in front of other Windows header files.
#include <psapi.h>
#include <stddef.h>
#include <stdint.h>
#include <winternl.h>
#include <algorithm>
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/process/memory.h"
#include "base/process/process_metrics_iocounters.h"
#include "base/sys_info.h"
namespace base {
namespace {
// System pagesize. This value remains constant on x86/64 architectures.
const int PAGESIZE_KB = 4;
typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength);
} // namespace
ProcessMetrics::~ProcessMetrics() { }
size_t GetMaxFds() {
// Windows is only limited by the amount of physical memory.
return std::numeric_limits<size_t>::max();
}
// static
std::unique_ptr<ProcessMetrics> ProcessMetrics::CreateProcessMetrics(
ProcessHandle process) {
return WrapUnique(new ProcessMetrics(process));
}
namespace {
class WorkingSetInformationBuffer {
public:
WorkingSetInformationBuffer() {}
~WorkingSetInformationBuffer() { Clear(); }
bool Reserve(size_t size) {
Clear();
// Use UncheckedMalloc here because this can be called from the code
// that handles low memory condition.
return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_));
}
const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; }
size_t GetPageEntryCount() const { return number_of_entries; }
// This function is used to get page entries for a process.
bool QueryPageEntries(const ProcessHandle& process) {
int retries = 5;
number_of_entries = 4096; // Just a guess.
for (;;) {
size_t buffer_size =
sizeof(PSAPI_WORKING_SET_INFORMATION) +
(number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK));
if (!Reserve(buffer_size))
return false;
// On success, |buffer_| is populated with info about the working set of
// |process|. On ERROR_BAD_LENGTH failure, increase the size of the
// buffer and try again.
if (QueryWorkingSet(process, buffer_, buffer_size))
break; // Success
if (GetLastError() != ERROR_BAD_LENGTH)
return false;
number_of_entries = buffer_->NumberOfEntries;
// Maybe some entries are being added right now. Increase the buffer to
// take that into account. Increasing by 10% should generally be enough,
// especially considering the potentially low memory condition during the
// call (when called from OomMemoryDetails) and the potentially high
// number of entries (300K was observed in crash dumps).
number_of_entries *= 1.1;
if (--retries == 0) {
// If we're looping, eventually fail.
return false;
}
}
// TODO(chengx): Remove the comment and the logic below. It is no longer
// needed since we don't have Win2000 support.
// On windows 2000 the function returns 1 even when the buffer is too small.
// The number of entries that we are going to parse is the minimum between
// the size we allocated and the real number of entries.
number_of_entries = std::min(number_of_entries,
static_cast<size_t>(buffer_->NumberOfEntries));
return true;
}
private:
void Clear() {
free(buffer_);
buffer_ = nullptr;
}
PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr;
// Number of page entries.
size_t number_of_entries = 0;
DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer);
};
} // namespace
static uint64_t FileTimeToUTC(const FILETIME& ftime) {
LARGE_INTEGER li;
li.LowPart = ftime.dwLowDateTime;
li.HighPart = ftime.dwHighDateTime;
return li.QuadPart;
}
double ProcessMetrics::GetPlatformIndependentCPUUsage() {
FILETIME creation_time;
FILETIME exit_time;
FILETIME kernel_time;
FILETIME user_time;
if (!GetProcessTimes(process_.Get(), &creation_time, &exit_time, &kernel_time,
&user_time)) {
// We don't assert here because in some cases (such as in the Task Manager)
// we may call this function on a process that has just exited but we have
// not yet received the notification.
return 0;
}
int64_t system_time = FileTimeToUTC(kernel_time) + FileTimeToUTC(user_time);
TimeTicks time = TimeTicks::Now();
if (last_system_time_ == 0) {
// First call, just set the last values.
last_system_time_ = system_time;
last_cpu_time_ = time;
return 0;
}
int64_t system_time_delta = system_time - last_system_time_;
// FILETIME is in 100-nanosecond units, so this needs microseconds times 10.
int64_t time_delta = (time - last_cpu_time_).InMicroseconds() * 10;
DCHECK_NE(0U, time_delta);
if (time_delta == 0)
return 0;
last_system_time_ = system_time;
last_cpu_time_ = time;
return static_cast<double>(system_time_delta * 100) / time_delta;
}
bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
return GetProcessIoCounters(process_.Get(), io_counters) != FALSE;
}
ProcessMetrics::ProcessMetrics(ProcessHandle process) : last_system_time_(0) {
if (process) {
HANDLE duplicate_handle = INVALID_HANDLE_VALUE;
BOOL result = ::DuplicateHandle(::GetCurrentProcess(), process,
::GetCurrentProcess(), &duplicate_handle,
PROCESS_QUERY_INFORMATION, FALSE, 0);
DPCHECK(result);
process_.Set(duplicate_handle);
}
}
size_t GetSystemCommitCharge() {
// Get the System Page Size.
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
PERFORMANCE_INFORMATION info;
if (!GetPerformanceInfo(&info, sizeof(info))) {
DLOG(ERROR) << "Failed to fetch internal performance info.";
return 0;
}
return (info.CommitTotal * system_info.dwPageSize) / 1024;
}
size_t GetPageSize() {
return PAGESIZE_KB * 1024;
}
// This function uses the following mapping between MEMORYSTATUSEX and
// SystemMemoryInfoKB:
// ullTotalPhys ==> total
// ullAvailPhys ==> avail_phys
// ullTotalPageFile ==> swap_total
// ullAvailPageFile ==> swap_free
bool GetSystemMemoryInfo(SystemMemoryInfoKB* meminfo) {
MEMORYSTATUSEX mem_status;
mem_status.dwLength = sizeof(mem_status);
if (!::GlobalMemoryStatusEx(&mem_status))
return false;
meminfo->total = mem_status.ullTotalPhys / 1024;
meminfo->avail_phys = mem_status.ullAvailPhys / 1024;
meminfo->swap_total = mem_status.ullTotalPageFile / 1024;
meminfo->swap_free = mem_status.ullAvailPageFile / 1024;
return true;
}
size_t ProcessMetrics::GetMallocUsage() {
// Unsupported as getting malloc usage on Windows requires iterating through
// the heap which is slow and crashes.
return 0;
}
} // namespace base
|