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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 WorkingSetInformationBuffer() {} | 148 WorkingSetInformationBuffer() {} |
| 149 ~WorkingSetInformationBuffer() { Clear(); } | 149 ~WorkingSetInformationBuffer() { Clear(); } |
| 150 | 150 |
| 151 bool Reserve(size_t size) { | 151 bool Reserve(size_t size) { |
| 152 Clear(); | 152 Clear(); |
| 153 // Use UncheckedMalloc here because this can be called from the code | 153 // Use UncheckedMalloc here because this can be called from the code |
| 154 // that handles low memory condition. | 154 // that handles low memory condition. |
| 155 return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_)); | 155 return UncheckedMalloc(size, reinterpret_cast<void**>(&buffer_)); |
| 156 } | 156 } |
| 157 | 157 |
| 158 PSAPI_WORKING_SET_INFORMATION* get() { return buffer_; } | |
| 159 const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; } | 158 const PSAPI_WORKING_SET_INFORMATION* operator ->() const { return buffer_; } |
| 160 | 159 |
| 160 size_t GetPageEntryCount() const { return number_of_entries; } | |
| 161 | |
| 162 // This function is used to get page entries for a process. | |
| 163 bool QueryPageEntries(const ProcessHandle& process_) { | |
| 164 int retries = 5; | |
| 165 number_of_entries = 4096; // Just a guess. | |
| 166 | |
| 167 for (;;) { | |
| 168 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | |
|
dcheng
2016/12/10 00:42:00
Nit: this should be size_t
| |
| 169 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | |
| 170 | |
| 171 if (!Reserve(buffer_size)) | |
| 172 return false; | |
| 173 | |
| 174 // On success, | buffer_ | is populated with info about the working set of | |
| 175 // | process_ | .On ERROR_BAD_LENGTH failure, increase the size of the | |
|
dcheng
2016/12/10 00:42:00
Nit: no spaces between the identifier and |: this
| |
| 176 // buffer and try again. | |
| 177 if (QueryWorkingSet(process_, buffer_, buffer_size)) | |
| 178 break; // Success | |
| 179 | |
| 180 if (GetLastError() != ERROR_BAD_LENGTH) | |
| 181 return false; | |
| 182 | |
| 183 number_of_entries = buffer_->NumberOfEntries; | |
| 184 | |
| 185 // Maybe some entries are being added right now. Increase the buffer to | |
| 186 // take that into account. Increasing by 10% should generally be enough, | |
| 187 // especially considering the potentially low memory condition during the | |
| 188 // call (when called from OomMemoryDetails) and the potentially high | |
| 189 // number of entries (300K was observed in crash dumps). | |
| 190 number_of_entries *= 1.1; | |
| 191 | |
| 192 if (--retries == 0) { | |
| 193 // If we're looping, eventually fail. | |
| 194 return false; | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 // On windows 2000 the function returns 1 even when the buffer is too small. | |
| 199 // The number of entries that we are going to parse is the minimum between | |
| 200 // the size we allocated and the real number of entries. | |
| 201 number_of_entries = std::min(number_of_entries, buffer_->NumberOfEntries); | |
| 202 | |
| 203 return true; | |
| 204 } | |
| 205 | |
| 161 private: | 206 private: |
| 162 void Clear() { | 207 void Clear() { |
| 163 free(buffer_); | 208 free(buffer_); |
| 164 buffer_ = nullptr; | 209 buffer_ = nullptr; |
| 165 } | 210 } |
| 166 | 211 |
| 167 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; | 212 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; |
| 168 | 213 |
| 214 // Number of page entries. | |
| 215 size_t number_of_entries = 0; | |
| 216 | |
| 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); | 217 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); |
| 170 }; | 218 }; |
| 171 | 219 |
| 172 } // namespace | 220 } // namespace |
| 173 | 221 |
| 174 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 222 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 175 size_t ws_private = 0; | 223 size_t ws_private = 0; |
| 176 size_t ws_shareable = 0; | 224 size_t ws_shareable = 0; |
| 177 size_t ws_shared = 0; | 225 size_t ws_shared = 0; |
| 178 | 226 |
| 179 DCHECK(ws_usage); | 227 DCHECK(ws_usage); |
| 180 memset(ws_usage, 0, sizeof(*ws_usage)); | 228 memset(ws_usage, 0, sizeof(*ws_usage)); |
| 181 | 229 |
| 182 DWORD number_of_entries = 4096; // Just a guess. | |
| 183 WorkingSetInformationBuffer buffer; | 230 WorkingSetInformationBuffer buffer; |
| 184 int retries = 5; | 231 if (!buffer.QueryPageEntries(process_)) |
| 185 for (;;) { | 232 return false; |
| 186 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | |
| 187 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | |
| 188 | 233 |
| 189 if (!buffer.Reserve(buffer_size)) | 234 size_t num_page_entries = buffer.GetPageEntryCount(); |
| 190 return false; | 235 for (size_t i = 0; i < num_page_entries; i++) { |
| 191 | |
| 192 // Call the function once to get number of items | |
| 193 if (QueryWorkingSet(process_, buffer.get(), buffer_size)) | |
| 194 break; // Success | |
| 195 | |
| 196 if (GetLastError() != ERROR_BAD_LENGTH) | |
| 197 return false; | |
| 198 | |
| 199 number_of_entries = static_cast<DWORD>(buffer->NumberOfEntries); | |
| 200 | |
| 201 // Maybe some entries are being added right now. Increase the buffer to | |
| 202 // take that into account. Increasing by 10% should generally be enough, | |
| 203 // especially considering the potentially low memory condition during the | |
| 204 // call (when called from OomMemoryDetails) and the potentially high | |
| 205 // number of entries (300K was observed in crash dumps). | |
| 206 number_of_entries = static_cast<DWORD>(number_of_entries * 1.1); | |
| 207 | |
| 208 if (--retries == 0) { | |
| 209 // If we're looping, eventually fail. | |
| 210 return false; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // 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 | |
| 216 // size we allocated and the real number of entries. | |
| 217 number_of_entries = | |
| 218 std::min(number_of_entries, static_cast<DWORD>(buffer->NumberOfEntries)); | |
| 219 for (unsigned int i = 0; i < number_of_entries; i++) { | |
| 220 if (buffer->WorkingSetInfo[i].Shared) { | 236 if (buffer->WorkingSetInfo[i].Shared) { |
| 221 ws_shareable++; | 237 ws_shareable++; |
| 222 if (buffer->WorkingSetInfo[i].ShareCount > 1) | 238 if (buffer->WorkingSetInfo[i].ShareCount > 1) |
| 223 ws_shared++; | 239 ws_shared++; |
| 224 } else { | 240 } else { |
| 225 ws_private++; | 241 ws_private++; |
| 226 } | 242 } |
| 227 } | 243 } |
| 228 | 244 |
| 229 ws_usage->priv = ws_private * PAGESIZE_KB; | 245 ws_usage->priv = ws_private * PAGESIZE_KB; |
| 230 ws_usage->shareable = ws_shareable * PAGESIZE_KB; | 246 ws_usage->shareable = ws_shareable * PAGESIZE_KB; |
| 231 ws_usage->shared = ws_shared * PAGESIZE_KB; | 247 ws_usage->shared = ws_shared * PAGESIZE_KB; |
| 248 | |
| 232 return true; | 249 return true; |
| 233 } | 250 } |
| 234 | 251 |
| 252 // This function calculates the proportional set size for a process. | |
| 253 bool ProcessMetrics::GetProportionalSetSizeBytes(uint64_t* pss_bytes) const { | |
| 254 double ws_pss = 0.0; | |
| 255 | |
| 256 WorkingSetInformationBuffer buffer; | |
| 257 if (!buffer.QueryPageEntries(process_)) | |
| 258 return false; | |
| 259 | |
| 260 size_t num_page_entries = buffer.GetPageEntryCount(); | |
| 261 for (size_t i = 0; i < num_page_entries; i++) { | |
| 262 if (buffer->WorkingSetInfo[i].Shared && | |
| 263 buffer->WorkingSetInfo[i].ShareCount > 0) | |
| 264 ws_pss += 1.0 / buffer->WorkingSetInfo[i].ShareCount; | |
| 265 else | |
| 266 ws_pss += 1.0; | |
| 267 } | |
| 268 | |
| 269 *pss_bytes = static_cast<uint64_t>(ws_pss * GetPageSize()); | |
| 270 return true; | |
| 271 } | |
| 272 | |
| 235 static uint64_t FileTimeToUTC(const FILETIME& ftime) { | 273 static uint64_t FileTimeToUTC(const FILETIME& ftime) { |
| 236 LARGE_INTEGER li; | 274 LARGE_INTEGER li; |
| 237 li.LowPart = ftime.dwLowDateTime; | 275 li.LowPart = ftime.dwLowDateTime; |
| 238 li.HighPart = ftime.dwHighDateTime; | 276 li.HighPart = ftime.dwHighDateTime; |
| 239 return li.QuadPart; | 277 return li.QuadPart; |
| 240 } | 278 } |
| 241 | 279 |
| 242 double ProcessMetrics::GetCPUUsage() { | 280 double ProcessMetrics::GetCPUUsage() { |
| 243 FILETIME creation_time; | 281 FILETIME creation_time; |
| 244 FILETIME exit_time; | 282 FILETIME exit_time; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 356 |
| 319 meminfo->total = mem_status.ullTotalPhys / 1024; | 357 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 320 meminfo->free = mem_status.ullAvailPhys / 1024; | 358 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 321 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 359 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 322 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 360 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 323 | 361 |
| 324 return true; | 362 return true; |
| 325 } | 363 } |
| 326 | 364 |
| 327 } // namespace base | 365 } // namespace base |
| OLD | NEW |