@@ -429,8 +429,8 @@ buffer_manager_init_sequence_point_thread_list (
429429 ep_thread_session_state_set_track_state (thread_session_state , EP_SEQUENCE_POINT_THREAD_ID_TRACKED );
430430 }
431431
432- dn_umap_ptr_uint32_insert ( ep_sequence_point_get_thread_sequence_numbers ( sequence_point ), thread_session_state , sequence_number );
433- ep_thread_addref ( ep_thread_holder_get_thread ( ep_thread_session_state_get_thread_holder_ref ( thread_session_state )) );
432+ ep_rt_thread_id_t os_thread_id = ep_thread_get_os_thread_id ( ep_thread_session_state_get_thread ( thread_session_state ) );
433+ dn_umap_threadid_uint32_insert ( ep_sequence_point_get_thread_sequence_numbers ( sequence_point ), os_thread_id , sequence_number );
434434 } DN_LIST_FOREACH_END ;
435435
436436 // This needs to come after querying the thread sequence numbers to ensure that any recorded
@@ -736,15 +736,8 @@ buffer_manager_advance_to_non_empty_buffer (
736736 // get the next buffer
737737 current_buffer = buffer_list -> head_buffer ;
738738
739- if (!current_buffer && (ep_rt_volatile_load_uint32_t_without_barrier (ep_thread_get_unregistered_ref (ep_thread_session_state_get_thread (thread_session_state ))) > 0 )) {
740- if (buffer_manager -> sequence_point_alloc_budget != 0 ) {
741- // The next sequence point needs to update the sequence number for this thread
742- // Delay the thread session state removal until the next sequence point is written.
743- ep_thread_session_state_set_delete_deferred (thread_session_state , true);
744- } else {
745- buffer_manager_remove_and_delete_thread_session_state (buffer_manager , thread_session_state );
746- }
747- }
739+ if (!current_buffer && (ep_rt_volatile_load_uint32_t_without_barrier (ep_thread_get_unregistered_ref (ep_thread_session_state_get_thread (thread_session_state ))) > 0 ))
740+ buffer_manager_remove_and_delete_thread_session_state (buffer_manager , thread_session_state );
748741
749742 if (!current_buffer || ep_buffer_get_creation_timestamp (current_buffer ) >= before_timestamp ) {
750743 // no more buffers in the list before this timestamp, we're done
@@ -1188,9 +1181,10 @@ ep_buffer_manager_write_all_buffers_to_file_v4 (
11881181 if (buffer_manager -> current_event == NULL )
11891182 break ;
11901183
1191- uint64_t capture_thread_id = ep_thread_get_os_thread_id (ep_buffer_get_writer_thread (buffer_manager -> current_buffer ));
1184+ uint64_t capture_thread_id = ep_rt_thread_id_t_to_uint64_t ( ep_thread_get_os_thread_id (ep_buffer_get_writer_thread (buffer_manager -> current_buffer ) ));
11921185
11931186 EventPipeThreadSessionState * current_thread_session_state = buffer_manager -> current_thread_session_state ;
1187+ ep_rt_thread_id_t os_thread_id = ep_thread_get_os_thread_id (ep_thread_session_state_get_thread (current_thread_session_state ));
11941188
11951189 // loop across events on this thread
11961190 bool events_written_for_thread = false;
@@ -1207,11 +1201,23 @@ ep_buffer_manager_write_all_buffers_to_file_v4 (
12071201 buffer_manager_move_next_event_same_thread (buffer_manager , current_timestamp_boundary );
12081202 }
12091203
1210- ep_thread_session_state_set_last_read_sequence_number (current_thread_session_state , sequence_number );
1204+ dn_list_it_t found = dn_list_find (buffer_manager -> thread_session_state_list , current_thread_session_state );
1205+ if (!dn_list_it_end (found )) {
1206+ // If the ThreadSessionState hasn't been removed through buffer_manager_advance_to_non_empty_buffer,
1207+ // we need to update the last_read_sequence_number for any future sequence points being initialized.
1208+ ep_thread_session_state_set_last_read_sequence_number (current_thread_session_state , sequence_number );
1209+ }
1210+
12111211 if (sequence_point != NULL ) {
1212- dn_umap_it_t found = dn_umap_ptr_uint32_find (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), current_thread_session_state );
1212+ dn_umap_it_t found = dn_umap_threadid_uint32_find (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), os_thread_id );
12131213 EP_ASSERT (!dn_umap_it_end (found ));
1214- dn_umap_ptr_uint32_insert_or_assign (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), current_thread_session_state , sequence_number );
1214+ uint32_t thread_sequence_number = !dn_umap_it_end (found ) ? dn_umap_it_value_uint32_t (found ) : 0 ;
1215+ // Sequence numbers can overflow so we can't use a direct last_read > sequence_number comparison
1216+ // If a thread is able to drop more than 0x80000000 events in between sequence points then we will
1217+ // miscategorize it, but that seems unlikely.
1218+ uint32_t last_read_delta = sequence_number - thread_sequence_number ;
1219+ if (0 < last_read_delta && last_read_delta < 0x80000000 )
1220+ dn_umap_threadid_uint32_insert_or_assign (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), os_thread_id , sequence_number );
12151221 }
12161222
12171223 // Have we written events in any sequence point?
@@ -1227,42 +1233,11 @@ ep_buffer_manager_write_all_buffers_to_file_v4 (
12271233
12281234 // stopped at sequence point case
12291235
1230- // the sequence point captured a lower bound for sequence number on each thread, but iterating
1231- // through the events we may have observed that a higher numbered event was recorded. If so we
1232- // should adjust the sequence numbers upwards to ensure the data in the stream is consistent.
1233- EP_SPIN_LOCK_ENTER (& buffer_manager -> rt_lock , section2 )
1234- for (dn_list_it_t it = dn_list_begin (buffer_manager -> thread_session_state_list ); !dn_list_it_end (it ); ) {
1235- EventPipeThreadSessionState * session_state = * dn_list_it_data_t (it , EventPipeThreadSessionState * );
1236- dn_umap_it_t found = dn_umap_ptr_uint32_find (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), session_state );
1237- uint32_t thread_sequence_number = !dn_umap_it_end (found ) ? dn_umap_it_value_uint32_t (found ) : 0 ;
1238- uint32_t last_read_sequence_number = ep_thread_session_state_get_last_read_sequence_number (session_state );
1239- EP_ASSERT (thread_sequence_number == 0 || thread_sequence_number == last_read_sequence_number );
1240- // Sequence numbers can overflow so we can't use a direct last_read > sequence_number comparison
1241- // If a thread is able to drop more than 0x80000000 events in between sequence points then we will
1242- // miscategorize it, but that seems unlikely.
1243- uint32_t last_read_delta = last_read_sequence_number - thread_sequence_number ;
1244- if (0 < last_read_delta && last_read_delta < 0x80000000 ) {
1245- dn_umap_ptr_uint32_insert_or_assign (ep_sequence_point_get_thread_sequence_numbers (sequence_point ), session_state , last_read_sequence_number );
1246- if (dn_umap_it_end (found ))
1247- ep_thread_addref (ep_thread_holder_get_thread (ep_thread_session_state_get_thread_holder_ref (session_state )));
1248- }
1249-
1250- it = dn_list_it_next (it );
1251-
1252- if (ep_thread_session_state_get_delete_deferred (session_state ))
1253- dn_vector_ptr_push_back (& session_states_to_delete , session_state );
1254- }
1255- EP_SPIN_LOCK_EXIT (& buffer_manager -> rt_lock , section2 )
1256-
12571236 // emit the sequence point into the file
12581237 ep_file_write_sequence_point (file , sequence_point );
12591238
12601239 // we are done with the sequence point
12611240 EP_SPIN_LOCK_ENTER (& buffer_manager -> rt_lock , section3 )
1262- DN_VECTOR_PTR_FOREACH_BEGIN (EventPipeThreadSessionState * , thread_session_state , & session_states_to_delete ) {
1263- if (ep_thread_session_state_get_delete_deferred (thread_session_state ))
1264- buffer_manager_remove_and_delete_thread_session_state (buffer_manager , thread_session_state );
1265- } DN_VECTOR_PTR_FOREACH_END ;
12661241 buffer_manager_dequeue_sequence_point (buffer_manager );
12671242 sequence_point = NULL ;
12681243 EP_SPIN_LOCK_EXIT (& buffer_manager -> rt_lock , section3 )
@@ -1349,27 +1324,6 @@ ep_buffer_manager_ensure_consistency (EventPipeBufferManager *buffer_manager)
13491324}
13501325#endif
13511326
1352- static
1353- void
1354- buffer_manager_remove_thread_session_state_from_sequence_points (
1355- EventPipeBufferManager * buffer_manager ,
1356- EventPipeThreadSessionState * thread_session_state )
1357- {
1358- EP_ASSERT (buffer_manager != NULL );
1359- EP_ASSERT (thread_session_state != NULL );
1360-
1361- ep_buffer_manager_requires_lock_held (buffer_manager );
1362-
1363- DN_LIST_FOREACH_BEGIN (EventPipeSequencePoint * , current_sequence_point , buffer_manager -> sequence_points ) {
1364- dn_umap_it_t found = dn_umap_ptr_uint32_find (ep_sequence_point_get_thread_sequence_numbers (current_sequence_point ), thread_session_state );
1365- if (!dn_umap_it_end (found )) {
1366- dn_umap_erase (found );
1367- // buffer_manager_init_sequence_point_thread_list - every entry of this map was holding an extra ref to the thread
1368- ep_thread_release (ep_thread_session_state_get_thread (thread_session_state ));
1369- }
1370- } DN_LIST_FOREACH_END ;
1371- }
1372-
13731327static
13741328void
13751329buffer_manager_remove_and_delete_thread_session_state (
@@ -1383,7 +1337,6 @@ buffer_manager_remove_and_delete_thread_session_state (
13831337
13841338 dn_list_remove (buffer_manager -> thread_session_state_list , thread_session_state );
13851339 ep_thread_set_session_state (ep_thread_session_state_get_thread (thread_session_state ), ep_thread_session_state_get_session (thread_session_state ), NULL );
1386- buffer_manager_remove_thread_session_state_from_sequence_points (buffer_manager , thread_session_state );
13871340 ep_thread_session_state_free (thread_session_state );
13881341}
13891342
0 commit comments