Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review SwiftTerm performance #63

Open
kirb opened this issue Apr 17, 2021 · 0 comments
Open

Review SwiftTerm performance #63

kirb opened this issue Apr 17, 2021 · 0 comments
Assignees
Labels
Milestone

Comments

@kirb
Copy link
Member

kirb commented Apr 17, 2021

As a contrived (but important) test, simply running yes has the following results:

  • Mac mini, 16GB RAM: Keeps up alright for a few minutes, before crashing with what feels like an OOM; 200%+ CPU (main thread and terminal queue both 100% busy)
  • iPhone 12 Pro Max: Runs for about 1 second, then terminal screen goes blank for about 1 second, then crashes

Of course, this is unacceptable performance. Terminal, iTerm2, GNOME Terminal, Mintty, Windows Terminal, likely even NewTerm 2 (iTerm1) can all handle this without issue as long as the buffer size isn’t set too crazy high. Specifically the same test with iTerm on the same Mac ran fine for 10 minutes with ~150% CPU usage. I think we’re definitely capable of getting the CPU usage down to at least 150% (100% main thread redrawing the UI 60 times a second, plus 50% on parsing), without crashing for any reason ever.

Incredibly annoyingly, the iOS issue seems to not generate a crash report! It leads me to believe it’s a very quick OOM or excessive CPU use watchdog kill. That should still generate a watchdog/runningboard report, yet, I’ve cleared out all crash reports so I don’t miss it and nothing is appearing under Analytics Data. Literally nothing useful in Console either! First it started falling over, and then it fell over.

macOS does generate a crash dialog, so that crash report is below. I expect fixing this will improve most or all of the iOS issue anyway.

The Mac crash report
Process:               NewTerm [4906]
Path:                  /Users/USER/Library/Developer/Xcode/DerivedData/NewTerm-asttuniqnnewnubyqqhgmsdqrzls/Build/Products/Debug-maccatalyst/NewTerm.app/Contents/MacOS/NewTerm
Identifier:            ws.hbang.Terminal
Version:               3.0b1 (11)
Code Type:             ARM-64 (Native)
Parent Process:        ??? [1]
Responsible:           NewTerm [4906]
User ID:               501

Date/Time:             2021-04-17 20:47:23.348 +0930
OS Version:            macOS 11.2.3 (20D91)
Report Version:        12
Anonymous UUID:        A1D68C53-E5E0-FF75-7521-0AB3EB2252E6


Time Awake Since Boot: 9000 seconds

System Integrity Protection: enabled

Crashed Thread:        5  Dispatch queue: ws.hbang.Terminal.terminal-queue

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000009
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [4906]

