Created
February 22, 2023 02:10
-
-
Save pofat/4f74fc9d193b180d1d43f8c3fdd320d5 to your computer and use it in GitHub Desktop.
Get permain and check if it's prewarm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// iOS 15+ | |
func isPrewarm() -> Bool { | |
if let prewawrmEnv = ProcessInfo.processInfo.environment["ActivePrewarm"], | |
prewawrmEnv == "1" { | |
return true | |
} else { | |
return false | |
} | |
} | |
/// Call this at main() | |
/// return: time in milliseconds | |
func getPreMainDuration() -> Double { | |
let currentTimeIntervalInMilliSecond = Date().timeIntervalSince1970 * 1000.0 | |
var procInfo = kinfo_proc() | |
let pid = ProcessInfo.processInfo.processIdentifier | |
var cmd: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid] | |
var size = MemoryLayout.stride(ofValue: procInfo) | |
// Retrieve information of current process | |
if sysctl(&cmd, UInt32(cmd.count), &procInfo, &size, nil, 0) == 0 { | |
// tv_sec -> timestamp in second; tv_usec -> rest fraction part in microsecond | |
let tvSecInMilliseconds = Double(procInfo.kp_proc.p_un.__p_starttime.tv_sec) * 1000.0 | |
let tvUsecInMilliseconds = Double(procInfo.kp_proc.p_un.__p_starttime.tv_usec) / 1000.0 | |
let processCreationTime = tvSecInMilliseconds + tvUsecInMilliseconds | |
var premainTime = currentTimeIntervalInMilliSecond - processCreationTime | |
premainTime.round() | |
return premainTime | |
} else { | |
print("Can't retrieve information of exeuction process") | |
return 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment