Skip to content

Commit 891f184

Browse files
lplewabratpiorka
authored andcommitted
initialize hwloc topology only before first fork, not always
1 parent f3dfe30 commit 891f184

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/libumf.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
4040

4141
void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }
4242

43+
// Benchmarks may fork multiple times and topology init is slow.
44+
// Init topology before fork (if not already) so children don't repeat it.
45+
// TODO: This is a hack. Better solution is needed.
46+
#if !defined(_WIN32) && !defined(UMF_NO_HWLOC)
47+
static void atfork_prepare(void) { umfGetTopologyReduced(); }
48+
#endif
49+
4350
umf_result_t umfInit(void) {
4451
utils_init_once(&initMutexOnce, initialize_init_mutex);
4552

@@ -74,11 +81,11 @@ umf_result_t umfInit(void) {
7481
if (TRACKER) {
7582
LOG_DEBUG("UMF library initialized");
7683
}
77-
#if !defined(UMF_NO_HWLOC)
78-
// some benchmarks uses multiple forks, and topology initialization is very slow
79-
// so if we initialize topology before the first fork, we can get significant performance gain.
80-
umfGetTopologyReduced();
84+
85+
#if !defined(_WIN32) && !defined(UMF_NO_HWLOC)
86+
pthread_atfork(atfork_prepare, NULL, NULL);
8187
#endif
88+
8289
return UMF_RESULT_SUCCESS;
8390
}
8491

0 commit comments

Comments
 (0)