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 CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_WIN_DEFINES_H_
6 #define CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_WIN_DEFINES_H_
7 
8 #include <windows.h>
9 #include <winternl.h>
10 
11 #include "build/build_config.h"
12 
13 namespace task_manager {
14 
15 // From <wdm.h>
16 typedef LONG KPRIORITY;
17 typedef LONG KWAIT_REASON;  // Full definition is in wdm.h
18 
19 // From ntddk.h
20 typedef struct _VM_COUNTERS {
21   SIZE_T PeakVirtualSize;
22   SIZE_T VirtualSize;
23   ULONG PageFaultCount;
24   // Padding here in 64-bit
25   SIZE_T PeakWorkingSetSize;
26   SIZE_T WorkingSetSize;
27   SIZE_T QuotaPeakPagedPoolUsage;
28   SIZE_T QuotaPagedPoolUsage;
29   SIZE_T QuotaPeakNonPagedPoolUsage;
30   SIZE_T QuotaNonPagedPoolUsage;
31   SIZE_T PagefileUsage;
32   SIZE_T PeakPagefileUsage;
33 } VM_COUNTERS;
34 
35 // Two possibilities available from here:
36 // http://stackoverflow.com/questions/28858849/where-is-system-information-class-defined
37 
38 typedef enum _SYSTEM_INFORMATION_CLASS {
39   SystemProcessInformation = 5,  // This is the number that we need.
40 } SYSTEM_INFORMATION_CLASS;
41 
42 // https://msdn.microsoft.com/en-us/library/gg750647.aspx?f=255&MSPPError=-2147217396
43 typedef struct {
44   HANDLE UniqueProcess;  // Actually process ID
45   HANDLE UniqueThread;   // Actually thread ID
46 } CLIENT_ID;
47 
48 // From http://alax.info/blog/1182, with corrections and modifications
49 // Originally from
50 // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FSystem%20Information%2FStructures%2FSYSTEM_THREAD.html
51 struct SYSTEM_THREAD_INFORMATION {
52   ULONGLONG KernelTime;
53   ULONGLONG UserTime;
54   ULONGLONG CreateTime;
55   ULONG WaitTime;
56   // Padding here in 64-bit
57   PVOID StartAddress;
58   CLIENT_ID ClientId;
59   KPRIORITY Priority;
60   LONG BasePriority;
61   ULONG ContextSwitchCount;
62   ULONG State;
63   KWAIT_REASON WaitReason;
64 };
65 #if defined(ARCH_CPU_64_BITS)
66 static_assert(sizeof(SYSTEM_THREAD_INFORMATION) == 80,
67               "Structure size mismatch");
68 #else
69 static_assert(sizeof(SYSTEM_THREAD_INFORMATION) == 64,
70               "Structure size mismatch");
71 #endif
72 
73 // From http://alax.info/blog/1182, with corrections and modifications
74 // Originally from
75 // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FSystem%20Information%2FStructures%2FSYSTEM_THREAD.html
76 struct SYSTEM_PROCESS_INFORMATION {
77   ULONG NextEntryOffset;
78   ULONG NumberOfThreads;
79   // http://processhacker.sourceforge.net/doc/struct___s_y_s_t_e_m___p_r_o_c_e_s_s___i_n_f_o_r_m_a_t_i_o_n.html
80   ULONGLONG WorkingSetPrivateSize;
81   ULONG HardFaultCount;
82   ULONG Reserved1;
83   ULONGLONG CycleTime;
84   ULONGLONG CreateTime;
85   ULONGLONG UserTime;
86   ULONGLONG KernelTime;
87   UNICODE_STRING ImageName;
88   KPRIORITY BasePriority;
89   HANDLE ProcessId;
90   HANDLE ParentProcessId;
91   ULONG HandleCount;
92   ULONG Reserved2[2];
93   // Padding here in 64-bit
94   VM_COUNTERS VirtualMemoryCounters;
95   size_t Reserved3;
96   IO_COUNTERS IoCounters;
97   SYSTEM_THREAD_INFORMATION Threads[1];
98 };
99 #if defined(ARCH_CPU_64_BITS)
100 static_assert(sizeof(SYSTEM_PROCESS_INFORMATION) == 336,
101               "Structure size mismatch");
102 #else
103 static_assert(sizeof(SYSTEM_PROCESS_INFORMATION) == 248,
104               "Structure size mismatch");
105 #endif
106 
107 typedef NTSTATUS(WINAPI* NTQUERYSYSTEMINFORMATION)(
108     SYSTEM_INFORMATION_CLASS SystemInformationClass,
109     PVOID SystemInformation,
110     ULONG SystemInformationLength,
111     PULONG ReturnLength);
112 
113 }  // namespace task_manager
114 
115 #endif  //  CHROME_BROWSER_TASK_MANAGER_SAMPLING_SHARED_SAMPLER_WIN_DEFINES_H_
116