当前位置:网站首页>How do I get the STW (pause) time of a GC (garbage collector)?
How do I get the STW (pause) time of a GC (garbage collector)?
2022-06-28 04:28:00 【Illusory private school】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
Preface
In modern containerization and microservice applications , Because of the distributed environment and the complicated calling relationship ,APM(Application Performance Monitoring Application performance monitoring ) It's very important , It collects various indicators of the application and requests links , Let you know the current state of the system and the points worthy of optimization , In addition, it can help you find exceptions in the application , Help you locate the problem more conveniently .
about .NET Take it like this GC(Garbage Collector Garbage collector ) In terms of platform ,GC The indicators are also particularly important , Collection can help us analyze memory leaks 、 Optimize system performance, etc . A comprehensive collection of .NET GC indicators , As shown in the figure below .
In most scenarios it meets the requirements , But if you come across a certain moment P95 The delay increases suddenly , Asynchronous task suddenly timed out , We want to check whether these exceptions are caused by GC Of STW Time(Stop The World Time finger GC The time during which all threads are suspended ) Caused by too long , There is no way , Because these indicators are not collected at present .
So this article will take you to know , How to collect .NET GC STW Time.
Method
Such as .NET Memory performance analysis guide As mentioned in ,.NET Runtime Many events will be released during the operation , These events represent the current Runtime Operating state , Again GC Many events will be released during the operation , We can use PerfView Tools to collect such events . Here is WorkStationGC happen GC A sequence of events .
Microsoft-Windows-DotNETRuntime/GC/SuspendEEStart // Start pausing managed threads
Microsoft-Windows-DotNETRuntime/GC/SuspendEEStop // Pause managed thread complete
Microsoft-Windows-DotNETRuntime/GC/Start // GC Start recycling
Microsoft-Windows-DotNETRuntime/GC/Stop // GC Recycling is over
Microsoft-Windows-DotNETRuntime/GC/RestartEEStart // Resume the managed thread that was suspended before
Microsoft-Windows-DotNETRuntime/GC/RestartEEStop // Resume managed thread run complete
PS: All events can be in .NET Document official Find , Very comprehensive .
and SuspendEEStart( Pause managed threads ) To RestartEEStop( Resume managed thread run complete ) The time elapsed in the process is STW Time, We just need to record the difference between the two events , You can know this time GC STW How long does it take .
BGC It's better than WorkStationGC It's a lot more complicated , But the same thing is to measure the time spent on these two events to collect STW Time, This article does not cover too much .
Use EventSource collection
So we know which two indicators can be obtained by calculating the difference STW Time , So how to collect through code ?
Here we need to know EventSource and EventListener Two classes , As the name suggests, we can pass EventSource To post events , Use EventListener To listen for events , In this article, we mainly use EventListener To collect GC event , For this EventSource Class, you can see the following Microsoft documentation link , I won't give you too much introduction here .
- EventSource
- EventListener
Let's see how to useEventListenerClass listening GC event , The code is as follows :
using System.Diagnostics.Tracing;
// Turn on GC Event monitoring
var gc = new GcStwMetricsCollector();
// Create some objects
var array = Enumerable.Range(0, 1000).Select(s => (decimal)s).ToArray();
// Do it manually GC
GC.Collect();
Console.ReadLine();
public class GcStwMetricsCollector : EventListener
{
// GC keyword
private const int GC_KEYWORD = 0x0000001;
// We need to focus on GC event
private const int GCSuspendEEBegin = 9;
private const int GCRestartEEEnd = 3;
private EventSource? _eventSource;
public void Stop()
{
if (_eventSource == null)
return;
DisableEvents(_eventSource);
}
protected override void OnEventSourceCreated(EventSource eventSource)
{
_eventSource = eventSource;
// GC The incident happened in Microsoft-Windows-DotNETRuntime Under namespace
if (eventSource.Name.Equals("Microsoft-Windows-DotNETRuntime"))
{
// Enable events , The event level is Informational, Monitor only GC event
EnableEvents(eventSource, EventLevel.Informational, (EventKeywords) (GC_KEYWORD));
}
}
private long _currentStwStartTime = 0;
protected override void OnEventWritten(EventWrittenEventArgs e)
{
switch (e.EventId)
{
// Freeze managed thread start , Record current time
case GCSuspendEEBegin:
_currentStwStartTime = e.TimeStamp.Ticks;
break;
// Resume managed thread ends , Calculate the difference between the current time and the start time of the frozen managed thread
case GCRestartEEEnd:
if (_currentStwStartTime > 0)
{
var ms = TimeSpan.FromTicks(e.TimeStamp.Ticks - _currentStwStartTime).TotalMilliseconds;
_currentStwStartTime = 0;
// Output results
Console.WriteLine($"STW: {ms}ms");
}
break;
}
}
}
Running results :
STW: 0.2568ms
as for GC The enumeration value corresponding to the event , You can find it in the document I gave above .
.NET7 new API
When implementing this requirement , I noticed .NET7 There's a new one issue, Directly provides a API, So we can get the total GC STW Time, I excerpted and translated the key information .
Background and motivation
Today we are already in GetGCMemoryInfo Open access to GC Percentage value of processing time and pause time API.
Specifically, through GCMemoryInfo Of PauseTimePercentage Field .
This is very useful , But if I just want one molecule ( namely : Total since the program was run GC Pause time ). There is no way to get it now .
API The proposal
I suggest that System.GC Add one such as the following on the API:
TimeSpan System.GC.GetTotalPauseDuration()
It will be returned GC Total pause time .
API Use
TimeSpan start = System.GC.GetTotalPauseDuration();
// ... Perform some work ...
TimeSpan end= System.GC.GetTotalPauseDuration();
Console.WriteLine(end - start + " was spent pausing in GC");
I see this API Already with the latest .NET7 Release together with the preview , We download the latest .NET7 SDK, Then change the project into .NET7, Let's try this API, The code is as follows :
using System.Diagnostics.Tracing;
// Turn on GC Event monitoring
var gc = new GcStwMetricsCollector();
// Create some objects
var array = Enumerable.Range(0, 1000).Select(s => (decimal)s).ToArray();
// Do it manually GC
GC.Collect();
Console.WriteLine($"API STW:{GC.GetTotalPauseDuration().TotalMilliseconds}ms");
Console.ReadLine();
// Omit the same code as above
Running results :
API STW: 0.223ms
Event STW: 0.296ms
API The statistics should be more accurate , We use events to get more or less a little extra overhead , However, the error is within the acceptable range .
summary
There are two ways to get .NET GC STW Time, We just need a little modification , It can be STW The monitoring function is added APM in , The following chart shows some data collected during the local test .
Of course by EventListener More can be achieved APM Information collection , You can also study it if you are interested .
This article code link Github: https://github.com/InCerryGit/BlogCodes/tree/main/Get-GC-STW-Time
The articles :
.NET performance optimization - Recommended Collections.Pooled( Add )
.NET performance optimization - Use ValueStringBuilder String concatenation
.NET performance optimization - Use structs instead of classes
边栏推荐
- 《性能之巅第2版》阅读笔记(二)--CPU监测
- Is the securities account opened by qiniu safe? How to open an account
- Is it true that qiniu business school gives away securities accounts? Is it safe to open an account
- 10: 00 interview, came out at 10:02, the question is really too
- 单一职责原则
- Go language -select statement
- 公司领导说,个人代码超10个Bug就开除,是什么体验?
- mysql导入文本文件时的pager
- Single responsibility principle
- With the transformation of automatic empowerment, Feihe dairy accelerates its move towards digitalization!
猜你喜欢

leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】

【Matlab红绿灯识别】红绿灯识别【含GUI源码 1908期】

Pinda general permission system (day 5~day 6)

JVM I: introduction to JVM and understanding of class files

Multi project design and development · introduction to class library project

2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。

Why are cloud vendors targeting this KPI?

Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal

10: 00 interview, came out at 10:02, the question is really too

领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能
随机推荐
Secouer le son et se battre ~ prêter attention au blogueur
Introduction to SQLSERVER database
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
Introduction notes to machine learning
从零到一,教你搭建「以文搜图」搜索服务(一)
What are the password requirements for waiting insurance 2.0? What are the legal bases?
Single responsibility principle
MSc 307 (88) (2010 FTPC code) Part 5 low flame spread test
How can we speed up the chunksplitter when CDC extracts MySQL data in full?
From zero to one, I will teach you to build a "search by text and map" search service (I)
视频直播系统源码,倒计时显示,商品秒杀倒计时
2022年中國音頻市場年度綜合分析
易周金融 | Q1手机银行活跃用户规模6.5亿;理财子公司布局新兴领域
RT thread bidirectional linked list (learning notes)
Why are cloud vendors targeting this KPI?
2022年中国音频市场年度综合分析
Problems with cat and dog queues
Is the securities account opened by qiniu safe? How to open an account
[small program practice series] e-commerce platform source code and function implementation
快速下载JDK,除了官方Oracle下载,还有国内可以有最新版本的下载地址吗