From 85497aed5f20f33083b0bd3f8f3f3e3af4b8b390 Mon Sep 17 00:00:00 2001 From: Zachary Marion Date: Wed, 3 Jan 2024 09:26:12 -0800 Subject: [PATCH 1/3] Working refactor --- src/import/trace-event.ts | 74 ++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 25 deletions(-) diff --git a/src/import/trace-event.ts b/src/import/trace-event.ts index 44702cbb..feed3737 100644 --- a/src/import/trace-event.ts +++ b/src/import/trace-event.ts @@ -140,7 +140,7 @@ interface TraceEventObject { type Trace = TraceEvent[] | TraceEventObject | TraceWithSamples -function pidTidKey(pid: number, tid: number): string { +function pidTidKey(pid: number | number, tid: number | number): string { // We zero-pad the PID and TID to make sorting them by pid/tid pair later easier. return `${zeroPad('' + pid, 10)}:${zeroPad('' + tid, 10)}` } @@ -349,14 +349,50 @@ function frameInfoForEvent( } } -/** - * Constructs an array mapping pid-tid keys to profile builders. Both the traceEvent[] - * format and the sample + stack frame based object format specify the process and thread - * names based on metadata so we share this logic. - * - * See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.xqopa5m0e28f - */ -function getProfileNameByPidTid( +function getProfileNameFromPidTid( + processName: string | undefined, + threadName: string | undefined, + pid: number, + tid: number, +): string { + if (processName != null && threadName != null) { + return `${processName} (pid ${pid}), ${threadName} (tid ${tid})` + } else if (processName != null) { + return `${processName} (pid ${pid}, tid ${tid})` + } else if (threadName != null) { + return `${threadName} (pid ${pid}, tid ${tid})` + } else { + return `pid ${pid}, tid ${tid}` + } +} + +function getProfileNamesFromSamples( + events: TraceEvent[], + partitionedSamples: Map, +): Map { + const processNamesByPid = getProcessNamesByPid(events) + const threadNamesByPidTid = getThreadNamesByPidTid(events) + + const profileNamesByPidTid = new Map() + + partitionedSamples.forEach(samples => { + if (samples.length === 0) return + + const pid = Number(samples[0].pid) + const tid = Number(samples[0].tid) + + const profileKey = pidTidKey(pid, tid) + const processName = processNamesByPid.get(pid) + const threadName = threadNamesByPidTid.get(profileKey) + const profileName = getProfileNameFromPidTid(processName, threadName, pid, tid) + + profileNamesByPidTid.set(profileKey, profileName) + }) + + return profileNamesByPidTid +} + +function getProfileNamesFromTraceEvents( events: TraceEvent[], partitionedTraceEvents: Map, ): Map { @@ -373,19 +409,9 @@ function getProfileNameByPidTid( const profileKey = pidTidKey(pid, tid) const processName = processNamesByPid.get(pid) const threadName = threadNamesByPidTid.get(profileKey) + const profileName = getProfileNameFromPidTid(processName, threadName, pid, tid) - if (processName != null && threadName != null) { - profileNamesByPidTid.set( - profileKey, - `${processName} (pid ${pid}), ${threadName} (tid ${tid})`, - ) - } else if (processName != null) { - profileNamesByPidTid.set(profileKey, `${processName} (pid ${pid}, tid ${tid})`) - } else if (threadName != null) { - profileNamesByPidTid.set(profileKey, `${threadName} (pid ${pid}, tid ${tid})`) - } else { - profileNamesByPidTid.set(profileKey, `pid ${pid}, tid ${tid}`) - } + profileNamesByPidTid.set(profileKey, profileName) }) return profileNamesByPidTid @@ -616,7 +642,7 @@ function eventListToProfileGroup( ): ProfileGroup { const importableEvents = filterIgnoredEventTypes(events) const partitionedTraceEvents = partitionByPidTid(importableEvents) - const profileNamesByPidTid = getProfileNameByPidTid(events, partitionedTraceEvents) + const profileNamesByPidTid = getProfileNamesFromTraceEvents(events, partitionedTraceEvents) const profilePairs: [string, Profile][] = [] @@ -647,10 +673,8 @@ function eventListToProfileGroup( } function sampleListToProfileGroup(contents: TraceWithSamples): ProfileGroup { - const importableEvents = filterIgnoredEventTypes(contents.traceEvents) - const partitionedTraceEvents = partitionByPidTid(importableEvents) const partitionedSamples = partitionByPidTid(contents.samples) - const profileNamesByPidTid = getProfileNameByPidTid(contents.traceEvents, partitionedTraceEvents) + const profileNamesByPidTid = getProfileNamesFromSamples(contents.traceEvents, partitionedSamples) const profilePairs: [string, Profile][] = [] From 071d8f8eba5357fe924470c6ea17ba736d140dfb Mon Sep 17 00:00:00 2001 From: Zachary Marion Date: Wed, 3 Jan 2024 09:34:49 -0800 Subject: [PATCH 2/3] Update test --- sample/profiles/trace-event/simple-with-samples.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sample/profiles/trace-event/simple-with-samples.json b/sample/profiles/trace-event/simple-with-samples.json index ae8456ac..1b0b83bf 100644 --- a/sample/profiles/trace-event/simple-with-samples.json +++ b/sample/profiles/trace-event/simple-with-samples.json @@ -21,16 +21,6 @@ "args": { "name": "mqt_js" } - }, - { - "name": "mqt_js", - "cat": "mqt_js", - "ph": "X", - "dur": 0, - "pid": 7512, - "ts": "11550183666", - "tid": "7695", - "args": {} } ], "samples": [ From 8be44afa506388b6ca375d3db67a5427c0853cf3 Mon Sep 17 00:00:00 2001 From: Zachary Marion Date: Wed, 3 Jan 2024 09:36:34 -0800 Subject: [PATCH 3/3] Remove change to pidTidKey function --- src/import/trace-event.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/import/trace-event.ts b/src/import/trace-event.ts index feed3737..ceb09ead 100644 --- a/src/import/trace-event.ts +++ b/src/import/trace-event.ts @@ -140,7 +140,7 @@ interface TraceEventObject { type Trace = TraceEvent[] | TraceEventObject | TraceWithSamples -function pidTidKey(pid: number | number, tid: number | number): string { +function pidTidKey(pid: number, tid: number): string { // We zero-pad the PID and TID to make sorting them by pid/tid pair later easier. return `${zeroPad('' + pid, 10)}:${zeroPad('' + tid, 10)}` } @@ -349,7 +349,7 @@ function frameInfoForEvent( } } -function getProfileNameFromPidTid( +function getProfileName( processName: string | undefined, threadName: string | undefined, pid: number, @@ -384,7 +384,7 @@ function getProfileNamesFromSamples( const profileKey = pidTidKey(pid, tid) const processName = processNamesByPid.get(pid) const threadName = threadNamesByPidTid.get(profileKey) - const profileName = getProfileNameFromPidTid(processName, threadName, pid, tid) + const profileName = getProfileName(processName, threadName, pid, tid) profileNamesByPidTid.set(profileKey, profileName) }) @@ -409,7 +409,7 @@ function getProfileNamesFromTraceEvents( const profileKey = pidTidKey(pid, tid) const processName = processNamesByPid.get(pid) const threadName = threadNamesByPidTid.get(profileKey) - const profileName = getProfileNameFromPidTid(processName, threadName, pid, tid) + const profileName = getProfileName(processName, threadName, pid, tid) profileNamesByPidTid.set(profileKey, profileName) })