Skip to content

Commit 9e5801d

Browse files
committed
issue: 4432879 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 6689188 commit 9e5801d

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
@@ -307,56 +307,53 @@ int event_handler_manager::start_thread()
307307
{
308308
cpu_set_t cpu_set;
309309
pthread_attr_t tattr;
310+
int ret;
311+
bool affinity_requested = false;
310312

311313
if (!m_b_continue_running) {
314+
errno = ECANCELED;
312315
return -1;
313316
}
314-
315317
if (m_event_handler_tid != 0) {
316318
return 0;
317319
}
318320

319-
// m_reg_action_q.reserve(); //todo change to vector and reserve
320-
321-
BULLSEYE_EXCLUDE_BLOCK_START
322-
if (pthread_attr_init(&tattr)) {
323-
evh_logpanic("Failed to initialize thread attributes");
321+
ret = pthread_attr_init(&tattr);
322+
if (ret != 0) {
323+
return -1;
324324
}
325-
BULLSEYE_EXCLUDE_BLOCK_END
326325

327326
cpu_set = safe_mce_sys().internal_thread_affinity;
328327
if (strcmp(safe_mce_sys().internal_thread_affinity_str, "-1") &&
329-
!strcmp(safe_mce_sys().internal_thread_cpuset,
330-
MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) { // no affinity
331-
BULLSEYE_EXCLUDE_BLOCK_START
332-
if (pthread_attr_setaffinity_np(&tattr, sizeof(cpu_set), &cpu_set)) {
333-
evh_logpanic("Failed to set CPU affinity");
328+
!strcmp(safe_mce_sys().internal_thread_cpuset, MCE_DEFAULT_INTERNAL_THREAD_CPUSET)) {
329+
ret = pthread_attr_setaffinity_np(&tattr, sizeof(cpu_set), &cpu_set);
330+
if (ret != 0) {
331+
evh_logwarn("Failed to set event handler thread affinity. (errno=%d)", errno);
334332
}
335-
BULLSEYE_EXCLUDE_BLOCK_END
333+
affinity_requested = (ret == 0);
336334
} else {
337335
evh_logdbg("Internal thread affinity not set.");
338336
}
339337

340-
int ret = pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
341-
if (ret) {
342-
// maybe it's the cset issue? try without affinity
343-
evh_logwarn("Failed to start event handler thread with thread affinity - trying without. "
344-
"[errno=%d %s]",
345-
ret, strerror(ret));
346-
BULLSEYE_EXCLUDE_BLOCK_START
347-
if (pthread_attr_init(&tattr)) {
348-
evh_logpanic("Failed to initialize thread attributes");
349-
}
350-
if (pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this)) {
351-
evh_logpanic("Failed to start event handler thread");
352-
}
353-
BULLSEYE_EXCLUDE_BLOCK_END
338+
ret = pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
339+
if (ret != 0 && affinity_requested) {
340+
// Try without affinity in case this is a cset issue.
341+
evh_logwarn("Failed to start event handler thread with a thread affinity. Trying default "
342+
"thread affinity. (errno=%d)",
343+
errno);
344+
pthread_attr_destroy(&tattr);
345+
ret = pthread_attr_init(&tattr)
346+
?: pthread_create(&m_event_handler_tid, &tattr, event_handler_thread, this);
354347
}
355-
348+
// Destroy will either succeed or return EINVAL if the init fails in the above block.
356349
pthread_attr_destroy(&tattr);
357350

358-
evh_logdbg("Started event handler thread");
359-
return 0;
351+
if (ret == 0) {
352+
evh_logdbg("Started event handler thread.");
353+
} else {
354+
evh_logerr("Failed to start event handler thread. (errno=%d)", errno);
355+
}
356+
return ret;
360357
}
361358

362359
void event_handler_manager::stop_thread()
@@ -448,15 +445,11 @@ void event_handler_manager::post_new_reg_action(reg_action_t &reg_action)
448445
return;
449446
}
450447

451-
start_thread();
452-
453448
evh_logfunc("add event action %s (%d)", reg_action_str(reg_action.type), reg_action.type);
454449

455-
bool is_empty = false;
450+
bool is_empty;
456451
m_reg_action_q_lock.lock();
457-
if (m_p_reg_action_q_to_push_to->empty()) {
458-
is_empty = true;
459-
}
452+
is_empty = m_p_reg_action_q_to_push_to->empty();
460453
m_p_reg_action_q_to_push_to->push_back(reg_action);
461454
m_reg_action_q_lock.unlock();
462455
if (is_empty) {

src/core/event/event_handler_manager.h

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

182182
void *thread_loop();
183+
int start_thread();
183184
void stop_thread();
184185
bool is_running() { return m_b_continue_running; };
185186

@@ -234,7 +235,6 @@ class event_handler_manager : public wakeup_pipe {
234235
* to the queue at that point.
235236
*/
236237
void process_remaining_registration_actions();
237-
int start_thread();
238238

239239
void event_channel_post_process_for_rdma_events(void *p_event);
240240
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
@@ -1045,6 +1045,7 @@ static void do_global_ctors_helper()
10451045
{
10461046
static lock_spin_recursive g_globals_lock;
10471047
std::lock_guard<decltype(g_globals_lock)> lock(g_globals_lock);
1048+
int rc;
10481049

10491050
if (!g_init_xlio_init_done || g_init_global_ctors_done) {
10501051
return;
@@ -1080,6 +1081,12 @@ static void do_global_ctors_helper()
10801081

10811082
// Create all global management objects
10821083
NEW_CTOR(g_p_event_handler_manager, event_handler_manager());
1084+
rc = g_p_event_handler_manager->start_thread();
1085+
if (rc != 0) {
1086+
BULLSEYE_EXCLUDE_BLOCK_START
1087+
throw_xlio_exception("Failed to start event handler thread.\n");
1088+
BULLSEYE_EXCLUDE_BLOCK_END
1089+
}
10831090

10841091
xlio_shmem_stats_open(&g_p_vlogger_level, &g_p_vlogger_details);
10851092
*g_p_vlogger_level = g_vlogger_level;

0 commit comments

Comments
 (0)