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