exception EXC_RESOURCE - WAKEUPS 分析(二)
一、问题:
直播助手在使用ReplayKit2 Extension的过程中,ReplayKit2的Upload进程工作在后台模式,苹果对处于后台的进程进行了内存和CPU资源的限制。
对于内存:
每种Extension的限制不同,ReplayKit2 Extension 的内存限制是 Active Memory limit = 50MB, 如果使用的物理内存超过这个限制,将会被系统直接干掉。(干掉之前会收到Memory Warning的警告)
对于CPU:
统计该进程一段时间内(300s)wakeup的次数,也就是线程切换调度的次数。
现象:
内存警告:
这个日志只能在Console中看到,对于用户而言,会收到一个弹框提示“某某进程中断”
CPU使用过多的现象:
Date/Time: 2018-09-20 14:59:44.493850 +0800 OS Version: iPhone OS 11.4.1 (Build 15G77) Architecture: arm64 Report Version: 19 Command: LABroadcastUpload Path: /private/var/containers/Bundle/Application/62E054DE-ADDD-4955-BAA9-BD4C7A487B7F/LiveAssistant.app/PlugIns/LABroadcastUpload.appex/LABroadcastUpload Version: 2.10.0 (2.10.0.11) Beta Identifier: DD6E7BB7-473A-4B7F-B5AF-CEC5E48FFE9E Parent: launchd [1] PID: 437 Event: wakeups Action taken: none Wakeups: 45001 wakeups over the last 162 seconds (277 wakeups per second average), exceeding limit of 150 wakeups per second over 300 seconds Wakeups limit: 45000 Limit duration: 300s Wakeups caused: 45001 Duration: 162.23s Steps: 59 Hardware model: iPhone9,2 Active cpus: 2 Powerstats for: LABroadcastUploa [437] UUID: 562B0868-A0CA-32D5-9E98-E87676053442 Start time: 2018-09-20 15:01:39 +0800 End time: 2018-09-20 15:02:26 +0800 Parent: launchd Microstackshots: 59 samples (100%) Primary state: 38 samples Frontmost App, User mode, Effective Thread QoS Default, Requested Thread QoS Default, Override Thread QoS Unspecified User Activity: 0 samples Idle, 59 samples Active Power Source: 0 samples on Battery, 59 samples on AC 27 _pthread_wqthread + 859 (libsystem_pthread.dylib + 3696) [0x182167e70] 24 _dispatch_workloop_worker_thread$VARIANT$mp + 667 (libdispatch.dylib + 86168) [0x181e48098] 24 _dispatch_root_queue_drain_deferred_wlh$VARIANT$mp + 339 (libdispatch.dylib + 52424) [0x181e3fcc8] 24 _dispatch_queue_invoke$VARIANT$mp + 335 (libdispatch.dylib + 49916) [0x181e3f2fc] 12 _dispatch_queue_serial_drain$VARIANT$mp + 607 (libdispatch.dylib + 47540) [0x181e3e9b4] 12 _dispatch_client_callout + 15 (libdispatch.dylib + 6752) [0x181e34a60] 12 _dispatch_call_block_and_release + 23 (libdispatch.dylib + 6816) [0x181e34aa0] 5 __59-[RPBroadcastSampleHandler _processPayloadWithVideoSample:]_block_invoke + 211 (ReplayKit + 57632) [0x19e0df120] 4 -[RPBroadcastSampleHandler _sampleBufferFromIOSurface:timingInfo:] + 51 (ReplayKit + 58792) [0x19e0df5a8] 4 CVPixelBufferCreateWithIOSurface + 1131 (CoreVideo + 43776) [0x1855efb00] 2 IOSurfaceClientCopyAllValues + 503 (IOSurface + 16664) [0x183629118] 2 unserializeReplacementIOKitTypeRef + 343 (IOSurface + 17196) [0x18362932c] 2 CFDictionaryApplyFunction + 287 (CoreFoundation + 84836) [0x182411b64] 2 CFBasicHashApply + 131 (CoreFoundation + 44952) [0x182407f98] 2 __CFDictionaryApplyFunction_block_invoke + 23 (CoreFoundation + 146620) [0x182420cbc] 2 unserializeReplacementDictionaryKeysAndValues + 31 (IOSurface + 29096) [0x18362c1a8] 2 unserializeReplacementIOKitTypeRef + 343 (IOSurface + 17196) [0x18362932c] 2 CFDictionaryApplyFunction + 287 (CoreFoundation + 84836) [0x182411b64] 2 CFBasicHashApply + 131 (CoreFoundation + 44952) [0x182407f98] 2 __CFDictionaryApplyFunction_block_invoke + 23 (CoreFoundation + 146620) [0x182420cbc] 2 unserializeReplacementDictionaryKeysAndValues + 31 (IOSurface + 29096) [0x18362c1a8] 2 unserializeReplacementIOKitTypeRef + 251 (IOSurface + 17104) [0x1836292d0] 2 CFArrayApplyFunction + 79 (CoreFoundation + 46720) [0x182408680] 2 unserializeReplacementArrayValues + 19 (IOSurface + 29176) [0x18362c1f8] 2 unserializeReplacementIOKitTypeRef + 343 (IOSurface + 17196) [0x18362932c] 2 CFDictionaryApplyFunction + 287 (CoreFoundation + 84836) [0x182411b64] 2 CFBasicHashApply + 131 (CoreFoundation + 44952) [0x182407f98] 2 __CFDictionaryApplyFunction_block_invoke + 23 (CoreFoundation + 146620) [0x182420cbc] 2 unserializeReplacementDictionaryKeysAndValues + 51 (IOSurface + 29116) [0x18362c1bc] 2 CFDictionaryGetValue + 223 (CoreFoundation + 21088) [0x182402260] 2 CFBasicHashFindBucket + 207 (CoreFoundation + 21324) [0x18240234c] 2 CFHash + 232 (CoreFoundation + 33616) [0x182405350] 1 IOSurfaceClientCopyAllValues + 419 (IOSurface + 16580) [0x1836290c4] 1 IOCFUnserializeBinary + 1083 (IOKit + 7704) [0x1827a7e18] 1 CFDictionarySetValue + 331 (CoreFoundation + 30712) [0x1824047f8] 1 CFBasicHashSetValue + 863 (CoreFoundation + 31668) [0x182404bb4] 1 __CFStringEqual + 168 (CoreFoundation + 1047752) [0x1824fccc8] 1 IOSurfaceClientCopyAllValues + 367 (IOSurface + 16528) [0x183629090] 1 IOConnectCallMethod + 231 (IOKit + 24632) [0x1827ac038] 1 io_connect_method + 415 (IOKit + 433080) [0x18280fbb8] 1 mach_msg_trap + 8 (libsystem_kernel.dylib + 3560) [0x181fa7de8] 1 <Kernel mode> 1 -[RPBroadcastSampleHandler _sampleBufferFromIOSurface:timingInfo:] + 67 (ReplayKit + 58808) [0x19e0df5b8] 1 CMVideoFormatDescriptionCreateForImageBuffer + 635 (CoreMedia + 157804) [0x1857d786c] 1 CMVideoFormatDescriptionCreate + 107 (CoreMedia + 157032) [0x1857d7568] 1 FigDerivedFormatDescriptionCreate + 415 (CoreMedia + 35360) [0x1857b9a20] 1 CFPropertyListCreateDeepCopy + 915 (CoreFoundation + 292876) [0x18244480c] 1 CFDictionaryCreate + 215 (CoreFoundation + 296564) [0x182445674] 1 CFBasicHashCreate + 107 (CoreFoundation + 28916) [0x1824040f4] 1 _CFRuntimeCreateInstance + 651 (CoreFoundation + 26056) [0x1824035c8] 1 object_setClass + 100 (libobjc.A.dylib + 26484) [0x1816fa774] 2 ??? (LABroadcastUpload + 7320248) [0x102eef2b8] 2 ??? (LABroadcastUpload + 7322612) [0x102eefbf4] 2 VTCompressionSessionEncodeFrame + 99 (VideoToolbox + 50016) [0x185ccf360] 2 VTCompressionSessionRemote_EncodeFrame + 59 (VideoToolbox + 313336) [0x185d0f7f8] 2 vtCompressionSessionRemote_EncodeFrameCommon + 247 (VideoToolbox + 313596) [0x185d0f8fc] 2 FigRemote_CreateSerializedAtomDataForPixelBuffer + 31 (CoreMedia + 1105980) [0x1858bf03c] 2 FigRemote_CreateSerializedAtomDataAndSurfaceForPixelBuffer + 267 (CoreMedia + 1106372) [0x1858bf1c4] 2 sbufAtom_createSerializedDataForPixelBuffer + 547 (CoreMedia + 1107144) [0x1858bf4c8] 2 sbufAtom_appendAtomWithMemoryBlock + 95 (CoreMedia + 1115024) [0x1858c1390] 2 FigNEAtomWriterAppendData + 91 (CoreMedia + 1115568) [0x1858c15b0] 2 _platform_memmove + 96 (libsystem_platform.dylib + 6704) [0x18215ea30] 2 <Kernel mode> 1 __64-[RPBroadcastSampleHandler _processPayloadWithAudioSample:type:]_block_invoke + 487 (ReplayKit + 58596) [0x19e0df4e4] 1 mvm_deallocate_pages + 75 (libsystem_malloc.dylib + 58880) [0x18200c600] 1 _kernelrpc_mach_vm_deallocate_trap + 8 (libsystem_kernel.dylib + 3356) [0x181fa7d1c] 1 <Kernel mode> 1 ??? (LABroadcastUpload + 344176) [0x102848070] 1 ??? (LABroadcastUpload + 346144) [0x102848820] 1 ??? (LABroadcastUpload + 697624) [0x10289e518] 1 ??? (LABroadcastUpload + 9076476) [0x10309befc] 1 _dispatch_queue_barrier_sync_invoke_and_complete + 55 (libdispatch.dylib + 42428) [0x181e3d5bc] 1 _dispatch_client_callout + 15 (libdispatch.dylib + 6752) [0x181e34a60] 1 ??? (LABroadcastUpload + 9077740) [0x10309c3ec] 1 ??? (LABroadcastUpload + 9055864) [0x103096e78] 1 ??? (LABroadcastUpload + 9044532) [0x103094234] 1 ??? (LABroadcastUpload + 9043328) [0x103093d80] 1 AudioConverterFillComplexBuffer + 963 (AudioToolbox + 3332076) [0x18639d7ec] 1 BufferedAudioConverter::FillBuffer + 803 (AudioToolbox + 57712) [0x18607e170] 1 AudioConverterChain::RenderOutput + 119 (AudioToolbox + 56784) [0x18607ddd0] 1 CodecConverter::EncoderFillBuffer + 1059 (AudioToolbox + 2281600) [0x18629d080] 1 ProduceOutputPackets + 27 (AudioCodecs + 33200) [0x1a2b2a1b0] 1 ACMP4AACBaseEncoder::ProduceOutputPackets + 231 (AudioCodecs + 894872) [0x1a2bfc798] 1 AACEncoder::EncodeFrame + 515 (AudioCodecs + 445952) [0x1a2b8ee00] 1 aacEncodeFrame + 339 (AudioCodecs + 670340) [0x1a2bc5a84] 1 AdvanceLoopKernel + 1099 (AudioCodecs + 164132) [0x1a2b4a124] 1 calcBitrateNMR + 83 (AudioCodecs + 168336) [0x1a2b4b190] 1 EvaluateLoopChannel + 95 (AudioCodecs + 21796) [0x1a2b27524] 1 InvQuantizeAndEvaluateSpectrum_Neon + 184 (AudioCodecs + 1261244) [0x1a2c55ebc] 1 invocation function for block in __CFURLCache::CreateAndStoreCacheNode + 2003 (CFNetwork + 493812) [0x182b3e8f4] 1 -[NSURLStorage_CacheClient addCachedResponseWithDictionary:key:] + 131 (CFNetwork + 905132) [0x182ba2fac] 1 _NSXPCDistantObjectSimpleMessageSend2 + 63 (Foundation + 2355548) [0x1830b415c] 1 -[NSXPCConnection _sendSelector:withProxy:arg1:arg2:] + 135 (Foundation + 228044) [0x182eacacc] 1 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 1763 (Foundation + 71044) [0x182e86584] 1 -[NSXPCInterface _interfaceForArgument:ofSelector:reply:] + 231 (Foundation + 73480) [0x182e86f08] 1 -[NSObject isKindOfClass:] + 32 (libobjc.A.dylib + 157520) [0x18171a750] 1 <Effective Thread QoS Background, Requested Thread QoS Background> 1 ??? (LABroadcastUpload + 444760) [0x102860958] 1 __59-[RPBroadcastSampleHandler _processPayloadWithVideoSample:]_block_invoke + 307 (ReplayKit + 57728) [0x19e0df180] 1 ??? (LABroadcastUpload + 444964) [0x102860a24] 1 ??? (LABroadcastUpload + 426324) [0x10285c154] 1 ??? (LABroadcastUpload + 426576) [0x10285c250] 1 ??? (LABroadcastUpload + 419812) [0x10285a7e4] 1 ??? (LABroadcastUpload + 415888) [0x102859890] 1 ??? (LABroadcastUpload + 410632) [0x102858408] 1 ??? (LABroadcastUpload + 410268) [0x10285829c] 1 +[NSString stringWithUTF8String:] + 147 (Foundation + 28888) [0x182e7c0d8] 1 CFStringCreateWithBytes + 43 (CoreFoundation + 30368) [0x1824046a0] 1 __CFStringCreateImmutableFunnel3 + 1787 (CoreFoundation + 1053888) [0x1824fe4c0] 1 __CFSearchStringROM + 104 (CoreFoundation + 610268) [0x182491fdc] 10 _dispatch_queue_serial_drain$VARIANT$mp + 279 (libdispatch.dylib + 47212) [0x181e3e86c] 8 _dispatch_mach_invoke$VARIANT$mp + 507 (libdispatch.dylib + 102124) [0x181e4beec] 8 _dispatch_queue_serial_drain$VARIANT$mp + 279 (libdispatch.dylib + 47212) [0x181e3e86c] 8 _dispatch_mach_msg_invoke$VARIANT$mp + 351 (libdispatch.dylib + 99392) [0x181e4b440] 8 _dispatch_client_callout4 + 15 (libdispatch.dylib + 6960) [0x181e34b30] 6 _xpc_connection_mach_event + 983 (libxpc.dylib + 10436) [0x1821a08c4] 6 _xpc_connection_call_event_handler + 67 (libxpc.dylib + 20272) [0x1821a2f30] 5 message_handler + 239 (Foundation + 355008) [0x182ecbac0] 2 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 407 (Foundation + 212724) [0x182ea8ef4] 2 -[NSXPCDecoder _decodeMessageFromXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:interface:] + 39 (Foundation + 217648) [0x182eaa230] 2 -[NSXPCDecoder __decodeXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:isReply:replySelector:interface:] + 1959 (Foundation + 82700) [0x182e8930c] 2 _NSXPCSerializationDecodeInvocationObjectOnlyArgumentArray + 523 (Foundation + 2516308) [0x1830db554] 2 -[NSXPCDecoder _decodeObjectOfClasses:atObject:] + 131 (Foundation + 83796) [0x182e89754] 2 _decodeObject + 1023 (Foundation + 2326612) [0x1830ad054] 1 -[NSDictionary initWithCoder:] + 327 (Foundation + 181160) [0x182ea13a8] 1 -[NSDictionary initWithObjects:forKeys:] + 263 (CoreFoundation + 427020) [0x18246540c] 1 _platform_memset + 232 (libsystem_platform.dylib + 7656) [0x18215ede8] 1 -[NSDictionary initWithCoder:] + 251 (Foundation + 181084) [0x182ea135c] 1 -[NSXPCDecoder _decodeArrayOfObjectsForKey:] + 195 (Foundation + 212284) [0x182ea8d3c] 1 _NSXPCSerializationIterateArrayObject + 179 (Foundation + 373240) [0x182ed01f8] 1 __44-[NSXPCDecoder _decodeArrayOfObjectsForKey:]_block_invoke + 35 (Foundation + 373364) [0x182ed0274] 1 _decodeObject + 507 (Foundation + 2326096) [0x1830ace50] 1 _NSXPCSerializationStringForObject + 39 (Foundation + 373576) [0x182ed0348] 1 _getStringAtMarker + 351 (Foundation + 373960) [0x182ed04c8] 1 CFStringCreateWithCharacters + 55 (CoreFoundation + 170392) [0x182426998] 1 __CFStringCreateImmutableFunnel3 + 2023 (CoreFoundation + 1054124) [0x1824fe5ac] 1 _CFRuntimeCreateInstance + 299 (CoreFoundation + 25704) [0x182403468] 1 malloc_zone_malloc + 159 (libsystem_malloc.dylib + 5596) [0x181fff5dc] 1 nano_malloc + 43 (libsystem_malloc.dylib + 76192) [0x1820109a0] 1 OSAtomicDequeue$VARIANT$mp + 24 (libsystem_platform.dylib + 26896) [0x182163910] 2 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 2427 (Foundation + 214744) [0x182ea96d8] 2 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S2__ + 19 (Foundation + 2316640) [0x1830aa960] 1 -[RPBroadcastSampleHandler processPayload:completion:] + 95 (ReplayKit + 59156) [0x19e0df714] 1 __58-[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:]_block_invoke + 155 (Foundation + 2312336) [0x1830a9890] 1 -[_NSXPCConnectionRequestedReplies endTransactionForSequence:completionHandler:] + 211 (Foundation + 2353748) [0x1830b3a54] 1 _sendReplyArgumentsOnly + 307 (Foundation + 2312688) [0x1830a99f0] 1 _xpc_dispose + 63 (libxpc.dylib + 9008) [0x1821a0330] 1 _xpc_serializer_dispose + 347 (libxpc.dylib + 11712) [0x1821a0dc0] 1 _dispatch_dispose$VARIANT$mp + 95 (libdispatch.dylib + 14336) [0x181e36800] 1 objc_destructInstance + 139 (libobjc.A.dylib + 84440) [0x1817089d8] 1 objc_object::sidetable_clearDeallocating + 76 (libobjc.A.dylib + 153348) [0x181719704] 1 -[RPBroadcastSampleHandler processPayload:completion:] + 75 (ReplayKit + 59136) [0x19e0df700] 1 -[RPBroadcastSampleHandler _processPayload:] + 231 (ReplayKit + 56588) [0x19e0ded0c] 1 -[RPBroadcastSampleHandler _processPayloadWithVideoSample:] + 139 (ReplayKit + 57376) [0x19e0df020] 1 _dispatch_event_loop_poke$VARIANT$mp + 427 (libdispatch.dylib + 117656) [0x181e4fb98] 1 kevent_id + 8 (libsystem_kernel.dylib + 145340) [0x181fca7bc] 1 <Kernel mode> 1 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 1819 (Foundation + 214136) [0x182ea9478] 1 +[NSMethodSignature signatureWithObjCTypes:] + 199 (CoreFoundation + 176656) [0x182428210] 1 CFDictionaryGetValue + 223 (CoreFoundation + 21088) [0x182402260] 1 CFBasicHashFindBucket + 431 (CoreFoundation + 21548) [0x18240242c] 1 typeStringEqual + 11 (CoreFoundation + 178768) [0x182428a50] 1 _platform_strcmp + 188 (libsystem_platform.dylib + 5452) [0x18215e54c] 1 ::AutoreleasePoolPage::pop(void*) + 843 (libobjc.A.dylib + 155964) [0x18171a13c] 1 objc_object::release + 80 (libobjc.A.dylib + 153652) [0x181719834] 2 _xpc_connection_mach_event + 963 (libxpc.dylib + 10416) [0x1821a08b0] 2 _xpc_connection_unpack_message + 99 (libxpc.dylib + 15492) [0x1821a1c84] 2 _xpc_serializer_unpack + 755 (libxpc.dylib + 16548) [0x1821a20a4] 2 _xpc_dictionary_deserialize + 111 (libxpc.dylib + 21740) [0x1821a34ec] 1 _xpc_dictionary_wire_length + 0 (libxpc.dylib + 17456) [0x1821a2430] 1 _xpc_dictionary_apply_wire_f + 547 (libxpc.dylib + 19512) [0x1821a2c38] 1 _xpc_dictionary_deserialize_apply + 47 (libxpc.dylib + 21828) [0x1821a3544] 1 _xpc_array_deserialize + 103 (libxpc.dylib + 21220) [0x1821a32e4] 1 _os_object_alloc_realized + 39 (libdispatch.dylib + 6872) [0x181e34ad8] 1 class_createInstance + 75 (libobjc.A.dylib + 83920) [0x1817087d0] 1 calloc + 39 (libsystem_malloc.dylib + 16224) [0x182001f60] 1 malloc_zone_calloc + 80 (libsystem_malloc.dylib + 16340) [0x182001fd4] 2 _dispatch_source_invoke$VARIANT$mp + 1363 (libdispatch.dylib + 79748) [0x181e46784] 2 _dispatch_continuation_pop$VARIANT$mp + 423 (libdispatch.dylib + 40596) [0x181e3ce94] 2 _dispatch_client_callout + 15 (libdispatch.dylib + 6752) [0x181e34a60] 1 dispatch_mig_server$VARIANT$mp + 499 (libdispatch.dylib + 106240) [0x181e4cf00] 1 vtcompressionsessioncallback_server + 95 (VideoToolbox + 561740) [0x185d4c24c] 1 _XFrameIsPending + 51 (VideoToolbox + 561400) [0x185d4c0f8] 1 VTCompressionSessionRemoteCallbackServer_FrameIsPending + 35 (VideoToolbox + 320396) [0x185d1138c] 1 vtcsr_dequeueAllPendingFramesAndCallbackClientForEach + 119 (VideoToolbox + 310840) [0x185d0ee38] 1 _dispatch_event_loop_poke$VARIANT$mp + 427 (libdispatch.dylib + 117656) [0x181e4fb98] 1 kevent_id + 8 (libsystem_kernel.dylib + 145340) [0x181fca7bc] 1 <Kernel mode> 1 __nw_channel_create_block_invoke + 51 (libsystem_network.dylib + 187176) [0x182047b28] 1 nw_channel_update_input_source + 135 (libsystem_network.dylib + 189232) [0x182048330] 1 nw_channel_add_input_frames + 4327 (libsystem_network.dylib + 193660) [0x18204947c] 1 tcp_input_flush + 83 (libusrtcp.dylib + 273224) [0x18360ab48] 1 nw_protocol_tcp_wake_read + 459 (libusrtcp.dylib + 106488) [0x1835e1ff8] 1 ConnectionProtocolInputAvailable + 63 (CFNetwork + 2369112) [0x182d08658] 1 ConnectionProtocolServiceReads + 379 (CFNetwork + 2365028) [0x182d07664] 1 ConnectionProtocolReadInner block_pointer, void (nw_frame_array_s*, CFStreamError) block_pointer) + 987 (CFNetwork + 2366192) [0x182d07af0] 1 TCPIOConnection::readCompleted block_pointer) + 323 (CFNetwork + 1711772) [0x182c67e9c] 1 HTTPEngine::_readHeadersDoParse + 299 (CFNetwork + 2267088) [0x182cef7d0] 1 invocation function for block in HTTPEngine::_readHeadersDoParse + 39 (CFNetwork + 2269244) [0x182cf003c] 1 HTTPMessage::appendBytes + 47 (CFNetwork + 857260) [0x182b974ac] 1 HTTPParser::parse + 387 (CFNetwork + 2101944) [0x182cc72b8] 1 HTTPParser::commitAccumulatedLine + 91 (CFNetwork + 2101512) [0x182cc7108] 1 HTTPParser::commitHeaderLine + 147 (CFNetwork + 2101280) [0x182cc7020] 1 HTTPHeaderKeyMixedValue::HTTPHeaderKeyMixedValue + 80 (CFNetwork + 859804) [0x182b97e9c] 2 _dispatch_queue_serial_drain$VARIANT$mp + 723 (libdispatch.dylib + 47656) [0x181e3ea28] 2 _dispatch_event_loop_drain$VARIANT$mp + 323 (libdispatch.dylib + 118916) [0x181e50084] 2 _dispatch_kq_drain + 107 (libdispatch.dylib + 119068) [0x181e5011c] 2 kevent_id + 8 (libsystem_kernel.dylib + 145340) [0x181fca7bc] 2 <Kernel mode> 3 _dispatch_workloop_worker_thread$VARIANT$mp + 491 (libdispatch.dylib + 85992) [0x181e47fe8] 2 _dispatch_mach_merge_msg$VARIANT$mp + 0 (libdispatch.dylib + 91668) [0x181e49614] 1 _dispatch_event_loop_merge$VARIANT$mp + 131 (libdispatch.dylib + 119388) [0x181e5025c] 1 _dispatch_mach_merge_msg$VARIANT$mp + 127 (libdispatch.dylib + 91796) [0x181e49694] 1 _voucher_create_with_mach_voucher + 219 (libdispatch.dylib + 134296) [0x181e53c98] 1 _os_object_alloc_realized + 39 (libdispatch.dylib + 6872) [0x181e34ad8] 1 class_createInstance + 75 (libobjc.A.dylib + 83920) [0x1817087d0] 1 calloc + 39 (libsystem_malloc.dylib + 16224) [0x182001f60] 1 malloc_zone_calloc + 167 (libsystem_malloc.dylib + 16428) [0x18200202c] 1 nano_calloc + 79 (libsystem_malloc.dylib + 76316) [0x182010a1c] 1 OSAtomicDequeue$VARIANT$mp + 24 (libsystem_platform.dylib + 26896) [0x182163910] 25 _pthread_start + 291 (libsystem_pthread.dylib + 8464) [0x182169110] 25 _pthread_body + 271 (libsystem_pthread.dylib + 8736) [0x182169220] 22 __NSThread__start__ + 1039 (Foundation + 1150716) [0x182f8defc] 21 ??? (LABroadcastUpload + 699360) [0x10289ebe0] 21 ??? (LABroadcastUpload + 579692) [0x10288186c] 21 ??? (LABroadcastUpload + 438420) [0x10285f094] 13 ??? (LABroadcastUpload + 340072) [0x102847068] 7 ??? (LABroadcastUpload + 271384) [0x102836418] 4 ??? (LABroadcastUpload + 369892) [0x10284e4e4] 1 <Override Thread QoS Default> 3 ??? (LABroadcastUpload + 369920) [0x10284e500] 1 <Override Thread QoS Default> 6 ??? (LABroadcastUpload + 271460) [0x102836464] 4 ??? (LABroadcastUpload + 369904) [0x10284e4f0] 2 <Kernel mode> 2 <Override Thread QoS Default> 2 ??? (LABroadcastUpload + 369892) [0x10284e4e4] 4 ??? (LABroadcastUpload + 339528) [0x102846e48] 3 ??? (LABroadcastUpload + 339512) [0x102846e38] 1 ??? (LABroadcastUpload + 339516) [0x102846e3c] 1 ??? (LABroadcastUpload + 699248) [0x10289eb70] 1 ??? (LABroadcastUpload + 704568) [0x1028a0038] 1 objc_loadWeakRetained + 148 (libobjc.A.dylib + 150444) [0x181718bac] 3 ??? (LABroadcastUpload + 8371680) [0x102fefde0] 3 ??? (LABroadcastUpload + 8334892) [0x102fe6e2c] 3 ??? (LABroadcastUpload + 8334068) [0x102fe6af4] 3 ??? (LABroadcastUpload + 8390348) [0x102ff46cc] 3 ??? (LABroadcastUpload + 8449800) [0x103002f08] 3 ??? (LABroadcastUpload + 8382704) [0x102ff28f0] 3 ??? (LABroadcastUpload + 8386660) [0x102ff3864] 3 -[NSRunLoop runMode:beforeDate:] + 303 (Foundation + 34420) [0x182e7d674] 3 CFRunLoopRunSpecific + 551 (CoreFoundation + 48552) [0x182408da8] 3 __CFRunLoopRun + 1203 (CoreFoundation + 964508) [0x1824e879c] 3 __CFRunLoopDoSources0 + 275 (CoreFoundation + 973868) [0x1824eac2c] 3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 23 (CoreFoundation + 975876) [0x1824eb404] 3 ??? (LABroadcastUpload + 8383980) [0x102ff2dec] 3 ??? (LABroadcastUpload + 8385160) [0x102ff3288] 3 ??? (LABroadcastUpload + 8451264) [0x1030034c0] 3 ??? (LABroadcastUpload + 8450596) [0x103003224] 3 ??? (LABroadcastUpload + 8450400) [0x103003160] 3 ??? (LABroadcastUpload + 8464936) [0x103006a28] 3 ??? (LABroadcastUpload + 8326176) [0x102fe4c20] 2 ??? (LABroadcastUpload + 8629576) [0x10302ed48] 2 ??? (LABroadcastUpload + 8936172) [0x103079aec] 2 ??? (LABroadcastUpload + 8964452) [0x103080964] 2 ??? (LABroadcastUpload + 8458888) [0x103005288] 2 ??? (LABroadcastUpload + 8444952) [0x103001c18] 2 <Effective Thread QoS Unspecified, Requested Thread QoS Unspecified> 1 ??? (LABroadcastUpload + 8629644) [0x10302ed8c] 1 ??? (LABroadcastUpload + 8363040) [0x102fedc20] 1 free + 0 (libsystem_malloc.dylib + 13756) [0x1820015bc] 1 <Effective Thread QoS Unspecified, Requested Thread QoS Unspecified> 7 _pthread_wqthread + 1175 (libsystem_pthread.dylib + 4012) [0x182167fac] 7 _dispatch_worker_thread3 + 119 (libdispatch.dylib + 59900) [0x181e419fc] 6 _dispatch_root_queue_drain + 923 (libdispatch.dylib + 60924) [0x181e41dfc] 6 _dispatch_client_callout + 15 (libdispatch.dylib + 6752) [0x181e34a60] 6 _dispatch_call_block_and_release + 23 (libdispatch.dylib + 6816) [0x181e34aa0] 6 ??? (LABroadcastUpload + 4247616) [0x102c01040] 3 ??? (LABroadcastUpload + 4256256) [0x102c03200] 3 usleep + 63 (libsystem_c.dylib + 53324) [0x181ee204c] 2 nanosleep + 211 (libsystem_c.dylib + 53548) [0x181ee212c] 2 __semwait_signal + 23 (libsystem_kernel.dylib + 140820) [0x181fc9614] 2 _pthread_exit_if_canceled + 0 (libsystem_pthread.dylib + 7012) [0x182168b64] 1 __semwait_signal + 8 (libsystem_kernel.dylib + 140804) [0x181fc9604] 1 <Kernel mode> 2 ??? (LABroadcastUpload + 4256168) [0x102c031a8] 2 ??? (LABroadcastUpload + 4205132) [0x102bf6a4c] 2 ??? (LABroadcastUpload + 11458408) [0x1032e1768] 1 ??? (LABroadcastUpload + 4256300) [0x102c0322c] 1 ??? (LABroadcastUpload + 4317016) [0x102c11f58] 1 ??? (LABroadcastUpload + 4316564) [0x102c11d94] 1 ??? (LABroadcastUpload + 8245184) [0x102fd0fc0] 1 ??? (LABroadcastUpload + 8629048) [0x10302eb38] 1 ??? (LABroadcastUpload + 8364372) [0x102fee154] 1 ??? (LABroadcastUpload + 8364204) [0x102fee0ac] 1 ??? (LABroadcastUpload + 8402332) [0x102ff759c] 1 __psynch_cvwait + 8 (libsystem_kernel.dylib + 139496) [0x181fc90e8] 1 <Kernel mode> 1 _dispatch_root_queue_drain + 587 (libdispatch.dylib + 60588) [0x181e41cac] 1 _dispatch_source_invoke$VARIANT$mp + 1363 (libdispatch.dylib + 79748) [0x181e46784] 1 _dispatch_continuation_pop$VARIANT$mp + 423 (libdispatch.dylib + 40596) [0x181e3ce94] 1 _dispatch_client_callout + 15 (libdispatch.dylib + 6752) [0x181e34a60] 1 ??? (LABroadcastUpload + 560800) [0x10287cea0] 1 ??? (LABroadcastUpload + 562864) [0x10287d6b0] 1 ??? (LABroadcastUpload + 599716) [0x1028866a4] 1 ??? (LABroadcastUpload + 615308) [0x10288a38c] 1 ??? (LABroadcastUpload + 8300024) [0x102fde5f8] 1 ??? (LABroadcastUpload + 8291616) [0x102fdc520] 1 ??? (LABroadcastUpload + 8296568) [0x102fdd878] 1 ??? (LABroadcastUpload + 8220012) [0x102fcad6c] 1 ??? (LABroadcastUpload + 8204516) [0x102fc70e4] 1 ??? (LABroadcastUpload + 8205116) [0x102fc733c] 1 SecItemCopyMatching + 339 (Security + 463608) [0x1831df2f8] 1 SecOSStatusWith + 51 (Security + 454740) [0x1831dd054] 1 __SecItemCopyMatching_block_invoke + 119 (Security + 468436) [0x1831e05d4] 1 SecItemAuthDoQuery + 519 (Security + 462564) [0x1831deee4] 1 SecItemAuthDo + 419 (Security + 460160) [0x1831de580] 1 __SecItemAuthDoQuery_block_invoke + 323 (Security + 465572) [0x1831dfaa4] 1 __SecItemCopyMatching_block_invoke_2 + 215 (Security + 468664) [0x1831e06b8] 1 securityd_send_sync_and_do + 79 (Security + 47184) [0x183179850] 1 securityd_message_with_reply_sync + 167 (Security + 46340) [0x183179504] 1 xpc_connection_send_message_with_reply_sync + 195 (libxpc.dylib + 26104) [0x1821a45f8] 1 dispatch_mach_send_with_result_and_wait_for_reply$VARIANT$mp + 55 (libdispatch.dylib + 97580) [0x181e4ad2c] 1 _dispatch_mach_send_and_wait_for_reply + 559 (libdispatch.dylib + 96308) [0x181e4a834] 1 mach_msg_trap + 8 (libsystem_kernel.dylib + 3560) [0x181fa7de8] 1 <Kernel mode> Binary Images: 0x1027f4000 - ??? com.tencent.liveassistant.dailybuild.db.broadcastupload 2.10.0 (2.10.0.11) <562B0868-A0CA-32D5-9E98-E87676053442> /private/var/containers/Bundle/Application/62E054DE-ADDD-4955-BAA9-BD4C7A487B7F/LiveAssistant.app/PlugIns/LABroadcastUpload.appex/LABroadcastUpload 0x1816f4000 - 0x181daffff libobjc.A.dylib <EB1135B2-BDE9-3B69-B96E-42CA98200183> /usr/lib/libobjc.A.dylib 0x181e33000 - 0x181e98fff libdispatch.dylib <0C931AC7-6013-3DE1-87BB-6F440BEED5EB> /usr/lib/system/libdispatch.dylib 0x181ed5000 - 0x181f52fff libsystem_c.dylib <61D2E950-ADD7-3139-AEA4-59B55997EA48> /usr/lib/system/libsystem_c.dylib 0x181fa7000 - 0x181fcffff libsystem_kernel.dylib <E102701E-F880-3CD4-A5D5-4F5F14433DBD> /usr/lib/system/libsystem_kernel.dylib 0x181ffe000 - 0x182019fff libsystem_malloc.dylib <6DD6981A-DEF5-30B3-B606-2F29ADE13BB2> /usr/lib/system/libsystem_malloc.dylib 0x18201a000 - 0x182145fff libsystem_network.dylib <9EC043D8-CB25-38DC-9BA8-4E324CD5B416> /usr/lib/system/libsystem_network.dylib 0x18215d000 - 0x182166fff libsystem_platform.dylib <97DAE109-BAD6-3E58-8E5D-63B8DBDDADCF> /usr/lib/system/libsystem_platform.dylib 0x182167000 - 0x182176fff libsystem_pthread.dylib <07C87E38-74B7-3D12-8F0F-A331D8894B97> /usr/lib/system/libsystem_pthread.dylib 0x18219e000 - 0x1821c8fff libxpc.dylib <05E40D46-2111-3A32-A4CD-B7AFC770B119> /usr/lib/system/libxpc.dylib 0x1823fd000 - 0x182793fff com.apple.CoreFoundation 6.9 (1452.23) <533C841E-D6E9-313D-8ADB-02388744E2EF> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation 0x1827a6000 - 0x18282afff com.apple.framework.IOKit 2.0.2 <54433B44-779D-3937-8D07-89A4017A2948> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit 0x182ac6000 - 0x182e74fff com.apple.CFNetwork 902.2 (902.2) <CE119938-B410-382B-B448-1E599405EF23> /System/Library/Frameworks/CFNetwork.framework/CFNetwork 0x182e75000 - 0x18316dfff com.apple.Foundation 6.9 (1452.23) <2EACEF3C-B1E5-323E-AC1A-2E0D743C81A5> /System/Library/Frameworks/Foundation.framework/Foundation 0x18316e000 - 0x18326ffff com.apple.Security 10.0 (58286.70.7) <B4A51FC3-CB16-3F7B-8786-D8D37B56E515> /System/Library/Frameworks/Security.framework/Security 0x1835c8000 - 0x183624fff libusrtcp.dylib <BA4A0D2E-24DD-3EA0-A930-7B03598564CC> /usr/lib/libusrtcp.dylib 0x183625000 - 0x18362ffff com.apple.IOSurface 211.14 (211.14) <1F24B805-6501-3F8D-94B5-E18C144F24BC> /System/Library/Frameworks/IOSurface.framework/IOSurface 0x1855e5000 - 0x18560efff com.apple.CoreVideo 1.8 (0.0) <6F568461-BB21-3003-BCF5-26D9D4EB20AF> /System/Library/Frameworks/CoreVideo.framework/CoreVideo 0x1857b1000 - 0x185936fff com.apple.CoreMedia 1.0 (2276.71.2) <784A7889-8519-3F92-BCBF-5170378405FC> /System/Library/Frameworks/CoreMedia.framework/CoreMedia 0x185cc3000 - 0x185d7afff com.apple.VideoToolbox 1.0 (2276.71.2) <C99ED6C9-2850-3FD6-9ECF-34BA2BAECE4E> /System/Library/Frameworks/VideoToolbox.framework/VideoToolbox 0x186070000 - 0x1865a8fff com.apple.audio.toolbox.AudioToolbox 1.8 (1.8) <082E0192-9022-3EDF-99B1-D4FF195F9276> /System/Library/Frameworks/AudioToolbox.framework/AudioToolbox 0x19e0d1000 - 0x19e0f5fff com.apple.ReplayKit 1.0 (1) <9B0FE0DA-9DDA-3347-9586-717BC1304DFE> /System/Library/Frameworks/ReplayKit.framework/ReplayKit 0x1a2b22000 - 0x1a2f60fff AudioCodecs <1BA749E0-2C9E-3C99-94C3-9295C0688017> /System/Library/Frameworks/AudioToolbox.framework/AudioCodecs
二、问题分析
对于内存问题,目前尚无好的解决办法,苹果似乎在系统升级之后会调高限制的大小值,但是不多,不能解决根本问题。
本文主要对wakeups这个错误,CPU使用过多进行分析
1)苹果为什么要针对后台进程做这个限制?
过多的线程调度意味着繁忙得使用CPU,在移动设备上,频繁使用CPU将导致耗电量增大,电量消耗过快影响整个手机的使用。
2)是否能通过配置修改
目前查询到的信息是无法修改,已经在苹果的开发者论坛提交了一个反馈:
https://forums.developer.apple.com/message/332006#332006
3)线程切换是怎样造成的。
线程切换可能由于系统中断(系统调用带来)、主动的线程调度带来(sleep函数)带来。
这是之前的一份wakeup的记录,显示NSLog 和 stringwithformate 会带来线程切换。
Powerstats for: LABroadcastUploa [1557] UUID: F91308C6-C89C-3571-94C0-BBE49DFB2013 Start time: 2017-09-29 12:18:04 +0800 End time: 2017-09-29 12:18:31 +0800 Microstackshots: 46 samples (100%) Primary state: 30 samples Frontmost App, User mode, Effective Thread QoS Default, Requested Thread QoS Default, Override Thread QoS Unspecified User Activity: 0 samples Idle, 46 samples Active Power Source: 0 samples on Battery, 46 samples on AC 33 _pthread_start + 311 (libsystem_pthread.dylib + 8696) [0x18618c1f8] 33 _pthread_body + 307 (libsystem_pthread.dylib + 9004) [0x18618c32c] 15 __NSThread__start__ + 995 (Foundation + 1103968) [0x186f4f860] 5 -[LAAudioMix mixAudioLoop:] (LAAudioMix.m:174 in LABroadcastUpload + 276808) [0x10264f948] 4 -[LAUploadManager pushCustomPCMData:len:] (LAUploadManager.m:164 in LABroadcastUpload + 157864) [0x1026328a8] 4 -[LAExLogger addOneLog:inFile:function:line:module:message:] (LAExLogger.m:115 in LABroadcastUpload + 77892) [0x10261f044] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:143 in LABroadcastUpload + 78616) [0x10261f318] 2 +[NSString stringWithFormat:] + 67 (Foundation + 10420) [0x186e448b4] 2 _CFStringCreateWithFormatAndArgumentsAux2 + 131 (CoreFoundation + 1054296) [0x18651f658] 2 __CFStringAppendFormatCore + 9436 (CoreFoundation + 1048212) [0x18651de94] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:161 in LABroadcastUpload + 78664) [0x10261f348] 2 NSLog + 31 (Foundation + 73876) [0x186e54094] 2 _NSLogv + 127 (Foundation + 972068) [0x186f2f524] 2 _CFLogvEx3 + 227 (CoreFoundation + 1239416) [0x18654c978] 2 __proc_info + 8 (libsystem_kernel.dylib + 135252) [0x186079054] 2 <Kernel mode>
NSLog可能比较好理解一点,因为NSLog不仅仅将log输出到控制台,还会将log输出(通过Socket)到远程的Console端口,Mac上的Console App能收到手机的Log就是这个原因。
根据glibc系统调用列表,socket中的操作都会走到系统调用中
对于stringWithFormate而言,格式化输出了一个字符串,也很可能走到malloc的系统调用中,这里猜测一下:
为了验证猜测,编写测试代码:
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.async { var i = 0; while(true) { String.init(format: "%dsadsdsd", i) i = i + 1 } } } }
代码比较简单,其中有一个Swift的字符串的格式化。其内部的实现会走到 __CFStringAppendFormatCore 方法中,这个和OC 这边的格式化方法是一致的。
上面的堆栈也能清楚看出来。
为了验证猜测,我下一个malloc的符号断点:
然后跑一下程序,果然断下来了
果然调用到了malloc,说明格式化字符串会产生内存分配的系统调用,从而导致线程切换。
4)优化方向
观察我们的助手log的输出,1s之内输出了一百多条的log
由此找到了一个方方向,减少输出log的系统调用。
5)优化
待完成
三、附录
1)系统调用列表:https://www.ibm.com/developerworks/cn/linux/kernel/syscall/part1/appendix.html
2)malloc 原理 http://legendtkl.com/2017/03/21/malloc-os-knowledge/
3)上一篇wakeups https://www.cnblogs.com/doudouyoutang/p/7610982.html
4)https://stackoverflow.com/questions/45511944/ios-how-to-measure-thread-wakeups
5)https://forums.developer.apple.com/message/332006#332006
6)https://www.theiphonewiki.com/wiki/Kernel_Syscalls