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 DWORD GetPageEntriesNumber() const { return number_of_entries; } | |
|
stanisc
2016/12/09 01:19:55
Nit: Consider renaming this to either GetPageEntry
chengx
2016/12/09 22:34:32
renamed to GetPageEntryCount
| |
| 161 | |
| 162 // This function is used to get page entries for a process. | |
| 163 bool GetPageEntries(const ProcessHandle& process_) { | |
|
stanisc
2016/12/09 01:19:55
Nit: This doesn't get actual entries so perhaps th
chengx
2016/12/09 22:34:32
Done.
| |
| 164 int retries = 5; | |
| 165 number_of_entries = 4096; // Just a guess. | |
| 166 | |
| 167 for (;;) { | |
| 168 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | |
| 169 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | |
| 170 | |
| 171 if (!Reserve(buffer_size)) | |
| 172 return false; | |
| 173 | |
| 174 // Call the function once to get number of items | |
|
dcheng
2016/12/08 20:25:30
Nit: I know this is just moving code, but this com
chengx
2016/12/08 23:03:13
That is a good comment and I will take it.
| |
| 175 if (QueryWorkingSet(process_, buffer_, buffer_size)) | |
| 176 break; // Success | |
| 177 | |
| 178 if (GetLastError() != ERROR_BAD_LENGTH) | |
| 179 return false; | |
| 180 | |
| 181 number_of_entries = static_cast<DWORD>(buffer_->NumberOfEntries); | |
|
dcheng
2016/12/08 20:25:30
Is it possible to just make this a ULONG_PTR as we
chengx
2016/12/08 23:03:13
I personally think ULONG_PTR should be fine. I wil
stanisc
2016/12/09 01:19:55
I am not the original author, but I did modify thi
chengx
2016/12/09 22:34:32
I will just leave the old comment there for now.
| |
| 182 | |
| 183 // Maybe some entries are being added right now. Increase the buffer to | |
| 184 // take that into account. Increasing by 10% should generally be enough, | |
| 185 // especially considering the potentially low memory condition during the | |
| 186 // call (when called from OomMemoryDetails) and the potentially high | |
| 187 // number of entries (300K was observed in crash dumps). | |
| 188 number_of_entries = static_cast<DWORD>(number_of_entries * 1.1); | |
| 189 | |
| 190 if (--retries == 0) { | |
| 191 // If we're looping, eventually fail. | |
| 192 return false; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // On windows 2000 the function returns 1 even when the buffer is too small. | |
|
dcheng
2016/12/08 20:25:30
I don't think we support win2k?
stanisc
2016/12/09 01:19:55
This might be an old comment.
chengx
2016/12/09 22:34:32
Acknowledged.
| |
| 197 // The number of entries that we are going to parse is the minimum between | |
| 198 // the size we allocated and the real number of entries. | |
| 199 number_of_entries = std::min(number_of_entries, | |
| 200 static_cast<DWORD>(buffer_->NumberOfEntries)); | |
| 201 | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 161 private: | 205 private: |
| 162 void Clear() { | 206 void Clear() { |
| 163 free(buffer_); | 207 free(buffer_); |
| 164 buffer_ = nullptr; | 208 buffer_ = nullptr; |
| 165 } | 209 } |
| 166 | 210 |
| 167 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; | 211 PSAPI_WORKING_SET_INFORMATION* buffer_ = nullptr; |
| 168 | 212 |
| 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); | 213 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); |
|
dcheng
2016/12/08 20:25:30
Nit: this is generally last
chengx
2016/12/08 23:03:13
Done.
| |
| 214 | |
| 215 // Number of page entries. | |
| 216 DWORD number_of_entries = 0; | |
| 170 }; | 217 }; |
| 171 | 218 |
| 172 } // namespace | 219 } // namespace |
| 173 | 220 |
| 174 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { | 221 bool ProcessMetrics::GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const { |
| 175 size_t ws_private = 0; | 222 size_t ws_private = 0; |
| 176 size_t ws_shareable = 0; | 223 size_t ws_shareable = 0; |
| 177 size_t ws_shared = 0; | 224 size_t ws_shared = 0; |
| 178 | 225 |
| 179 DCHECK(ws_usage); | 226 DCHECK(ws_usage); |
| 180 memset(ws_usage, 0, sizeof(*ws_usage)); | 227 memset(ws_usage, 0, sizeof(*ws_usage)); |
| 181 | 228 |
| 182 DWORD number_of_entries = 4096; // Just a guess. | |
| 183 WorkingSetInformationBuffer buffer; | 229 WorkingSetInformationBuffer buffer; |
| 184 int retries = 5; | 230 if (!buffer.GetPageEntries(process_)) |
| 185 for (;;) { | 231 return false; |
| 186 DWORD buffer_size = sizeof(PSAPI_WORKING_SET_INFORMATION) + | |
| 187 (number_of_entries * sizeof(PSAPI_WORKING_SET_BLOCK)); | |
| 188 | 232 |
| 189 if (!buffer.Reserve(buffer_size)) | 233 DWORD numPageEntries = buffer.GetPageEntriesNumber(); |
|
dcheng
2016/12/08 20:25:30
Nit: num_page_entries
chengx
2016/12/08 23:03:13
Done.
| |
| 190 return false; | 234 for (unsigned int i = 0; i < numPageEntries; i++) { |
|
dcheng
2016/12/08 20:25:30
Match types (this should be DWORD or ULONG_PTR, wh
chengx
2016/12/08 23:03:13
I will do DWORD for now.
stanisc
2016/12/09 01:19:55
Consider using size_t as I mentioned above.
chengx
2016/12/09 22:34:32
Done.
| |
| 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) { | 235 if (buffer->WorkingSetInfo[i].Shared) { |
| 221 ws_shareable++; | 236 ws_shareable++; |
| 222 if (buffer->WorkingSetInfo[i].ShareCount > 1) | 237 if (buffer->WorkingSetInfo[i].ShareCount > 1) |
| 223 ws_shared++; | 238 ws_shared++; |
| 224 } else { | 239 } else { |
| 225 ws_private++; | 240 ws_private++; |
| 226 } | 241 } |
| 227 } | 242 } |
| 228 | 243 |
| 229 ws_usage->priv = ws_private * PAGESIZE_KB; | 244 ws_usage->priv = ws_private * PAGESIZE_KB; |
| 230 ws_usage->shareable = ws_shareable * PAGESIZE_KB; | 245 ws_usage->shareable = ws_shareable * PAGESIZE_KB; |
| 231 ws_usage->shared = ws_shared * PAGESIZE_KB; | 246 ws_usage->shared = ws_shared * PAGESIZE_KB; |
| 247 | |
| 232 return true; | 248 return true; |
| 233 } | 249 } |
| 234 | 250 |
| 251 // This function calculates the proportional set size for a process. | |
| 252 bool ProcessMetrics::GetProportionalSetSizeBytes(uint64_t* pss_bytes) const { | |
| 253 double ws_pss = 0.0; | |
| 254 | |
| 255 WorkingSetInformationBuffer buffer; | |
| 256 if (!buffer.GetPageEntries(process_)) | |
| 257 return false; | |
| 258 | |
| 259 DWORD numPageEntries = buffer.GetPageEntriesNumber(); | |
| 260 for (unsigned int i = 0; i < numPageEntries; i++) { | |
|
dcheng
2016/12/08 20:25:30
Ditto.
| |
| 261 if (buffer->WorkingSetInfo[i].Shared && | |
| 262 buffer->WorkingSetInfo[i].ShareCount > 0) | |
| 263 ws_pss += 1.0 / buffer->WorkingSetInfo[i].ShareCount; | |
| 264 else | |
| 265 ws_pss += 1.0; | |
| 266 } | |
| 267 | |
| 268 *pss_bytes = static_cast<uint64_t>(ws_pss * GetPageSize()); | |
| 269 return true; | |
| 270 } | |
| 271 | |
| 235 static uint64_t FileTimeToUTC(const FILETIME& ftime) { | 272 static uint64_t FileTimeToUTC(const FILETIME& ftime) { |
| 236 LARGE_INTEGER li; | 273 LARGE_INTEGER li; |
| 237 li.LowPart = ftime.dwLowDateTime; | 274 li.LowPart = ftime.dwLowDateTime; |
| 238 li.HighPart = ftime.dwHighDateTime; | 275 li.HighPart = ftime.dwHighDateTime; |
| 239 return li.QuadPart; | 276 return li.QuadPart; |
| 240 } | 277 } |
| 241 | 278 |
| 242 double ProcessMetrics::GetCPUUsage() { | 279 double ProcessMetrics::GetCPUUsage() { |
| 243 FILETIME creation_time; | 280 FILETIME creation_time; |
| 244 FILETIME exit_time; | 281 FILETIME exit_time; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 355 |
| 319 meminfo->total = mem_status.ullTotalPhys / 1024; | 356 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 320 meminfo->free = mem_status.ullAvailPhys / 1024; | 357 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 321 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 358 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 322 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 359 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 323 | 360 |
| 324 return true; | 361 return true; |
| 325 } | 362 } |
| 326 | 363 |
| 327 } // namespace base | 364 } // namespace base |
| OLD | NEW |