Skip to content

Commit 63c222a

Browse files
authored
Merge pull request #55 from kristrev/ft-improve-tstamp
Remove epoch assumption
2 parents 2076e2e + 5fcfc91 commit 63c222a

File tree

3 files changed

+82
-55
lines changed

3 files changed

+82
-55
lines changed

metadata_exporter.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ static struct json_object *create_fake_restart_obj()
262262
return obj;
263263
}
264264

265-
#if 0
266265
static struct json_object *create_fake_conn_obj(uint64_t l3_id, uint64_t l4_id,
267266
uint8_t event_param, char *event_value_str, uint64_t tstamp)
268267
{
@@ -412,7 +411,6 @@ static struct json_object *create_fake_conn_obj(uint64_t l3_id, uint64_t l4_id,
412411

413412
return obj;
414413
}
415-
#endif
416414

417415
static ssize_t send_netlink_json(uint8_t *snd_buf,
418416
struct json_object *parsed_obj, int32_t sock_fd,
@@ -592,17 +590,17 @@ static void test_netlink(uint32_t packets)
592590
while(1) {
593591
gettimeofday(&tv, NULL);
594592

595-
/*if (i == 0)
593+
if (i == 0)
596594
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,", i+1);
597595
else
598-
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,4", i+1);*/
596+
obj_to_send = create_fake_conn_obj(0, 0, CONN_EVENT_META_UPDATE, "1,2,1,4", i+1);
599597

600-
/*if (i < 4)
598+
if (i < 4)
601599
obj_to_send = create_fake_conn_obj(1, 2, CONN_EVENT_L3_UP, "1,2,1", i+1);
602600
else
603-
obj_to_send = create_fake_conn_obj(1, 2, CONN_EVENT_DATA_USAGE_UPDATE, "1,2,1,4", tv.tv_sec);*/
601+
obj_to_send = create_fake_conn_obj(1, 2, CONN_EVENT_DATA_USAGE_UPDATE, "1,2,1,4", tv.tv_sec);
604602

605-
obj_to_send = create_fake_restart_obj();
603+
//obj_to_send = create_fake_restart_obj();
606604

607605
if (!obj_to_send)
608606
continue;

metadata_writer_sqlite.c

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void md_sqlite_copy_db(struct md_writer_sqlite *mws, uint8_t from_timeout
7171
(mws->session_id_file && !mws->session_id))
7272
{
7373
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Can't export DB. # node_id %d "
74-
"# valid_timestamp %u # session_id_file %s # session_id %d\n",
74+
"# valid_timestamp %u # session_id_file %s # session_id %lu\n",
7575
mws->node_id,
7676
mws->valid_timestamp,
7777
mws->session_id_file ? mws->session_id_file : "EMPTY",
@@ -163,7 +163,7 @@ static uint8_t md_sqlite_update_nodeid_db(struct md_writer_sqlite *mws, const ch
163163
}
164164

165165
static uint8_t md_sqlite_update_timestamp_db(struct md_writer_sqlite *mws,
166-
const char *sql_str, uint64_t tstamp_offset)
166+
const char *sql_str, uint64_t orig_boot_time, uint64_t real_boot_time)
167167
{
168168
int32_t retval;
169169
sqlite3_stmt *update_timestamp;
@@ -174,12 +174,17 @@ static uint8_t md_sqlite_update_timestamp_db(struct md_writer_sqlite *mws,
174174
return RETVAL_FAILURE;
175175
}
176176

177-
if ((retval = sqlite3_bind_int64(update_timestamp, 1, tstamp_offset))) {
177+
if ((retval = sqlite3_bind_int64(update_timestamp, 1, orig_boot_time))) {
178178
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Bind failed %s\n", sqlite3_errstr(retval));
179179
return RETVAL_FAILURE;
180180
}
181181

182-
if ((retval = sqlite3_bind_int64(update_timestamp, 2, FIRST_VALID_TIMESTAMP))) {
182+
if ((retval = sqlite3_bind_int64(update_timestamp, 2, real_boot_time))) {
183+
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Bind failed %s\n", sqlite3_errstr(retval));
184+
return RETVAL_FAILURE;
185+
}
186+
187+
if ((retval = sqlite3_bind_int64(update_timestamp, 3, real_boot_time))) {
183188
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Bind failed %s\n", sqlite3_errstr(retval));
184189
return RETVAL_FAILURE;
185190
}
@@ -291,11 +296,32 @@ static sqlite3* md_sqlite_configure_db(struct md_writer_sqlite *mws, const char
291296
return db_handle;
292297
}
293298

299+
static int md_sqlite_read_boot_time(struct md_writer_sqlite *mws, uint64_t *boot_time)
300+
{
301+
struct timeval tv;
302+
uint64_t uptime;
303+
304+
//read uptime
305+
if (system_helpers_read_uint64_from_file("/proc/uptime", &uptime)) {
306+
return RETVAL_FAILURE;
307+
}
308+
309+
gettimeofday(&tv, NULL);
310+
311+
*boot_time = tv.tv_sec - uptime;
312+
313+
if (*boot_time < mws->orig_boot_time) {
314+
return RETVAL_FAILURE;
315+
}
316+
317+
return RETVAL_SUCCESS;
318+
}
319+
294320
static int md_sqlite_configure(struct md_writer_sqlite *mws,
295321
const char *db_filename, uint32_t node_id, uint32_t db_interval,
296322
uint32_t db_events, const char *meta_prefix, const char *gps_prefix,
297323
const char *monitor_prefix, const char *usage_prefix,
298-
const char *system_prefix)
324+
const char *system_prefix, const char *ntp_fix_file)
299325
{
300326
sqlite3 *db_handle = md_sqlite_configure_db(mws, db_filename);
301327
const char *dump_events, *dump_updates, *dump_gps, *dump_monitor,
@@ -439,6 +465,11 @@ static int md_sqlite_configure(struct md_writer_sqlite *mws,
439465
mws->system_prefix_len = strlen(system_prefix);
440466
}
441467

468+
if (ntp_fix_file) {
469+
memset(mws->ntp_fix_file, 0, sizeof(mws->ntp_fix_file));
470+
memcpy(mws->ntp_fix_file, ntp_fix_file, strlen(ntp_fix_file));
471+
}
472+
442473
if (mws->node_id && (md_sqlite_update_nodeid_db(mws, UPDATE_EVENT_ID) ||
443474
md_sqlite_update_nodeid_db(mws, UPDATE_UPDATES_ID) ||
444475
md_sqlite_update_nodeid_db(mws, UPDATE_SYSTEM_ID))) {
@@ -454,7 +485,7 @@ static int md_sqlite_configure(struct md_writer_sqlite *mws,
454485
system_helpers_read_uint64_from_file(mws->last_conn_tstamp_path,
455486
&(mws->dump_tstamp));
456487

457-
return RETVAL_SUCCESS;
488+
return md_sqlite_read_boot_time(mws, &(mws->orig_boot_time));
458489
}
459490

460491
void md_sqlite_usage()
@@ -474,6 +505,7 @@ void md_sqlite_usage()
474505
fprintf(stderr, " \"api_version\":\tbackend API version (default: 1)\n");
475506
fprintf(stderr, " \"last_conn_tstamp_path\":\toptional path to file where we read/store timestamp of last conn dump\n");
476507
fprintf(stderr, " \"output_format\":\tJSON/SQL (default SQL)\n");
508+
fprintf(stderr, " \"ntp_fix_file\":\tFile to check for NTP fix\n");
477509
fprintf(stderr, "}\n");
478510
}
479511

@@ -483,7 +515,7 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
483515
uint32_t node_id = 0, interval = DEFAULT_TIMEOUT, num_events = EVENT_LIMIT;
484516
const char *db_filename = NULL, *meta_prefix = NULL, *gps_prefix = NULL,
485517
*monitor_prefix = NULL, *usage_prefix = NULL,
486-
*output_format = NULL, *system_prefix = NULL;
518+
*output_format = NULL, *system_prefix = NULL, *ntp_fix_file = NULL;
487519

488520
json_object* subconfig;
489521
if (json_object_object_get_ex(config, "sqlite", &subconfig)) {
@@ -515,7 +547,9 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
515547
else if (!strcmp(key, "last_conn_tstamp_path"))
516548
mws->last_conn_tstamp_path = strdup(json_object_get_string(val));
517549
else if (!strcmp(key, "output_format"))
518-
output_format = json_object_get_string(val);
550+
output_format = json_object_get_string(val);
551+
else if (!strcmp(key, "ntp_fix_file"))
552+
ntp_fix_file = json_object_get_string(val);
519553
}
520554
}
521555

@@ -528,7 +562,8 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
528562
(gps_prefix && strlen(gps_prefix) > 117) ||
529563
(monitor_prefix && strlen(monitor_prefix) > 117) ||
530564
(usage_prefix && strlen(usage_prefix) > 117) ||
531-
(system_prefix && strlen(system_prefix) > 117)) {
565+
(system_prefix && strlen(system_prefix) > 117) ||
566+
(ntp_fix_file && strlen(ntp_fix_file) > 127)) {
532567
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "SQLite temp file prefix too long\n");
533568
return RETVAL_FAILURE;
534569
}
@@ -563,31 +598,31 @@ int32_t md_sqlite_init(void *ptr, json_object* config)
563598

564599
return md_sqlite_configure(mws, db_filename, node_id, interval,
565600
num_events, meta_prefix, gps_prefix, monitor_prefix, usage_prefix,
566-
system_prefix);
601+
system_prefix, ntp_fix_file);
567602
}
568603

569604
static uint8_t md_sqlite_check_valid_tstamp(struct md_writer_sqlite *mws)
570605
{
571606
struct timeval tv;
572-
uint64_t real_boot_time, uptime;
607+
uint64_t real_boot_time;
608+
573609
gettimeofday(&tv, NULL);
574610

575-
//We have yet to get proper timestamp, so do not export any events
576-
if (tv.tv_sec < FIRST_VALID_TIMESTAMP)
611+
if (mws->ntp_fix_file[0] && access(mws->ntp_fix_file, F_OK))
577612
return RETVAL_FAILURE;
578613

579-
//read uptime
580-
if (system_helpers_read_uint64_from_file("/proc/uptime", &uptime))
614+
if (md_sqlite_read_boot_time(mws, &real_boot_time)) {
581615
return RETVAL_FAILURE;
616+
}
582617

583-
real_boot_time = tv.tv_sec - uptime;
618+
META_PRINT_SYSLOG(mws->parent, LOG_INFO, "Real boot %" PRIu64 " orig boot %" PRIu64 "\n", real_boot_time, mws->orig_boot_time);
584619

585620
if (md_sqlite_update_timestamp_db(mws, UPDATE_EVENT_TSTAMP,
586-
real_boot_time) ||
621+
mws->orig_boot_time, real_boot_time) ||
587622
md_sqlite_update_timestamp_db(mws, UPDATE_UPDATES_TSTAMP,
588-
real_boot_time) ||
623+
mws->orig_boot_time, real_boot_time) ||
589624
md_sqlite_update_timestamp_db(mws, UPDATE_SYSTEM_TSTAMP,
590-
real_boot_time)) {
625+
mws->orig_boot_time, real_boot_time)) {
591626
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Could not update tstamp in database\n");
592627
return RETVAL_FAILURE;
593628
}
@@ -680,7 +715,7 @@ static void md_sqlite_handle(struct md_writer *writer, struct md_event *event)
680715

