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