Skip to content

Commit dbad677

Browse files
authored
Merge pull request #12811 from Mikejo5000/mikejo-br24
Add user marks to profiling timeline
2 parents b96ebaf + 38dc50d commit dbad677

10 files changed

+146
-9
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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)
Loading
Loading
Loading
Loading
Loading
Loading
Loading

docs/profiling/profiling-feature-tour.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ms.subservice: debug-diagnostics
1818

1919
Application performance measuring tools are essential for developers who want to optimize their code and improve application performance. Visual Studio offers a range of profiling and diagnostics tools that can help you diagnose memory and CPU usage and other application-level issues. With these tools, you can accumulate performance data while you run your application. A profiler can help you make informed decisions quickly by providing a visual depiction of execution times and CPU usage for your application. In this article, we give a quick look at the most common profiling tools.
2020

21-
For help choosing the correct tool, or to see profiling tool support for different app types, see [Which tool should I use?](../profiling/choose-performance-tool.md) For a tutorial that shows a general approach to optimizing code using the profiling tools, see [Beginner's guide to optimizing code](../profiling/optimize-code-using-profiling-tools.md).
21+
For help with choosing the correct tool, or to see profiling tool support for different app types, see [Which tool should I use?](../profiling/choose-performance-tool.md) For a tutorial that shows a general approach to optimizing code using the profiling tools, see [Beginner's guide to optimizing code](../profiling/optimize-code-using-profiling-tools.md).
2222

2323
## <a name="post_mortem"></a> Measure performance in release builds
2424

@@ -230,14 +230,6 @@ The tool shows each async operation in a list view. You can see information such
230230
![.NET Async Tool Stopped](../profiling/media/async-tool-opened.png ".NET Async Tool Stopped")
231231
::: moniker-end
232232

233-
## Examine application events
234-
235-
The generic [events viewer](../profiling/events-viewer.md) allows you to view the activity of your application through a list of events, such as module load, thread start, and system configurations, to help better diagnose how your application is performing right within the Visual Studio profiler. This tool is available in the Performance Profiler. Open the Performance Profiler by choosing **Debug** > **Performance Profiler** (or **Alt + F2**).
236-
237-
The tool shows each event in a list view. Columns provide information about each event, such as the event name, timestamp, and process ID.
238-
239-
![Event Viewer Trace](../profiling/media/prof-tour-events-viewer.png "Event Viewer Trace")
240-
241233
## Analyze database performance (.NET Core)
242234

243235
For .NET Core apps that use ADO.NET or Entity Framework Core, the [Database tool](../profiling/analyze-database.md) allows you to record the database queries that your application makes during a diagnostic session. You can then analyze information about individual queries in order to find places where your app's performance can be improved. This tool is available in the Performance Profiler. Open the Performance Profiler by choosing **Debug** > **Performance Profiler** (or **Alt + F2**).
@@ -254,6 +246,20 @@ The tool shows live values for each counter in a list view.
254246

255247
:::image type="content" source="../profiling/media/dotnet-counters-tool-collecting.png" alt-text=".NET Counter tool collecting.":::
256248

249+
## Examine application events
250+
251+
The generic [events viewer](../profiling/events-viewer.md) allows you to view the activity of your application through a list of events, such as module load, thread start, and system configurations, to help better diagnose how your application is performing right within the Visual Studio profiler. This tool is available in the Performance Profiler. Open the Performance Profiler by choosing **Debug** > **Performance Profiler** (or **Alt + F2**).
252+
253+
The tool shows each event in a list view. Columns provide information about each event, such as the event name, timestamp, and process ID.
254+
255+
![Event Viewer Trace](../profiling/media/prof-tour-events-viewer.png "Event Viewer Trace")
256+
257+
::: moniker range=">=vs-2022"
258+
## View custom events in the timeline graphs
259+
260+
You can programatically create custom events that appear as icons in the timeline graphs such as the CPU utilization and memory usage timeline graphs. For more information, see [Add user marks to timeline](../profiling/add-timeline-graph-user-marks.md).
261+
::: moniker-end
262+
257263
## Analyze resource consumption (XAML)
258264

259265
In XAML apps, such as Windows desktop WPF apps and UWP apps, you can analyze resource consumption using the Application Timeline tool. For example, you can analyze the time spent by your application preparing UI frames (layout and render), servicing network and disk requests, and in scenarios like application startup, page load, and Window resize. To use the tool, choose **Application Timeline** in the Performance Profiler, and then choose **Start**. In your app, go through the scenario with a suspected resource consumption issue, and then choose **Stop collection** to generate the report.

docs/profiling/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
href: events-viewer.md
111111
- name: Analyze Resource Consumption
112112
href: application-timeline.md
113+
- name: Add user marks to timeline
114+
href: add-timeline-graph-user-marks.md
113115
- name: BenchmarkDotNet
114116
href: profiling-with-benchmark-dotnet.md
115117
- name: Analyze GPU usage

0 commit comments

Comments
 (0)