Search code examples
launchfoundationios15

Unexpected launch time on iOS 15


I noticed some strange reports when the app started on iOS 15. I use the following code method to get the process creation time,

NSInteger rm_task_create_time(void) {
    static dispatch_once_t onceToken;
    static NSInteger taskCreateTimeMs = 0;
    dispatch_once(&onceToken, ^{
        struct kinfo_proc kProcInfo;
        if (rm_process_info([[NSProcessInfo processInfo] processIdentifier], &kProcInfo)) {
            taskCreateTimeMs = kProcInfo.kp_proc.p_starttime.tv_sec * 1000 + kProcInfo.kp_proc.p_starttime.tv_usec * 1.0E-3;
        }
    });
    return taskCreateTimeMs;
}

use the following code to get the first runloop execution time of the app as the first frame rendering time,

static NSInteger firstDrawTime;
- (void)startFirstDrawMonitor:(void(^)(void))completeHandler {
    CFRunLoopRef mainRunloop = [[NSRunLoop mainRunLoop] getCFRunLoop];
    CFRunLoopActivity activities = kCFRunLoopAllActivities;
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(
        kCFAllocatorDefault, activities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
            if (activity == kCFRunLoopBeforeTimers) {
                firstDrawTime = [NSDate date].timeIntervalSince1970 * 1E3;

                CFRunLoopRemoveObserver(mainRunloop, observer, kCFRunLoopCommonModes);
                CFRelease(observer);
            }
        });
    CFRunLoopAddObserver(mainRunloop, observer, kCFRunLoopCommonModes);
}

Then use "firstDrawTime - rm_task_create_time()" to calculate the launch time of the app. But from the data we got, there are a lot of outliers in the cold start time, such as 31405290ms、8310805ms、5177209ms, but the time from didFinishLaunch to the first runloop execution is reasonable,only a few hundred milliseconds. There was no such problem before ios15. So I speculate that there may be some paths on iOS 15 that will cause the process to start without calling didFinishLaunch, or some other possibilities. So can anyone tell me what changes in iOS15 will affect the launch time? Or any other changes that may cause this problem? thanks.


Solution

  • I found that this is a new feature prewarming of ios15.

    In iOS 15 and later, the system may, depending on device conditions, prewarm your app — launch nonrunning application processes to reduce the amount of time the user waits before the app is usable. Prewarming executes an app’s launch sequence up until, but not including, when main() calls UIApplicationMain(::::). This provides the system with an opportunity to build and cache any low-level structures it requires in anticipation of a full launch.

    you can read this document. about_the_app_launch_sequence