| 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 #ifndef CHROME_BROWSER_PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
| 6 #define CHROME_BROWSER_PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
| 7 | |
| 8 #include "build/build_config.h" | |
| 9 | |
| 10 #if defined(OS_WIN) | |
| 11 #include <pdh.h> | |
| 12 #include <stddef.h> | |
| 13 | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/macros.h" | |
| 17 #include "base/process/process_handle.h" | |
| 18 #include "base/win/scoped_handle.h" | |
| 19 | |
| 20 namespace win { | |
| 21 | |
| 22 // The traits class for PDH handles that can be closed via PdhCloseQuery() API. | |
| 23 struct PDHHandleTraits { | |
| 24 typedef PDH_HQUERY Handle; | |
| 25 static PDH_HQUERY NullHandle() { return nullptr; } | |
| 26 static bool IsHandleValid(PDH_HQUERY handle) { return handle != nullptr; } | |
| 27 static bool CloseHandle(PDH_HQUERY handle) { | |
| 28 return (PdhCloseQuery(handle) == ERROR_SUCCESS); | |
| 29 } | |
| 30 | |
| 31 private: | |
| 32 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHHandleTraits); | |
| 33 }; | |
| 34 | |
| 35 // DummyVerifierTraits can be used because PDH_HQUERY is just a typedef for | |
| 36 // HANDLE. However HandleTraits cannot be used because PdhCloseQuery must be | |
| 37 // called rather than CloseHandle to dispose of the resources. | |
| 38 using ScopedPDH = | |
| 39 base::win::GenericScopedHandle<PDHHandleTraits, | |
| 40 base::win::DummyVerifierTraits>; | |
| 41 | |
| 42 } // namespace win | |
| 43 | |
| 44 // This class can be used to do bulk collection of private working sets. This | |
| 45 // exists because on Windows it is much faster to collect a group of private | |
| 46 // working sets all at once using PdhOpenQuery than to calculate the private | |
| 47 // working sets for each process individually. | |
| 48 class PrivateWorkingSetSnapshot { | |
| 49 public: | |
| 50 PrivateWorkingSetSnapshot(); | |
| 51 ~PrivateWorkingSetSnapshot(); | |
| 52 | |
| 53 // Add a process name that this object should monitor, such as "chrome". All | |
| 54 // processes whose name starts with this string will be monitored. | |
| 55 void AddToMonitorList(const std::string& process_name); | |
| 56 | |
| 57 // Query the system for working-set information for all monitored processes | |
| 58 // and update the results cache. This function may take a few ms to run. | |
| 59 // The time it takes seems to be independent of the number of processes it | |
| 60 // retrieves data for. This makes it faster than using QueryWorkingSet as soon | |
| 61 // as the process count exceeds two or three. | |
| 62 void Sample(); | |
| 63 | |
| 64 // Ask for the working set for a specific process, from the most recent call | |
| 65 // to Sample. If no data is available then zero will be returned. The result | |
| 66 // is in bytes. | |
| 67 size_t GetPrivateWorkingSet(base::ProcessId process_id) const; | |
| 68 | |
| 69 private: | |
| 70 // Initialize the PDH |query_handle_| and process any pending AddToMonitorList | |
| 71 // calls. | |
| 72 void Initialize(); | |
| 73 | |
| 74 // This holds a pair of Pdh counters to queries for the process ID and private | |
| 75 // working set for a particular process name being monitored. The results from | |
| 76 // the two queries can be matched up so that we can associate a private | |
| 77 // working set with a process ID. | |
| 78 struct PdhCounterPair { | |
| 79 // These are bound to query_handle_ and will be freed when it is closed. | |
| 80 // The handle to the 'counter' that retrieves process IDs. | |
| 81 PDH_HCOUNTER process_id_handle = nullptr; | |
| 82 // The handle to the 'counter' that retrieves private working sets. | |
| 83 PDH_HCOUNTER private_ws_handle = nullptr; | |
| 84 }; | |
| 85 | |
| 86 // Struct for storing a process ID and associated private working set. | |
| 87 struct PidAndPrivateWorkingSet { | |
| 88 base::ProcessId process_id; | |
| 89 size_t private_ws; | |
| 90 // Comparison function for sorting by process ID. | |
| 91 bool operator<(const PidAndPrivateWorkingSet& other) const { | |
| 92 // private_ws is intentionally *not* part of the comparison because it is | |
| 93 // the payload and process_id is the key. | |
| 94 return process_id < other.process_id; | |
| 95 } | |
| 96 // Comparison function for searching by process ID. | |
| 97 bool operator<(const base::ProcessId other_process_id) const { | |
| 98 return process_id < other_process_id; | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 // True if Initialize() has been called, whether it succeeded or not. | |
| 103 bool initialized_ = false; | |
| 104 | |
| 105 // The handle to the query object. | |
| 106 win::ScopedPDH query_handle_; | |
| 107 | |
| 108 // A vector of process names to monitor when initialization occurs. | |
| 109 std::vector<std::string> process_names_; | |
| 110 | |
| 111 // A PdhCounterPair for each successful AddToMonitorList call. | |
| 112 std::vector<PdhCounterPair> counter_pairs_; | |
| 113 | |
| 114 // After each call to Sample this will hold the results, sorted by process id. | |
| 115 std::vector<PidAndPrivateWorkingSet> records_; | |
| 116 | |
| 117 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot); | |
| 118 }; | |
| 119 | |
| 120 #endif // defined(OS_WIN) | |
| 121 | |
| 122 #endif // CHROME_BROWSER_PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
| OLD | NEW |