@@ -35,6 +35,74 @@ void jl_lock_profile(void);
3535void jl_unlock_profile (void );
3636void jl_shuffle_int_array_inplace (volatile uint64_t * carray , size_t size , uint64_t * seed );
3737
38+ ///////////////////////
39+ // Utility functions //
40+ ///////////////////////
41+ JL_DLLEXPORT int jl_profile_init (size_t maxsize , uint64_t delay_nsec )
42+ {
43+ bt_size_max = maxsize ;
44+ nsecprof = delay_nsec ;
45+ if (bt_data_prof != NULL )
46+ free ((void * )bt_data_prof );
47+ if (profile_round_robin_thread_order == NULL ) {
48+ // NOTE: We currently only allocate this once, since jl_n_threads cannot change
49+ // during execution of a julia process. If/when this invariant changes in the
50+ // future, this will have to be adjusted.
51+ profile_round_robin_thread_order = (uint64_t * ) calloc (jl_n_threads , sizeof (uint64_t ));
52+ for (int i = 0 ; i < jl_n_threads ; i ++ ) {
53+ profile_round_robin_thread_order [i ] = i ;
54+ }
55+ }
56+ profile_cong_rng_seed = jl_rand ();
57+ unbias_cong (jl_n_threads , & profile_cong_rng_unbias );
58+ bt_data_prof = (jl_bt_element_t * ) calloc (maxsize , sizeof (jl_bt_element_t ));
59+ if (bt_data_prof == NULL && maxsize > 0 )
60+ return -1 ;
61+ bt_size_cur = 0 ;
62+ return 0 ;
63+ }
64+
65+ void jl_shuffle_int_array_inplace (volatile uint64_t * carray , size_t size , uint64_t * seed ) {
66+ // The "modern Fisher–Yates shuffle" - O(n) algorithm
67+ // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
68+ for (size_t i = size - 1 ; i >= 1 ; -- i ) {
69+ size_t j = cong (i , profile_cong_rng_unbias , seed );
70+ uint64_t tmp = carray [j ];
71+ carray [j ] = carray [i ];
72+ carray [i ] = tmp ;
73+ }
74+ }
75+
76+ JL_DLLEXPORT uint8_t * jl_profile_get_data (void )
77+ {
78+ return (uint8_t * ) bt_data_prof ;
79+ }
80+
81+ JL_DLLEXPORT size_t jl_profile_len_data (void )
82+ {
83+ return bt_size_cur ;
84+ }
85+
86+ JL_DLLEXPORT size_t jl_profile_maxlen_data (void )
87+ {
88+ return bt_size_max ;
89+ }
90+
91+ JL_DLLEXPORT uint64_t jl_profile_delay_nsec (void )
92+ {
93+ return nsecprof ;
94+ }
95+
96+ JL_DLLEXPORT void jl_profile_clear_data (void )
97+ {
98+ bt_size_cur = 0 ;
99+ }
100+
101+ JL_DLLEXPORT int jl_profile_is_running (void )
102+ {
103+ return running ;
104+ }
105+
38106JL_DLLEXPORT int jl_profile_is_buffer_full (void )
39107{
40108 // declare buffer full if there isn't enough room to take samples across all threads
@@ -323,74 +391,6 @@ void jl_critical_error(int sig, bt_context_t *context, jl_task_t *ct)
323391 jl_gc_debug_critical_error ();
324392}
325393
326- ///////////////////////
327- // Utility functions //
328- ///////////////////////
329- JL_DLLEXPORT int jl_profile_init (size_t maxsize , uint64_t delay_nsec )
330- {
331- bt_size_max = maxsize ;
332- nsecprof = delay_nsec ;
333- if (bt_data_prof != NULL )
334- free ((void * )bt_data_prof );
335- if (profile_round_robin_thread_order == NULL ) {
336- // NOTE: We currently only allocate this once, since jl_n_threads cannot change
337- // during execution of a julia process. If/when this invariant changes in the
338- // future, this will have to be adjusted.
339- profile_round_robin_thread_order = (uint64_t * ) calloc (jl_n_threads , sizeof (uint64_t ));
340- for (int i = 0 ; i < jl_n_threads ; i ++ ) {
341- profile_round_robin_thread_order [i ] = i ;
342- }
343- }
344- profile_cong_rng_seed = jl_rand ();
345- unbias_cong (jl_n_threads , & profile_cong_rng_unbias );
346- bt_data_prof = (jl_bt_element_t * ) calloc (maxsize , sizeof (jl_bt_element_t ));
347- if (bt_data_prof == NULL && maxsize > 0 )
348- return -1 ;
349- bt_size_cur = 0 ;
350- return 0 ;
351- }
352-
353- void jl_shuffle_int_array_inplace (volatile uint64_t * carray , size_t size , uint64_t * seed ) {
354- // The "modern Fisher–Yates shuffle" - O(n) algorithm
355- // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
356- for (size_t i = size - 1 ; i >= 1 ; -- i ) {
357- size_t j = cong (i , profile_cong_rng_unbias , seed );
358- uint64_t tmp = carray [j ];
359- carray [j ] = carray [i ];
360- carray [i ] = tmp ;
361- }
362- }
363-
364- JL_DLLEXPORT uint8_t * jl_profile_get_data (void )
365- {
366- return (uint8_t * ) bt_data_prof ;
367- }
368-
369- JL_DLLEXPORT size_t jl_profile_len_data (void )
370- {
371- return bt_size_cur ;
372- }
373-
374- JL_DLLEXPORT size_t jl_profile_maxlen_data (void )
375- {
376- return bt_size_max ;
377- }
378-
379- JL_DLLEXPORT uint64_t jl_profile_delay_nsec (void )
380- {
381- return nsecprof ;
382- }
383-
384- JL_DLLEXPORT void jl_profile_clear_data (void )
385- {
386- bt_size_cur = 0 ;
387- }
388-
389- JL_DLLEXPORT int jl_profile_is_running (void )
390- {
391- return running ;
392- }
393-
394394#ifdef __cplusplus
395395}
396396#endif
0 commit comments