Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/process/process_metrics.h" | 5 #include "base/process/process_metrics.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 buffer_ = nullptr; | 164 buffer_ = nullptr; |
| 165 } | 165 } |
| 166 | 166 |
| 167 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; | 167 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; |
| 168 | 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); | 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); |
| 170 }; | 170 }; |
| 171 | 171 |
| 172 } // namespace | 172 } // namespace |
| 173 | 173 |
| 174 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 174 // This is a helper function to get number of page entries for a process. |
| 175 size_t ws_private = 0; | 175 bool GetPageEntriesNum(WorkingSetInformationBuffer& buffer, |
|
brucedawson
2016/12/05 19:22:02
The name of this function is misleading. It sounds
chengx
2016/12/06 02:51:38
Done.
| |
| 176 size_t ws_shareable = 0; | 176 const ProcessHandle& process_, |
| 177 size_t ws_shared = 0; | 177 DWORD* number_of_entries) { |
| 178 int retries = 5; | |
| 178 | 179 |
| 179 DCHECK(ws_usage); | |
| 180 memset(ws_usage, 0, sizeof(*ws_usage)); | |
| 181 | |
| 182 DWORD number_of_entries = 4096; // Just a guess. | |
| 183 WorkingSetInformationBuffer buffer; | |
| 184 int retries = 5; | |
| 185 for (;;) { | 180 for (;;) { |
| 186 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | 181 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + |
| 187 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | 182 (*number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); |
| 188 | 183 |
| 189 if (!buffer.Reserve(buffer_size)) | 184 if (!buffer.Reserve(buffer_size)) |
| 190 return false; | 185 return false; |
| 191 | 186 |
| 192 // Call the function once to get number of items | 187 // Call the function once to get number of items |
| 193 if (QueryWorkingSet(process_, buffer.get(), buffer_size)) | 188 if (QueryWorkingSet(process_, buffer.get(), buffer_size)) |
| 194 break; // Success | 189 break; // Success |
| 195 | 190 |
| 196 if (GetLastError() != ERROR_BAD_LENGTH) | 191 if (GetLastError() != ERROR_BAD_LENGTH) |
| 197 return false; | 192 return false; |
| 198 | 193 |
| 199 number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries); | 194 *number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries); |
| 200 | 195 |
| 201 // Maybe some entries are being added right now. Increase the buffer to | 196 // Maybe some entries are being added right now. Increase the buffer to |
| 202 // take that into account. Increasing by 10% should generally be enough, | 197 // take that into account. Increasing by 10% should generally be enough, |
| 203 // especially considering the potentially low memory condition during the | 198 // especially considering the potentially low memory condition during the |
| 204 // call (when called from OomMemoryDetails) and the potentially high | 199 // call (when called from OomMemoryDetails) and the potentially high |
| 205 // number of entries (300K was observed in crash dumps). | 200 // number of entries (300K was observed in crash dumps). |
| 206 number_of_entries = static_cast<DWORD>(number_of_entries * 1.1); | 201 *number_of_entries = static_cast<DWORD>(*number_of_entries * 1.1); |
| 207 | 202 |
| 208 if (--retries == 0) { | 203 if (--retries == 0) { |
| 209 // If we're looping, eventually fail. | 204 // If we're looping, eventually fail. |
| 210 return false; | 205 return false; |
| 211 } | 206 } |
| 212 } | 207 } |
| 213 | 208 |
| 214 // On windows 2000 the function returns 1 even when the buffer is too small. | 209 // On windows 2000 the function returns 1 even when the buffer is too small. |
| 215 // The number of entries that we are going to parse is the minimum between the | 210 // The number of entries that we are going to parse is the minimum between the |
| 216 // size we allocated and the real number of entries. | 211 // size we allocated and the real number of entries. |
| 217 number_of_entries = | 212 *number_of_entries = |
| 218 std::min(number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries)); | 213 std::min(*number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries)); |
| 214 | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | |
| 219 size_t ws_private = 0; | |
| 220 size_t ws_shareable = 0; | |
| 221 size_t ws_shared = 0; | |
| 222 | |
| 223 DCHECK(ws_usage); | |
| 224 memset(ws_usage, 0, sizeof(*ws_usage)); | |
| 225 | |
| 226 DWORD number_of_entries = 4096; // Just a guess. | |
|
brucedawson
2016/12/05 19:22:02
Probably no need to put this guess here. Instead h
chengx
2016/12/06 02:51:38
Done.
| |
| 227 WorkingSetInformationBuffer buffer; | |
| 228 if (!GetPageEntriesNum(buffer, process_, &number_of_entries)) | |
| 229 return false; | |
| 230 | |
| 219 for (unsigned int i = 0; i < number_of_entries; i++) { | 231 for (unsigned int i = 0; i < number_of_entries; i++) { |
| 220 if (buffer->WorkingSetInfo[i].Shared) { | 232 if (buffer->WorkingSetInfo[i].Shared) { |
| 221 ws_shareable++; | 233 ws_shareable++; |
| 222 if (buffer->WorkingSetInfo[i].ShareCount > 1) | 234 if (buffer->WorkingSetInfo[i].ShareCount > 1) |
| 223 ws_shared++; | 235 ws_shared++; |
| 224 } else { | 236 } else { |
| 225 ws_private++; | 237 ws_private++; |
| 226 } | 238 } |
| 227 } | 239 } |
| 228 | 240 |
| 229 ws_usage->priv = ws_private * PAGESIZE_KB; | 241 ws_usage->priv = ws_private * PAGESIZE_KB; |
| 230 ws_usage->shareable = ws_shareable * PAGESIZE_KB; | 242 ws_usage->shareable = ws_shareable * PAGESIZE_KB; |
| 231 ws_usage->shared = ws_shared * PAGESIZE_KB; | 243 ws_usage->shared = ws_shared * PAGESIZE_KB; |
| 244 | |
| 232 return true; | 245 return true; |
| 233 } | 246 } |
| 234 | 247 |
| 248 // This function calculates the proportional set size for a process. | |
| 249 bool ProcessMetrics::GetProportionalSetSizeBytes(uint64_t* pss_bytes) const { | |
| 250 double ws_pss = 0.0f; | |
| 251 | |
| 252 DWORD number_of_entries = 4096; // Just a guess. | |
| 253 WorkingSetInformationBuffer buffer; | |
| 254 if (!GetPageEntriesNum(buffer, process_, &number_of_entries)) | |
| 255 return false; | |
| 256 | |
| 257 for (unsigned int i = 0; i < number_of_entries; i++) { | |
| 258 if (buffer->WorkingSetInfo[i].Shared && | |
| 259 buffer->WorkingSetInfo[i].ShareCount > 0) | |
| 260 ws_pss += 1.0f / buffer->WorkingSetInfo[i].ShareCount; | |
| 261 else | |
| 262 ws_pss += 1.0f; | |
| 263 } | |
| 264 | |
| 265 *pss_bytes = static_cast<uint64_t>(ws_pss); | |
| 266 return true; | |
| 267 } | |
| 268 | |
| 235 static uint64_t FileTimeToUTC(const FILETIME& ftime) { | 269 static uint64_t FileTimeToUTC(const FILETIME& ftime) { |
| 236 LARGE_INTEGER li; | 270 LARGE_INTEGER li; |
| 237 li.LowPart = ftime.dwLowDateTime; | 271 li.LowPart = ftime.dwLowDateTime; |
| 238 li.HighPart = ftime.dwHighDateTime; | 272 li.HighPart = ftime.dwHighDateTime; |
| 239 return li.QuadPart; | 273 return li.QuadPart; |
| 240 } | 274 } |
| 241 | 275 |
| 242 double ProcessMetrics::GetCPUUsage() { | 276 double ProcessMetrics::GetCPUUsage() { |
| 243 FILETIME creation_time; | 277 FILETIME creation_time; |
| 244 FILETIME exit_time; | 278 FILETIME exit_time; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 352 |
| 319 meminfo->total = mem_status.ullTotalPhys / 1024; | 353 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 320 meminfo->free = mem_status.ullAvailPhys / 1024; | 354 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 321 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 355 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 322 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 356 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 323 | 357 |
| 324 return true; | 358 return true; |
| 325 } | 359 } |
| 326 | 360 |
| 327 } // namespace base | 361 } // namespace base |
| OLD | NEW |