Overlapping trace events in same thread display incorrectly (with protobufs)
I couldn't reopen https://github.com/google/perfetto/issues/432, so opening a new case.
I converted my JSON dumping to protobufs, but I am having the same issue as described in https://github.com/google/perfetto/issues/432.
I have attached the generated protobuf file, as well as a small C++ reproducer.
In my case, I have 5 events ({start, end}):
Event0: {0, 100}
Event1: {100, 200}
Event2: {200, 300}
Event3: {280, 350}
Event4: {360, 400}
Notably, Event3 overlaps with Event2.
Event3 should start at 280 and end at 350, for a duration of 70.
However, as shown in the screenshot, it starts a 280 (correct) but ends at 300 (incorrect). Additionally, Event2 ends incorrectly at 350, which is when Event3 should end.
So, it looks like the overlapping events are having their end times flipped.
This seems to only be an issue when the event that starts later ends later (e.g. Event3 starts and ends later than Event2). If I make the following change, the render looks fine.
Event0: {0, 100}
Event1: {100, 200}
Event2: {200, 300}
Event3: {280, 290}
Event4: {350, 400}
This is in main.cpp as the #define GOOD case.
Almost certainly this is a mistake in how you are emitting protos. Overlaps on a single track UUID are not allowed. Instead you should emit potentially overlapping slices with a different track UUID but with the same name (also the tracks should be process based not thread based). The UI will then logically show the multiple tracks grouped together.
So if I currently create 1 track per thread like so:
auto* thread = trace.pb.add_packet();
thread->set_trusted_packet_sequence_id(1);
auto* trackDesc = thread->mutable_track_descriptor();
trackDesc->set_uuid(getThreadUUID(tid));
trackDesc->mutable_thread()->set_pid(hid);
trackDesc->mutable_thread()->set_tid(tid);
trackDesc->mutable_thread()->set_thread_name(threadNames[tid]);
where tid is some unique thread ID.
then, when I add events, it looks like this:
auto* start = trace.pb.add_packet();
start->set_trusted_packet_sequence_id(1);
start->set_timestamp(s);
start->mutable_track_event()->set_name(name);
start->mutable_track_event()->set_track_uuid(trackUUID);
start->mutable_track_event()->set_type(TrackEvent::TYPE_SLICE_BEGIN);
auto* end = trace.pb.add_packet();
end->set_trusted_packet_sequence_id(1);
end->set_timestamp(e);
end->mutable_track_event()->set_track_uuid(trackUUID);
end->mutable_track_event()->set_type(TrackEvent::TYPE_SLICE_END);
where trackUUID is the same as getThreadUUID(tid).
Are you saying I need to figure out if the given event overlaps with any other op in the same track and create a new one for it?
Overlaps on a single track UUID are not allowed. Instead you should emit potentially overlapping slices with a different track UUID but with the same name
Same name as what? If I want to place SliceA and SliceB (which overlap) in the same track you would give them different track UUIDs? How would I tie the fact that they are in the same "track"?
also the tracks should be process based not thread based
I am trying to display the fact that I have 3 "processes" and each has some number of "threads" (I used quotes since this it not 100% accurate) where the slices (which may overlap) are executing.
Something is not adding up,:
- If you are modelling execution of a single thread, slices there represent function calls and so cannot overlap only nest. This corresponds to one "track"
- However it seems like to me that you are merging slices from many different threads onto the same track. This is a problem as execution of threads can of course overlap. Each thread can be placed on a completely different track (given a different track UUID)
- Grouping threads under a process should work implicitly based on the pid field of track descriptor. That doesn't seem to be happening in the screenshot you linked - there seems to only be one process.
Sorry for not being clear and I really do appreciate your help.
The structure I have is as follows:
- Physical machines (modelled in perfetto as processes)
- Threads on machine (modelled in perfetto as threads)
- Operations running in threads (modelled in perfetto as slices)
- Threads on machine (modelled in perfetto as threads)
-
the shortest answer I can give as to why the operations (and thus slices from above) can overlap is pipeline parallelism.
-
I don't think so. I give every thread a different UUID, and then place the slices (operations) into the different threads using the track UUID I created for each thread - it is just that operations can overlap to a certain degree (in my case).
-
I simplified the case to remove multiple processes. Here is a screen shot of my larger design. I do notice that I can't get rid of the process track being added (which is empty) - notice the
CMD 2,WGT 1, andACT 0empty tracks under the same-named process? If I remove the code used to name my process, this goes away, but then my process is just namedProcess X.
FWIW, the thin slice in the "Context 0 12" thread under the third RxAct is an example of these overlapping slices.

