Skip to content

Commit 19bdde1

Browse files
committed
issue: Start event handler thread during XLIO init
Event handler manager is a global object with a single internal thread. Once XLIO is initialized, there is no option not to start the internal thread and it's used starting from the initialization method (to create a timer event for netlink). Therefore, there is no point in trying to start the thread with each post action to the event handler. This can save CPU in CPS scenario. Signed-off-by: Dmytro Podgornyi <[email protected]>
1 parent 27c08ff commit 19bdde1

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

src/core/event/event_handler_manager.cpp

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -344,56 +344,53 @@ int event_handler_manager::start_thread()
344344
{
345345
cpu_set_t cpu_set;
346346
pthread_attr_t tattr;
347+
int ret;
348+
bool affinity_requested = false;
347349

348350
if (!m_b_continue_running) {
351+
errno = ECANCELED;
349352
return -1;
350353
}
351-
352354
if (m_event_handler_tid != 0) {
353355
return 0;
354356
}
355357

356-
// m_reg_action_q.reserve(); //todo change to vector and reserve
357-
358-
BULLSEYE_EXCLUDE_BLOCK_START
359-
if (pthread_attr_init(&tattr)) {
360-
evh_logpanic("Failed to initialize thread attributes");
358+
ret = pthread_attr_init(&tattr);
359+
if (ret != 0) {
360+
return -1;
361361
}
362-
BULLSEYE_EXCLUDE_BLOCK_END
363362

364363
cpu_set = safe_mce_sys().internal_thread_affinity;
365364
if (strcmp(safe_mce_sys().internal_thread_affinity_str, "-1") &&
366-
!strcmp(safe_mce_sys().internal_thread_cpuset,
367-
MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) { // no affinity
368-
BULLSEYE_EXCLUDE_BLOCK_START
369-
if (pthread_attr_setaffinity_np(&tattr, sizeof(cpu_set), &cpu_set)) {
370-
evh_logpanic("Failed to set CPU affinity");
365+
!strcmp(safe_mce_sys().internal_thread_cpuset, MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) {
366+
ret = pthread_attr_setaffinity_np(&tattr, sizeof(cpu_set), &cpu_set);
367+
if (ret != 0) {
368+
evh_logwarn("Failed to set event handler thread affinity. (errno=%d)", errno);
371369
}
372-
BULLSEYE_EXCLUDE_BLOCK_END
370+
affinity_requested = (ret == 0);
373371
} else {
374372
evh_logdbg("Internal thread affinity not set.");
375373
}
376374

377-
int ret = pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
378-
if (ret) {
379-
// maybe it's the cset issue? try without affinity
380-
evh_logwarn("Failed to start event handler thread with thread affinity - trying without. "
381-
"[errno=%d %s]",
382-
ret, strerror(ret));
383-
BULLSEYE_EXCLUDE_BLOCK_START
384-
if (pthread_attr_init(&tattr)) {
385-
evh_logpanic("Failed to initialize thread attributes");
386-
}
387-
if (pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this)) {
388-
evh_logpanic("Failed to start event handler thread");
389-
}
390-
BULLSEYE_EXCLUDE_BLOCK_END
375+
ret = pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
376+
if (ret != 0 && affinity_requested) {
377+
// Try without affinity in case this is a cset issue.
378+
evh_logwarn("Failed to start event handler thread with a thread affinity. Trying default "
379+
"thread affinity. (errno=%d)",
380+
errno);
381+
pthread_attr_destroy(&tattr);
382+
ret = pthread_attr_init(&tattr)
383+
?: pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
391384
}
392-
385+
// Destroy will either succeed or return EINVAL if the init fails in the above block.
393386
pthread_attr_destroy(&tattr);
394387

395-
evh_logdbg("Started event handler thread");
396-
return 0;
388+
if (ret == 0) {
389+
evh_logdbg("Started event handler thread.");
390+
} else {
391+
evh_logerr("Failed to start event handler thread. (errno=%d)", errno);
392+
}
393+
return ret;
397394
}
398395

399396
void event_handler_manager::stop_thread()
@@ -483,15 +480,11 @@ void event_handler_manager::post_new_reg_action(reg_action_t &reg_action)
483480
return;
484481
}
485482

486-
start_thread();
487-
488483
evh_logfunc("add event action %s (%d)", reg_action_str(reg_action.type), reg_action.type);
489484

490-
bool is_empty = false;
485+
bool is_empty;
491486
m_reg_action_q_lock.lock();
492-
if (m_p_reg_action_q_to_push_to->empty()) {
493-
is_empty = true;
494-
}
487+
is_empty = m_p_reg_action_q_to_push_to->empty();
495488
m_p_reg_action_q_to_push_to->push_back(reg_action);
496489
m_reg_action_q_lock.unlock();
497490
if (is_empty) {

src/core/event/event_handler_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class event_handler_manager : public wakeup_pipe {
208208
void register_command_event(int fd, command *cmd);
209209

210210
void *thread_loop();
211+
int start_thread();
211212
void stop_thread();
212213
bool is_running() { return m_b_continue_running; };
213214

@@ -251,7 +252,6 @@ class event_handler_manager : public wakeup_pipe {
251252
void handle_registration_action(reg_action_t &reg_action);
252253
void process_ibverbs_event(event_handler_map_t::iterator &i);
253254
void process_rdma_cm_event(event_handler_map_t::iterator &i);
254-
int start_thread();
255255

256256
void event_channel_post_process_for_rdma_events(void *p_event);
257257
void *event_channel_pre_process_for_rdma_events(void *p_event_channel_handle, void **p_event);

src/core/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ static void do_global_ctors_helper()
983983
{
984984
static lock_spin_recursive g_globals_lock;
985985
std::lock_guard<decltype(g_globals_lock)> lock(g_globals_lock);
986+
int rc;
986987

987988
if (g_init_global_ctors_done) {
988989
return;
@@ -1018,6 +1019,12 @@ static void do_global_ctors_helper()
10181019

10191020
// Create all global management objects
10201021
NEW_CTOR(g_p_event_handler_manager, event_handler_manager());
1022+
rc = g_p_event_handler_manager->start_thread();
1023+
if (rc != 0) {
1024+
BULLSEYE_EXCLUDE_BLOCK_START
1025+
throw_xlio_exception("Failed to start event handler thread.\n");
1026+
BULLSEYE_EXCLUDE_BLOCK_END
1027+
}
10211028

10221029
xlio_shmem_stats_open(&g_p_vlogger_level, &g_p_vlogger_details);
10231030
*g_p_vlogger_level = g_vlogger_level;

0 commit comments

Comments
 (0)