Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: base/process/private_working_set_snapshot.h

Issue 1181263005: Make task manager memory data more efficient and meaningful. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reduced PdhGetFormattedCounterArray calls and now presubmit clean Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 BASE_PROCESS_PRIVATE_WORKING_SET_SNAPSHOT_
6 #define BASE_PROCESS_PRIVATE_WORKING_SET_SNAPSHOT_
7
8 #include <vector>
9
10 #include "base/base_export.h"
11 #include "base/process/process_handle.h"
12
13 #if defined(OS_WIN)
14 #include <pdh.h>
15 #include "base/win/scoped_handle.h"
16
17 namespace base {
18
19 namespace win {
20
21 // The traits class for PDH handles that can be closed via PdhCloseQuery() API.
22 struct PDHHandleTraits {
23 typedef PDH_HQUERY Handle;
24 static PDH_HQUERY NullHandle() { return nullptr; }
25 static bool IsHandleValid(PDH_HQUERY handle) { return handle != nullptr; }
26 static bool BASE_EXPORT CloseHandle(PDH_HQUERY handle) {
27 return (PdhCloseQuery(handle) == ERROR_SUCCESS);
28 }
29
30 private:
31 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHHandleTraits);
32 };
33
34 // DummyVerifierTraits can be used because PDH_HQUERY is just a typedef for
35 // HANDLE. However HandleTraits cannot be used because PdhCloseQuery must be
36 // called rather than CloseHandle to dispose of the resources.
37 typedef GenericScopedHandle<PDHHandleTraits, DummyVerifierTraits> ScopedPDH;
38 } // namespace win
ncarter (slow) 2015/06/29 20:03:19 Add a blank line before the namespace close
brucedawson 2015/06/29 22:09:52 Done.
39
40 // This class can be used to do bulk collection of private working sets. This
41 // exists because on Windows it is much faster to collect a group of private
42 // working sets all at once using PdhOpenQuery than to calculate the private
43 // working sets for each process individually.
44 class BASE_EXPORT PrivateWorkingSetSnapshot {
45 public:
46 PrivateWorkingSetSnapshot();
ncarter (slow) 2015/06/29 20:03:19 You should still declare a destructor in the .h wi
brucedawson 2015/06/29 22:09:52 Ah - right. Avoiding nontrivial hidden inlined fun
47
48 // Add a process name that this object should monitor, such as "chrome". All
49 // processes whose name starts with this string will be monitored.
50 void AddToMonitorList(const std::string& process_name);
51
52 // Query the system for working-set information for all monitored processes
53 // and update the results cache. This function may take a few ms to run.
54 // The time it takes seems to be independent of the number of processes it
55 // retrieves data for. This makes it faster than using QueryWorkingSet as soon
56 // as the process count exceeds two or three.
57 void Sample();
58
59 // Ask for the working set for a specific process, from the most recent call
60 // to Sample. If no data is available then zero will be returned. The result
61 // is in bytes.
62 size_t GetPrivateWorkingSet(base::ProcessId process_id) const;
63
64 private:
65 // This holds a pair of Pdh counters to queries for the process ID and private
66 // working set for a particular process name being monitored. The results from
67 // the two queries can be matched up so that we can associate a private
68 // working set with a process ID.
69 struct PdhCounterPair {
70 // These are bound to query_handle_ and will be freed when it is closed.
71 // The handle to the 'counter' that retrieves process IDs.
72 PDH_HCOUNTER process_id_handle = nullptr;
73 // The handle to the 'counter' that retrieves private working sets.
74 PDH_HCOUNTER private_ws_handle = nullptr;
ncarter (slow) 2015/06/29 20:03:19 I'm okay with leaving these as ws, but there's a g
brucedawson 2015/06/29 22:09:52 Let's see what happens...
75 };
76
77 // Struct for storing a process ID and associated private working set.
78 struct PidAndPrivateWorkingSet {
79 base::ProcessId process_id;
80 size_t private_ws;
81 // Comparison function for sorting.
82 bool operator<(const PidAndPrivateWorkingSet& other) const {
83 return process_id < other.process_id;
84 }
85 // Comparison function for searching.
86 bool operator<(const base::ProcessId other_process_id) const {
87 return process_id < other_process_id;
88 }
89 };
90
91 // The handle to the query object.
92 win::ScopedPDH query_handle_;
93
94 // A PdhCounterPair for each successful AddToMonitorList call.
95 std::vector<PdhCounterPair> counter_pairs_;
96
97 // After each call to Sample this will hold the results, sorted by process id.
98 std::vector<PidAndPrivateWorkingSet> records_;
99
100 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot);
101 };
102
103 } // namespace base
104
105 #endif // defined(OS_WIN)
106
107 #endif // BASE_PROCESS_PRIVATE_WORKING_SET_SNAPSHOT_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698