Patricia tree, Ahocarasick automa, LRU cache: add statistics (#1683)

Add (basic) internal stats to the main data structures used by the
library; they might be usefull to check how effective these structures
are.

Add an option to `ndpiReader` to dump them; enabled by default in the
unit tests.
This new option enables/disables dumping of "num dissectors calls"
values, too (see b4cb14ec).
This commit is contained in:
Ivan Nardi
2022-07-29 15:25:00 +02:00
committed by GitHub
parent df2e11ef51
commit 405a52ed65
369 changed files with 5773 additions and 18 deletions
+7
View File
@@ -212,6 +212,11 @@ struct ac_path {
unsigned short int idx,l;
};
struct ac_stats {
uint64_t n_search;
uint64_t n_found;
};
typedef struct
{
/* The root of the Aho-Corasick trie */
@@ -236,6 +241,7 @@ typedef struct
int add_to_range; /* for convert to range */
int n_oc,n_range,n_find; /* statistics */
char name[32]; /* if debug != 0 */
struct ac_stats stats;
} AC_AUTOMATA_t;
typedef AC_ERROR_t (*NODE_CALLBACK_f)(AC_AUTOMATA_t *, AC_NODE_t *,int idx, void *);
@@ -266,4 +272,5 @@ void ac_automata_enable_debug (int debug);
/* See man open_memstream() for get result as string */
void ac_automata_dump (AC_AUTOMATA_t * thiz, FILE *);
#endif
void ac_automata_get_stats(AC_AUTOMATA_t * thiz, struct ac_stats *stats);
#endif
+21 -2
View File
@@ -434,6 +434,8 @@ int ac_automata_search (AC_AUTOMATA_t * thiz,
AC_NODE_t *next;
AC_ALPHABET_t *apos;
thiz->stats.n_search++;
if(thiz->automata_open)
/* you must call ac_automata_locate_failure() first */
return -1;
@@ -493,15 +495,20 @@ int ac_automata_search (AC_AUTOMATA_t * thiz,
match->position = position;
match->match_num = curr->matched_patterns->num;
match->patterns = curr->matched_patterns->patterns;
if (thiz->match_handler(match, txt, param))
if (thiz->match_handler(match, txt, param)) {
thiz->stats.n_found++;
return 1;
}
}
} /* match->match_map */
}
}
}
if(thiz->match_handler)
if(thiz->match_handler) {
if(match->match_counter > 0)
thiz->stats.n_found++;
return match->match_counter > 0 ? 1:0;
}
for(i = 0; i < 4; i++)
if(txt->match.matched[i]) {
@@ -516,6 +523,7 @@ int ac_automata_search (AC_AUTOMATA_t * thiz,
pattern->rep.number);
}
#endif
thiz->stats.n_found++;
return 1;
}
return 0;
@@ -1238,5 +1246,16 @@ static inline void node_sort_edges (AC_NODE_t * thiz)
}
}
void ac_automata_get_stats(AC_AUTOMATA_t * thiz, struct ac_stats *stats)
{
if (thiz) {
stats->n_search = thiz->stats.n_search;
stats->n_found = thiz->stats.n_found;
} else {
stats->n_search = 0;
stats->n_found = 0;
}
}
/* vim: set ts=4 sw=4 et : */
+6
View File
@@ -462,6 +462,8 @@ ndpi_patricia_search_exact (ndpi_patricia_tree_t *patricia, ndpi_prefix_t *prefi
assert (prefix);
assert (prefix->bitlen <= patricia->maxbits);
patricia->stats.n_search++;
if(patricia->head == NULL)
return (NULL);
@@ -517,6 +519,7 @@ ndpi_patricia_search_exact (ndpi_patricia_tree_t *patricia, ndpi_prefix_t *prefi
fprintf (stderr, "patricia_search_exact: found %s/%d\n",
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
#endif /* PATRICIA_DEBUG */
patricia->stats.n_found++;
return (node);
}
return (NULL);
@@ -537,6 +540,8 @@ ndpi_patricia_search_best2 (ndpi_patricia_tree_t *patricia, ndpi_prefix_t *prefi
assert (prefix);
assert (prefix->bitlen <= patricia->maxbits);
patricia->stats.n_search++;
if(patricia->head == NULL)
return (NULL);
@@ -614,6 +619,7 @@ ndpi_patricia_search_best2 (ndpi_patricia_tree_t *patricia, ndpi_prefix_t *prefi
fprintf (stderr, "patricia_search_best: found %s/%d\n",
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
#endif /* PATRICIA_DEBUG */
patricia->stats.n_found++;
return (node);
}
}