#[cfg(target_os = "macos")]
use objc2_foundation::{NSActivityOptions, NSProcessInfo, ns_string};
#[derive(Clone, Copy, Debug)]
pub enum Reply {
Accepted,
Refused,
Unsupported,
}
impl std::fmt::Display for Reply {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
Self::Accepted => "accepted",
Self::Refused => "refused",
Self::Unsupported => "not supported",
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct Report {
pub request: &'static str,
pub reply: Reply,
}
#[cfg(target_os = "macos")]
const OPTIONS: NSActivityOptions = NSActivityOptions::UserInitiatedAllowingIdleSystemSleep
.union(NSActivityOptions::LatencyCritical);
#[cfg(target_os = "macos")]
const _: () = {
assert!(
OPTIONS.contains(NSActivityOptions::LatencyCritical),
"the activity must carry what turns timer coalescing off"
);
assert!(
!OPTIONS.contains(NSActivityOptions::IdleSystemSleepDisabled),
"the machine must still be allowed to sleep"
);
assert!(
!OPTIONS.contains(NSActivityOptions::IdleDisplaySleepDisabled),
"the display must still be allowed to sleep"
);
};
#[cfg(target_os = "macos")]
#[must_use]
pub fn exempt_from_slowdown() -> Option<Report> {
const REQUEST: &str = "NSProcessInfo beginActivityWithOptions:NSActivityUserInitiated\
AllowingIdleSystemSleep|NSActivityLatencyCritical";
let token = NSProcessInfo::processInfo()
.beginActivityWithOptions_reason(OPTIONS, ns_string!("MahBot dispatches work"));
std::mem::forget(token);
Some(Report {
request: REQUEST,
reply: Reply::Accepted,
})
}
#[cfg(windows)]
#[must_use]
pub fn exempt_from_slowdown() -> Option<Report> {
use std::mem::size_of;
use windows_sys::Win32::Foundation::{ERROR_INVALID_PARAMETER, GetLastError};
use windows_sys::Win32::System::Threading::{
GetCurrentProcess, PROCESS_POWER_THROTTLING_CURRENT_VERSION,
PROCESS_POWER_THROTTLING_EXECUTION_SPEED, PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION,
PROCESS_POWER_THROTTLING_STATE, ProcessPowerThrottling, SetProcessInformation,
};
const REQUEST_BOTH: &str = "SetProcessInformation(ProcessPowerThrottling, ControlMask=\
EXECUTION_SPEED|IGNORE_TIMER_RESOLUTION, StateMask=0)";
const REQUEST_SPEED_ONLY: &str = "SetProcessInformation(ProcessPowerThrottling, \
ControlMask=EXECUTION_SPEED, StateMask=0)";
fn ask(control: u32) -> Result<(), u32> {
let state = PROCESS_POWER_THROTTLING_STATE {
Version: PROCESS_POWER_THROTTLING_CURRENT_VERSION,
ControlMask: control,
StateMask: 0,
};
#[allow(clippy::cast_possible_truncation)]
let size = size_of::<PROCESS_POWER_THROTTLING_STATE>() as u32;
let ok = unsafe {
SetProcessInformation(
GetCurrentProcess(),
ProcessPowerThrottling,
std::ptr::from_ref(&state).cast(),
size,
)
};
if ok != 0 {
Ok(())
} else {
Err(unsafe { GetLastError() })
}
}
let (request, error) =
match ask(PROCESS_POWER_THROTTLING_EXECUTION_SPEED
| PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION)
{
Ok(()) => {
return Some(Report {
request: REQUEST_BOTH,
reply: Reply::Accepted,
});
}
Err(ERROR_INVALID_PARAMETER) => match ask(PROCESS_POWER_THROTTLING_EXECUTION_SPEED) {
Ok(()) => {
return Some(Report {
request: REQUEST_SPEED_ONLY,
reply: Reply::Accepted,
});
}
Err(error) => (REQUEST_SPEED_ONLY, error),
},
Err(error) => (REQUEST_BOTH, error),
};
Some(Report {
request,
reply: if error == ERROR_INVALID_PARAMETER {
Reply::Unsupported
} else {
Reply::Refused
},
})
}
#[cfg(not(any(target_os = "macos", windows)))]
#[must_use]
pub fn exempt_from_slowdown() -> Option<Report> {
None
}