OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2009 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 PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
6 #define PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/base_export.h" | |
11 #include "base/process/process_handle.h" | |
12 #include "base/win/scoped_handle.h" | |
13 | |
14 #if defined(OS_WIN) | |
15 | |
16 #include <pdh.h> | |
17 | |
18 namespace base { | |
19 | |
20 namespace win { | |
21 | |
22 // The traits class for PDH handles that can be closed via PdhCloseQuery() API. | |
23 class PDHHandleTraits { | |
24 public: | |
25 typedef PDH_HQUERY Handle; | |
26 | |
27 // Closes the handle. | |
28 static bool BASE_EXPORT CloseHandle(PDH_HQUERY handle) { | |
29 return (PdhCloseQuery(handle) == ERROR_SUCCESS); | |
30 } | |
31 | |
32 // Returns true if the handle value is valid. | |
33 static bool IsHandleValid(PDH_HQUERY handle) { | |
34 return handle != nullptr; | |
35 } | |
36 | |
37 // Returns NULL handle value. | |
38 static PDH_HQUERY NullHandle() { | |
39 return nullptr; | |
40 } | |
41 | |
42 private: | |
43 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHHandleTraits); | |
44 }; | |
45 | |
46 | |
47 // Do-nothing verifier. | |
48 class PDHDummyVerifierTraits { | |
ncarter (slow)
2015/06/26 21:50:46
Does using the preexisting DummyVerifier not compi
brucedawson
2015/06/27 00:13:06
You're right, it is. I also followed the pattern y
| |
49 public: | |
50 typedef PDH_HQUERY Handle; | |
51 | |
52 static void StartTracking(PDH_HQUERY handle, const void* owner, | |
53 const void* pc1, const void* pc2) {} | |
54 static void StopTracking(PDH_HQUERY handle, const void* owner, | |
55 const void* pc1, const void* pc2) {} | |
56 | |
57 private: | |
58 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHDummyVerifierTraits); | |
59 }; | |
60 | |
61 typedef GenericScopedHandle<PDHHandleTraits, PDHDummyVerifierTraits> ScopedPDH; | |
62 } // namespace win | |
63 | |
64 // This class can be used to do bulk collection of private working sets. This | |
65 // exists because on Windows it is much faster to collect a group of private | |
66 // working sets all at once using PdhOpenQuery than to calculate the private | |
67 // working sets for each process individually. | |
68 class BASE_EXPORT PrivateWorkingSetSnapshot { | |
69 public: | |
70 PrivateWorkingSetSnapshot(); | |
71 // Add a process name that this object should monitor, such as "chrome". All | |
72 // processes whose name starts with this string will be monitored. | |
73 void AddToMonitorList(const std::string& process_name); | |
74 // Query the system for working-set information for all monitored processes | |
75 // and update the results cache. This function may take a few ms to run. | |
76 void Sample(); | |
77 // Ask for the working set for a specific process, from the most recent call | |
78 // to Sample. If no data is available then zero will be returned. The result | |
79 // is in bytes. | |
80 size_t GetPrivateWorkingSet(base::ProcessId process_id) const; | |
81 | |
82 private: | |
83 // The handle to the query object. | |
84 win::ScopedPDH query_handle_; | |
85 | |
86 // This holds a pair of Pdh counters to queries for the process ID and private | |
87 // working set for a particular process name being monitored. The results from | |
88 // the two queries can be matched up so that we can associate a private | |
89 // working set with a process ID. | |
90 struct PdhCounterPair { | |
91 // The handle to the 'counter' that retrieves process IDs. | |
92 PDH_HCOUNTER process_id_handle = nullptr; | |
93 // The handle to the 'counter' that retrieves private working sets. | |
94 PDH_HCOUNTER private_ws_handle = nullptr; | |
95 }; | |
96 std::vector<PdhCounterPair> counter_pairs_; | |
97 | |
98 struct PidAndPrivateWS { | |
99 base::ProcessId process_id; | |
100 size_t private_ws; | |
101 // Comparison function for sorting. | |
102 bool operator<(const PidAndPrivateWS& other) const { | |
103 return process_id < other.process_id; | |
104 } | |
105 // Comparison function for searching. | |
106 bool operator<(const base::ProcessId other_process_id) const { | |
107 return process_id < other_process_id; | |
108 } | |
109 }; | |
110 // After each call to Sample this will hold the results, sorted by process id. | |
111 std::vector<PidAndPrivateWS> records_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot); | |
114 }; | |
115 | |
116 } // namespace base | |
117 | |
118 #endif // defined(OS_WIN) | |
119 | |
120 #endif // PRIVATE_WORKING_SET_SNAPSHOT_H_ | |
OLD | NEW |