Skip to content

Commit 13fef94

Browse files
authored
Support getting thread id in GC (#108207)
1 parent bdcfb10 commit 13fef94

File tree

11 files changed

+44
-21
lines changed

11 files changed

+44
-21
lines changed

src/coreclr/gc/env/gcenv.ee.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class GCToEEInterface
9696
static void DiagAddNewRegion(int generation, uint8_t* rangeStart, uint8_t* rangeEnd, uint8_t* rangeEndReserved);
9797

9898
static void LogErrorToHost(const char *message);
99+
100+
static uint64_t GetThreadOSThreadId(Thread* thread);
99101
};
100102

101103
#endif // __GCENV_EE_H__

src/coreclr/gc/env/gctoeeinterface.standalone.inl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,10 @@ namespace standalone
258258
{
259259
::GCToEEInterface::LogErrorToHost(message);
260260
}
261+
262+
uint64_t GetThreadOSThreadId(Thread* thread)
263+
{
264+
return ::GCToEEInterface::GetThreadOSThreadId(thread);
265+
}
261266
};
262267
}

src/coreclr/gc/gc.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26684,20 +26684,12 @@ void gc_heap::add_to_hc_history_worker (hc_history* hist, int* current_index, hc
2668426684
current_hist->bgc_t_join_joined_p = (bool)bgc_t_join.joined();
2668526685
current_hist->concurrent_p = (bool)settings.concurrent;
2668626686
current_hist->bgc_thread_running = (bool)bgc_thread_running;
26687-
26688-
#if defined(TARGET_AMD64) && defined(TARGET_WINDOWS) && !defined(_DEBUG) && !defined(FEATURE_NATIVEAOT)
26689-
if (GCConfig::GetGCLogBGCThreadId())
26687+
int bgc_thread_os_id = 0;
26688+
if (bgc_thread)
2669026689
{
26691-
int bgc_thread_os_id = 0;
26692-
26693-
if (bgc_thread)
26694-
{
26695-
bgc_thread_os_id = (int)(*(size_t*)((uint8_t*)bgc_thread + 0x130));
26696-
}
26697-
26698-
current_hist->bgc_thread_os_id = bgc_thread_os_id;
26690+
bgc_thread_os_id = (int) GCToEEInterface::GetThreadOSThreadId(bgc_thread);
2669926691
}
26700-
#endif //TARGET_AMD64 && TARGET_WINDOWS && !_DEBUG && !FEATURE_NATIVEAOT
26692+
current_hist->bgc_thread_os_id = bgc_thread_os_id;
2670126693
#endif //BACKGROUND_GC
2670226694

2670326695
*current_index = (*current_index + 1) % max_hc_history_count;

src/coreclr/gc/gcconfig.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ class GCConfigStringHolder
141141
STRING_CONFIG(GCPath, "GCPath", "System.GC.Path", "Specifies the path of the standalone GC implementation.") \
142142
INT_CONFIG (GCSpinCountUnit, "GCSpinCountUnit", NULL, 0, "Specifies the spin count unit used by the GC.") \
143143
INT_CONFIG (GCDynamicAdaptationMode, "GCDynamicAdaptationMode", "System.GC.DynamicAdaptationMode", 1, "Enable the GC to dynamically adapt to application sizes.") \
144-
INT_CONFIG (GCDTargetTCP, "GCDTargetTCP", "System.GC.DTargetTCP", 0, "Specifies the target tcp for DATAS") \
145-
BOOL_CONFIG (GCLogBGCThreadId, "GCLogBGCThreadId", NULL, false, "Specifies if BGC ThreadId should be logged")
146-
144+
INT_CONFIG (GCDTargetTCP, "GCDTargetTCP", "System.GC.DTargetTCP", 0, "Specifies the target tcp for DATAS")
147145

148146
// This class is responsible for retreiving configuration information
149147
// for how the GC should operate.

src/coreclr/gc/gcenv.ee.standalone.inl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,16 @@ inline void GCToEEInterface::LogErrorToHost(const char *message)
325325
}
326326
}
327327

328+
inline uint64_t GCToEEInterface::GetThreadOSThreadId(Thread* thread)
329+
{
330+
if (g_runtimeSupportedVersion.MajorVersion >= 3)
331+
{
332+
return g_theGCToCLR->GetThreadOSThreadId(thread);
333+
}
334+
else
335+
{
336+
return 0;
337+
}
338+
}
339+
328340
#endif // __GCTOENV_EE_STANDALONE_INL__

src/coreclr/gc/gcinterface.ee.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ class IGCToCLR {
459459
// The following method is available only with EE_INTERFACE_MAJOR_VERSION >= 1
460460
virtual
461461
void LogErrorToHost(const char *message) PURE_VIRTUAL
462+
463+
virtual
464+
uint64_t GetThreadOSThreadId(Thread* thread) PURE_VIRTUAL;
462465
};
463466

464467
#endif // _GCINTERFACE_EE_H_

src/coreclr/gc/gcinterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
// The major version of the IGCToCLR interface. Breaking changes to this interface
1717
// require bumps in the major version number.
18-
#define EE_INTERFACE_MAJOR_VERSION 2
18+
#define EE_INTERFACE_MAJOR_VERSION 3
1919

2020
struct ScanContext;
2121
struct gc_alloc_context;

src/coreclr/gc/gcpriv.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,8 @@ struct hc_history
615615
#ifdef BACKGROUND_GC
616616
short bgc_t_join_n_threads;
617617
// We have observed a problem on Windows in production where GC indicates a BGC thread was created successfully yet we have
618-
// invalid fields on the Thread object such as m_OSThreadId. This is to help with debugging that problem so I
619-
// only enable it for retail builds on Windows. We can extend this with a GCToEEInterface interface method to get the offset
620-
// of that particular field on the Thread object.
621-
#if defined(TARGET_AMD64) && defined(TARGET_WINDOWS) && !defined(_DEBUG) && !defined(FEATURE_NATIVEAOT)
618+
// invalid fields on the Thread object such as m_OSThreadId. This is to help with debugging that problem.
622619
int bgc_thread_os_id;
623-
#endif
624620
short bgc_t_join_join_lock;
625621
#endif //BACKGROUND_GC
626622
bool gc_t_join_joined_p;

src/coreclr/gc/sample/gcenv.ee.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,8 @@ void GCToEEInterface::DiagAddNewRegion(int generation, uint8_t* rangeStart, uint
362362
void GCToEEInterface::LogErrorToHost(const char *message)
363363
{
364364
}
365+
366+
uint64_t GCToEEInterface::GetThreadOSThreadId(Thread* thread)
367+
{
368+
return 0;
369+
}

src/coreclr/nativeaot/Runtime/gcenv.ee.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,11 @@ void GCToEEInterface::LogErrorToHost(const char *message)
764764
{
765765
}
766766

767+
uint64_t GCToEEInterface::GetThreadOSThreadId(Thread* thread)
768+
{
769+
return (uint64_t)thread->GetPalThreadIdForLogging();
770+
}
771+
767772
bool GCToEEInterface::GetStringConfigValue(const char* privateKey, const char* publicKey, const char** value)
768773
{
769774
UNREFERENCED_PARAMETER(privateKey);

0 commit comments

Comments
 (0)