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 // This holds a pair of Pdh counters to queries for the process ID and private
69 // working set for a particular process name being monitored. The results from
70 // the two queries can be matched up so that we can associate a private
71 // working set with a process ID.
72 struct PdhCounterPair
{
73 // These are bound to query_handle_ and will be freed when it is closed.
74 // The handle to the 'counter' that retrieves process IDs.
75 PDH_HCOUNTER process_id_handle
= nullptr;
76 // The handle to the 'counter' that retrieves private working sets.
77 PDH_HCOUNTER private_ws_handle
= nullptr;
80 // Struct for storing a process ID and associated private working set.
81 struct PidAndPrivateWorkingSet
{
82 base::ProcessId process_id
;
84 // Comparison function for sorting by process ID.
85 bool operator<(const PidAndPrivateWorkingSet
& other
) const {
86 // private_ws is intentionally *not* part of the comparison because it is
87 // the payload and process_id is the key.
88 return process_id
< other
.process_id
;
90 // Comparison function for searching by process ID.
91 bool operator<(const base::ProcessId other_process_id
) const {
92 return process_id
< other_process_id
;
96 // The handle to the query object.
97 win::ScopedPDH query_handle_
;
99 // A PdhCounterPair for each successful AddToMonitorList call.
100 std::vector
<PdhCounterPair
> counter_pairs_
;
102 // After each call to Sample this will hold the results, sorted by process id.
103 std::vector
<PidAndPrivateWorkingSet
> records_
;
105 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot
);
108 #endif // defined(OS_WIN)
110 #endif // CHROME_BROWSER_PRIVATE_WORKING_SET_SNAPSHOT_H_