681716
//We have received an indication that a valid timestamp is present, so
682717
//check and update
683-
if (!mws->valid_timestamp && event->tstamp > FIRST_VALID_TIMESTAMP) {
718+
if (!mws->valid_timestamp) {
684719
if (md_sqlite_check_valid_tstamp(mws)) {
685720
printf("Invalid timestamp\n");
686721
return;

metadata_writer_sqlite.h

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@
3636
#define MAX_PATH_LEN 128
3737
#define FAKE_UPDATE_LIMIT 120
3838

39-
//This the first valid timestamp of an event and the value is not randomly
40-
//chosen, it is the time when this piece of code was written. And
41-
//since time is never supposed to move backwards ... Note that this check
42-
//assumes that all nodes will have some offset time lower than this, and
43-
//then ntp (or something else) will set a correct time. A good starting
44-
//point is epoch
45-
#define FIRST_VALID_TIMESTAMP 1455740094
46-
4739
#define CREATE_SQL "CREATE TABLE IF NOT EXISTS NetworkEvent(" \
4840
"NodeId INTEGER NOT NULL," \
4941
"SessionId INTEGER NOT NULL," \
@@ -206,15 +198,15 @@
206198
"WHERE NodeId=0"
207199

208200
#define UPDATE_EVENT_TSTAMP "UPDATE NetworkEvent SET " \
209-
"Timestamp = Timestamp + ? "\
201+
"Timestamp = (Timestamp - ?) + ? "\
210202
"WHERE Timestamp < ?"
211203

212204
#define UPDATE_UPDATES_TSTAMP "UPDATE NetworkUpdates SET " \
213-
"Timestamp = Timestamp + ? "\
205+
"Timestamp = (Timestamp - ?) + ? "\
214206
"WHERE Timestamp < ?"
215207

216208
#define UPDATE_SYSTEM_TSTAMP "UPDATE RebootEvent SET " \
217-
"Timestamp = Timestamp + ? "\
209+
"Timestamp = (Timestamp - ?) + ? "\
218210
"WHERE Timestamp < ?"
219211

220212
#define UPDATE_EVENT_SESSION_ID "UPDATE NetworkEvent SET "\
@@ -325,6 +317,22 @@ struct backend_timeout_handle;
325317
struct md_writer_sqlite {
326318
MD_WRITER;
327319

320+
uint64_t dump_tstamp;
321+
uint64_t last_msg_tstamp;
322+
uint64_t last_gps_insert;
323+
uint64_t orig_boot_time;
324+
325+
//TODO: Consider moving this to the generic writer struct if need be
326+
//These values keep track of the unique session id (and multiplier), which
327+
//are normally assumed to be the boot counter (+ multiplier)
328+
uint64_t session_id;
329+
uint64_t session_id_multip;
330+
331+
float gps_speed;
332+
333+
size_t meta_prefix_len, gps_prefix_len, monitor_prefix_len,
334+
usage_prefix_len, system_prefix_len;
335+
328336
sqlite3 *db_handle;
329337

330338
sqlite3_stmt *insert_event, *insert_update;
@@ -342,6 +350,8 @@ struct md_writer_sqlite {
342350
char *session_id_file;
343351
char *node_id_file;
344352
const char *last_conn_tstamp_path;
353+
struct backend_timeout_handle *timeout_handle;
354+
struct timeval first_fake_update;
345355

346356
uint32_t node_id;
347357
uint32_t db_interval;
@@ -356,25 +366,9 @@ struct md_writer_sqlite {
356366
uint8_t file_failed;
357367
uint8_t do_fake_updates;
358368
uint8_t valid_timestamp;
359-
struct timeval first_fake_update;
360369

361-
uint64_t dump_tstamp;
362-
uint64_t last_msg_tstamp;
363-
uint64_t last_gps_insert;
364-
365-
//TODO: Consider moving this to the generic writer struct if need be
366-
//These values keep track of the unique session id (and multiplier), which
367-
//are normally assumed to be the boot counter (+ multiplier)
368-
uint64_t session_id;
369-
uint64_t session_id_multip;
370-
371-
float gps_speed;
372-
373-
struct backend_timeout_handle *timeout_handle;
374370
char meta_prefix[128], gps_prefix[128], monitor_prefix[128],
375-
usage_prefix[128], system_prefix[128];
376-
size_t meta_prefix_len, gps_prefix_len, monitor_prefix_len,
377-
usage_prefix_len, system_prefix_len;
371+
usage_prefix[128], system_prefix[128], ntp_fix_file[128];
378372

379373
uint8_t api_version;
380374
uint8_t delete_conn_update;

0 commit comments

Comments
 (0)