|
| 1 | +--- |
| 2 | +title: Add user marks to the profiling timeline |
| 3 | +description: Learn how to include your own user marks in the profiling timeline to track custom events |
| 4 | +ms.date: 07/18/2024 |
| 5 | +ms.topic: how-to |
| 6 | +author: mikejo5000 |
| 7 | +ms.author: mikejo |
| 8 | +manager: mijacobs |
| 9 | +ms.subservice: debug-diagnostics |
| 10 | +monikerRange: '>= vs-2022' |
| 11 | +--- |
| 12 | + |
| 13 | +# Add user marks to the profiling timeline (C#, C++) |
| 14 | + |
| 15 | +User Marks enable you to track custom events against Visual Studio's profiling tools by adding user mark functions into your code. The user marks appear as visual marks on the timeline graphs. You can do this to track a single event, or track a range. For example, you could track a "stop" and "start" event range. |
| 16 | + |
| 17 | +:::image type="content" source="../profiling/media/vs-2022/profiling-user-marks.png" alt-text="Screenshot showing profiling user marks."::: |
| 18 | + |
| 19 | +> [!NOTE] |
| 20 | +> This feature requires Visual Studio 2022 version 17.11 Preview 3. The feature is available in the Performance Profiler and not while debugging in the Diagnostics Hub. |
| 21 | +
|
| 22 | +## Installation and setup |
| 23 | + |
| 24 | +You can install support for the feature using either the NuGet package or by adding the correct source files. |
| 25 | + |
| 26 | +- Install using the NuGet Package (C#) |
| 27 | + |
| 28 | + Download the package, [Microsoft.VisualStudio.DiagnosticsHub.UserMarks](https://www.nuget.org/packages/Microsoft.VisualStudio.DiagnosticsHub.UserMarks). |
| 29 | + |
| 30 | +- Install by adding source files (C#, C++) |
| 31 | + |
| 32 | + After installing Visual Studio, open the following folder: *[VS installation path]\Common7\IDE\CommonExtensions\Platform\DiagnosticsHub*. |
| 33 | + |
| 34 | + For C# code, add *UserMarks.cs* and *UserMarksRange.cs* to your project. |
| 35 | + |
| 36 | + For native C++ code, add *UserMarks.h* to your project. |
| 37 | + |
| 38 | +## Add user marks (C#) |
| 39 | + |
| 40 | +Add the following preprocessor definition to the project build configuration: `DIAGHUB_ENABLE_TRACE_SYSTEM`. |
| 41 | + |
| 42 | +To add the definition in Visual Studio: |
| 43 | + |
| 44 | +1. Right-click the project in Solution Explorer and then select **Properties**. |
| 45 | + |
| 46 | +1. Select **Build > General > Conditional compilation symbols**. |
| 47 | + |
| 48 | +1. In the custom symbol fields for Debug and Release builds, add `DIAGHUB_ENABLE_TRACE_SYSTEM` as the preprocessor definition. |
| 49 | + |
| 50 | +Add the user marks object and emit. The following code shows an example of adding user marks. |
| 51 | + |
| 52 | +```csharp |
| 53 | +using Microsoft.DiagnosticsHub; |
| 54 | + |
| 55 | +// Create a user mark |
| 56 | +var myUserMark = new UserMarks("MyMark"); |
| 57 | + |
| 58 | +// Emit the usermark |
| 59 | +myUserMark.Emit("Event happened!"); |
| 60 | + |
| 61 | +// Emit statically |
| 62 | +UserMarks.EmitMessage("Emitting just a message"); |
| 63 | + |
| 64 | +// Emitting a range |
| 65 | +using (var myRange = new UserMarkRange("MyMethodRange")) |
| 66 | +{ |
| 67 | + // Your code here |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Add user marks (C++) |
| 72 | + |
| 73 | +To use the native user marks API, add *UserMarks.h* to the project and add the following code in your app: |
| 74 | + |
| 75 | +```cpp |
| 76 | +#define DIAGHUB_ENABLE_TRACE_SYSTEM |
| 77 | +#include "UserMarks.h" |
| 78 | +DIAGHUB_DECLARE_TRACE; |
| 79 | + |
| 80 | +// Start the trace system |
| 81 | +DIAGHUB_START_TRACE_SYSTEM(); |
| 82 | + |
| 83 | +// Initialize user marks |
| 84 | +USERMARKS_INITIALIZE(L"User mark name"); |
| 85 | + |
| 86 | +// Initialize user mark range |
| 87 | +USERMARKRANGE_INITIALIZE(L"Range name"); |
| 88 | + |
| 89 | +// Emit events |
| 90 | +USERMARKS_EMIT(L"Message to emit with user mark"); |
| 91 | +USERMARKRANGE_START(L"Message to emit with range"); |
| 92 | +USERMARKRANGE_END(); |
| 93 | + |
| 94 | +// Stop the trace system |
| 95 | +DIAGHUB_STOP_TRACE_SYSTEM(); |
| 96 | +``` |
| 97 | +
|
| 98 | +## View user marks |
| 99 | +
|
| 100 | +1. With your project open in Visual Studio, open the Performance Profiler (**Alt + F2**), then select a tool and select the **Start** button. |
| 101 | +
|
| 102 | +1. Make sure that the user mark code you added to your app runs. |
| 103 | +
|
| 104 | + The user marks show on the ruler above the timeline graph and in a table with details about the timestamps, related process, and any messages you emitted. |
| 105 | +
|
| 106 | + In the timeline, the user mark appears as an inverted triangle. |
| 107 | +
|
| 108 | +1. In the **Summary** pane, select **Show user marks** to see details about the user marks. |
| 109 | +
|
| 110 | + :::image type="content" source="../profiling/media/vs-2022/profiling-user-marks-timeline-initial-view.png" alt-text="Screenshot showing timeline."::: |
| 111 | +
|
| 112 | + Details appear in the User Marks tab. |
| 113 | +
|
| 114 | + :::image type="content" source="../profiling/media/vs-2022/profiling-user-marks-view-user-marks.png" alt-text="Screenshot showing user marks."::: |
| 115 | +
|
| 116 | +1. To see the user mark details in the timeline, hover over the marks in the ruler. |
| 117 | +
|
| 118 | + :::image type="content" source="../profiling/media/vs-2022/profiling-user-marks-hover-2.png" alt-text="Screenshot showing hovering over a user mark."::: |
| 119 | +
|
| 120 | +1. To filter the details view, select a time range in the timeline graph, or right-click a row and select start and end times to filter the timeline graph. |
| 121 | +
|
| 122 | + You can also filter the view by setting intervals on ranges or by selecting multiple user marks. |
| 123 | +
|
| 124 | + :::image type="content" source="../profiling/media/vs-2022/profiling-user-marks-set-interval.png" alt-text="Screenshot showing setting a user mark interval."::: |
| 125 | +
|
| 126 | +## Related content |
| 127 | +
|
| 128 | +- [Analyze performance by using CPU profiling](../profiling/cpu-usage.md) |
| 129 | +- [Analyze memory usage](../profiling/memory-usage-without-debugging2.md) |
0 commit comments