proper thread names

This commit is contained in:
Ryan Fleury
2024-02-22 16:20:27 -08:00
parent 74d051f383
commit 5813cb2af8
16 changed files with 184 additions and 102 deletions
+2 -1
View File
@@ -8,7 +8,7 @@
#define RADDBG_LAYER_COLOR 0.20f, 0.60f, 0.80f
#include "base_types.c"
#include "base_markup.c"
#include "base_profile.c"
#include "base_arena.c"
#include "base_math.c"
#include "base_string.c"
@@ -16,3 +16,4 @@
#include "base_command_line.c"
#include "base_arena_dev.c"
#include "base_bits.c"
#include "base_markup.c"
+2 -1
View File
@@ -9,7 +9,7 @@
#include "base_context_cracking.h"
#include "base_types.h"
#include "base_markup.h"
#include "base_profile.h"
#include "base_ins.h"
#include "base_linked_lists.h"
#include "base_arena.h"
@@ -19,5 +19,6 @@
#include "base_command_line.h"
#include "base_arena_dev.h"
#include "base_bits.h"
#include "base_markup.h"
#endif // BASE_INC_H
+19
View File
@@ -1,2 +1,21 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
internal void
thread_name(String8 string)
{
ProfThreadName("%.*s", str8_varg(string));
os_set_thread_name(string);
}
internal void
thread_namef(char *fmt, ...)
{
Temp scratch = scratch_begin(0, 0);
va_list args;
va_start(args, fmt);
String8 string = push_str8fv(scratch.arena, fmt, args);
thread_name(string);
va_end(args);
scratch_end(scratch);
}
+4 -71
View File
@@ -4,76 +4,9 @@
#ifndef BASE_MARKUP_H
#define BASE_MARKUP_H
////////////////////////////////
//~ rjf: Zero Settings
#if !defined(PROFILE_TELEMETRY)
# define PROFILE_TELEMETRY 0
#endif
#if !defined(MARKUP_LAYER_COLOR)
# define MARKUP_LAYER_COLOR 1.00f, 0.00f, 1.00f
#endif
////////////////////////////////
//~ rjf: Third Party Includes
#if PROFILE_TELEMETRY
# include "rad_tm.h"
# if OS_WINDOWS
# pragma comment(lib, "rad_tm_win64.lib")
# endif
#endif
////////////////////////////////
//~ rjf: Telemetry Profile Defines
#if PROFILE_TELEMETRY
# define ProfBegin(...) tmEnter(0, 0, __VA_ARGS__)
# define ProfBeginDynamic(...) (TM_API_PTR ? TM_API_PTR->_tmEnterZoneV_Core(0, 0, __FILE__, &g_telemetry_filename_id, __LINE__, __VA_ARGS__) : (void)0)
# define ProfEnd(...) (TM_API_PTR ? TM_API_PTR->_tmLeaveZone(0) : (void)0)
# define ProfTick(...) tmTick(0)
# define ProfIsCapturing(...) tmRunning()
# define ProfBeginCapture(...) tmOpen(0, __VA_ARGS__, __DATE__, "localhost", TMCT_TCP, TELEMETRY_DEFAULT_PORT, TMOF_INIT_NETWORKING|TMOF_CAPTURE_CONTEXT_SWITCHES, 100)
# define ProfEndCapture(...) tmClose(0)
# define ProfThreadName(...) (TM_API_PTR ? TM_API_PTR->_tmThreadName(0, 0, __VA_ARGS__) : (void)0)
# define ProfMsg(...) (TM_API_PTR ? TM_API_PTR->_tmMessageV_Core(0, TMMF_ICON_NOTE, __FILE__, &g_telemetry_filename_id, __LINE__, __VA_ARGS__) : (void)0)
# define ProfBeginLockWait(...) tmStartWaitForLock(0, 0, __VA_ARGS__)
# define ProfEndLockWait(...) tmEndWaitForLock(0)
# define ProfLockTake(...) tmAcquiredLock(0, 0, __VA_ARGS__)
# define ProfLockDrop(...) tmReleasedLock(0, __VA_ARGS__)
# define ProfColor(color) tmZoneColorSticky(color)
#endif
////////////////////////////////
//~ rjf: Zeroify Undefined Defines
#if !defined(ProfBegin)
# define ProfBegin(...) (0)
# define ProfBeginDynamic(...) (0)
# define ProfEnd(...) (0)
# define ProfTick(...) (0)
# define ProfIsCapturing(...) (0)
# define ProfBeginCapture(...) (0)
# define ProfEndCapture(...) (0)
# define ProfThreadName(...) (0)
# define ProfMsg(...) (0)
# define ProfBeginLockWait(...) (0)
# define ProfEndLockWait(...) (0)
# define ProfLockTake(...) (0)
# define ProfLockDrop(...) (0)
# define ProfColor(...) (0)
#endif
////////////////////////////////
//~ rjf: Helper Wrappers
#define ProfBeginFunction(...) ProfBegin(this_function_name)
#define ProfScope(...) DeferLoop(ProfBeginDynamic(__VA_ARGS__), ProfEnd())
////////////////////////////////
//~ rjf: General Markup
#define ThreadName(...) (ProfThreadName(__VA_ARGS__))
internal void thread_namef(char *fmt, ...);
internal void thread_name(String8 string);
#define ThreadNameF(...) (ProfThreadName(__VA_ARGS__), thread_namef(__VA_ARGS__))
#define ThreadName(str) (ProfThreadName("%s", str8_varg(str)), thread_name(str))
#endif // BASE_MARKUP_H
+2
View File
@@ -0,0 +1,2 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
+74
View File
@@ -0,0 +1,74 @@
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef BASE_PROFILE_H
#define BASE_PROFILE_H
////////////////////////////////
//~ rjf: Zero Settings
#if !defined(PROFILE_TELEMETRY)
# define PROFILE_TELEMETRY 0
#endif
#if !defined(MARKUP_LAYER_COLOR)
# define MARKUP_LAYER_COLOR 1.00f, 0.00f, 1.00f
#endif
////////////////////////////////
//~ rjf: Third Party Includes
#if PROFILE_TELEMETRY
# include "rad_tm.h"
# if OS_WINDOWS
# pragma comment(lib, "rad_tm_win64.lib")
# endif
#endif
////////////////////////////////
//~ rjf: Telemetry Profile Defines
#if PROFILE_TELEMETRY
# define ProfBegin(...) tmEnter(0, 0, __VA_ARGS__)
# define ProfBeginDynamic(...) (TM_API_PTR ? TM_API_PTR->_tmEnterZoneV_Core(0, 0, __FILE__, &g_telemetry_filename_id, __LINE__, __VA_ARGS__) : (void)0)
# define ProfEnd(...) (TM_API_PTR ? TM_API_PTR->_tmLeaveZone(0) : (void)0)
# define ProfTick(...) tmTick(0)
# define ProfIsCapturing(...) tmRunning()
# define ProfBeginCapture(...) tmOpen(0, __VA_ARGS__, __DATE__, "localhost", TMCT_TCP, TELEMETRY_DEFAULT_PORT, TMOF_INIT_NETWORKING|TMOF_CAPTURE_CONTEXT_SWITCHES, 100)
# define ProfEndCapture(...) tmClose(0)
# define ProfThreadName(...) (TM_API_PTR ? TM_API_PTR->_tmThreadName(0, 0, __VA_ARGS__) : (void)0)
# define ProfMsg(...) (TM_API_PTR ? TM_API_PTR->_tmMessageV_Core(0, TMMF_ICON_NOTE, __FILE__, &g_telemetry_filename_id, __LINE__, __VA_ARGS__) : (void)0)
# define ProfBeginLockWait(...) tmStartWaitForLock(0, 0, __VA_ARGS__)
# define ProfEndLockWait(...) tmEndWaitForLock(0)
# define ProfLockTake(...) tmAcquiredLock(0, 0, __VA_ARGS__)
# define ProfLockDrop(...) tmReleasedLock(0, __VA_ARGS__)
# define ProfColor(color) tmZoneColorSticky(color)
#endif
////////////////////////////////
//~ rjf: Zeroify Undefined Defines
#if !defined(ProfBegin)
# define ProfBegin(...) (0)
# define ProfBeginDynamic(...) (0)
# define ProfEnd(...) (0)
# define ProfTick(...) (0)
# define ProfIsCapturing(...) (0)
# define ProfBeginCapture(...) (0)
# define ProfEndCapture(...) (0)
# define ProfThreadName(...) (0)
# define ProfMsg(...) (0)
# define ProfBeginLockWait(...) (0)
# define ProfEndLockWait(...) (0)
# define ProfLockTake(...) (0)
# define ProfLockDrop(...) (0)
# define ProfColor(...) (0)
#endif
////////////////////////////////
//~ rjf: Helper Wrappers
#define ProfBeginFunction(...) ProfBegin(this_function_name)
#define ProfScope(...) DeferLoop(ProfBeginDynamic(__VA_ARGS__), ProfEnd())
#endif // BASE_PROFILE_H
+1 -1
View File
@@ -1518,7 +1518,7 @@ ctrl_u2ms_dequeue_req(CTRL_MachineID *out_machine_id, CTRL_Handle *out_process,
internal void
ctrl_thread__entry_point(void *p)
{
ThreadName("[ctrl] thread");
ThreadNameF("[ctrl] thread");
ProfBeginFunction();
demon_primary_thread_begin();
Temp scratch = scratch_begin(0, 0);
+1 -1
View File
@@ -140,7 +140,7 @@ fs_u2s_dequeue_path(Arena *arena)
internal void
fs_streamer_thread__entry_point(void *p)
{
ThreadName("[fs] streamer #%I64u", (U64)p);
ThreadNameF("[fs] streamer #%I64u", (U64)p);
for(;;)
{
Temp scratch = scratch_begin(0, 0);
+19 -20
View File
@@ -5,40 +5,41 @@
//~ MSF Parser Function
internal MSF_Parsed*
msf_parsed_from_data(Arena *arena, String8 msf_data){
ProfBegin("msf_parsed_from_data");
msf_parsed_from_data(Arena *arena, String8 msf_data)
{
Temp scratch = scratch_begin(&arena, 1);
MSF_Parsed *result = 0;
//- determine msf type
U32 index_size = 0;
if (msf_data.size >= MSF_MIN_SIZE){
if (str8_match(msf_data, str8_lit(msf_msf20_magic),
StringMatchFlag_RightSideSloppy)){
if(msf_data.size >= MSF_MIN_SIZE)
{
if(str8_match(msf_data, str8_lit(msf_msf20_magic), StringMatchFlag_RightSideSloppy))
{
index_size = 2;
}
else if (str8_match(msf_data, str8_lit(msf_msf70_magic),
StringMatchFlag_RightSideSloppy)){
else if(str8_match(msf_data, str8_lit(msf_msf70_magic), StringMatchFlag_RightSideSloppy))
{
index_size = 4;
}
}
if (index_size == 2 || index_size == 4){
if(index_size == 2 || index_size == 4)
{
//- extract info from header
U32 block_size_raw = 0;
U32 whole_file_block_count_raw = 0;
U32 directory_size_raw = 0;
U32 directory_super_map_raw = 0;
if (index_size == 2){
if(index_size == 2)
{
MSF_Header20 *header = (MSF_Header20*)(msf_data.str + MSF_MSF20_MAGIC_SIZE);
block_size_raw = header->block_size;
whole_file_block_count_raw = header->block_count;
directory_size_raw = header->directory_size;
}
else if (index_size == 4){
else if(index_size == 4)
{
MSF_Header70 *header = (MSF_Header70*)(msf_data.str + MSF_MSF70_MAGIC_SIZE);
block_size_raw = header->block_size;
whole_file_block_count_raw = header->block_count;
@@ -91,7 +92,6 @@ msf_parsed_from_data(Arena *arena, String8 msf_data){
//- parse stream directory
U8 *directory_buf = push_array(scratch.arena, U8, directory_size);
B32 got_directory = 1;
{
U32 directory_super_map_dummy = 0;
U32 *directory_super_map = 0;
@@ -174,7 +174,8 @@ msf_parsed_from_data(Arena *arena, String8 msf_data){
B32 got_streams = 0;
String8 *streams = 0;
if (got_directory){
if(got_directory)
{
got_streams = 1;
// read stream count
@@ -258,7 +259,8 @@ msf_parsed_from_data(Arena *arena, String8 msf_data){
parse_streams_done:;
}
if (got_streams){
if(got_streams)
{
result = push_array(arena, MSF_Parsed, 1);
result->streams = streams;
result->stream_count = stream_count;
@@ -268,10 +270,7 @@ msf_parsed_from_data(Arena *arena, String8 msf_data){
}
scratch_end(scratch);
ProfEnd();
return(result);
return result;
}
internal String8
+1 -1
View File
@@ -984,7 +984,7 @@ os_logical_core_count(void)
}
////////////////////////////////
//~ rjf: @os_hooks Process Info (Implemented Per-OS)
//~ rjf: @os_hooks Process & Thread Info (Implemented Per-OS)
internal String8List
os_get_command_line_arguments(void)
+6 -1
View File
@@ -232,7 +232,7 @@ internal U64 os_allocation_granularity(void);
internal U64 os_logical_core_count(void);
////////////////////////////////
//~ rjf: @os_hooks Process Info (Implemented Per-OS)
//~ rjf: @os_hooks Process & Thread Info (Implemented Per-OS)
internal String8List os_get_command_line_arguments(void);
internal S32 os_get_pid(void);
@@ -240,6 +240,11 @@ internal S32 os_get_tid(void);
internal String8List os_get_environment(void);
internal U64 os_string_list_from_system_path(Arena *arena, OS_SystemPath path, String8List *out);
////////////////////////////////
//~ rjf: @os_hooks Thread Names
internal void os_set_thread_name(String8 string);
////////////////////////////////
//~ rjf: @os_hooks Process Control (Implemented Per-OS)
+48
View File
@@ -595,6 +595,54 @@ os_string_list_from_system_path(Arena *arena, OS_SystemPath path, String8List *o
return(result);
}
////////////////////////////////
//~ rjf: @os_hooks Thread Names
internal void
os_set_thread_name(String8 name)
{
Temp scratch = scratch_begin(0, 0);
// rjf: windows 10 style
{
String16 name16 = str16_from_8(scratch.arena, name);
HRESULT hr = SetThreadDescription(GetCurrentThread(), (WCHAR*)name16.str);
}
// rjf: raise-exception style
{
String8 name_copy = push_str8_copy(scratch.arena, name);
#pragma pack(push,8)
typedef struct THREADNAME_INFO THREADNAME_INFO;
struct THREADNAME_INFO
{
U32 dwType; // Must be 0x1000.
char *szName; // Pointer to name (in user addr space).
U32 dwThreadID; // Thread ID (-1=caller thread).
U32 dwFlags; // Reserved for future use, must be zero.
};
#pragma pack(pop)
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = (char *)name_copy.str;
info.dwThreadID = os_get_tid();
info.dwFlags = 0;
#pragma warning(push)
#pragma warning(disable: 6320 6322)
__try
{
RaiseException(0x406D1388, 0, sizeof(info) / sizeof(void *), (const ULONG_PTR *)&info);
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}
#pragma warning(pop)
}
scratch_end(scratch);
}
////////////////////////////////
//~ rjf: @os_hooks Process Control (Implemented Per-OS)
+1 -1
View File
@@ -343,7 +343,7 @@ entry_point(int argc, char **argv)
tmSetMaxThreadCount(1024);
tmInitialize(sizeof(tm_data), (char *)tm_data);
#endif
ThreadName("[main]");
ThreadNameF("[main]");
//- rjf: initialize basic dependencies
os_init(argc, argv);
+1 -1
View File
@@ -410,7 +410,7 @@ main(int argc, char **argv){
tmInitialize(tm_data_size, tm_data);
#endif
ThreadName("[main]");
ThreadNameF("[main]");
Arena *arena = arena_alloc();
String8List args = os_string_list_from_argcv(arena, argc, argv);
+1 -1
View File
@@ -46,7 +46,7 @@ main(int argc, char **argv)
tmSetMaxThreadCount(1024);
tmInitialize(tm_data_size, tm_data);
#endif
ThreadName("[main]");
ThreadNameF("[main]");
//- rjf: initialize dependencies
os_init(argc, argv);
+2 -2
View File
@@ -36,7 +36,7 @@ ts_init(void)
ts_shared->artifact_stripes[idx].cv = os_condition_variable_alloc();
ts_shared->artifact_stripes[idx].rw_mutex = os_rw_mutex_alloc();
}
ts_shared->u2t_ring_size = KB(256);
ts_shared->u2t_ring_size = KB(1024);
ts_shared->u2t_ring_base = push_array_no_zero(arena, U8, ts_shared->u2t_ring_size);
ts_shared->u2t_ring_mutex = os_mutex_alloc();
ts_shared->u2t_ring_cv = os_condition_variable_alloc();
@@ -168,7 +168,7 @@ internal void
ts_task_thread__entry_point(void *p)
{
U64 thread_idx = (U64)p;
ThreadName("[ts] task thread #%I64u", thread_idx+1);
ThreadNameF("[ts] task thread #%I64u", thread_idx+1);
TS_TaskThread *thread = &ts_shared->task_threads[thread_idx];
for(;;)
{