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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 } | 138 } |
| 139 usage->image = committed_image / 1024; | 139 usage->image = committed_image / 1024; |
| 140 usage->mapped = committed_mapped / 1024; | 140 usage->mapped = committed_mapped / 1024; |
| 141 usage->priv = committed_private / 1024; | 141 usage->priv = committed_private / 1024; |
| 142 } | 142 } |
| 143 | 143 |
| 144 namespace { | 144 namespace { |
| 145 | 145 |
| 146 class WorkingSetInformationBuffer { | 146 class WorkingSetInformationBuffer { |
| 147 public: | 147 public: |
| 148 WorkingSetInformationBuffer() {} | 148 WorkingSetInformationBuffer() { number_of_entries = 0; } |
| 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() { return number_of_entries; } | |
|
brucedawson
2016/12/06 02:58:55
Should be const.
chengx
2016/12/06 19:40:09
Done.
| |
| 161 | |
| 162 // This function is used to get page entries for a process. | |
| 163 bool GetPageEntries(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) + | |
| 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 | |
| 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); | |
| 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. | |
| 197 // The number of entries that we are going to parse is the minimum between | |
| 198 // the | |
|
Primiano Tucci (use gerrit)
2016/12/06 17:47:57
I think clang-format did bite you here (It's a KI
chengx
2016/12/06 19:40:09
Acknowledged.
| |
| 199 // size we allocated and the real number of entries. | |
| 200 number_of_entries = std::min(number_of_entries, | |
| 201 static_cast<DWORD>(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 |
| 169 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); | 214 DISALLOW_COPY_AND_ASSIGN(WorkingSetInformationBuffer); |
| 215 | |
| 216 // Number of page entries. | |
| 217 DWORD number_of_entries; | |
|
brucedawson
2016/12/06 02:58:55
Could/should go "= 0;" here rather than listing th
chengx
2016/12/06 19:40:09
Done.
| |
| 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.GetPageEntries(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 for (unsigned int i = 0; i < buffer.GetPageEntriesNumber(); i++) { |
|
brucedawson
2016/12/06 02:58:55
Should generally avoid calling functions like this
chengx
2016/12/06 19:40:09
After a second thought, I think I will stick with
| |
| 190 return false; | |
| 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.0f; | |
| 254 | |
| 255 WorkingSetInformationBuffer buffer; | |
| 256 if (!buffer.GetPageEntries(process_)) | |
| 257 return false; | |
| 258 | |
| 259 for (unsigned int i = 0; i < buffer.GetPageEntriesNumber(); i++) { | |
| 260 if (buffer->WorkingSetInfo[i].Shared && | |
| 261 buffer->WorkingSetInfo[i].ShareCount > 0) | |
| 262 ws_pss += 1.0f / buffer->WorkingSetInfo[i].ShareCount; | |
|
brucedawson
2016/12/06 02:58:55
Remove trailing 'f' from this line and line 264. T
chengx
2016/12/06 19:40:09
Done.
| |
| 263 else | |
| 264 ws_pss += 1.0f; | |
| 265 } | |
| 266 | |
| 267 *pss_bytes = static_cast<uint64_t>(ws_pss); | |
|
Primiano Tucci (use gerrit)
2016/12/06 17:47:56
1) isn't this missing a * 4096 ? As the name sugge
chengx
2016/12/06 19:40:09
Yes, you are right. I am missing a *4096, which is
| |
| 268 return true; | |
| 269 } | |
| 270 | |
| 235 static uint64_t FileTimeToUTC(const FILETIME& ftime) { | 271 static uint64_t FileTimeToUTC(const FILETIME& ftime) { |
| 236 LARGE_INTEGER li; | 272 LARGE_INTEGER li; |
| 237 li.LowPart = ftime.dwLowDateTime; | 273 li.LowPart = ftime.dwLowDateTime; |
| 238 li.HighPart = ftime.dwHighDateTime; | 274 li.HighPart = ftime.dwHighDateTime; |
| 239 return li.QuadPart; | 275 return li.QuadPart; |
| 240 } | 276 } |
| 241 | 277 |
| 242 double ProcessMetrics::GetCPUUsage() { | 278 double ProcessMetrics::GetCPUUsage() { |
| 243 FILETIME creation_time; | 279 FILETIME creation_time; |
| 244 FILETIME exit_time; | 280 FILETIME exit_time; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 | 354 |
| 319 meminfo->total = mem_status.ullTotalPhys / 1024; | 355 meminfo->total = mem_status.ullTotalPhys / 1024; |
| 320 meminfo->free = mem_status.ullAvailPhys / 1024; | 356 meminfo->free = mem_status.ullAvailPhys / 1024; |
| 321 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; | 357 meminfo->swap_total = mem_status.ullTotalPageFile / 1024; |
| 322 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; | 358 meminfo->swap_free = mem_status.ullAvailPageFile / 1024; |
| 323 | 359 |
| 324 return true; | 360 return true; |
| 325 } | 361 } |
| 326 | 362 |
| 327 } // namespace base | 363 } // namespace base |
| OLD | NEW |