| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/private_working_set_snapshot.h" | 5 #include "chrome/browser/private_working_set_snapshot.h" |
| 6 | 6 |
| 7 #include <pdh.h> | 7 #include <pdh.h> |
| 8 #include <pdhmsg.h> | 8 #include <pdhmsg.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 if (PdhCollectQueryData(query_handle_.Get()) != ERROR_SUCCESS) | 108 if (PdhCollectQueryData(query_handle_.Get()) != ERROR_SUCCESS) |
| 109 return; | 109 return; |
| 110 | 110 |
| 111 for (auto& counter_pair : counter_pairs_) { | 111 for (auto& counter_pair : counter_pairs_) { |
| 112 // Find out how much space is required for the two counter arrays. | 112 // Find out how much space is required for the two counter arrays. |
| 113 // A return code of PDH_MORE_DATA indicates that we should call again with | 113 // A return code of PDH_MORE_DATA indicates that we should call again with |
| 114 // the buffer size returned. | 114 // the buffer size returned. |
| 115 DWORD buffer_size1 = 0; | 115 DWORD buffer_size1 = 0; |
| 116 DWORD item_count1 = 0; | 116 DWORD item_count1 = 0; |
| 117 // Process IDs should be retrieved as PDH_FMT_LONG | 117 // Process IDs should be retrieved as PDH_FMT_LONG |
| 118 if (PdhGetFormattedCounterArray(counter_pair.process_id_handle, | 118 if (PdhGetFormattedCounterArray( |
| 119 PDH_FMT_LONG, &buffer_size1, &item_count1, | 119 counter_pair.process_id_handle, PDH_FMT_LONG, &buffer_size1, |
| 120 nullptr) != PDH_MORE_DATA) | 120 &item_count1, nullptr) != static_cast<PDH_STATUS>(PDH_MORE_DATA)) |
| 121 continue; | 121 continue; |
| 122 if (buffer_size1 == 0 || item_count1 == 0) | 122 if (buffer_size1 == 0 || item_count1 == 0) |
| 123 continue; | 123 continue; |
| 124 | 124 |
| 125 DWORD buffer_size2 = 0; | 125 DWORD buffer_size2 = 0; |
| 126 DWORD item_count2 = 0; | 126 DWORD item_count2 = 0; |
| 127 // Working sets should be retrieved as PDH_FMT_LARGE (LONGLONG) | 127 // Working sets should be retrieved as PDH_FMT_LARGE (LONGLONG) |
| 128 // Note that if this second call to PdhGetFormattedCounterArray with the | 128 // Note that if this second call to PdhGetFormattedCounterArray with the |
| 129 // buffer size and count variables being zero is omitted then the PID and | 129 // buffer size and count variables being zero is omitted then the PID and |
| 130 // working-set results are not reliably correlated. | 130 // working-set results are not reliably correlated. |
| 131 if (PdhGetFormattedCounterArray(counter_pair.private_ws_handle, | 131 if (PdhGetFormattedCounterArray( |
| 132 PDH_FMT_LARGE, &buffer_size2, &item_count2, | 132 counter_pair.private_ws_handle, PDH_FMT_LARGE, &buffer_size2, |
| 133 nullptr) != PDH_MORE_DATA) | 133 &item_count2, nullptr) != static_cast<PDH_STATUS>(PDH_MORE_DATA)) |
| 134 continue; | 134 continue; |
| 135 | 135 |
| 136 // It is not clear whether Pdh guarantees that the two counters in the same | 136 // It is not clear whether Pdh guarantees that the two counters in the same |
| 137 // query will execute atomically - if they will see the same set of | 137 // query will execute atomically - if they will see the same set of |
| 138 // processes. If they do not then the correspondence between "ID Process" | 138 // processes. If they do not then the correspondence between "ID Process" |
| 139 // and "Working Set - Private" is lost and we have to discard these results. | 139 // and "Working Set - Private" is lost and we have to discard these results. |
| 140 // In testing these values have always matched. If this check fails then | 140 // In testing these values have always matched. If this check fails then |
| 141 // the old per-process memory calculations will be used instead. | 141 // the old per-process memory calculations will be used instead. |
| 142 if (buffer_size1 != buffer_size2 || item_count1 != item_count2) | 142 if (buffer_size1 != buffer_size2 || item_count1 != item_count2) |
| 143 continue; | 143 continue; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 size_t PrivateWorkingSetSnapshot::GetPrivateWorkingSet( | 182 size_t PrivateWorkingSetSnapshot::GetPrivateWorkingSet( |
| 183 base::ProcessId process_id) const { | 183 base::ProcessId process_id) const { |
| 184 // Do a binary search for the requested process ID and return the working set | 184 // Do a binary search for the requested process ID and return the working set |
| 185 // if found. | 185 // if found. |
| 186 auto p = std::lower_bound(records_.begin(), records_.end(), process_id); | 186 auto p = std::lower_bound(records_.begin(), records_.end(), process_id); |
| 187 if (p != records_.end() && p->process_id == process_id) | 187 if (p != records_.end() && p->process_id == process_id) |
| 188 return p->private_ws; | 188 return p->private_ws; |
| 189 | 189 |
| 190 return 0; | 190 return 0; |
| 191 } | 191 } |
| OLD | NEW |