Skip to content

Commit 740c9ea

Browse files
committed
initialize hwloc topology only before first fork, not always
1 parent f3dfe30 commit 740c9ea

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/libumf.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
*/
99

10+
#include <pthread.h>
1011
#include <stdarg.h>
1112
#include <stddef.h>
1213
#include <string.h>
@@ -40,6 +41,11 @@ static umf_ctl_node_t CTL_NODE(umf)[] = {CTL_CHILD(provider), CTL_CHILD(pool),
4041

4142
void initialize_global_ctl(void) { CTL_REGISTER_MODULE(NULL, umf); }
4243

44+
// Benchmarks may fork multiple times and topology init is slow.
45+
// Init topology before fork (if not already) so children don't repeat it.
46+
// TODO: This is a hack. Better solution is needed.
47+
static void atfork_prepare(void) { umfGetTopologyReduced(); }
48+
4349
umf_result_t umfInit(void) {
4450
utils_init_once(&initMutexOnce, initialize_init_mutex);
4551

@@ -74,11 +80,11 @@ umf_result_t umfInit(void) {
7480
if (TRACKER) {
7581
LOG_DEBUG("UMF library initialized");
7682
}
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();
83+
84+
#if !defined(_WIN32) && !defined(UMF_NO_HWLOC)
85+
pthread_atfork(atfork_prepare, NULL, NULL);
8186
#endif
87+
8288
return UMF_RESULT_SUCCESS;
8389
}
8490

0 commit comments

Comments
 (0)