Skip to content

Commit

Permalink
Merge branch 'master' into private/edvint/warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
GeraldEV authored Mar 5, 2025
2 parents d7919a1 + 93b821c commit dbbe629
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 289 deletions.
3 changes: 2 additions & 1 deletion commands/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "mtctypes.h"
#include "mtcerrno.h"
#include "log.h"

void
log_message(
Expand All @@ -51,7 +52,7 @@ log_message(
va_list ap;

va_start(ap, fmt);
fprintf(stderr, fmt, ap);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);

fflush(stderr);
Expand Down
1 change: 1 addition & 0 deletions daemon/bond_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ bm(
static MTC_BOND_STATUS bond_status = BOND_STATUS_NOERR;
PCOM_DATA_BM pbm;

log_thread_id("BM");
do
{
log_maskable_debug_message(TRACE, "BM: bonding monitor thread activity log.\n");
Expand Down
11 changes: 6 additions & 5 deletions daemon/com.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>

#include "mtctypes.h"
#include "mtcerrno.h"
Expand Down Expand Up @@ -269,7 +270,7 @@ MTC_STATIC HA_COMMON_OBJECT_HANDLE_INTERNAL
new_handle = malloc(sizeof(HA_COMMON_OBJECT_HANDLE_INTERNAL));
if (new_handle == NULL)
{
log_internal(MTC_LOG_ERR, "COM: cannot allocate for object_handle (size=%d).\n", sizeof(HA_COMMON_OBJECT_HANDLE_INTERNAL));
log_internal(MTC_LOG_ERR, "COM: cannot allocate for object_handle (size=%zu).\n", sizeof(HA_COMMON_OBJECT_HANDLE_INTERNAL));
return NULL;
}
new_handle->object = object;
Expand Down Expand Up @@ -307,7 +308,7 @@ new_object(
new = malloc(sizeof(HA_COMMON_OBJECT));
if (new == NULL)
{
log_internal(MTC_LOG_ERR, "COM: cannot allocate for object (size=%d).\n", sizeof(HA_COMMON_OBJECT));
log_internal(MTC_LOG_ERR, "COM: cannot allocate for object (size=%zu).\n", sizeof(HA_COMMON_OBJECT));
return NULL;
}
new->next = NULL;
Expand Down Expand Up @@ -452,7 +453,7 @@ MTC_STATIC HA_COMMON_OBJECT_CALLBACK_LIST_ITEM
new = malloc(sizeof(HA_COMMON_OBJECT_CALLBACK_LIST_ITEM));
if (new == NULL)
{
log_internal(MTC_LOG_ERR, "COM: cannot allocate for callback (size=%d).\n", sizeof(HA_COMMON_OBJECT_CALLBACK_LIST_ITEM));
log_internal(MTC_LOG_ERR, "COM: cannot allocate for callback (size=%zu).\n", sizeof(HA_COMMON_OBJECT_CALLBACK_LIST_ITEM));
return NULL;
}
new->next = NULL;
Expand Down Expand Up @@ -573,7 +574,7 @@ set_thread_id_record(
return;
}
}
log_message(MTC_LOG_WARNING, "COM: thread_id %d not found in thraed_id_record_table.\n", self);
log_message(MTC_LOG_WARNING, "COM: thread_id %lu not found in thread_id_record_table.\n", self);
break;
}
assert(FALSE);
Expand Down Expand Up @@ -619,7 +620,7 @@ com_log_all_objects(
{
if (object->thread_id_record_table[tid_index].lock_state != LOCK_STATE_NONE)
{
log_message(MTC_LOG_DEBUG, "COM: lock_state=%d thread_id=%x changed_time=%d(ms) .\n",
log_message(MTC_LOG_DEBUG, "COM: lock_state=%d thread_id=0x%lx changed_time=%"PRId64"(ms) .\n",
object->thread_id_record_table[tid_index].lock_state,
object->thread_id_record_table[tid_index].thread_id,
now - object->thread_id_record_table[tid_index].changed_time
Expand Down
131 changes: 100 additions & 31 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 <inttypes.h>


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

#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 @@ MTC_STATIC void
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 @@ MTC_S32
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 @@ hb_initialize(
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)
{
ret = MTC_ERROR_HB_PTHREAD;
Expand All @@ -338,19 +360,26 @@ hb_initialize(
default:
log_message(MTC_LOG_INFO, "HB: hb_initialize(-1).\n");

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

if (hb_receive_thread)
{
#if 0
hb_spin_lock();
hbvar.terminate = TRUE;
hb_spin_unlock();
// wait for receive thread termination
if ((ret = pthread_join(hb_receive_thread, NULL)))
{
pthread_kill(hb_receive_thread, SIGKILL);
}
}

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

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

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

log_thread_id("HB_receive");
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 +803,73 @@ hb(
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)
{
MTC_BOOLEAN term = FALSE;
MTC_CLOCK last, now;

log_thread_id("HB_send");
do
{
// check fist
hb_check_fist_sticky();

last = _getms();

// send heartbeat
send_hb();

Expand All @@ -814,18 +889,12 @@ hb(
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);

return NULL;
Expand Down Expand Up @@ -1454,7 +1523,7 @@ hb_check_fist(
if (target_delay != 0)
{
log_message(MTC_LOG_DEBUG,
"HB(FIST): heartbeat delay is %d ms\n", target_delay);
"HB(FIST): heartbeat delay is %"PRId64" ms\n", target_delay);

ts = ts_rem = mstots(target_delay);
while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
Expand Down Expand Up @@ -1485,7 +1554,7 @@ hb_check_fist_sticky()
if (target_delay != 0)
{
log_message(MTC_LOG_DEBUG,
"HB(FIST): heartbeat delay is %d ms\n", target_delay);
"HB(FIST): heartbeat delay is %"PRId64" ms\n", target_delay);

ts = mstots(target_delay);
while (nanosleep(&ts, &ts_rem)) ts = ts_rem;
Expand Down
1 change: 1 addition & 0 deletions daemon/lock_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ lock_mgr(
PCOM_DATA_SF psf;
MTC_S32 index;

log_thread_id("LM");
while (TRUE)
{
// wait until state-file is updated or request status is changed
Expand Down
Loading

0 comments on commit dbbe629

Please sign in to comment.