Skip to content

Commit

Permalink
pal init: InitializeFlushProcessWriteBuffers() before first thread to…
Browse files Browse the repository at this point in the history
… improve start time

InitializeFlushProcessWriteBuffers() initializes expedited membarrier()
syscall on Linux, which is much slower when called in a multi-thread
process. Move this init earlier to improve dotnet process start time.
A detailed explanation can be found in issue 106722.

Fixes #106722
  • Loading branch information
Haris Okanovic committed Aug 21, 2024
1 parent 57502fc commit 27896a2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ PAL_IsDebuggerPresent();
#define PAL_INITIALIZE_ENSURE_STACK_SIZE 0x20
#define PAL_INITIALIZE_REGISTER_SIGNALS 0x40
#define PAL_INITIALIZE_REGISTER_ACTIVATION_SIGNAL 0x80
#define PAL_INITIALIZE_FLUSH_PROCES_WRITE_BUFFERS 0x100

// PAL_Initialize() flags
#define PAL_INITIALIZE (PAL_INITIALIZE_SYNC_THREAD | \
Expand All @@ -206,7 +207,8 @@ PAL_IsDebuggerPresent();
PAL_INITIALIZE_DEBUGGER_EXCEPTIONS | \
PAL_INITIALIZE_ENSURE_STACK_SIZE | \
PAL_INITIALIZE_REGISTER_SIGNALS | \
PAL_INITIALIZE_REGISTER_ACTIVATION_SIGNAL)
PAL_INITIALIZE_REGISTER_ACTIVATION_SIGNAL | \
PAL_INITIALIZE_FLUSH_PROCES_WRITE_BUFFERS)

typedef DWORD (PALAPI_NOEXPORT *PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);
typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;
Expand Down
15 changes: 10 additions & 5 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,16 @@ Initialize(
goto CLEANUP0a;
}

if (flags & PAL_INITIALIZE_FLUSH_PROCES_WRITE_BUFFERS)
{
// Initialize before first thread is created for faster load on Linux
if (!InitializeFlushProcessWriteBuffers())
{
palError = ERROR_PALINIT_INITIALIZE_FLUSH_PROCESS_WRITE_BUFFERS;
goto CLEANUP0a;
}
}

// The gSharedFilesPath is allocated dynamically so its destructor does not get
// called unexpectedly during cleanup
gSharedFilesPath = InternalNew<PathCharString>();
Expand Down Expand Up @@ -787,11 +797,6 @@ PAL_InitializeCoreCLR(const char *szExePath, BOOL runningInExe)
return ERROR_PALINIT_PROCABORT_INITIALIZE;
}

if (!InitializeFlushProcessWriteBuffers())
{
return ERROR_PALINIT_INITIALIZE_FLUSH_PROCESS_WRITE_BUFFERS;
}

return ERROR_SUCCESS;
}

Expand Down

0 comments on commit 27896a2

Please sign in to comment.