diff --git a/example/ndpiReader.c b/example/ndpiReader.c index e95989eba..ea3ac3713 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -6867,21 +6867,22 @@ void domainCacheTestUnit() { /* *********************************************** */ -void checkRankingUnitTest() { +void checkRankingUnitTest(bool do_trace) { ndpi_ranking rank; char path[64] = {0}; const u_int num = 3; ndpi_ranking_epoch_entry entries[4]; u_int i, j; ndpi_ranking_change curr_ranking[4], prev_ranking[4]; - u_int32_t now = (u_int32_t)time(NULL); - bool do_trace = false; + u_int32_t now = (u_int32_t)time(NULL), prev_epoch; u_int16_t num_changes; - + srand(now); - /* On GitHub Actions, ndpiReader might be called multiple times in parallel, so - every instance must use its own file */ + /* + On GitHub Actions, ndpiReader might be called multiple times in parallel, + so every instance must use its own file + */ snprintf(path, sizeof(path), "/tmp/ranking.%u.test", (unsigned int)getpid()); ndpi_init_ranking(&rank, num+1 /* max_num_items */, 8 /* num_epochs */); @@ -6892,10 +6893,12 @@ void checkRankingUnitTest() { assert(ndpi_deserialize_ranking(&rank, path) == true); for(j=0; j 0) { - printf("[loop %u] %u ranking changes at epoch %u\n", j+1, num_changes, now); + printf("[loop %u] %u ranking changes at epoch %u\n", + j+1, num_changes, now); } else printf("[loop %u] No ranking changes at epoch %u\n", j+1, now); } if(do_trace) ndpi_print_ranking(&rank); - + assert(num_changes == 0); now++; } @@ -6930,21 +6936,52 @@ void checkRankingUnitTest() { entries[num].value = 999; num_changes = ndpi_ranking_add_epoch(&rank, now, entries, num+1, - curr_ranking, prev_ranking); - + curr_ranking, prev_ranking, + &prev_epoch); + if(do_trace) { if(num_changes > 0) { - printf("[loop %u] %u ranking changes at epoch %u\n", j+1, num_changes, now); + printf("[loop %u] %u ranking changes at epoch %u\n", j+1, + num_changes, now); } else printf("[loop %u] No ranking changes at epoch %u\n", j+1, now); } if(do_trace) ndpi_print_ranking(&rank); - + assert(num_changes > 0); now++; - + + /* *** */ + + if(do_trace) { + printf("***** loop *****\n"); + } + + for(j=0; j<5; j++) { + for(i=0; i 0) { + printf("[loop %u] %u ranking changes at epoch %u\n", + j+1, num_changes, now); + } else + printf("[loop %u] No ranking changes at epoch %u\n", j+1, now); + } + + if(do_trace) + ndpi_print_ranking(&rank); + + assert(num_changes == 0); + now++; + } + ndpi_term_ranking(&rank); } @@ -6962,10 +6999,10 @@ int main(int argc, char **argv) { #endif #ifdef FORCE_RANKING_CHECK - checkRankingUnitTest(); + checkRankingUnitTest(true); exit(0); #endif - + #ifdef DEBUG_TRACE trace = fopen("/tmp/ndpiReader.log", "a"); @@ -6989,7 +7026,7 @@ int main(int argc, char **argv) { #ifndef DEBUG_TRACE /* Skip tests when debugging */ - checkRankingUnitTest(); + checkRankingUnitTest(false); #ifdef HW_TEST hwUnitTest2(); diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 7101cca01..eee8dad20 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -2425,7 +2425,8 @@ extern "C" { ndpi_ranking_epoch_entry *entries, u_int16_t num_epoch_entries, ndpi_ranking_change *curr_ranking,/* Out */ - ndpi_ranking_change *prev_ranking /* Out */); + ndpi_ranking_change *prev_ranking /* Out */, + u_int32_t *prev_ranking_epoch /* Out */); #ifdef __cplusplus } #endif diff --git a/src/lib/ndpi_analyze.c b/src/lib/ndpi_analyze.c index 1a44f866e..ea1bbd0f8 100644 --- a/src/lib/ndpi_analyze.c +++ b/src/lib/ndpi_analyze.c @@ -2209,9 +2209,11 @@ void ndpi_init_ranking(ndpi_ranking *rank, u_int16_t max_num_entries, u_int16_t rank->header.ranking_version = NDPI_RANKING_VERSION; rank->header.max_num_entries = max_num_entries; rank->header.num_epochs = (u_int16_t)ndpi_min(num_epochs, 1024); - rank->header.epochs_memory_len = rank->header.num_epochs * (sizeof(ndpi_ranking_header) + (rank->header.max_num_entries * sizeof(ndpi_ranking_epoch_entry))); + rank->header.epochs_memory_len = rank->header.num_epochs * + (sizeof(ndpi_ranking_header) + (rank->header.max_num_entries * sizeof(ndpi_ranking_epoch_entry))); rank->header.next_epoch_id = 0; rank->epochs = (char*)ndpi_calloc(1, rank->header.epochs_memory_len); + rank->num_updates_without_ranking_changes = 0; } /* *********************** */ @@ -2309,12 +2311,13 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, ndpi_ranking_epoch_entry *entries, u_int16_t num_epoch_entries, ndpi_ranking_change *curr_ranking, - ndpi_ranking_change *prev_ranking) { + ndpi_ranking_change *prev_ranking, + u_int32_t *prev_ranking_epoch) { u_int epoch_len, offset, i; ndpi_ranking_epoch *this_epoch, *prev_epoch; ndpi_ranking_epoch_entry *this_entries, *prev_entries; u_int16_t num_value_changed = 0; - u_int32_t el = num_epoch_entries * sizeof(ndpi_ranking_epoch_entry); + u_int32_t e_len = num_epoch_entries * sizeof(ndpi_ranking_epoch_entry); bool first_run = false; /* Avoid overflow */ @@ -2336,8 +2339,9 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, /* Prev epoch was filled up */ prev_entries = (ndpi_ranking_epoch_entry*)&(rank->epochs[offset+sizeof(prev_epoch->epoch)]); - memcpy(prev_ranking, prev_entries, el); - memcpy(curr_ranking, entries, el); + *prev_ranking_epoch = prev_epoch->epoch; + memcpy(prev_ranking, prev_entries, e_len); + memcpy(curr_ranking, entries, e_len); for(i=0; iheader.max_num_entries; i++) { if(entries[i].item_unique_id != prev_entries[i].item_unique_id) { @@ -2345,8 +2349,10 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, } } /* for */ } else { - memset(prev_ranking, 0, el); - memset(curr_ranking, 0, el); + *prev_ranking_epoch = 0; + memset(prev_ranking, 0, e_len); + memset(curr_ranking, 0, e_len); + first_run = true; } @@ -2357,7 +2363,7 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, memset(this_entries, 0, rank->header.max_num_entries * sizeof(ndpi_ranking_epoch_entry)); /* Then copy values */ - memcpy(this_entries, entries, el); + memcpy(this_entries, entries, e_len); /* Move to the next slot *only* if something has changed */ if(++rank->header.next_epoch_id == rank->header.num_epochs) rank->header.next_epoch_id = 0; @@ -2365,7 +2371,7 @@ u_int16_t ndpi_ranking_add_epoch(ndpi_ranking *rank, prev_epoch->epoch = epoch; rank->num_updates_without_ranking_changes++; memset(prev_entries, 0, rank->header.max_num_entries * sizeof(ndpi_ranking_epoch_entry)); - memcpy(prev_entries, entries, el); + memcpy(prev_entries, entries, e_len); } return(num_value_changed);