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

Side by Side Diff: chrome/browser/win/private_working_set_snapshot.h

Issue 2238403003: Task manager: Get physical memory efficiently for all processes from SharedSampler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed CR feedback Created 4 years, 3 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 CHROME_BROWSER_WIN_PRIVATE_WORKING_SET_SNAPSHOT_H_
6 #define CHROME_BROWSER_WIN_PRIVATE_WORKING_SET_SNAPSHOT_H_
7
8 #include "build/build_config.h"
9
10 #include <pdh.h>
11 #include <stddef.h>
12
13 #include <vector>
14
15 #include "base/macros.h"
16 #include "base/process/process_handle.h"
17 #include "base/win/scoped_handle.h"
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 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 using ScopedPDH =
38 base::win::GenericScopedHandle<PDHHandleTraits,
39 base::win::DummyVerifierTraits>;
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 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 // Initialize the PDH |query_handle_| and process any pending AddToMonitorList
70 // calls.
71 void Initialize();
72
73 // This holds a pair of Pdh counters to queries for the process ID and private
74 // working set for a particular process name being monitored. The results from
75 // the two queries can be matched up so that we can associate a private
76 // working set with a process ID.
77 struct PdhCounterPair {
78 // These are bound to query_handle_ and will be freed when it is closed.
79 // The handle to the 'counter' that retrieves process IDs.
80 PDH_HCOUNTER process_id_handle = nullptr;
81 // The handle to the 'counter' that retrieves private working sets.
82 PDH_HCOUNTER private_ws_handle = nullptr;
83 };
84
85 // Struct for storing a process ID and associated private working set.
86 struct PidAndPrivateWorkingSet {
87 base::ProcessId process_id;
88 size_t private_ws;
89 // Comparison function for sorting by process ID.
90 bool operator<(const PidAndPrivateWorkingSet& other) const {
91 // private_ws is intentionally *not* part of the comparison because it is
92 // the payload and process_id is the key.
93 return process_id < other.process_id;
94 }
95 // Comparison function for searching by process ID.
96 bool operator<(const base::ProcessId other_process_id) const {
97 return process_id < other_process_id;
98 }
99 };
100
101 // True if Initialize() has been called, whether it succeeded or not.
102 bool initialized_ = false;
103
104 // The handle to the query object.
105 win::ScopedPDH query_handle_;
106
107 // A vector of process names to monitor when initialization occurs.
108 std::vector<std::string> process_names_;
109
110 // A PdhCounterPair for each successful AddToMonitorList call.
111 std::vector<PdhCounterPair> counter_pairs_;
112
113 // After each call to Sample this will hold the results, sorted by process id.
114 std::vector<PidAndPrivateWorkingSet> records_;
115
116 DISALLOW_COPY_AND_ASSIGN(PrivateWorkingSetSnapshot);
117 };
118
119 #endif // CHROME_BROWSER_WIN_PRIVATE_WORKING_SET_SNAPSHOT_H_
OLDNEW
« no previous file with comments | « chrome/browser/ui/task_manager/task_manager_table_model.cc ('k') | chrome/browser/win/private_working_set_snapshot.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698