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

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: Resurrce having two query calls to PdhGetFormattedCounterArray 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 "build/build_config.h"
9
10 #if defined(OS_WIN)
11 #include <pdh.h>
12
13 #include <vector>
14
15 #include "base/base_export.h"
16 #include "base/process/process_handle.h"
17 #include "base/win/scoped_handle.h"
18
19 namespace base {
20
21 namespace win {
22
23 // The traits class for PDH handles that can be closed via PdhCloseQuery() API.
24 struct PDHHandleTraits {
25 typedef PDH_HQUERY Handle;
26 static PDH_HQUERY NullHandle() { return nullptr; }
27 static bool IsHandleValid(PDH_HQUERY handle) { return handle != nullptr; }
28 static bool BASE_EXPORT CloseHandle(PDH_HQUERY handle) {
29 return (PdhCloseQuery(handle) == ERROR_SUCCESS);
30 }
31
32 private:
33 DISALLOW_IMPLICIT_CONSTRUCTORS(PDHHandleTraits);
34 };
35
36 // DummyVerifierTraits can be used because PDH_HQUERY is just a typedef for
37 // HANDLE. However HandleTraits cannot be used because PdhCloseQuery must be
38 // called rather than CloseHandle to dispose of the resources.
39 typedef GenericScopedHandle<PDHHandleTraits, DummyVerifierTraits> ScopedPDH;
40
41 } // namespace win
42
43 // This class can be used to do bulk collection of private working sets. This
44 // exists because on Windows it is much faster to collect a group of private
45 // working sets all at once using PdhOpenQuery than to calculate the private
46 // working sets for each process individually.
47 class BASE_EXPORT PrivateWorkingSetSnapshot {
48 public:
49 PrivateWorkingSetSnapshot();
50 ~PrivateWorkingSetSnapshot();
51
52 // Add a process name that this object should monitor, such as "chrome". All
53 // processes whose name starts with this string will be monitored.
54 void AddToMonitorList(const std::string& process_name);
55
56 // Query the system for working-set information for all monitored processes
57 // and update the results cache. This function may take a few ms to run.
58 // The time it takes seems to be independent of the number of processes it
59 // retrieves data for. This makes it faster than using QueryWorkingSet as soon
60 // as the process count exceeds two or three.
61 void Sample();
62
63 // Ask for the working set for a specific process, from the most recent call
64 // to Sample. If no data is available then zero will be returned. The result
65 // is in bytes.
66 size_t GetPrivateWorkingSet(base::ProcessId process_id) const;
67
68 private:
69 // This holds a pair of Pdh counters to queries for the process ID and private
70 // working set for a particular process name being monitored. The results from
71 // the two queries can be matched up so that we can associate a private
72 // working set with a process ID.
73 struct PdhCounterPair {
74 // These are bound to query_handle_ and will be freed when it is closed.
75 // The handle to the 'counter' that retrieves process IDs.
76 PDH_HCOUNTER process_id_handle = nullptr;
77 // The handle to the 'counter' that retrieves private working sets.
78 PDH_HCOUNTER private_ws_handle = nullptr;
79 };
80
81 // Struct for storing a process ID and associated private working set.
82 struct PidAndPrivateWorkingSet {
83 base::ProcessId process_id;
84 size_t private_ws;
85 // Comparison function for sorting.
86 bool operator<(const PidAndPrivateWorkingSet& other) const {
Nico 2015/06/30 00:04:42 Should this have a `CHECK(process_id != other.proc
87 return process_id < other.process_id;
88 }
89 // Comparison function for searching.
90 bool operator<(const base::ProcessId other_process_id) const {
91 return process_id < other_process_id;
92 }
93 };
94
95 // The handle to the query object.
96 win::ScopedPDH query_handle_;
97
98 // A PdhCounterPair for each successful AddToMonitorList call.
99 std::vector<PdhCounterPair> counter_pairs_;
100
101 // After each call to Sample this will hold the results, sorted by process id.
102 std::vector<PidAndPrivateWorkingSet> records_;
103
104 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot);
105 };
106
107 } // namespace base
108
109 #endif // defined(OS_WIN)
110
111 #endif // BASE_PROCESS_PRIVATE_WORKING_SET_SNAPSHOT_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698