1 // Copyright 2016 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 TOOLS_WIN_IDLEWAKEUPS_SYSTEM_INFORMATION_SAMPLER_H_
6 #define TOOLS_WIN_IDLEWAKEUPS_SYSTEM_INFORMATION_SAMPLER_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include <windows.h>
13 
14 // SYSTEM_PROCESS_INFORMATION and SYSTEM_THREAD_INFORMATION structures
15 // use HANDLE for the thread / process IDs.
16 typedef HANDLE ThreadId;
17 typedef HANDLE ProcessId;
18 
19 // Contains per thread data stored in each data snapshot.
20 struct ThreadData {
21   ThreadId thread_id;
22   ULONG context_switches;
23 };
24 
25 typedef std::vector<ThreadData> ThreadsVector;
26 
27 // Contains per process data stored in each data snapshot.
28 struct ProcessData {
29   ULONGLONG cpu_time;
30   ULONGLONG working_set;
31   ThreadsVector threads;
32 };
33 
34 typedef std::map<ProcessId, ProcessData> ProcessDataMap;
35 
36 struct ProcessDataSnapshot {
37   ProcessDataMap processes;
38   double timestamp;
39 };
40 
41 class SystemInformationSampler {
42  public:
43   SystemInformationSampler(const wchar_t* process_name);
44   ~SystemInformationSampler();
45 
46   std::unique_ptr<ProcessDataSnapshot> TakeSnapshot();
47 
target_process_name_filter()48   const wchar_t* target_process_name_filter() const {
49     return target_process_name_;
50   }
51 
52  private:
53   wchar_t target_process_name_[256] = {};
54   LARGE_INTEGER perf_frequency_;
55   LARGE_INTEGER initial_counter_;
56   size_t previous_buffer_size_ = 0;
57 
58   SystemInformationSampler& operator=(const SystemInformationSampler&) = delete;
59   SystemInformationSampler(const SystemInformationSampler&) = delete;
60 };
61 
62 #endif  // TOOLS_WIN_IDLEWAKEUPS_SYSTEM_INFORMATION_SAMPLER_H_