Search code examples
c++linuxlttng

LTTNG: using with a popular library


I have a trivially simple question that I could not find the answer yet.

Say, I have a shared library X that is used by 100 simultaneously running applications A0, A1, ... A99. I had instrumented my library and with LTTNG using "X-Provider" as the provider name. How can my user distinguish between X-Provider events that happen in A10 and A15?


Solution

  • With the lttng command-line utility, add the vpid context field to be recorded to your event records, for example:

    $ lttng add-context -u -t vpid
    

    This targets all the user space channels of the current recording session; you can of course select a specific recording session and/or channel (see lttng-add-context(1)).

    You need to use this command before you start the tracers (lttng start).

    All your event records will have a context field named vpid which is the virtual IDs of the processes which emitted them.

    Now, you need to find a way to associate such VPIDs to instance IDs (A10, A15, etc.). You can do so with your own application-specific strategy, or you can simply record an initial event when starting the application which indicates the instance ID/name, for example:

    tracepoint(x_provider, app_state, app_id);
    

    Then, when reading the resulting trace(s), you can correlate the x_provider:app_state event record info with the records of your library-emitted events. For example, using the Babeltrace 2 Python bindings:

    import bt2
    
    vpid_app_ids = {}
    
    for msg in bt2.TraceCollectionMessageIterator('/path/to/trace'):
        if type(msg) is not bt2._EventMessageConst:
            continue
    
        vpid = int(msg.event.common_context_field['vpid'])
    
        if msg.event.name == 'x_provider:app_state':
            app_id = int(msg.event.payload_field['app_id'])
            vpid_app_ids[vpid] = app_id
        else:
            app_id = vpid_app_ids[vpid]
    
            # Continue to process `msg.event` knowing the instance ID is
            # `app_id`...
    

    I didn't test the example above, but you get the gist.