So, as Lalit mentioned the basic problem is that overlapping slices in the same track (where track is identified by the a track_uuid) are not allowed.
This is a result of how SLICE_BEGIN and SLICE_END are encoded and the fact that all packets are sorted by timestamp before being rendered on a track.
Here's the relevant data from your trace:
packet {
timestamp: 200
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_BEGIN
track_uuid: 1
name: "event 2"
}
}
packet {
timestamp: 300
trusted_packet_sequence_id: 1
track_event {
# You'd like this to be the END for event 2, but that is not encoded in the data
type: TYPE_SLICE_END
track_uuid: 1
}
}
packet {
timestamp: 280
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_BEGIN
track_uuid: 1
name: "event 3"
}
}
packet {
timestamp: 350
trusted_packet_sequence_id: 1
track_event {
# You'd like this to be the END for event 3, but that is not encoded in the data
type: TYPE_SLICE_END
track_uuid: 1
}
}
Trace processor sorts these packets by timestamp and groups them by track_uuid.
So, for track_uuid: 1 trace processor is going to see the following data in order:
- timestamp: 200 BEGIN "name 2"
- timestamp: 280 BEGIN "name 3"
- timestamp: 300 END (The protobuf data does not encode the matching BEGIN packet)
- timestamp: 350 END (The protobuf data does not encode the matching BEGIN packet)
Inside the same track the semantics are that an END event terminates the most BEGIN event that has not been terminated yet (that's why you can't have overlapping nesting events).
When parsing packet number 3, trace processor thinks that the END refers to the BEGIN "name 3" at timestamp 280.
This is the semantic of BEGIN and END event in the same track.
If you want to encode your overlapping nesting events, you're going to need to put "event 2" and "event 3" into two separate tracks (i.e. give them two separate track_uuids, and provide two separate track_descriptors).
In your case I would encode it as something like that:
packet {
track_descriptor {
uuid: 1
thread {
thread_name: "my thread"
}
}
}
packet {
track_descriptor {
uuid: 2
parent_uuid: 1
name: "events"
}
}
packet {
track_descriptor {
uuid: 3
parent_uuid: 1
name: "events"
}
}
packet {
timestamp: 200
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_BEGIN
track_uuid: 2
name: "event 2"
}
}
packet {
timestamp: 300
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_END
track_uuid: 2
}
}
packet {
timestamp: 280
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_BEGIN
track_uuid: 3
name: "event 3"
}
}
packet {
timestamp: 350
trusted_packet_sequence_id: 1
track_event {
type: TYPE_SLICE_END
track_uuid: 3
}
}
So you use separate tracks (track_uuid:2, track_uuid: 3) for the two slices, since they are overlapping. Then, since they have the same name ("events") in their track_descriptor, the UI will display them as a single "UI track".
Note that you don't need to list all your track at the beginning of the trace, you can serialize them immediately before referencing their uuid on a begin packet.
Hope this helps.
Thanks for the detailed response!
Through experimentation I kind of got there myself too, so glad it was confirmed. This works fine, but I have a few more problems that are ~small.
- Referencing your packets above, if I name the "subthreads" (packets uuid=2 and uuid=3), I get this:

Is there anyway to get Perfetto to merge the rows into one so I only see "events" once? Not a huge deal.
- How can I change the name of the process? If I start off with a packet as shown below, I get what is shown in the image further below. Notice the extra row with the same name as the process?
packet {
track_descriptor {
uuid: 123
process {
pid: 0
process_name: "my process"
}
}
}

Is there anyway to get Perfetto to merge the rows into one so I only see "events" once? Not a huge deal.
I thought having the same name was enough to show them as one track in the UI. (it works for sure in other cases where the protos are generated from the SDK).
Maybe it has something to do with the fact that the parent of your "events" tracks is a thread and not a process? I'm not an expert in the UI code, but if you share this trace I can take a look.
How can I change the name of the process? If I start off with a packet as shown below, I get what is shown in the image further below. Notice the extra row with the same name as the process?
I'm not sure, there might be some confusion between threads and processes. Are you still using pid:0? Zero might be treated as a special value.
If you want to share the trace that has these problem I can take a look at it.
Maybe it has something to do with the fact that the parent of your "events" tracks is a thread and not a process?
This was indeed the problem. I removed "my thread" and made both "event" tracks children of the process and the rows were merged :)
W.r.t. the process name, I've attached the PB file. I changed the PID to 1 and the problem is still there.
W.r.t. the process name, I've attached the PB file. I changed the PID to 1 and the problem is still there.
Yes, it looks like we're showing an empty process track even for a trace generated with the SDK example. An example trace is here
Given that in most real traces the process track actually has some events, it has probably never been a priority to optimize this case (and hide the track). @chromy might have more thoughts.
I refactored my code to create multiple tracks for a given thread on-demand and the output looks great. I can deal with the extra row for the process, although it'd be nice to omit it if it's empty :).
Thanks for all the help!