Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jan 3, 2025
1 parent 4ef6728 commit 9911226
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,7 @@ protected unsafe void WriteEventWithRelatedActivityIdCore(int eventId, Guid* rel

if (m_Dispatchers != null && metadata.EnabledForAnyListener)
{
// TODO Pavel review
#if MONO && !TARGET_BROWSER && !TARGET_WASI
// On Mono, managed events from NativeRuntimeEventSource are written using WriteEventCore which can be
// written doubly because EventPipe tries to pump it back up to EventListener via NativeRuntimeEventSource.ProcessEvents.
Expand Down
1 change: 1 addition & 0 deletions src/mono/mono/eventpipe/ep-rt-mono-runtime-provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,7 @@ ep_rt_mono_sample_profiler_write_sampling_event_for_threads (
MonoThreadUnwindState *thread_state = mono_thread_info_get_suspend_state (thread_info);
if (thread_state->valid) {
if (sampled_thread_count < _max_sampled_thread_count) {
// TODO Pavel refactor to be called from interp.c probably as MINT_PROF_ENTER, MINT_PROF_EXIT
SampleProfileStackWalkData *data = &g_array_index (_sampled_thread_callstacks, SampleProfileStackWalkData, sampled_thread_count);
data->thread_id = ep_rt_thread_id_t_to_uint64_t (mono_thread_info_get_tid (thread_info));
data->thread_ip = (uintptr_t)MONO_CONTEXT_GET_IP (&thread_state->ctx);
Expand Down
28 changes: 27 additions & 1 deletion src/native/eventpipe/ep-sample-profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,40 @@ EP_RT_DEFINE_THREAD_FUNC (sampling_thread)

ep_rt_thread_params_t *thread_params = (ep_rt_thread_params_t *)data;

#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
// TODO Pavel
// THIS IS QUICK END FOR BROWSER, because stop the world is not supported
// TODO call sampling from interp.c probably as MINT_PROF_ENTER, MINT_PROF_EXIT or similar
return (ep_rt_thread_start_func_return_t)1;

if (true) {
#else
if (thread_params->thread && ep_rt_thread_has_started (thread_params->thread)) {
#endif
EP_GCX_PREEMP_ENTER
while (sample_profiler_load_profiling_enabled ()) {
do {
if(!sample_profiler_load_profiling_enabled ()){
break;
}
// Sample all threads.
ep_rt_sample_profiler_write_sampling_event_for_threads (thread_params->thread, _thread_time_event);
// Wait until it's time to sample again.
ep_rt_thread_sleep (_sampling_rate_in_ns);
}
#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
while(false);
#else
while(true);
#endif
EP_GCX_PREEMP_EXIT
}

#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
if(sample_profiler_load_profiling_enabled ()){
return 1; // re-schedule
}
#endif

// Signal disable () that the thread has been destroyed.
ep_rt_wait_event_set (&_thread_shutdown_event);

Expand Down Expand Up @@ -289,7 +312,10 @@ ep_sample_profiler_disable (void)
sample_profiler_store_profiling_enabled (false);

// Wait for the sampling thread to clean itself up.
#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
#else
ep_rt_wait_event_wait (&_thread_shutdown_event, EP_INFINITE_WAIT, false);
#endif
ep_rt_wait_event_free (&_thread_shutdown_event);

if (_time_period_is_set)
Expand Down
23 changes: 21 additions & 2 deletions src/native/eventpipe/ep-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,35 @@ EP_RT_DEFINE_THREAD_FUNC (streaming_thread)
if (session->session_type != EP_SESSION_TYPE_IPCSTREAM && session->session_type != EP_SESSION_TYPE_FILESTREAM)
return 1;

#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
#else
if (!thread_params->thread || !ep_rt_thread_has_started (thread_params->thread))
return 1;
#endif

session->streaming_thread = thread_params->thread;

bool success = true;
#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
#else
ep_rt_wait_event_handle_t *wait_event = ep_session_get_wait_event (session);
#endif

ep_rt_volatile_store_uint32_t (&session->started, 1);

EP_GCX_PREEMP_ENTER
while (ep_session_get_streaming_enabled (session)) {
do {
if (!ep_session_get_streaming_enabled (session)){
return 1; // do not schedule again
}
bool events_written = false;
if (!ep_session_write_all_buffers_to_file (session, &events_written)) {
success = false;
break;
}

#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
#else
if (!events_written) {
// No events were available, sleep until more are available
ep_rt_wait_event_wait (wait_event, EP_INFINITE_WAIT, false);
Expand All @@ -72,10 +83,15 @@ EP_RT_DEFINE_THREAD_FUNC (streaming_thread)
// Wait until it's time to sample again.
const uint32_t timeout_ns = 100000000; // 100 msec.
ep_rt_thread_sleep (timeout_ns);
#endif
}

#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
while (false);
#else
while (true);
session->streaming_thread = NULL;
ep_rt_wait_event_set (&session->rt_thread_shutdown_event);
#endif
EP_GCX_PREEMP_EXIT

if (!success)
Expand Down Expand Up @@ -122,7 +138,10 @@ session_disable_streaming_thread (EventPipeSession *session)

// Wait for the streaming thread to clean itself up.
ep_rt_wait_event_handle_t *rt_thread_shutdown_event = &session->rt_thread_shutdown_event;
#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
#else
ep_rt_wait_event_wait (rt_thread_shutdown_event, EP_INFINITE_WAIT, false /* bAlertable */);
#endif
ep_rt_wait_event_free (rt_thread_shutdown_event);
}

Expand Down
6 changes: 6 additions & 0 deletions src/native/eventpipe/ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,8 +1339,14 @@ ep_delete_provider (EventPipeProvider *provider)
// Helps prevent the EventPipeEventProvider Unregister logic from
// freeing freeing the provider's weak reference gchandle before
// callbacks using that handle have completed.
#if defined(HOST_BROWSER) && defined(DISABLE_THREADS)
// TODO Pavel review the wait and deferred delete logic
if (wait_for_provider_callbacks_completion)
ep_rt_wait_event_wait (&provider->callbacks_complete_event, 0, false);
#else
if (wait_for_provider_callbacks_completion)
ep_rt_wait_event_wait (&provider->callbacks_complete_event, EP_INFINITE_WAIT, false);
#endif

EP_LOCK_ENTER (section2)
if (!enabled ())
Expand Down

0 comments on commit 9911226

Please sign in to comment.