VM Regions Near 0x9:
--> 
    __TEXT                      102d08000-102dbc000    [  720K] r-x/r-x SM=COW  /Users/*/Library/Developer/Xcode/DerivedData/NewTerm-asttuniqnnewnubyqqhgmsdqrzls/Build/Products/Debug-maccatalyst/NewTerm.app/Contents/MacOS/NewTerm

Thread 0:: Dispatch queue: com.apple.main-thread
0   libsystem_malloc.dylib        	0x0000000185d52eb8 nanov2_allocate_from_block + 500
1   libsystem_malloc.dylib        	0x0000000185d52520 nanov2_allocate + 128
2   libsystem_malloc.dylib        	0x0000000185d52520 nanov2_allocate + 128
3   libsystem_malloc.dylib        	0x0000000185d5243c nanov2_malloc + 64
4   libsystem_malloc.dylib        	0x0000000185d6e0d8 _malloc_zone_malloc + 152
5   libswiftCore.dylib            	0x0000000191910cac Range<>.subscript.read + 120
6   libswiftCore.dylib            	0x0000000191910b6c protocol witness for Collection.subscript.read in conformance <> Range<A> + 72
7   libswiftCore.dylib            	0x00000001917b87f8 IndexingIterator.next() + 644
8   ws.hbang.NewTerm-Common       	0x00000001032bee90 StringSupplier.attributedString() + 736 (StringSupplier.swift:31)
9   ws.hbang.NewTerm-Common       	0x00000001032c731c TerminalController.updateTimerFired() + 2168 (TerminalController.swift:186)
10  ws.hbang.NewTerm-Common       	0x00000001032c7594 @objc TerminalController.updateTimerFired() + 32
11  com.apple.Foundation          	0x0000000186df2148 __NSFireTimer + 68
12  com.apple.CoreFoundation      	0x0000000186043068 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 32
13  com.apple.CoreFoundation      	0x0000000186042c68 __CFRunLoopDoTimer + 1064
14  com.apple.CoreFoundation      	0x00000001860426fc __CFRunLoopDoTimers + 392
15  com.apple.CoreFoundation      	0x000000018602763c __CFRunLoopRun + 1936
16  com.apple.CoreFoundation      	0x0000000186026740 CFRunLoopRunSpecific + 600
17  com.apple.HIToolbox           	0x000000018db4b5c4 RunCurrentEventLoopInMode + 292
18  com.apple.HIToolbox           	0x000000018db4b3f4 ReceiveNextEventCommon + 688
19  com.apple.HIToolbox           	0x000000018db4b124 _BlockUntilNextEventMatchingListInModeWithFilter + 76
20  com.apple.AppKit              	0x000000018880082c _DPSNextEvent + 868
21  com.apple.AppKit              	0x00000001887ff1ac -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1312
22  com.apple.AppKit              	0x00000001887f1060 -[NSApplication run] + 600
23  com.apple.AppKit              	0x00000001887c2804 NSApplicationMain + 1064
24  com.apple.AppKit              	0x0000000188ab46d4 _NSApplicationMainWithInfoDictionary + 24
25  com.apple.UIKitMacHelper      	0x0000000198c24fc4 UINSApplicationMain + 1276
26  com.apple.UIKitCore           	0x00000001a9331a60 UIApplicationMain + 164
27  ws.hbang.Terminal             	0x0000000102d49d58 main + 88 (AppDelegate.swift:13)
28  libdyld.dylib                 	0x0000000185f49f34 start + 4

Thread 1:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib        	0x0000000185ef4ce8 mach_msg_trap + 8
1   libsystem_kernel.dylib        	0x0000000185ef5098 mach_msg + 76
2   com.apple.CoreFoundation      	0x0000000186028e98 __CFRunLoopServiceMachPort + 380
3   com.apple.CoreFoundation      	0x000000018602736c __CFRunLoopRun + 1216
4   com.apple.CoreFoundation      	0x0000000186026740 CFRunLoopRunSpecific + 600
5   com.apple.Foundation          	0x0000000186dafa88 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 232
6   com.apple.Foundation          	0x0000000186e3e254 -[NSRunLoop(NSRunLoop) runUntilDate:] + 124
7   com.apple.UIKitCore           	0x00000001a9332ff4 -[UIEventFetcher threadMain] + 156
8   com.apple.Foundation          	0x0000000186da80f4 __NSThread__start__ + 864
9   libsystem_pthread.dylib       	0x0000000185f2e06c _pthread_start + 320
10  libsystem_pthread.dylib       	0x0000000185f28da0 thread_start + 8

Thread 2:: com.apple.NSEventThread
0   libsystem_kernel.dylib        	0x0000000185ef4ce8 mach_msg_trap + 8
1   libsystem_kernel.dylib        	0x0000000185ef5098 mach_msg + 76
2   com.apple.CoreFoundation      	0x0000000186028e98 __CFRunLoopServiceMachPort + 380
3   com.apple.CoreFoundation      	0x000000018602736c __CFRunLoopRun + 1216
4   com.apple.CoreFoundation      	0x0000000186026740 CFRunLoopRunSpecific + 600
5   com.apple.AppKit              	0x0000000188980864 _NSEventThread + 216
6   libsystem_pthread.dylib       	0x0000000185f2e06c _pthread_start + 320
7   libsystem_pthread.dylib       	0x0000000185f28da0 thread_start + 8

Thread 3:
0   libsystem_pthread.dylib       	0x0000000185f28d8c start_wqthread + 0

Thread 4:
0   libsystem_pthread.dylib       	0x0000000185f28d8c start_wqthread + 0

Thread 5 Crashed:: Dispatch queue: ws.hbang.Terminal.terminal-queue
0   libswiftCore.dylib            	0x0000000191b01c74 swift::metadataimpl::ValueWitnesses<swift::metadataimpl::NativeBox<unsigned long long, 8ul, 8ul, 8ul> >::initializeWithCopy(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) + 0
1   libswiftCore.dylib            	0x00000001917f4ccc Set.insert(_:) + 2068
2   ws.hbang.NewTerm-Common       	0x000000010337549c Terminal.updateRange(_:scrolling:updateDirtySet:) + 604
3   ws.hbang.NewTerm-Common       	0x0000000103378464 Terminal.scroll(isWrapped:) + 2608
4   ws.hbang.NewTerm-Common       	0x0000000103355a60 Terminal.insertCharacter(_:) + 960
5   ws.hbang.NewTerm-Common       	0x0000000103354ed8 Terminal.handlePrint(_:) + 5780
6   ws.hbang.NewTerm-Common       	0x000000010334c8e8 implicit closure #2 in implicit closure #1 in Terminal.configureParser(_:) + 152
7   ws.hbang.NewTerm-Common       	0x0000000103329240 EscapeSequenceParser.parse(data:) + 14808
8   ws.hbang.NewTerm-Common       	0x0000000103375004 Terminal.parse(buffer:) + 176
9   ws.hbang.NewTerm-Common       	0x0000000103374d08 Terminal.feed(byteArray:) + 204
10  ws.hbang.NewTerm-Common       	0x00000001032c7544 closure #1 in TerminalController.updateTimerFired() + 152 (TerminalController.swift:171)
11  ws.hbang.NewTerm-Common       	0x00000001032a4dac thunk for @escaping @callee_guaranteed () -> () + 52
12  libdispatch.dylib             	0x0000000185d7f8bc _dispatch_call_block_and_release + 32
13  libdispatch.dylib             	0x0000000185d81420 _dispatch_client_callout + 20
14  libdispatch.dylib             	0x0000000185d8877c _dispatch_lane_serial_drain + 580
15  libdispatch.dylib             	0x0000000185d892cc _dispatch_lane_invoke + 408
16  libdispatch.dylib             	0x0000000185d933fc _dispatch_workloop_worker_thread + 708
17  libsystem_pthread.dylib       	0x0000000185f2a060 _pthread_wqthread + 276
18  libsystem_pthread.dylib       	0x0000000185f28d94 start_wqthread + 8

Thread 6:
0   libsystem_pthread.dylib       	0x0000000185f28d8c start_wqthread + 0

Thread 5 crashed with ARM Thread State (64-bit):
    x0: 0x000000016d17c088   x1: 0x0000000000000009   x2: 0x00000001ec4d9990   x3: 0x00000001ec4d3f68
    x4: 0x0000000191b80b7c   x5: 0x0000000000000007   x6: 0x0000000000002800   x7: 0x0000000000000010
    x8: 0xe3ba0001ec4dd4a8   x9: 0x0000000191b01c74  x10: 0xfffffffe00000000  x11: 0x0000000200000003
   x12: 0x0000000000000003  x13: 0x000000016d17bea0  x14: 0x00000000b1428800  x15: 0x0000000000000051
   x16: 0x0000000191b75d90  x17: 0x00000001ec4dd498  x18: 0x0000000000000000  x19: 0x04f80001ec4dd4a0
   x20: 0x00000001ec4d9990  x21: 0x000000016d17be70  x22: 0x0000000000000008  x23: 0x00000001ec4dd4a0
   x24: 0x0000000191b01c70  x25: 0x0000000000000001  x26: 0x0000000000000001  x27: 0x97eb0001ec4d3f70
   x28: 0x0000600003aa2d00   fp: 0x000000016d17bff0   lr: 0x00000001917f4ccc
    sp: 0x000000016d17be70   pc: 0x0000000191b01c74 cpsr: 0x60001000
   far: 0x0000000000000009  esr: 0x92000006


VM Region Summary:
ReadOnly portion of Libraries: Total=975.5M resident=0K(0%) swapped_out_or_unallocated=975.5M(100%)
Writable regions: Total=1.8G written=0K(0%) resident=0K(0%) swapped_out=0K(0%) unallocated=1.8G(100%)
 
                                VIRTUAL   REGION 
REGION TYPE                        SIZE    COUNT (non-coalesced) 
===========                     =======  ======= 
Accelerate framework              1152K        9 
Activity Tracing                   256K        1 
CG backing stores                 3648K       12 
CG image                          1024K       22 
CoreAnimation                     7360K       67 
CoreGraphics                        48K        3 
CoreImage                          288K        3 
CoreUI image data                 2272K       25 
Foundation                          48K        2 
Kernel Alloc Once                   32K        1 
MALLOC                           654.8M       84 
MALLOC guard page                  192K       10 
MALLOC_MEDIUM (reserved)         944.0M        8         reserved VM address space (unallocated)
MALLOC_NANO (reserved)           256.0M        1         reserved VM address space (unallocated)
STACK GUARD                       56.1M        7 
Stack                             11.2M        7 
VM_ALLOCATE                       1536K       36 
__AUTH                            1813K      385 
__AUTH_CONST                      22.3M      604 
__CTF                               759        1 
__DATA                            12.3M      598 
__DATA_CONST                      24.0M      612 
__DATA_DIRTY                      3033K      309 
__FONT_DATA                          4K        1 
__LINKEDIT                       483.2M       15 
__OBJC_CONST                      5146K      373 
__OBJC_RO                         64.5M        1 
__OBJC_RW                         2544K        1 
__TEXT                           492.5M      634 
__UNICODE                          588K        1 
mapped file                      290.3M       51 
shared memory                       64K        4 
===========                     =======  ======= 
TOTAL                              3.3G     3888 
TOTAL, minus reserved VM space     2.1G     3888 
@kirb kirb added the bug label Apr 17, 2021
@kirb kirb added this to the 3.0 milestone Apr 17, 2021
@kirb kirb self-assigned this Apr 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant