Skip to content

Commit 926685b

Browse files
committed
Support non-heartbeat processes
1 parent dd0b32a commit 926685b

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

src/apps.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,45 @@ int find_pid(int pid)
107107

108108
time_t get_heartbeat_time(int i)
109109
{
110-
time_t t = time(NULL);
111-
return t - apps[i].last_heartbeat;
110+
time_t now = time(NULL);
111+
return now - apps[i].last_heartbeat;
112112
}
113113

114114
bool is_timeup(int i)
115115
{
116-
bool ret = false;
117-
time_t t = time(NULL);
116+
const Application_t *app = &apps[i];
118117

119-
if(t < apps[i].last_heartbeat)
118+
if(!app->started)
120119
{
120+
return false; // App not running yet
121+
}
122+
123+
if(app->heartbeat_interval <= 0)
124+
{
125+
return false; // Heartbeat not expected for this app
126+
}
127+
128+
const time_t now = time(NULL);
129+
130+
if(now < app->last_heartbeat)
131+
{
132+
LOGW("Time anomaly detected for %s (system clock changed?)", app->name);
121133
update_heartbeat_time(i);
134+
return false; // Reset and give another interval
122135
}
123136

124-
if(t - apps[i].last_heartbeat >= (apps[i].first_heartbeat ? apps[i].heartbeat_interval : apps[i].heartbeat_delay))
137+
const time_t first_heartbeat_threshold = (time_t)MAX(app->heartbeat_interval, app->heartbeat_delay); // delay is designed to be larger than interval
138+
const time_t regular_threshold = (time_t)app->heartbeat_interval;
139+
const time_t threshold = app->first_heartbeat ? regular_threshold : first_heartbeat_threshold;
140+
const time_t elapsed = now - app->last_heartbeat;
141+
142+
if(elapsed >= threshold)
125143
{
126-
ret = true;
127-
LOGD("Heartbeat time up for %s", apps[i].name);
144+
LOGD("Heartbeat time up for %s", app->name);
145+
return true;
128146
}
129147

130-
return ret;
148+
return false;
131149
}
132150

133151
void set_first_heartbeat(int i)

src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ typedef uint64_t clk_t;
7373

7474
/* macros */
7575
#define UNUSED(x) (void)(x) /**< Macro to suppress compiler warnings about unused variables. */
76+
#define MAX(a,b) ((a) > (b) ? (a) : (b))
77+
#define MIN(a,b) ((a) < (b) ? (a) : (b))
7678

7779
/* functions */
7880

test_child.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
time.sleep(heartbeat_delay)
7878
wait_time = heartbeat_delay
7979

80+
if heartbeat_interval == 0:
81+
heartbeat_interval = 1000
82+
8083
# Set the running time of the program
8184
start_time = time.time()
8285

0 commit comments

Comments
 (0)