Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly use and visualize Start Stop activities in a custom ETW provider #2116

Open
exelix11 opened this issue Oct 8, 2024 · 0 comments

Comments

@exelix11
Copy link

exelix11 commented Oct 8, 2024

Context
I'm adding profiling support to an application by emitting ETW events that represent logical function calls.
Currently, after i capture an ETL trace i use a tool to convert it to json for use on speedscope.app, but this produces jsons up to 300MB which does not feel optimal.

The problem
I would like to use something like perfview or Windows Performance Analyzer directly, so i looked into the ETW activity pattern which seems to be meant for this kind of use case, however there is not much documentation.

I tried to follow this reference from ms docs, from what i understand this event pattern should allow perfview to group activities and plot them in a hierarchy view.

Several stackoverflow questions point to the fact that this should be possible, however i couldn't find any working reference code in C.

Repro

I tried writing the following code

#include <stdint.h>
#include <stdbool.h>

#include <windows.h>
#include <winmeta.h>
#include <traceloggingprovider.h>

TRACELOGGING_DEFINE_PROVIDER(
	g_profiler,
	"MyProfiler",
	// Example from https://learn.microsoft.com/it-it/windows/win32/api/traceloggingprovider/nf-traceloggingprovider-tracelogging_define_provider
	// {ce5fa4ea-ab00-5402-8b76-9f76ac858fb5}
	(0xce5fa4ea, 0xab00, 0x5402, 0x8b, 0x76, 0x9f, 0x76, 0xac, 0x85, 0x8f, 0xb5));

void RandomSleep() 
{
	Sleep(10 + rand() % 1000);
}

int main(int argc, char** argv)
{
	if (!SUCCEEDED(TraceLoggingRegister(g_profiler)))
		return -1;
	
	// Expected flamegraph:
	//  inner()         |---------|
	//  outer()  |---------------------| ... repeat

	while (true) {
		GUID outer, inner;
		if (!SUCCEEDED(CoCreateGuid(&outer))) return -2;
		if (!SUCCEEDED(CoCreateGuid(&inner))) return -3;

		printf("> enter outer\n");
		TraceLoggingWriteActivity(
			g_profiler, "Function",
			&outer, NULL,
			TraceLoggingOpcode(WINEVENT_OPCODE_START),
			TraceLoggingString("outer()", "func_name")
		);

		RandomSleep();

		printf(">> enter inner\n");
		TraceLoggingWriteActivity(
			g_profiler, "Function",
			&inner, &outer,
			TraceLoggingOpcode(WINEVENT_OPCODE_START),
			TraceLoggingString("inner()", "func_name")
		);

		RandomSleep();

		printf(">> leave inner\n");
		TraceLoggingWriteActivity(
			g_profiler, "Function",
			&inner, NULL,
			TraceLoggingOpcode(WINEVENT_OPCODE_STOP)
		);

		RandomSleep();

		printf("> leave outer\n");
		TraceLoggingWriteActivity(
			g_profiler, "Function",
			&outer, NULL,
			TraceLoggingOpcode(WINEVENT_OPCODE_STOP)
		);

		RandomSleep();
	}

	return 0;
}

You can build it from Visual Studio or just the command line tools with cl Repro.c Advapi32.lib Ole32.lib (i only tried x64 tools).

Then I start an event collection like this:

And the events are collected properly:

However, when i try to open the stacks view for start-stop activities i get Error: Could not find stack source Any Stacks (with StartStop Activities)

Questions

  • Is there a mistake in the code or have I misunderstood the purpose of start stop activities ?
  • If not activities, is there a way to plot custom function stacks in perfview ?
  • Is there a way to generate flamegraphs from this data ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant