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 | |
13 #include <vector> | |
14 | |
15 #include "base/process/process_handle.h" | |
16 #include "base/win/scoped_handle.h" | |
17 | |
18 namespace win { | |
19 | |
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); | |
27 } | |
28 | |
29 private: | |
30 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHHandleTraits); | |
31 }; | |
32 | |
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. | |
36 typedef base::win::GenericScopedHandle<PDHHandleTraits, | |
37 base::win::DummyVerifierTraits> | |
38 ScopedPDH; | |
ncarter (slow)
2015/07/01 16:50:26
This could be a "using =" alias.
brucedawson
2015/07/01 17:47:40
Done.
| |
39 | |
40 } // namespace win | |
41 | |
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 { | |
47 public: | |
48 PrivateWorkingSetSnapshot(); | |
49 ~PrivateWorkingSetSnapshot(); | |
50 | |
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); | |
54 | |
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. | |
60 void Sample(); | |
61 | |
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 | |
64 // is in bytes. | |
65 size_t GetPrivateWorkingSet(base::ProcessId process_id) const; | |
66 | |
67 private: | |
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; | |
78 }; | |
79 | |
80 // Struct for storing a process ID and associated private working set. | |
81 struct PidAndPrivateWorkingSet { | |
82 base::ProcessId process_id; | |
83 size_t private_ws; | |
84 // Comparison function for sorting by process ID. | |
85 bool operator<(const PidAndPrivateWorkingSet& other) const { | |
86 return process_id < other.process_id; | |
ncarter (slow)
2015/07/01 16:50:26
I read the comment about "incomplete operator< imp
brucedawson
2015/07/01 17:47:40
I'll add a comment to make that crystal clear. I t
| |
87 } | |
88 // Comparison function for searching. | |
89 bool operator<(const base::ProcessId other_process_id) const { | |
90 return process_id < other_process_id; | |
91 } | |
92 }; | |
93 | |
94 // The handle to the query object. | |
95 win::ScopedPDH query_handle_; | |
96 | |
97 // A PdhCounterPair for each successful AddToMonitorList call. | |
98 std::vector<PdhCounterPair> counter_pairs_; | |
99 | |
100 // After each call to Sample this will hold the results, sorted by process id. | |
101 std::vector<PidAndPrivateWorkingSet> records_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot); | |
104 }; | |
105 | |
106 #endif // defined(OS_WIN) | |
107 | |
108 #endif // CHROME_BROWSER_PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
OLD | NEW |