<commit_before>#[ // SPDX-License-Identifier: Apache-2.0
#[ // Copyright 2020 Eotvos Lorand University, Budapest, Hungary

from compiler_common import unique_everseen

#[ #include "common.h"
#[ #include "dpdk_lib.h"

#[ extern char* action_short_names[];
#[ extern char* action_names[];

parser = hlir.parsers[0]

known_parser_state_names = ('start', 'accept', 'reject')
_, parser_state_names = zip(*sorted([(0 if s.name in known_parser_state_names else 1, s.name) for s in parser.states]))

#{ #ifdef T4P4S_STATS
#[ t4p4s_stats_t t4p4s_stats_global;
#[ t4p4s_stats_t t4p4s_stats_per_packet;

#[ char stats_buf[128 * 1024];
#[ int stats_counter;

#{ void t4p4s_init_stats(t4p4s_stats_t* t4p4s_stats) {
for name in parser_state_names:
    #[     t4p4s_stats->parser_state__${name} = false;

for table in hlir.tables:
    #[     t4p4s_stats->table_apply__${table.name} = false;

    if 'key' in table:
        #[     t4p4s_stats->table_hit__${table.name} = false;
        #[     t4p4s_stats->table_miss__${table.name} = false;
    else:
        #[     t4p4s_stats->table_used__${table.name} = false;

    for action_name in table.actions.map('expression.method.path.name'):
        #[     t4p4s_stats->table_action_used__${table.name}_${action_name} = false;
#} }

#{ void t4p4s_init_global_stats() {
#[     t4p4s_init_stats(&t4p4s_stats_global);
#} }
#[

#{ void t4p4s_init_per_packet_stats() {
#[     t4p4s_init_stats(&t4p4s_stats_per_packet);
#} }
#[

#{ void t4p4s_print_stats_parser_states(bool is_on, t4p4s_stats_t* t4p4s_stats, bool is_global) {
#[     sprintf(stats_buf, "%s", "");
#[     char* printout = stats_buf;

#[     stats_counter = 0;
#[     bool is_used;
#[     bool cond;
for name in parser_state_names:
    #[     is_used = t4p4s_stats->parser_state__${name};
    #[     cond = !(is_used ^ is_on);
    #{     if (cond) {
    #[         printout += sprintf(printout, T4LIT($name,parserstate) ", ");
    #[         ++stats_counter;
    #}     }

#[     if (stats_counter == 0 && !is_on)    return;
#[     // do not complain if only "reject" is unused
#[     if (stats_counter == 1 && !is_on && !t4p4s_stats->parser_state__reject)    return;
#[     if (is_global) {
#[         debug("    - %4d %s parser states: %s\n", stats_counter, is_on ? "used" : "unused", stats_buf);
#[     } else {
#[         debug("    - parser states: %s\n", stats_buf);
#[     }
#} }

#[ enum t4p4s_table_stat_e { T4TABLE_APPLIED };

#{ void t4p4s_print_stats_tables(bool is_on, bool hidden, enum t4p4s_table_stat_e stat, t4p4s_stats_t* t4p4s_stats, bool is_global) {
#[     sprintf(stats_buf, "%s", "");
#[     char* printout = stats_buf;

#[     stats_counter = 0;
#[     bool is_used;
#[     bool is_hidden;
#[     bool cond;
for table in hlir.tables:
    #[     is_used = t4p4s_stats->table_apply__${table.name};
    #[     is_hidden  = ${"true" if table.is_hidden else "false"};
    #[     cond = !(is_used ^ is_on) && !(hidden ^ is_hidden);
    #{     if (cond) {
    #[         printout += sprintf(printout, "$$[table]{table.name}, ");
    #[         ++stats_counter;
    #}     }

#[         if (stats_counter == 0 && !is_on)    return;
#[         if (is_global) {
#[             debug("    - %4d %s %stables: %s\n", stats_counter, is_on ? "applied" : "unapplied", hidden ? "hidden " : "", stats_buf);
#[         } else {
#[             debug("    - tables: %s\n", stats_buf);
#[         }
#} }
#[

#[ #define NO_ACTION_NAME ".NoAction"

for idx, (table, action) in enumerate((t, a) for t in hlir.tables for a in unique_everseen(t.actions)):
    ao = action.action_object<commit_msg>Extending stat printing with hit/miss info<commit_after>#[ // SPDX-License-Identifier: Apache-2.0
#[ // Copyright 2020 Eotvos Lorand University, Budapest, Hungary

from compiler_common import unique_everseen

#[ #include "common.h"
#[ #include "dpdk_lib.h"

#[ extern char* action_short_names[];
#[ extern char* action_names[];

parser = hlir.parsers[0]

known_parser_state_names = ('start', 'accept', 'reject')
_, parser_state_names = zip(*sorted([(0 if s.name in known_parser_state_names else 1, s.name) for s in parser.states]))

