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