]> gitweb.factorcode.org Git - factor.git/commitdiff
windows: Add cpu-mhz on Windows
authorDoug Coleman <doug.coleman@gmail.com>
Sun, 31 Jan 2021 04:35:06 +0000 (22:35 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 31 Jan 2021 04:35:06 +0000 (22:35 -0600)
basis/system-info/windows/windows.factor
basis/windows/powrprof/authors.txt [new file with mode: 0644]
basis/windows/powrprof/powrprof.factor [new file with mode: 0644]
basis/windows/windows.factor

index d9f9317fb843c5903f39573043cbf099171049d4..7e344d3c574950174a490555396631cb3eca4897 100644 (file)
@@ -2,9 +2,10 @@
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types alien.data alien.strings
 arrays byte-arrays classes.struct combinators kernel math
-namespaces specialized-arrays system
-vocabs.loader windows windows.advapi32
-windows.errors windows.kernel32 words system-info ;
+namespaces sequences specialized-arrays
+specialized-arrays.instances.alien.c-types.ushort system
+system-info vocabs.loader windows windows.advapi32
+windows.errors windows.kernel32 windows.powrprof words ;
 SPECIALIZED-ARRAY: ushort
 IN: system-info.windows
 
@@ -70,6 +71,9 @@ M: windows os-version
 M: windows cpus
     system-info dwNumberOfProcessors>> ;
 
+M: windows cpu-mhz
+    get-processor-power-information first MaxMhz>> 1,000,000 * ;
+
 : memory-status ( -- MEMORYSTATUSEX )
     MEMORYSTATUSEX <struct>
     MEMORYSTATUSEX heap-size >>dwLength
diff --git a/basis/windows/powrprof/authors.txt b/basis/windows/powrprof/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/basis/windows/powrprof/powrprof.factor b/basis/windows/powrprof/powrprof.factor
new file mode 100644 (file)
index 0000000..9460fd3
--- /dev/null
@@ -0,0 +1,255 @@
+! Copyright (C) 2021 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien.c-types alien.syntax classes.struct kernel math
+specialized-arrays system-info windows.errors windows.types
+windows.user32 ;
+IN: windows.powrprof
+
+LIBRARY: powrprof
+
+TYPEDEF: UINT NTSTATUS
+TYPEDEF: void* PHPOWERNOTIFY
+
+CONSTANT: STATUS_SUCCESS 0
+
+ENUM: SYSTEM_POWER_STATE
+    PowerSystemUnspecified
+    PowerSystemWorking
+    PowerSystemSleeping1
+    PowerSystemSleeping2
+    PowerSystemSleeping3
+    PowerSystemHibernate
+    PowerSystemShutdown
+    PowerSystemMaximum ;
+TYPEDEF: SYSTEM_POWER_STATE* PSYSTEM_POWER_STATE
+
+ENUM: POWER_PLATFORM_ROLE
+    PlatformRoleUnspecified
+    PlatformRoleDesktop
+    PlatformRoleMobile
+    PlatformRoleWorkstation
+    PlatformRoleEnterpriseServer
+    PlatformRoleSOHOServer
+    PlatformRoleAppliancePC
+    PlatformRolePerformanceServer
+    PlatformRoleSlate
+    PlatformRoleMaximum ;
+TYPEDEF: POWER_PLATFORM_ROLE* PPOWER_PLATFORM_ROLE
+
+ENUM: POWER_INFORMATION_LEVEL
+    SystemPowerPolicyAc
+    SystemPowerPolicyDc
+    VerifySystemPolicyAc
+    VerifySystemPolicyDc
+    SystemPowerCapabilities
+    SystemBatteryState
+    SystemPowerStateHandler
+    ProcessorStateHandler
+    SystemPowerPolicyCurrent
+    AdministratorPowerPolicy
+    SystemReserveHiberFile
+    ProcessorInformation
+    SystemPowerInformation
+    ProcessorStateHandler2
+    LastWakeTime
+    LastSleepTime
+    SystemExecutionState
+    SystemPowerStateNotifyHandler
+    ProcessorPowerPolicyAc
+    ProcessorPowerPolicyDc
+    VerifyProcessorPowerPolicyAc
+    VerifyProcessorPowerPolicyDc
+    ProcessorPowerPolicyCurrent
+    SystemPowerStateLogging
+    SystemPowerLoggingEntry
+    SetPowerSettingValue
+    NotifyUserPowerSetting
+    PowerInformationLevelUnused0
+    SystemMonitorHiberBootPowerOff
+    SystemVideoState
+    TraceApplicationPowerMessage
+    TraceApplicationPowerMessageEnd
+    ProcessorPerfStates
+    ProcessorIdleStates
+    ProcessorCap
+    SystemWakeSource
+    SystemHiberFileInformation
+    TraceServicePowerMessage
+    ProcessorLoad
+    PowerShutdownNotification
+    MonitorCapabilities
+    SessionPowerInit
+    SessionDisplayState
+    PowerRequestCreate
+    PowerRequestAction
+    GetPowerRequestList
+    ProcessorInformationEx
+    NotifyUserModeLegacyPowerEvent
+    GroupPark
+    ProcessorIdleDomains
+    WakeTimerList
+    SystemHiberFileSize
+    ProcessorIdleStatesHv
+    ProcessorPerfStatesHv
+    ProcessorPerfCapHv
+    ProcessorSetIdle
+    LogicalProcessorIdling
+    UserPresence
+    PowerSettingNotificationName
+    GetPowerSettingValue
+    IdleResiliency
+    SessionRITState
+    SessionConnectNotification
+    SessionPowerCleanup
+    SessionLockState
+    SystemHiberbootState
+    PlatformInformation
+    PdcInvocation
+    MonitorInvocation
+    FirmwareTableInformationRegistered
+    SetShutdownSelectedTime
+    SuspendResumeInvocation
+    PlmPowerRequestCreate
+    ScreenOff
+    CsDeviceNotification
+    PlatformRole
+    LastResumePerformance
+    DisplayBurst
+    ExitLatencySamplingPercentage
+    RegisterSpmPowerSettings
+    PlatformIdleStates
+    ProcessorIdleVeto
+    PlatformIdleVeto
+    SystemBatteryStatePrecise
+    ThermalEvent
+    PowerRequestActionInternal
+    BatteryDeviceState
+    PowerInformationInternal
+    ThermalStandby
+    SystemHiberFileType
+    PhysicalPowerButtonPress
+    QueryPotentialDripsConstraint
+    EnergyTrackerCreate
+    EnergyTrackerQuery
+    UpdateBlackBoxRecorder
+    SessionAllowExternalDmaDevices
+    SendSuspendResumeNotification
+    PowerInformationLevelMaximum ;
+
+STRUCT: BATTERY_REPORTING_SCALE
+    { Granularity DWORD }
+    { Capacity DWORD } ;
+TYPEDEF: BATTERY_REPORTING_SCALE* PBATTERY_REPORTING_SCALE
+
+
+STRUCT: PROCESSOR_POWER_INFORMATION
+    { Number ULONG }
+    { MaxMhz ULONG }
+    { CurrentMhz ULONG }
+    { MhzLimit ULONG }
+    { MaxIdleState ULONG }
+    { CurrentIdleState ULONG } ;
+TYPEDEF: PROCESSOR_POWER_INFORMATION* PPROCESSOR_POWER_INFORMATION
+
+STRUCT: SYSTEM_POWER_CAPABILITIES
+    { PowerButtonPresent BOOLEAN }
+    { SleepButtonPresent BOOLEAN }
+    { LidPresent BOOLEAN }
+    { SystemS1 BOOLEAN }
+    { SystemS2 BOOLEAN }
+    { SystemS3 BOOLEAN }
+    { SystemS4 BOOLEAN }
+    { SystemS5 BOOLEAN }
+    { HiberFilePresent BOOLEAN }
+    { FullWake BOOLEAN }
+    { VideoDimPresent BOOLEAN }
+    { ApmPresent BOOLEAN }
+    { UpsPresent BOOLEAN }
+    { ThermalControl BOOLEAN }
+    { ProcessorThrottle BOOLEAN }
+    { ProcessorMinThrottle BYTE }
+    { ProcessorThrottleScale BYTE }
+    { spare2 BYTE[4] }
+    { ProcessorMaxThrottle BYTE }
+    { FastSystemS4 BOOLEAN }
+    { Hiberboot BOOLEAN }
+    { WakeAlarmPresent BOOLEAN }
+    { AoAc BOOLEAN }
+    { DiskSpinDown BOOLEAN }
+    { spare3 BYTE[8] }
+    { HiberFileType BYTE }
+    { AoAcConnectivitySupported BOOLEAN }
+! #else
+!  BYTE                    spare3[6];
+! #endif
+    { SystemBatteriesPresent BOOLEAN }
+    { BatteriesAreShortTerm BOOLEAN }
+    { BatteryScale BATTERY_REPORTING_SCALE[3] }
+    { AcOnLineWake SYSTEM_POWER_STATE }
+    { SoftLidWake SYSTEM_POWER_STATE }
+    { RtcWake SYSTEM_POWER_STATE }
+    { MinDeviceWakeState SYSTEM_POWER_STATE }
+    { DefaultLowLatencyWake SYSTEM_POWER_STATE } ;
+TYPEDEF: SYSTEM_POWER_CAPABILITIES* PSYSTEM_POWER_CAPABILITIES
+
+STRUCT: SYSTEM_BATTERY_STATE
+    { AcOnLine BOOLEAN }
+    { BatteryPresent BOOLEAN }
+    { Charging BOOLEAN }
+    { Discharging BOOLEAN }
+    { Spare1 BOOLEAN[3] }
+    { Tag BYTE }
+    { MaxCapacity DWORD }
+    { RemainingCapacity DWORD }
+    { Rate DWORD }
+    { EstimatedTime DWORD }
+    { DefaultAlert1 DWORD }
+    { DefaultAlert2 DWORD } ;
+TYPEDEF: SYSTEM_BATTERY_STATE* PSYSTEM_BATTERY_STATE
+
+STRUCT: SYSTEM_POWER_INFORMATION
+    { MaxIdlenessAllowed ULONG }
+    { Idleness ULONG }
+    { TimeRemaining ULONG }
+    { CoolingMode UCHAR } ;
+TYPEDEF: SYSTEM_POWER_INFORMATION* PSYSTEM_POWER_INFORMATION 
+
+
+SPECIALIZED-ARRAY: PROCESSOR_POWER_INFORMATION
+
+FUNCTION: NTSTATUS CallNtPowerInformation (
+    POWER_INFORMATION_LEVEL InformationLevel
+    PVOID                   InputBuffer,
+    ULONG                   InputBufferLength,
+    PVOID                   OutputBuffer,
+    ULONG                   OutputBufferLength
+)
+
+FUNCTION: BOOLEAN GetPwrCapabilities (
+    PSYSTEM_POWER_CAPABILITIES lpspc
+)
+
+FUNCTION: POWER_PLATFORM_ROLE PowerDeterminePlatformRoleEx (
+    ULONG Version
+)
+FUNCTION: DWORD PowerRegisterSuspendResumeNotification (
+    DWORD         Flags,
+    HANDLE        Recipient,
+    PHPOWERNOTIFY RegistrationHandle
+)
+FUNCTION: DWORD PowerUnregisterSuspendResumeNotification (
+    HPOWERNOTIFY RegistrationHandle
+)
+
+: get-processor-power-information ( -- structs )
+    ProcessorInformation
+    f 0
+    cpus <PROCESSOR_POWER_INFORMATION-array>
+    PROCESSOR_POWER_INFORMATION heap-size cpus *
+    [
+        CallNtPowerInformation win32-error=0/f
+    ] keepd ;
+
+: get-power-capabilities ( -- struct )
+    SYSTEM_POWER_CAPABILITIES <struct>
+    [ GetPwrCapabilities win32-error=0/f ] keep ;
index 938ceae37058af2b32cb8ed76940e4f574417623..56c836a1852c8087c26ff018f2c500654f3bfaf4 100644 (file)
@@ -26,4 +26,5 @@ CONSTANT: MAX_UNICODE_PATH 32768
     { "winmm"       "winmm.dll"          stdcall }
     { "ntdll"       "ntdll.dll"          stdcall }
     { "crypt32"     "crypt32.dll"        stdcall }
+    { "powrprof"    "powrprof.dll"       stdcall }
 } [ first3 add-library ] each