tensorpipe icon indicating copy to clipboard operation
tensorpipe copied to clipboard

Include context ID in thread names

Open lw opened this issue 4 years ago • 0 comments

Threads spawned by contexts (transports, channels, ...) are given a name so that they can be more easily identified in GDB when debugging. The names currently are of the form TP_UV_loop or TP_SHM_reactor. If we have multiple UV contexts, however, it will be impossible to distinguish which one the thread belongs to. This will become important for multiplexing channels.

We already have a system to assign unique IDs to every object, so we could that ID, for a context, in the context's threads. Sounds easy, but there are two tricky aspects:

  • The contexts don't know their ID when they are created, it is given to them later with the setId method. So, when they receive their ID, they need to update their threads' names. Under pthread, this is easy to do under Linux, but impossible to do under OSX, because there a thread's name can only be changed by the threads itself. It may be fine to only support Linux, but it would be nice for this to work everywhere. For that, we'd have to defer the renaming to be executed on the target thread. This is often easy, but not always possible: for example, the SHM loop doesn't offer a way to defer work to it (all the work is deferred to the reactor).
  • The name of a thread is limited to 16 chars by pthread (in fact 15 chars, as the last one has to be \0). Currently the ID of most contexts is already beyond that, as it contains the PID and then a "hierarchy", e.g., 1234567:c0.ch_mp_uv.42 could be the ID of the 42nd UV context used by a multiplexing channel of the first core context created in process 1234567. We need to find a shorter format in order to fit it in 15 chars. For one, the PID is probably useless, as threads are already scoped within a process.

Note that a bunch of utilities around setting thread names can be found here: https://github.com/facebook/folly/blob/4ad9455e2d38a0d267d0f6db474060f96bd659f4/folly/system/ThreadName.cpp

lw avatar May 18 '20 12:05 lw