Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA-404658: Split heartbeat thread #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions daemon/bond_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <sys/syscall.h>


//
Expand Down Expand Up @@ -375,6 +376,7 @@ bm(
static MTC_BOND_STATUS bond_status = BOND_STATUS_NOERR;
PCOM_DATA_BM pbm;

log_message(MTC_LOG_INFO, "BM: thread ID: %ld.\n", syscall(SYS_gettid));
do
{
log_maskable_debug_message(TRACE, "BM: bonding monitor thread activity log.\n");
Expand Down
122 changes: 82 additions & 40 deletions daemon/heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/syscall.h>


//
Expand Down Expand Up @@ -131,6 +132,13 @@

#define HB_SIG 'hahx'

#define mssleep(X) \
{ \
struct timespec sleep_ts = mstots(X), ts_rem; \
ts_rem = sleep_ts; \
while (nanosleep(&sleep_ts, &ts_rem)) sleep_ts = ts_rem; \
}

typedef struct _HB_PACKET {
MTC_U32 signature; // 4 bytes
MTC_U32 checksum; // 4 bytes
Expand Down Expand Up @@ -228,7 +236,11 @@
hb_cleanup_objects();

MTC_STATIC void *
hb(
hb_send(
void *ignore);

MTC_STATIC void *
hb_receive(
void *ignore);

MTC_STATIC MTC_BOOLEAN
Expand Down Expand Up @@ -289,7 +301,8 @@
hb_initialize(
MTC_S32 phase)
{
static pthread_t hb_thread = 0;
static pthread_t hb_receive_thread = 0;
static pthread_t hb_send_thread = 0;
MTC_S32 ret = MTC_SUCCESS;

assert(-1 <= phase && phase <= 1);
Expand Down Expand Up @@ -321,9 +334,18 @@
goto error;
}

// start heartbeat thread
hbvar.terminate = FALSE;
ret = pthread_create(&hb_thread, xhad_pthread_attr, hb, NULL);

// start heartbeat receiving thread
ret = pthread_create(&hb_receive_thread, xhad_pthread_attr, hb_receive, NULL);
if (ret)
{
ret = MTC_ERROR_HB_PTHREAD;
goto error;
}

// start heartbeat sending thread
ret = pthread_create(&hb_send_thread, xhad_pthread_attr, hb_send, NULL);
if (ret)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do anything here to cleanup the receiving thread if we fail to start the sending thread?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. Currently, if heartbeat threads fails to create, the return status of hb_initialize is non-0, then hb_cleanup_objects is called, but it doesn't do anything (code in it will not be compiled).

{
ret = MTC_ERROR_HB_PTHREAD;
Expand All @@ -338,21 +360,6 @@
default:
log_message(MTC_LOG_INFO, "HB: hb_initialize(-1).\n");

if (hb_thread)
{
#if 0
hb_spin_lock();
hbvar.terminate = TRUE;
hb_spin_unlock();

// wait for thread termination
if ((ret = pthread_join(hb_thread, NULL)))
{
pthread_kill(hb_thread, SIGKILL);
}
#endif
}

if (hbvar.watchdog != INVALID_WATCHDOG_HANDLE_VALUE)
{
watchdog_close(hbvar.watchdog);
Expand Down Expand Up @@ -732,11 +739,11 @@
//
// NAME:
//
// hb
// hb_receive
//
// DESCRIPTION:
//
// The heartbeat main thread.
// The heartbeat receiving thread.
//
// FORMAL PARAMETERS:
//
Expand All @@ -749,17 +756,16 @@
//

MTC_STATIC void *
hb(
hb_receive(
void *ignore)
{
MTC_BOOLEAN term = FALSE;
MTC_CLOCK last, now;

log_message(MTC_LOG_INFO, "HB_receive: thread ID: %ld.\n", syscall(SYS_gettid));
now = last = _getms();
do
{
log_maskable_debug_message(TRACE, "HB: heartbeat thread activity log.\n");

if (update_hbdomain())
{
start_fh(FALSE);
Expand All @@ -775,26 +781,72 @@
FD_SET(hbvar.socket, &fds);
nfds = _max(nfds, hbvar.socket);

wait = mstotv((_t1 * ONE_SEC - (now - last) < 0)? 0: _t1 * ONE_SEC - (now - last));
wait = mstotv(_max(_t1 * ONE_SEC - (now - last), 0));
if (select(nfds + 1, &fds, NULL, NULL, &wait) > 0)
{
receive_hb();
}
}

// time to send haertbeat?
// time to next cycle
now = _getms();
if (now - last < _t1 * ONE_SEC)
{
// no, still have to wait.
continue;
// note: the variable 'now' is used in next cycle
}
// yes, it's time to send heartbeat

last = now;

hb_spin_lock();
// Refresh watchdog counter to Wh
if (hbvar.watchdog != INVALID_WATCHDOG_HANDLE_VALUE)
{
watchdog_set(hbvar.watchdog, _Wh);
}
term = hbvar.terminate;
hb_spin_unlock();

// note: the variable 'now' is used in next cycle
} while (!term);

return NULL;
}


//
// NAME:
//
// hb_send
//
// DESCRIPTION:
//
// The heartbeat sending thread.
//
// FORMAL PARAMETERS:
//
//
// RETURN VALUE:
//
//
// ENVIRONMENT:
//
//

MTC_STATIC void *
hb_send(
void *ignore)

Check warning

Code scanning / CodeChecker

unused parameter 'ignore' Warning

unused parameter 'ignore'
{
MTC_CLOCK last, now;

log_message(MTC_LOG_INFO, "HB_send: thread ID: %ld.\n", syscall(SYS_gettid));
do
{
// check fist
hb_check_fist_sticky();

last = _getms();

// send heartbeat
send_hb();

Expand All @@ -814,19 +866,9 @@
com_writer_unlock(hb_object);
}

last = now;
mssleep(_max(_t1 * ONE_SEC - (now - last), 0));

hb_spin_lock();
// Refresh watchdog counter to Wh
if (hbvar.watchdog != INVALID_WATCHDOG_HANDLE_VALUE)
{
watchdog_set(hbvar.watchdog, _Wh);
}
term = hbvar.terminate;
hb_spin_unlock();

// note: the variable 'now' is used in next cycle
} while (!term);
} while (!hbvar.terminate);

return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions daemon/lock_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <sys/syscall.h>


//
Expand Down Expand Up @@ -592,6 +593,7 @@ lock_mgr(
PCOM_DATA_SF psf;
MTC_S32 index;

log_message(MTC_LOG_INFO, "LM: thread ID: %ld.\n", syscall(SYS_gettid));
while (TRUE)
{
// wait until state-file is updated or request status is changed
Expand Down
2 changes: 2 additions & 0 deletions daemon/sc_sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <pthread.h>
#include <signal.h>
#include <assert.h>
#include <sys/syscall.h>



Expand Down Expand Up @@ -305,6 +306,7 @@ script_service_thread(
service_func_num = ((SCRIPT_SERVICE_THREAD_PARAM *) param)->funcnum;
service_func = ((SCRIPT_SERVICE_THREAD_PARAM *) param)->func;

log_message(MTC_LOG_INFO, "SC: thread ID: %ld.\n", syscall(SYS_gettid));
do
{
fd_set fds;
Expand Down
3 changes: 3 additions & 0 deletions daemon/sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/syscall.h>



Expand Down Expand Up @@ -715,6 +716,7 @@ sm(
MTC_STATUS status;
MTC_U32 weight;

log_message(MTC_LOG_INFO, "SM: thread ID: %ld.\n", syscall(SYS_gettid));
// commit initial weight
weight = commit_weight();
log_message(MTC_LOG_INFO, "Initial weight = %d.\n", weight);
Expand Down Expand Up @@ -1654,6 +1656,7 @@ MTC_STATIC void *
sm_worker(
void *ignore)
{
log_message(MTC_LOG_INFO, "SM_Worker: thread ID: %ld.\n", syscall(SYS_gettid));
while (!smvar.terminate)
{
mssleep(SM_WORKER_INTERVAL);
Expand Down
2 changes: 2 additions & 0 deletions daemon/statefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/syscall.h>

//
//
Expand Down Expand Up @@ -502,6 +503,7 @@ sfthread(
#define PSTATUS_SUCCESS 1
#define PSTATUS_ERROR 2

log_message(MTC_LOG_INFO, "SF: thread ID: %ld.\n", syscall(SYS_gettid));
int print_status = PSTATUS_NONE;

last = _getms();
Expand Down
2 changes: 2 additions & 0 deletions daemon/xapi_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/syscall.h>


//
Expand Down Expand Up @@ -300,6 +301,7 @@ xapimon(
PMTC_S8 perr_string = err_string;
MTC_STATUS status;

log_message(MTC_LOG_INFO, "Xapimon: thread ID: %ld.\n", syscall(SYS_gettid));
while (!terminate)
{
log_maskable_debug_message(TRACE, "Xapimon: Xapi monitor thread activity log.\n");
Expand Down
Loading