Skip to content

Commit

Permalink
avoid disk writes when SDR is failing
Browse files Browse the repository at this point in the history
don't save state to disk on exit if initializing the SDR has failed
this avoids writing to disk a lot if the program is restarting over and
over when the SDR is not available
  • Loading branch information
wiedehopf committed May 15, 2024
1 parent 75eddd7 commit 2dd5272
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
26 changes: 16 additions & 10 deletions readsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ void priorityTasksRun() {
antiSpam = mono;
}

//fprintf(stderr, "running for %ld ms\n", mstime() - Modes.startup_time);
//fprintf(stderr, "removeStale took %"PRIu64" ms, running for %ld ms\n", elapsed, now - Modes.startup_time);
//fprintf(stderr, "running for %ld ms\n", getUptime());
//fprintf(stderr, "removeStale took %"PRIu64" ms, running for %ld ms\n", elapsed, getUptime());

if (Modes.updateStats) {
Modes.currentTask = "statsReset";
Expand Down Expand Up @@ -497,6 +497,7 @@ static void *readerEntryPoint(void *arg) {
srandom(get_seed());

if (!sdrOpen()) {
Modes.sdrOpenFailed = 1;
setExit(2); // unexpected exit
log_with_timestamp("sdrOpen() failed, exiting!");
return NULL;
Expand Down Expand Up @@ -812,7 +813,7 @@ static void *decodeEntryPoint(void *arg) {
* This rules also in case a local Mode-S Beast is connected via USB.
*/

//fprintf(stderr, "startup complete after %.3f seconds.\n", (mstime() - Modes.startup_time) / 1000.0);
//fprintf(stderr, "startup complete after %.3f seconds.\n", getUptime() / 1000.0);

interactiveInit();

Expand Down Expand Up @@ -1045,7 +1046,7 @@ static void writeTraces(int64_t mono) {
Modes.triggerPastDayTraceWrite = 1; // activated for one sweep
}

if (elapsed > 30 * SECONDS && mstime() - Modes.startup_time > 10 * MINUTES) {
if (elapsed > 30 * SECONDS && getUptime() > 10 * MINUTES) {
fprintf(stderr, "trace writing iteration took %.1f seconds (roughly %.0f minutes), live traces will lag behind (historic traces are fine), "
"consider alloting more CPU cores or increasing json-trace-interval!\n",
elapsed / 1000.0, elapsed / (double) MINUTES);
Expand Down Expand Up @@ -2647,7 +2648,7 @@ int main(int argc, char **argv) {
// Set sane defaults
configSetDefaults();

Modes.startup_time = mstime();
Modes.startup_time = mono_milli_seconds();

if (lzo_init() != LZO_E_OK)
{
Expand Down Expand Up @@ -2808,8 +2809,7 @@ int main(int argc, char **argv) {
while (!Modes.exit) {
int64_t wait_time = 5 * SECONDS;
if (Modes.auto_exit) {
int64_t now = mstime();
int64_t uptime = now - Modes.startup_time;
int64_t uptime = getUptime();
if (uptime + wait_time >= Modes.auto_exit) {
wait_time = imax(1, Modes.auto_exit - uptime);
}
Expand Down Expand Up @@ -2933,16 +2933,22 @@ int main(int argc, char **argv) {
destroy_task_group(Modes.traceTasks);
}

if (Modes.state_dir && Modes.sdrOpenFailed) {
fprintf(stderr, "not saving state: SDR failed\n");
sfree(Modes.state_dir);
Modes.state_dir = NULL;
}

Modes.free_aircraft = 1;
// frees aircraft when Modes.free_aircraft is set
// writes state if Modes.state_dir is set
Modes.free_aircraft = 1;
writeInternalState();

if (Modes.exit != 1) {
log_with_timestamp("Abnormal exit.");
log_with_timestamp("Abnormal exit. (runtime: %.3f s", getUptime() / (double) SECONDS);
cleanup_and_exit(1);
}

log_with_timestamp("Normal exit.");
log_with_timestamp("Normal exit. (runtime: %.3f s", getUptime() / (double) SECONDS);
cleanup_and_exit(0);
}
5 changes: 3 additions & 2 deletions readsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,10 @@ struct _Modes
input_format_t input_format; // --iformat option
iq_convert_fn converter_function;
char * dev_name;
int gain;
int sdrInitialized;
pthread_mutex_t sdrControlMutex;
int8_t sdrInitialized;
int8_t sdrOpenFailed;
int gain;
int dc_filter; // should we apply a DC filter?
int enable_agc;
sdr_type_t sdr_type; // where are we getting data from?
Expand Down
4 changes: 4 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ int64_t mono_milli_seconds() {
return milli;
}

int64_t getUptime() {
return mono_milli_seconds() - Modes.startup_time;
}

int snprintHMS(char *buf, size_t bufsize, int64_t now) {
time_t nowTime = nearbyint(now / 1000.0);
struct tm local;
Expand Down
1 change: 1 addition & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ int64_t microtime(void);
void milli_micro_seconds(int64_t *milli, int64_t *micro);
int64_t mono_micro_seconds();
int64_t mono_milli_seconds();
int64_t getUptime();

int snprintHMS(char *buf, size_t bufsize, int64_t now);

Expand Down

0 comments on commit 2dd5272

Please sign in to comment.