1*043036a2SApple OSS Distributions# Multithreaded Stackshot 2*043036a2SApple OSS Distributions 3*043036a2SApple OSS DistributionsStackshot has been retrofitted to take advantage of multiple CPUs. This document 4*043036a2SApple OSS Distributionsdetails the design of multithreaded stackshot. 5*043036a2SApple OSS Distributions 6*043036a2SApple OSS Distributions## Terminology 7*043036a2SApple OSS Distributions 8*043036a2SApple OSS Distributions- **Initiating / Calling CPU**: The CPU which stackshot was called from. 9*043036a2SApple OSS Distributions- **Main CPU**: The CPU which populates workqueues and collects global state. 10*043036a2SApple OSS Distributions- **Auxiliary CPU**: A CPU which is not the main CPU. 11*043036a2SApple OSS Distributions- **KCData**: The containerized data structure that stackshot outputs. See 12*043036a2SApple OSS Distributions `osfmk/kern/kcdata.h` for more information. 13*043036a2SApple OSS Distributions 14*043036a2SApple OSS Distributions## Overview 15*043036a2SApple OSS Distributions 16*043036a2SApple OSS DistributionsWhen a stackshot is taken, the initiating CPU (the CPU from which stackshot was 17*043036a2SApple OSS Distributionscalled) sets up state. Then, it enters the debugger trap, and IPIs the other 18*043036a2SApple OSS Distributionscores into the debugger trap as well. The other CPUs call into stackshot from 19*043036a2SApple OSS Distributionsthe debugger trap instead of spinning, and determine if they are eligible to 20*043036a2SApple OSS Distributionswork based on perfcontrol's recommendation. (We need to do this because even if 21*043036a2SApple OSS Distributionsa CPU is derecommended due to thermal limits or otherwise, it will still be 22*043036a2SApple OSS DistributionsIPI'd into the debugger trap, and we want to avoid overheating the CPU). 23*043036a2SApple OSS Distributions 24*043036a2SApple OSS DistributionsOn AMP systems, a suitable P-core is chosen to be the “main” CPU, and begins 25*043036a2SApple OSS Distributionspopulating queues of tasks to be put into the stackshot and collecting bits of 26*043036a2SApple OSS Distributionsglobal state (On SMP systems, the initiating CPU is always assigned to be the 27*043036a2SApple OSS Distributionsmain CPU). 28*043036a2SApple OSS Distributions 29*043036a2SApple OSS DistributionsThe other CPUs begin chipping away at the queues, and the main CPU joins 30*043036a2SApple OSS Distributionsin once it is done populating them. Once all CPUs are finished, they exit the 31*043036a2SApple OSS Distributionsdebugger trap, interrupts are re-enabled, and the kcdata from all of the CPUs 32*043036a2SApple OSS Distributionsare collated together by the caller CPU. The output is identical to 33*043036a2SApple OSS Distributionssingle-threaded stackshot. 34*043036a2SApple OSS Distributions 35*043036a2SApple OSS DistributionsIt is important to note that since stackshot happens outside of the context of 36*043036a2SApple OSS Distributionsthe scheduler and with interrupts disabled, it does not use "actual" threads to 37*043036a2SApple OSS Distributionsdo its work - each CPU has its own execution context and no context switching 38*043036a2SApple OSS Distributionsoccurs. Nothing else runs on the system while a stackshot is happening; this 39*043036a2SApple OSS Distributionsallows for stackshot to grab an atomic snapshot of the entire system's state. 40*043036a2SApple OSS Distributions 41*043036a2SApple OSS Distributions## Work Queues 42*043036a2SApple OSS Distributions 43*043036a2SApple OSS DistributionsIn order to split up work between CPUs, each task is put into a workqueue for 44*043036a2SApple OSS DistributionsCPUs to pull from. On SMP systems, there is only one queue. On AMP systems, 45*043036a2SApple OSS Distributionsthere are two, and tasks are sorted between the queues based on their 46*043036a2SApple OSS Distributions"difficulty" (i.e. the number of threads they have). E cores will work on the 47*043036a2SApple OSS Distributionseasier queue first, and P cores will work on the harder queue first. Once a CPU 48*043036a2SApple OSS Distributionsfinishes with its first queue, it will move on to the other. 49*043036a2SApple OSS Distributions 50*043036a2SApple OSS DistributionsIf latency collection is enabled, each CPU will record information about its run 51*043036a2SApple OSS Distributionsin a `stackshot_latency_cpu` structure in the KCData. This includes information 52*043036a2SApple OSS Distributionssuch as the amount of time spent waiting for the queue and the number of tasks / 53*043036a2SApple OSS Distributionsthreads processed by the CPU during its run. 54*043036a2SApple OSS Distributions 55*043036a2SApple OSS Distributions## Buffers and Memory 56*043036a2SApple OSS Distributions 57*043036a2SApple OSS DistributionsStackshot is given a fixed-size buffer upfront since it cannot allocate any 58*043036a2SApple OSS Distributionsmemory for itself. The size estimation logic in multithreaded stackshot is 59*043036a2SApple OSS Distributionsimproved from that of singlethreaded stackshot - it uses various heuristics such 60*043036a2SApple OSS Distributionsas the number of tasks and threads on the system, the flags passed, sizes of 61*043036a2SApple OSS Distributionsdata structures, and a fudge factor to give a reasonable estimate for a buffer 62*043036a2SApple OSS Distributionssize. Should the buffer be too small, stackshot will try again with a bigger 63*043036a2SApple OSS Distributionsone. The number of tries is recorded in the `stackshot_latency_collection_v2` 64*043036a2SApple OSS Distributionsstruct if latency collection is enabled. 65*043036a2SApple OSS Distributions 66*043036a2SApple OSS Distributions### Bump Allocator 67*043036a2SApple OSS Distributions 68*043036a2SApple OSS DistributionsStackshot uses a basic per-cluster bump allocator to allocate space within the 69*043036a2SApple OSS Distributionsbuffer. Each cluster gets its own bump allocator to mitigate cache contention, 70*043036a2SApple OSS Distributionswith space split evenly between each cluster. If a cluster runs out of buffer 71*043036a2SApple OSS Distributionsspace, it can reach into other clusters for more. 72*043036a2SApple OSS Distributions 73*043036a2SApple OSS DistributionsMemory that is freed is put into a per-cluster freelist. Even if the data was 74*043036a2SApple OSS Distributionsoriginally allocated from a different cluster's buffer, it will be put into the 75*043036a2SApple OSS Distributionscurrent cluster's freelist (again, to reduce cache effects). The freelist is a 76*043036a2SApple OSS Distributionslast resort, and is only used if the current cluster's buffer space fills. 77*043036a2SApple OSS Distributions 78*043036a2SApple OSS DistributionsEach CPU will report information about its buffers in its 79*043036a2SApple OSS Distributions`stackshot_latency_cpu` struct. This includes the total amount of buffer space 80*043036a2SApple OSS Distributionsused and the amount of buffer space allocated from other clusters. 81*043036a2SApple OSS Distributions 82*043036a2SApple OSS Distributions### Linked-List kcdata 83*043036a2SApple OSS Distributions 84*043036a2SApple OSS DistributionsEach CPU needs its own kcdata descriptor, but we don't know exactly how big each 85*043036a2SApple OSS Distributionsone should be ahead of time. Because of this, allocate kcdata buffers in 86*043036a2SApple OSS Distributionsreasonably-sized chunks as we need them. We also want the output to have each 87*043036a2SApple OSS Distributionstask in order (to keep the output identical to singlethreaded stackshot), so we 88*043036a2SApple OSS Distributionsmaintain a linked list of these kcdata chunks for each task in the queue. 89*043036a2SApple OSS Distributions 90*043036a2SApple OSS DistributionsThe chunks are sized such that only one is needed for the average task. If we 91*043036a2SApple OSS Distributionshave any extra room at the end of the current chunk once we finish with a task, 92*043036a2SApple OSS Distributionswe can add it to the freelist - but this is not ideal. So, stackshot uses 93*043036a2SApple OSS Distributionsvarious heuristics including flags and current task / thread counts to estimate 94*043036a2SApple OSS Distributionsa good chunk size. The amount of memory added to the freelist is reported by 95*043036a2SApple OSS Distributionsnamed uint64 in the KCData (`stackshot_buf_overhead`). 96*043036a2SApple OSS Distributions 97*043036a2SApple OSS Distributions``` 98*043036a2SApple OSS Distributions Workqueue 99*043036a2SApple OSS Distributions 100*043036a2SApple OSS Distributions⎡ Task #1 ⎤ 101*043036a2SApple OSS Distributions⎢ CPU 0 ⎥ 102*043036a2SApple OSS Distributions⎣ kcdata* ⎦-->[ KCData A ]--[ KCData B ] 103*043036a2SApple OSS Distributions⎡ Task #2 ⎤ 104*043036a2SApple OSS Distributions⎢ CPU 1 ⎥ 105*043036a2SApple OSS Distributions⎣ kcdata* ⎦-->[ KCData C ] 106*043036a2SApple OSS Distributions⎡ Task #3 ⎤ 107*043036a2SApple OSS Distributions⎢ CPU 2 ⎥ 108*043036a2SApple OSS Distributions⎣ kcdata* ⎦-->[ KCData D ]--[ KCData E ]--[ KCData F ] 109*043036a2SApple OSS Distributions ... 110*043036a2SApple OSS Distributions``` 111*043036a2SApple OSS Distributions 112*043036a2SApple OSS DistributionsOne the stackshot is finished and interrupts are reenabled, this data is woven 113*043036a2SApple OSS Distributionsback together into a single KCData buffer by the initiating thread, such that it 114*043036a2SApple OSS Distributionsis indistinguishable from the output of a singlethreaded stackshot (essentially, 115*043036a2SApple OSS Distributionswe memcpy the contents of each kcdata chunk into a single buffer, stripping off 116*043036a2SApple OSS Distributionsthe headers and footers). 117*043036a2SApple OSS Distributions 118*043036a2SApple OSS Distributions## “Tracing” 119*043036a2SApple OSS Distributions 120*043036a2SApple OSS DistributionsIn debug and development builds, Stackshot takes a "trace" of itself during 121*043036a2SApple OSS Distributionsexecution. There are circular per-cpu buffers containing a list of tracepoints, 122*043036a2SApple OSS Distributionswhich consist of a timestamp, line number, and an arbitrary uintpr_t-sized piece 123*043036a2SApple OSS Distributionsof extra data. This allows for basic tracing of stackshot's execution on each 124*043036a2SApple OSS DistributionsCPU which can be seen from a debugger. 125*043036a2SApple OSS Distributions 126*043036a2SApple OSS DistributionsBy default, tracepoints are only emitted when stackshot runs into an error (with 127*043036a2SApple OSS Distributionsthe error number as the data), but it's trivial to add more with the 128*043036a2SApple OSS Distributions`STACKSHOT_TRACE(data)` macro. 129*043036a2SApple OSS Distributions 130*043036a2SApple OSS DistributionsAn lldb macro is in the works which will allow this data to be examined more 131*043036a2SApple OSS Distributionseasily, but for now, it can be examined in lldb with `showpcpu -V 132*043036a2SApple OSS Distributionsstackshot_trace_buffer`. 133*043036a2SApple OSS Distributions 134*043036a2SApple OSS Distributions## Panics 135*043036a2SApple OSS Distributions 136*043036a2SApple OSS DistributionsDuring a panic stackshot, stackshot handles basically identically to how it did 137*043036a2SApple OSS Distributionsbefore (with a single CPU/thread) - with the only difference being that we can 138*043036a2SApple OSS Distributionsnow take a stackshot if the system panicked during a stackshot, since state has 139*043036a2SApple OSS Distributionsbeen compartmentalized. If the system panics during a panic stackshot, another 140*043036a2SApple OSS Distributionsstackshot will not be taken. 141*043036a2SApple OSS Distributions 142*043036a2SApple OSS DistributionsSince stackshot takes place entirely from within the debugger trap, if an 143*043036a2SApple OSS Distributionsauxilliary CPU (i.e. a CPU other than the one which initiated the stackshot) 144*043036a2SApple OSS Distributionspanics, it will not be able to acquire the debugger lock since it is already 145*043036a2SApple OSS Distributionsbeing held by the initiating CPU. To mitigate this, when a CPU panics during a 146*043036a2SApple OSS Distributionsstackshot, it sets a flag in stackshot's state to indicate there was a panic by 147*043036a2SApple OSS Distributionscalling into `stackshot_cpu_signal_panic`. 148*043036a2SApple OSS Distributions 149*043036a2SApple OSS DistributionsThere are checks for this flag at various points in stackshot, and once a CPU 150*043036a2SApple OSS Distributionsnotices it is set, it will spin in place. Before the initiating CPU spins in 151*043036a2SApple OSS Distributionsplace, it will release the debugger lock. Once all CPUs are spinning, the panic 152*043036a2SApple OSS Distributionswill continue. 153*043036a2SApple OSS Distributions 154*043036a2SApple OSS Distributions## Future Work 155*043036a2SApple OSS Distributions 156*043036a2SApple OSS Distributions- It might be more elegant to give stackshot its own IPI flavor instead of 157*043036a2SApple OSS Distributions piggybacking on the debugger trap. 158*043036a2SApple OSS Distributions- The tracing buffer isn't easily inspected - an LLDB macro to walk the circular 159*043036a2SApple OSS Distributions buffer and print a trace would be helpful. 160*043036a2SApple OSS Distributions- Chunk size is currently static for the entire stackshot - instead of 161*043036a2SApple OSS Distributions estimating it once, we could estimate it for every task to further eliminate 162*043036a2SApple OSS Distributions overhead. 163