OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/process/private_working_set_snapshot.h" |
| 6 |
| 7 #include <pdh.h> |
| 8 #include <pdhmsg.h> |
| 9 |
| 10 #include <algorithm> |
| 11 |
| 12 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/win/windows_version.h" |
| 14 |
| 15 namespace base { |
| 16 |
| 17 PrivateWorkingSetSnapshot::PrivateWorkingSetSnapshot() { |
| 18 // The Pdh APIs are supported on Windows XP, but the "Working Set - Private" |
| 19 // counter that PrivateWorkingSetSnapshot depends on is not defined until |
| 20 // Windows Vista. Early-out to avoid wasted effort. All queries will return |
| 21 // zero and will have to use the fallback calculations. |
| 22 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 23 return; |
| 24 |
| 25 // Create a Pdh query |
| 26 PDH_HQUERY query_handle; |
| 27 if (PdhOpenQuery(NULL, NULL, &query_handle) != ERROR_SUCCESS) { |
| 28 return; |
| 29 } |
| 30 |
| 31 query_handle_.Set(query_handle); |
| 32 } |
| 33 |
| 34 PrivateWorkingSetSnapshot::~PrivateWorkingSetSnapshot() { |
| 35 } |
| 36 |
| 37 void PrivateWorkingSetSnapshot::AddToMonitorList( |
| 38 const std::string& process_name) { |
| 39 if (!query_handle_.IsValid()) |
| 40 return; |
| 41 |
| 42 // Create the magic strings that will return a list of process IDs and a list |
| 43 // of private working sets. The 'process_name' variable should be something |
| 44 // like "chrome". The '*' character indicates that we want records for all |
| 45 // processes whose names start with process_name - all chrome processes, but |
| 46 // also all 'chrome_editor.exe' processes or other matching names. The excess |
| 47 // information is unavoidable but harmless. |
| 48 std::string process_id_query = "\\Process(" + process_name + "*)\\ID Process"; |
| 49 std::string private_ws_query = |
| 50 "\\Process(" + process_name + "*)\\Working Set - Private"; |
| 51 |
| 52 // Add the two counters to the query. |
| 53 PdhCounterPair new_counters; |
| 54 if (PdhAddCounterA(query_handle_.Get(), process_id_query.c_str(), NULL, |
| 55 &new_counters.process_id_handle) != ERROR_SUCCESS) { |
| 56 return; |
| 57 } |
| 58 |
| 59 // If adding the second counter fails then we should remove the first one. |
| 60 if (PdhAddCounterA(query_handle_.Get(), private_ws_query.c_str(), NULL, |
| 61 &new_counters.private_ws_handle) != ERROR_SUCCESS) { |
| 62 PdhRemoveCounter(new_counters.process_id_handle); |
| 63 } |
| 64 |
| 65 // Record the pair of counter query handles so that we can query them later. |
| 66 counter_pairs_.push_back(new_counters); |
| 67 } |
| 68 |
| 69 void PrivateWorkingSetSnapshot::Sample() { |
| 70 if (counter_pairs_.empty()) |
| 71 return; |
| 72 |
| 73 // Destroy all previous data. |
| 74 records_.resize(0); |
| 75 // Record the requested data into PDH's internal buffers. |
| 76 if (PdhCollectQueryData(query_handle_.Get()) != ERROR_SUCCESS) |
| 77 return; |
| 78 |
| 79 for (auto& counter_pair : counter_pairs_) { |
| 80 // Find out how much space is required for the two counter arrays. |
| 81 // A return code of PDH_MORE_DATA indicates that we should call again with |
| 82 // the buffer size returned. |
| 83 DWORD buffer_size1 = 0; |
| 84 DWORD item_count1 = 0; |
| 85 // Process IDs should be retrieved as PDH_FMT_LONG |
| 86 if (PdhGetFormattedCounterArray(counter_pair.process_id_handle, |
| 87 PDH_FMT_LONG, &buffer_size1, &item_count1, |
| 88 nullptr) != PDH_MORE_DATA) |
| 89 continue; |
| 90 if (buffer_size1 == 0 || item_count1 == 0) |
| 91 continue; |
| 92 |
| 93 DWORD buffer_size2 = 0; |
| 94 DWORD item_count2 = 0; |
| 95 // Working sets should be retrieved as PDH_FMT_LARGE (LONGLONG) |
| 96 // Note that if this second call to PdhGetFormattedCounterArray with the |
| 97 // buffer size and count variables being zero is omitted then the PID and |
| 98 // working-set results are not reliably correlated. |
| 99 if (PdhGetFormattedCounterArray(counter_pair.private_ws_handle, |
| 100 PDH_FMT_LARGE, &buffer_size2, &item_count2, |
| 101 nullptr) != PDH_MORE_DATA) |
| 102 continue; |
| 103 |
| 104 // It is not clear whether Pdh guarantees that the two counters in the same |
| 105 // query will execute atomically - if they will see the same set of |
| 106 // processes. If they do not then the correspondence between "ID Process" |
| 107 // and "Working Set - Private" is lost and we have to discard these results. |
| 108 // In testing these values have always matched. If this check fails then |
| 109 // the old per-process memory calculations will be used instead. |
| 110 if (buffer_size1 != buffer_size2 && item_count1 != item_count2) |
| 111 continue; |
| 112 |
| 113 // Allocate enough space for the results of both queries. |
| 114 std::vector<char> buffer(buffer_size1 * 2); |
| 115 // Retrieve the process ID data. |
| 116 auto process_id_data = |
| 117 reinterpret_cast<PDH_FMT_COUNTERVALUE_ITEM*>(&buffer[0]); |
| 118 if (PdhGetFormattedCounterArray(counter_pair.process_id_handle, |
| 119 PDH_FMT_LONG, &buffer_size1, &item_count1, |
| 120 process_id_data) != ERROR_SUCCESS) |
| 121 continue; |
| 122 // Retrieve the private working set data. |
| 123 auto private_ws_data = |
| 124 reinterpret_cast<PDH_FMT_COUNTERVALUE_ITEM*>(&buffer[buffer_size1]); |
| 125 if (PdhGetFormattedCounterArray(counter_pair.private_ws_handle, |
| 126 PDH_FMT_LARGE, &buffer_size1, &item_count1, |
| 127 private_ws_data) != ERROR_SUCCESS) |
| 128 continue; |
| 129 |
| 130 // Make room for the new set of records. |
| 131 size_t start_offset = records_.size(); |
| 132 records_.resize(start_offset + item_count1); |
| 133 |
| 134 for (DWORD i = 0; i < item_count1; ++i) { |
| 135 records_[start_offset + i].process_id = |
| 136 process_id_data[i].FmtValue.longValue; |
| 137 // Integer overflow can happen here if a 32-bit process is monitoring a |
| 138 // 64-bit process so we do a saturated_cast. |
| 139 records_[start_offset + i].private_ws = |
| 140 saturated_cast<size_t>(private_ws_data[i].FmtValue.largeValue); |
| 141 } |
| 142 } |
| 143 |
| 144 // The results will include all processes that match the passed in name, |
| 145 // regardless of whether they are spawned by the calling process. |
| 146 // The results must be sorted by process ID for efficient lookup. |
| 147 std::sort(records_.begin(), records_.end()); |
| 148 } |
| 149 |
| 150 size_t PrivateWorkingSetSnapshot::GetPrivateWorkingSet( |
| 151 base::ProcessId process_id) const { |
| 152 // Do a binary search for the requested process ID and return the working set |
| 153 // if found. |
| 154 auto p = std::lower_bound(records_.begin(), records_.end(), process_id); |
| 155 if (p != records_.end() && p->process_id == process_id) |
| 156 return p->private_ws; |
| 157 |
| 158 return 0; |
| 159 } |
| 160 |
| 161 } // namespace base |
OLD | NEW |