Skip to content

Instantly share code, notes, and snippets.

@pofat
Created February 22, 2023 02:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pofat/4f74fc9d193b180d1d43f8c3fdd320d5 to your computer and use it in GitHub Desktop.
Save pofat/4f74fc9d193b180d1d43f8c3fdd320d5 to your computer and use it in GitHub Desktop.
Get permain and check if it's prewarm
/// 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