#{ #ifdef T4P4S_STATS
#[ t4p4s_stats_t t4p4s_stats_global;
#[ t4p4s_stats_t t4p4s_stats_per_packet;

#[ char stats_buf[128 * 1024];
#[ int stats_counter;

#{ void t4p4s_init_stats(t4p4s_stats_t* t4p4s_stats) {
for name in parser_state_names:
    #[     t4p4s_stats->parser_state__${name} = false;

for table in hlir.tables:
    #[     t4p4s_stats->table_apply__${table.name} = false;

    if 'key' in table:
        #[     t4p4s_stats->table_hit__${table.name} = false;
        #[     t4p4s_stats->table_miss__${table.name} = false;
    else:
        #[     t4p4s_stats->table_used__${table.name} = false;

    for action_name in table.actions.map('expression.method.path.name'):
        #[     t4p4s_stats->table_action_used__${table.name}_${action_name} = false;
#} }

#{ void t4p4s_init_global_stats() {
#[     t4p4s_init_stats(&t4p4s_stats_global);
#} }
#[

#{ void t4p4s_init_per_packet_stats() {
#[     t4p4s_init_stats(&t4p4s_stats_per_packet);
#} }
#[

#{ void t4p4s_print_stats_parser_states(bool is_on, t4p4s_stats_t* t4p4s_stats, bool is_global) {
#[     sprintf(stats_buf, "%s", "");
#[     char* printout = stats_buf;

#[     stats_counter = 0;
#[     bool is_used;
#[     bool cond;
for name in parser_state_names:
    #[     is_used = t4p4s_stats->parser_state__${name};
    #[     cond = !(is_used ^ is_on);
    #{     if (cond) {
    #[         printout += sprintf(printout, T4LIT($name,parserstate) ", ");
    #[         ++stats_counter;
    #}     }

#[     if (stats_counter == 0 && !is_on)    return;
#[     // do not complain if only "reject" is unused
#[     if (stats_counter == 1 && !is_on && !t4p4s_stats->parser_state__reject)    return;
#[     if (is_global) {
#[         debug("    - %4d %s parser states: %s\n", stats_counter, is_on ? "used" : "unused", stats_buf);
#[     } else {
#[         debug("    - parser states: %s\n", stats_buf);
#[     }
#} }

#[ enum t4p4s_table_stat_e { T4TABLE_APPLIED };

#{ void t4p4s_print_stats_tables(bool is_on, bool hidden, enum t4p4s_table_stat_e stat, t4p4s_stats_t* t4p4s_stats, bool is_global) {
#[     sprintf(stats_buf, "%s", "");
#[     char* printout = stats_buf;

#[     stats_counter = 0;
#[     bool is_used;
#[     bool is_hit;
#[     bool is_miss;
#[     bool is_hidden;
#[     bool cond;
for table in hlir.tables:
    #[     is_hit  = (is_on && t4p4s_stats->table_hit__${table.name}) || (!is_on && !t4p4s_stats->table_hit__${table.name});
    #[     is_miss = (is_on && t4p4s_stats->table_miss__${table.name}) || (!is_on && !t4p4s_stats->table_miss__${table.name});
    #[     if (hidden) {
    #[         is_used = t4p4s_stats->table_apply__${table.name};
    #[     } else {   
    #[         is_used = !(is_on ^ (is_hit || is_miss));
    #[     }
    #[     is_hidden  = ${"true" if table.is_hidden else "false"};
    #[     cond = !(is_used ^ is_on) && !(hidden ^ is_hidden);
    #{     if (cond) {
    #[         if (hidden) {
    #[             printout += sprintf(printout, "$$[table]{table.name}, ");
    #[         } else {
    #[             printout += sprintf(printout, "$$[table]{table.name}[%s%s], ", is_hit ? "hit": "", is_miss ? "miss": "");
    #[         }
    #[         ++stats_counter;
    #}     }

#[         if (stats_counter == 0 && !is_on)    return;
#[         if (is_global) {
#[             debug("    - %4d %s %stables: %s\n", stats_counter, is_on ? "applied" : "unapplied", hidden ? "hidden " : "", stats_buf);
#[         } else {
#[             debug("    - tables: %s\n", stats_buf);
#[         }
#} }
#[

#[ #define NO_ACTION_NAME ".NoAction"

for idx, (table, action) in enumerate((t, a) for t in hlir.tables for a in unique_everseen(t.actions)):
    ao = action.action_object
{'fraction_non_alphanumeric': 0.1648098348059931, 'fraction_numerical': 0.023306441285695992, 'mean_word_length': 3.093291404612159, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '902738', 'n_tokens_mistral': 3014, 'n_tokens_neox': 3132, 'n_words': 551}