My program has many parallel processes each on their own queue. I'd like to be able to visualize/measure the back pressure of the queues.
One approach is to count every block that enters and exits, but I'm sure GCD has this information already. Is there a better approach to measuring back pressure?
If you want to visualize what is going on, you can use
OSLog
, post.begin
and.end
events and watch it in Instruments’ “Points of Interest”. (See WWDC 2019 Getting Started with Instruments.)So, import
os.log
:Then create a log:
Enqueue 100 tasks:
Where the routine posts a
.begin
event to the points of interest log before dispatching the task, and posts a.end
event when the task actually starts, e.g.Then profile the app (with command+i or “Product” » “Profile”) and choose, for example, “Time Profiler” (which includes the “Points of Interest” tool). Start a recording an you will see a visual representation of your backlog:
(I expanded the “Points of Interest” to be big enough to show all 100 backlogged tasks.)
This is one way to visualize your backlog. I must confess that I generally use “Points of Interest” not to show the backlog, but rather to show when these tasks are actually running (i.e.
.begin
when the dispatched task actually starts running and.end
when the dispatched task finishes). Or I use the “thread” view in Instruments to see how my worker threads are being used. Or I use the “CPU” view in Instruments to see how my CPUs are being used.Regardless, as you can see, Instruments can be used to visualize whatever time ranges you want, in this case, the time between the adding of the task to the queue and when it starts running.