]> gitweb.factorcode.org Git - factor.git/blob - basis/windows/powrprof/powrprof.factor
windows: Add cpu-mhz on Windows
[factor.git] / basis / windows / powrprof / powrprof.factor
1 ! Copyright (C) 2021 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.syntax classes.struct kernel math
4 specialized-arrays system-info windows.errors windows.types
5 windows.user32 ;
6 IN: windows.powrprof
7
8 LIBRARY: powrprof
9
10 TYPEDEF: UINT NTSTATUS
11 TYPEDEF: void* PHPOWERNOTIFY
12
13 CONSTANT: STATUS_SUCCESS 0
14
15 ENUM: SYSTEM_POWER_STATE
16     PowerSystemUnspecified
17     PowerSystemWorking
18     PowerSystemSleeping1
19     PowerSystemSleeping2
20     PowerSystemSleeping3
21     PowerSystemHibernate
22     PowerSystemShutdown
23     PowerSystemMaximum ;
24 TYPEDEF: SYSTEM_POWER_STATE* PSYSTEM_POWER_STATE
25
26 ENUM: POWER_PLATFORM_ROLE
27     PlatformRoleUnspecified
28     PlatformRoleDesktop
29     PlatformRoleMobile
30     PlatformRoleWorkstation
31     PlatformRoleEnterpriseServer
32     PlatformRoleSOHOServer
33     PlatformRoleAppliancePC
34     PlatformRolePerformanceServer
35     PlatformRoleSlate
36     PlatformRoleMaximum ;
37 TYPEDEF: POWER_PLATFORM_ROLE* PPOWER_PLATFORM_ROLE
38
39 ENUM: POWER_INFORMATION_LEVEL
40     SystemPowerPolicyAc
41     SystemPowerPolicyDc
42     VerifySystemPolicyAc
43     VerifySystemPolicyDc
44     SystemPowerCapabilities
45     SystemBatteryState
46     SystemPowerStateHandler
47     ProcessorStateHandler
48     SystemPowerPolicyCurrent
49     AdministratorPowerPolicy
50     SystemReserveHiberFile
51     ProcessorInformation
52     SystemPowerInformation
53     ProcessorStateHandler2
54     LastWakeTime
55     LastSleepTime
56     SystemExecutionState
57     SystemPowerStateNotifyHandler
58     ProcessorPowerPolicyAc
59     ProcessorPowerPolicyDc
60     VerifyProcessorPowerPolicyAc
61     VerifyProcessorPowerPolicyDc
62     ProcessorPowerPolicyCurrent
63     SystemPowerStateLogging
64     SystemPowerLoggingEntry
65     SetPowerSettingValue
66     NotifyUserPowerSetting
67     PowerInformationLevelUnused0
68     SystemMonitorHiberBootPowerOff
69     SystemVideoState
70     TraceApplicationPowerMessage
71     TraceApplicationPowerMessageEnd
72     ProcessorPerfStates
73     ProcessorIdleStates
74     ProcessorCap
75     SystemWakeSource
76     SystemHiberFileInformation
77     TraceServicePowerMessage
78     ProcessorLoad
79     PowerShutdownNotification
80     MonitorCapabilities
81     SessionPowerInit
82     SessionDisplayState
83     PowerRequestCreate
84     PowerRequestAction
85     GetPowerRequestList
86     ProcessorInformationEx
87     NotifyUserModeLegacyPowerEvent
88     GroupPark
89     ProcessorIdleDomains
90     WakeTimerList
91     SystemHiberFileSize
92     ProcessorIdleStatesHv
93     ProcessorPerfStatesHv
94     ProcessorPerfCapHv
95     ProcessorSetIdle
96     LogicalProcessorIdling
97     UserPresence
98     PowerSettingNotificationName
99     GetPowerSettingValue
100     IdleResiliency
101     SessionRITState
102     SessionConnectNotification
103     SessionPowerCleanup
104     SessionLockState
105     SystemHiberbootState
106     PlatformInformation
107     PdcInvocation
108     MonitorInvocation
109     FirmwareTableInformationRegistered
110     SetShutdownSelectedTime
111     SuspendResumeInvocation
112     PlmPowerRequestCreate
113     ScreenOff
114     CsDeviceNotification
115     PlatformRole
116     LastResumePerformance
117     DisplayBurst
118     ExitLatencySamplingPercentage
119     RegisterSpmPowerSettings
120     PlatformIdleStates
121     ProcessorIdleVeto
122     PlatformIdleVeto
123     SystemBatteryStatePrecise
124     ThermalEvent
125     PowerRequestActionInternal
126     BatteryDeviceState
127     PowerInformationInternal
128     ThermalStandby
129     SystemHiberFileType
130     PhysicalPowerButtonPress
131     QueryPotentialDripsConstraint
132     EnergyTrackerCreate
133     EnergyTrackerQuery
134     UpdateBlackBoxRecorder
135     SessionAllowExternalDmaDevices
136     SendSuspendResumeNotification
137     PowerInformationLevelMaximum ;
138
139 STRUCT: BATTERY_REPORTING_SCALE
140     { Granularity DWORD }
141     { Capacity DWORD } ;
142 TYPEDEF: BATTERY_REPORTING_SCALE* PBATTERY_REPORTING_SCALE
143
144
145 STRUCT: PROCESSOR_POWER_INFORMATION
146     { Number ULONG }
147     { MaxMhz ULONG }
148     { CurrentMhz ULONG }
149     { MhzLimit ULONG }
150     { MaxIdleState ULONG }
151     { CurrentIdleState ULONG } ;
152 TYPEDEF: PROCESSOR_POWER_INFORMATION* PPROCESSOR_POWER_INFORMATION
153
154 STRUCT: SYSTEM_POWER_CAPABILITIES
155     { PowerButtonPresent BOOLEAN }
156     { SleepButtonPresent BOOLEAN }
157     { LidPresent BOOLEAN }
158     { SystemS1 BOOLEAN }
159     { SystemS2 BOOLEAN }
160     { SystemS3 BOOLEAN }
161     { SystemS4 BOOLEAN }
162     { SystemS5 BOOLEAN }
163     { HiberFilePresent BOOLEAN }
164     { FullWake BOOLEAN }
165     { VideoDimPresent BOOLEAN }
166     { ApmPresent BOOLEAN }
167     { UpsPresent BOOLEAN }
168     { ThermalControl BOOLEAN }
169     { ProcessorThrottle BOOLEAN }
170     { ProcessorMinThrottle BYTE }
171     { ProcessorThrottleScale BYTE }
172     { spare2 BYTE[4] }
173     { ProcessorMaxThrottle BYTE }
174     { FastSystemS4 BOOLEAN }
175     { Hiberboot BOOLEAN }
176     { WakeAlarmPresent BOOLEAN }
177     { AoAc BOOLEAN }
178     { DiskSpinDown BOOLEAN }
179     { spare3 BYTE[8] }
180     { HiberFileType BYTE }
181     { AoAcConnectivitySupported BOOLEAN }
182 ! #else
183 !  BYTE                    spare3[6];
184 ! #endif
185     { SystemBatteriesPresent BOOLEAN }
186     { BatteriesAreShortTerm BOOLEAN }
187     { BatteryScale BATTERY_REPORTING_SCALE[3] }
188     { AcOnLineWake SYSTEM_POWER_STATE }
189     { SoftLidWake SYSTEM_POWER_STATE }
190     { RtcWake SYSTEM_POWER_STATE }
191     { MinDeviceWakeState SYSTEM_POWER_STATE }
192     { DefaultLowLatencyWake SYSTEM_POWER_STATE } ;
193 TYPEDEF: SYSTEM_POWER_CAPABILITIES* PSYSTEM_POWER_CAPABILITIES
194
195 STRUCT: SYSTEM_BATTERY_STATE
196     { AcOnLine BOOLEAN }
197     { BatteryPresent BOOLEAN }
198     { Charging BOOLEAN }
199     { Discharging BOOLEAN }
200     { Spare1 BOOLEAN[3] }
201     { Tag BYTE }
202     { MaxCapacity DWORD }
203     { RemainingCapacity DWORD }
204     { Rate DWORD }
205     { EstimatedTime DWORD }
206     { DefaultAlert1 DWORD }
207     { DefaultAlert2 DWORD } ;
208 TYPEDEF: SYSTEM_BATTERY_STATE* PSYSTEM_BATTERY_STATE
209
210 STRUCT: SYSTEM_POWER_INFORMATION
211     { MaxIdlenessAllowed ULONG }
212     { Idleness ULONG }
213     { TimeRemaining ULONG }
214     { CoolingMode UCHAR } ;
215 TYPEDEF: SYSTEM_POWER_INFORMATION* PSYSTEM_POWER_INFORMATION 
216
217
218 SPECIALIZED-ARRAY: PROCESSOR_POWER_INFORMATION
219
220 FUNCTION: NTSTATUS CallNtPowerInformation (
221     POWER_INFORMATION_LEVEL InformationLevel
222     PVOID                   InputBuffer,
223     ULONG                   InputBufferLength,
224     PVOID                   OutputBuffer,
225     ULONG                   OutputBufferLength
226 )
227
228 FUNCTION: BOOLEAN GetPwrCapabilities (
229     PSYSTEM_POWER_CAPABILITIES lpspc
230 )
231
232 FUNCTION: POWER_PLATFORM_ROLE PowerDeterminePlatformRoleEx (
233     ULONG Version
234 )
235 FUNCTION: DWORD PowerRegisterSuspendResumeNotification (
236     DWORD         Flags,
237     HANDLE        Recipient,
238     PHPOWERNOTIFY RegistrationHandle
239 )
240 FUNCTION: DWORD PowerUnregisterSuspendResumeNotification (
241     HPOWERNOTIFY RegistrationHandle
242 )
243
244 : get-processor-power-information ( -- structs )
245     ProcessorInformation
246     f 0
247     cpus <PROCESSOR_POWER_INFORMATION-array>
248     PROCESSOR_POWER_INFORMATION heap-size cpus *
249     [
250         CallNtPowerInformation win32-error=0/f
251     ] keepd ;
252
253 : get-power-capabilities ( -- struct )
254     SYSTEM_POWER_CAPABILITIES <struct>
255     [ GetPwrCapabilities win32-error=0/f ] keep ;