static void benchmark(struct classifier *cls) { struct timespec before, after; struct rusage rbefore, rafter; struct flow_wildcards wc; struct match match_wc_str; struct flow flow; size_t i; memset(&flow, 0, sizeof flow); flow.in_port.ofp_port = OFPP_LOCAL; flow.dl_type = htons(ETH_TYPE_IP); flow.nw_proto = IPPROTO_TCP; fat_rwlock_rdlock(&cls->rwlock); clock_gettime(CLOCK_MONOTONIC_RAW, &before); getrusage(RUSAGE_SELF, &rbefore); for (i = 0; i < 10000000; i++) { eth_addr_random(flow.dl_src); eth_addr_random(flow.dl_dst); flow.nw_src = htonl(random_uint32()); flow.nw_dst = htonl(random_uint32()); flow.tp_src = htons(random_uint16()); flow.tp_dst = htons(random_uint16()); flow.tp_dst = htons(i); flow_wildcards_init_catchall(&wc); //VLOG_DBG("Finding relevant wc's for flow: tp_dst=%d (0x%04x)", // ntohs(flow.tp_dst), ntohs(flow.tp_dst)); classifier_lookup(cls, &flow, &wc); match_init(&match_wc_str, &flow, &wc); //VLOG_DBG("Relevant fields: %s", match_to_string(&match_wc_str, 0)); } getrusage(RUSAGE_SELF, &rafter); clock_gettime(CLOCK_MONOTONIC_RAW, &after); fat_rwlock_unlock(&cls->rwlock); printf("real %lldms\n" "user %lldms\n" "sys %lldms\n" "soft faults %ld\n" "hard faults %ld\n", timespec_to_msec(&after) - timespec_to_msec(&before), timeval_to_msec(&rafter.ru_utime) - timeval_to_msec(&rbefore.ru_utime), timeval_to_msec(&rafter.ru_stime) - timeval_to_msec(&rbefore.ru_stime), rafter.ru_minflt - rbefore.ru_minflt, rafter.ru_majflt - rbefore.ru_majflt); }
static int initialize_state(state *s) { if (match_init(s)) return TRUE; s->mode = mode_none; s->first_file_processed = TRUE; s->found_meaningful_file = FALSE; s->processed_file = FALSE; s->threshold = 0; return FALSE; }
/* 意大利面条式代码,但是暂时还没什么好的错误处理方法 */ static int server_start(void) { int flag; #ifdef HAVE_DB flag = db_init(oracle_servername, oracle_username, oracle_password); if (flag == -1) { return -1; } INFO("Database initilize!"); #endif flag = spam_init(spam_thresold, spam_data); if (flag == -1) { return -1; } flag = match_init(); if (flag == -1) { return -1; } flag = kill_init(); if (flag == -1) { match_close(); return -1; } flag = log_init(); if (flag == -1) { match_close(); kill_close(); return -1; } flag = monitor_init(); if (flag == -1) { match_close(); kill_close(); log_close(); return -1; } flag = analy_init(netcard); if (flag == -1) { match_close(); kill_close(); log_close(); monitor_close(); } return 0; }
static int json_start_map(void *ctx) { LOG("start of map, last_key = %s\n", last_key); if (parsing_swallows) { LOG("creating new swallow\n"); current_swallow = smalloc(sizeof(Match)); match_init(current_swallow); TAILQ_INSERT_TAIL(&(json_node->swallow_head), current_swallow, matches); } else { if (!parsing_rect && !parsing_window_rect && !parsing_geometry) { if (last_key && strcasecmp(last_key, "floating_nodes") == 0) { DLOG("New floating_node\n"); Con *ws = con_get_workspace(json_node); json_node = con_new(NULL, NULL); json_node->parent = ws; DLOG("Parent is workspace = %p\n", ws); } else { Con *parent = json_node; json_node = con_new(NULL, NULL); json_node->parent = parent; } } } return 1; }
static int parse_filter(char *filter_parse) { struct ds in; struct flow flow_filter; struct flow_wildcards wc_filter; char *error, *filter = NULL; vlog_set_levels_from_string_assert("odp_util:console:dbg"); if (filter_parse && !strncmp(filter_parse, "filter=", 7)) { filter = xstrdup(filter_parse + 7); memset(&flow_filter, 0, sizeof(flow_filter)); memset(&wc_filter, 0, sizeof(wc_filter)); error = parse_ofp_exact_flow(&flow_filter, &wc_filter, NULL, filter, NULL); if (error) { ovs_fatal(0, "Failed to parse filter (%s)", error); } } else { ovs_fatal(0, "No filter to parse."); } ds_init(&in); while (!ds_get_test_line(&in, stdin)) { struct ofpbuf odp_key; struct ofpbuf odp_mask; struct ds out; /* Convert string to OVS DP key. */ ofpbuf_init(&odp_key, 0); ofpbuf_init(&odp_mask, 0); if (odp_flow_from_string(ds_cstr(&in), NULL, &odp_key, &odp_mask)) { printf("odp_flow_from_string: error\n"); goto next; } if (filter) { struct flow flow; struct flow_wildcards wc; struct match match, match_filter; struct minimatch minimatch; odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); odp_flow_key_to_mask(odp_mask.data, odp_mask.size, &wc, &flow); match_init(&match, &flow, &wc); match_init(&match_filter, &flow_filter, &wc); match_init(&match_filter, &match_filter.flow, &wc_filter); minimatch_init(&minimatch, &match_filter); if (!minimatch_matches_flow(&minimatch, &match.flow)) { minimatch_destroy(&minimatch); goto next; } minimatch_destroy(&minimatch); } /* Convert odp_key to string. */ ds_init(&out); odp_flow_format(odp_key.data, odp_key.size, odp_mask.data, odp_mask.size, NULL, &out, false); puts(ds_cstr(&out)); ds_destroy(&out); next: ofpbuf_uninit(&odp_key); ofpbuf_uninit(&odp_mask); } ds_destroy(&in); free(filter); return 0; }
static int parse_keys(bool wc_keys) { int exit_code = 0; struct ds in; ds_init(&in); vlog_set_levels_from_string_assert("odp_util:console:dbg"); while (!ds_get_test_line(&in, stdin)) { enum odp_key_fitness fitness; struct ofpbuf odp_key; struct ofpbuf odp_mask; struct flow flow; struct ds out; int error; /* Convert string to OVS DP key. */ ofpbuf_init(&odp_key, 0); ofpbuf_init(&odp_mask, 0); error = odp_flow_from_string(ds_cstr(&in), NULL, &odp_key, &odp_mask); if (error) { printf("odp_flow_from_string: error\n"); goto next; } if (!wc_keys) { struct odp_flow_key_parms odp_parms = { .flow = &flow, .recirc = true, }; /* Convert odp_key to flow. */ fitness = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); switch (fitness) { case ODP_FIT_PERFECT: break; case ODP_FIT_TOO_LITTLE: printf("ODP_FIT_TOO_LITTLE: "); break; case ODP_FIT_TOO_MUCH: printf("ODP_FIT_TOO_MUCH: "); break; case ODP_FIT_ERROR: printf("odp_flow_key_to_flow: error\n"); goto next; } /* Convert cls_rule back to odp_key. */ ofpbuf_uninit(&odp_key); ofpbuf_init(&odp_key, 0); odp_parms.odp_in_port = flow.in_port.odp_port; odp_flow_key_from_flow(&odp_parms, &odp_key); if (odp_key.size > ODPUTIL_FLOW_KEY_BYTES) { printf ("too long: %"PRIu32" > %d\n", odp_key.size, ODPUTIL_FLOW_KEY_BYTES); exit_code = 1; } } /* Convert odp_key to string. */ ds_init(&out); if (wc_keys) { odp_flow_format(odp_key.data, odp_key.size, odp_mask.data, odp_mask.size, NULL, &out, false); } else { odp_flow_key_format(odp_key.data, odp_key.size, &out); } puts(ds_cstr(&out)); ds_destroy(&out); next: ofpbuf_uninit(&odp_key); } ds_destroy(&in); return exit_code; } static int parse_actions(void) { struct ds in; ds_init(&in); vlog_set_levels_from_string_assert("odp_util:console:dbg"); while (!ds_get_test_line(&in, stdin)) { struct ofpbuf odp_actions; struct ds out; int error; /* Convert string to OVS DP actions. */ ofpbuf_init(&odp_actions, 0); error = odp_actions_from_string(ds_cstr(&in), NULL, &odp_actions); if (error) { printf("odp_actions_from_string: error\n"); goto next; } /* Convert odp_actions back to string. */ ds_init(&out); format_odp_actions(&out, odp_actions.data, odp_actions.size); puts(ds_cstr(&out)); ds_destroy(&out); next: ofpbuf_uninit(&odp_actions); } ds_destroy(&in); return 0; } static int parse_filter(char *filter_parse) { struct ds in; struct flow flow_filter; struct flow_wildcards wc_filter; char *error, *filter = NULL; vlog_set_levels_from_string_assert("odp_util:console:dbg"); if (filter_parse && !strncmp(filter_parse, "filter=", 7)) { filter = xstrdup(filter_parse + 7); memset(&flow_filter, 0, sizeof(flow_filter)); memset(&wc_filter, 0, sizeof(wc_filter)); error = parse_ofp_exact_flow(&flow_filter, &wc_filter.masks, filter, NULL); if (error) { ovs_fatal(0, "Failed to parse filter (%s)", error); } } else { ovs_fatal(0, "No filter to parse."); } ds_init(&in); while (!ds_get_test_line(&in, stdin)) { struct ofpbuf odp_key; struct ofpbuf odp_mask; struct ds out; int error; /* Convert string to OVS DP key. */ ofpbuf_init(&odp_key, 0); ofpbuf_init(&odp_mask, 0); error = odp_flow_from_string(ds_cstr(&in), NULL, &odp_key, &odp_mask); if (error) { printf("odp_flow_from_string: error\n"); goto next; } if (filter) { struct flow flow; struct flow_wildcards wc; struct match match, match_filter; struct minimatch minimatch; odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); odp_flow_key_to_mask(odp_mask.data, odp_mask.size, &wc.masks, &flow); match_init(&match, &flow, &wc); match_init(&match_filter, &flow_filter, &wc); match_init(&match_filter, &match_filter.flow, &wc_filter); minimatch_init(&minimatch, &match_filter); if (!minimatch_matches_flow(&minimatch, &match.flow)) { minimatch_destroy(&minimatch); goto next; } minimatch_destroy(&minimatch); } /* Convert odp_key to string. */ ds_init(&out); odp_flow_format(odp_key.data, odp_key.size, odp_mask.data, odp_mask.size, NULL, &out, false); puts(ds_cstr(&out)); ds_destroy(&out); next: ofpbuf_uninit(&odp_key); ofpbuf_uninit(&odp_mask); } ds_destroy(&in); free(filter); return 0; } static void test_odp_main(int argc, char *argv[]) { int exit_code = 0; set_program_name(argv[0]); if (argc == 2 &&!strcmp(argv[1], "parse-keys")) { exit_code =parse_keys(false); } else if (argc == 2 &&!strcmp(argv[1], "parse-wc-keys")) { exit_code =parse_keys(true); } else if (argc == 2 && !strcmp(argv[1], "parse-actions")) { exit_code = parse_actions(); } else if (argc == 3 && !strcmp(argv[1], "parse-filter")) { exit_code =parse_filter(argv[2]); } else { ovs_fatal(0, "usage: %s parse-keys | parse-wc-keys | parse-actions", argv[0]); } exit(exit_code); }
/* =========================================================================== * Initialize the "longest match" routines for a new file * * IN assertion: window_size is > 0 if the input file is already read or * mmap'ed in the window[] array, 0 otherwise. In the first case, * window_size is sufficient to contain the whole input file plus * MIN_LOOKAHEAD bytes (to avoid referencing memory beyond the end * of window[] when looking for matches towards the end). */ void IZDeflate::lm_init (int pack_level, ush *flags) //int pack_level; /* 0: store, 1: best speed, 9: best compression */ //ush *flags; /* general purpose bit flag */ { unsigned j; if (pack_level < 1 || pack_level > 9) error("bad pack level"); /* Do not slide the window if the whole input is already in memory * (window_size > 0) */ sliding = 0; if (window_size == 0L) { sliding = 1; window_size = (ulg)2L*WSIZE; } /* Use dynamic allocation if compiler does not like big static arrays: */ #ifdef DYN_ALLOC if (window == NULL) { window = (uch *) zcalloc(WSIZE, 2*sizeof(uch)); if (window == NULL) ziperr(ZE_MEM, "window allocation"); } if (prev == NULL) { prev = (Pos *) zcalloc(WSIZE, sizeof(Pos)); head = (Pos *) zcalloc(HASH_SIZE, sizeof(Pos)); if (prev == NULL || head == NULL) { ziperr(ZE_MEM, "hash table allocation"); } } #endif /* DYN_ALLOC */ /* Initialize the hash table (avoiding 64K overflow for 16 bit systems). * prev[] will be initialized on the fly. */ head[HASH_SIZE-1] = NIL; memset((char*)head, NIL, (unsigned)(HASH_SIZE-1)*sizeof(*head)); /* Set the default configuration parameters: */ max_lazy_match = configuration_table[pack_level].max_lazy; good_match = configuration_table[pack_level].good_length; #ifndef FULL_SEARCH nice_match = configuration_table[pack_level].nice_length; #endif max_chain_length = configuration_table[pack_level].max_chain; if (pack_level <= 2) { *flags |= FAST; } else if (pack_level >= 8) { *flags |= SLOW; } /* ??? reduce max_chain_length for binary files */ strstart = 0; block_start = 0L; #if defined(ASMV) && !defined(RISCOS) match_init(); /* initialize the asm code */ #endif j = WSIZE; #ifndef MAXSEG_64K if (sizeof(int) > 2) j <<= 1; /* Can read 64K in one step */ #endif lookahead = (*read_buf)(read_handle, (char*)window, j); if (lookahead == 0 || lookahead == (unsigned)EOF) { eofile = 1, lookahead = 0; return; } eofile = 0; /* Make sure that we always have enough lookahead. This is important * if input comes from a device such as a tty. */ if (lookahead < MIN_LOOKAHEAD) fill_window(); ins_h = 0; for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]); /* If lookahead < MIN_MATCH, ins_h is garbage, but this is * not important since only literal bytes will be emitted. */ }
/* * Initializes the specified 'Match' data structure and the initial state of * commands.c for matching target windows of a command. * */ CFGFUN(criteria_init, int _state) { criteria_next_state = _state; DLOG("Initializing criteria, current_match = %p, state = %d\n", current_match, _state); match_init(current_match); }
/* Test driver for hashtable. */ int main(int argc, char **argv) { /* Test key_hashtable instance. */ hashtable_t *kt; hashtable_iter_t ki; key_t k1, k2; key_init(&k1, 1); key_init(&k2, 2); assert((kt = key_hashtable_new(16)) != NULL); assert(key_hashtable_add(kt, &k1) == &k1); assert(key_hashtable_find(kt, &k1) == &k1); assert(key_hashtable_find(kt, &k2) == NULL); assert(key_hashtable_iter(&ki, kt) == &k1); assert(key_hashtable_next(&ki) == NULL); /* Test hashtable instance. */ hashtable_t *t; entry_t entry[256]; entry_t e; match_t m; int i; entry_init(&e, 0); for (i = 0; i < 256; i++) entry_init(&entry[i], i); /* Test hashtable_new() */ t = hashtable_new(256); assert(t->size == 512); assert(t->count == 0); assert(t->etable != NULL); assert(t->ktable != NULL); /* Test hashtable_add() */ assert(hashtable_add(t, &e) == &e); /* Added duplicated copy. */ assert(hashtable_add(t, &entry[0]) == &entry[0]); /* Added duplicated instance. */ for (i = 0; i < 256; i++) assert(hashtable_add(t, &entry[i]) == &entry[i]); assert(t->count == 258); /* Test hashtable_find() */ match_init(&m, 0); assert(hashtable_find(t, &m) == &e); /* Finds first duplicate added. */ assert(m.value == m.source); /* match_cmp() updated m.value. */ for (i = 1; i < 256; i++) { match_init(&m, i); assert(hashtable_find(t, &m) == &entry[i]); assert(m.value == m.source); /* match_cmp() updated m.value. */ } match_init(&m, 256); assert(hashtable_find(t, &m) == NULL); /* Find missing entry. */ assert(m.value == 0); /* match_cmp() didn't update m.value. */ #ifndef HASHTABLE_NSTATS assert(t->find_count == 257); assert(t->match_count == 256); assert(t->hashcmp_count >= 256); assert(t->entrycmp_count >= 256); hashtable_stats_init(t); assert(t->find_count == 0); assert(t->match_count == 0); assert(t->hashcmp_count == 0); assert(t->entrycmp_count == 0); #endif /* Test hashtable iterators */ entry_t *p; hashtable_iter_t iter; int count = 0; for (p = hashtable_iter(&iter, t); p != NULL; p = hashtable_next(&iter)) { assert(p == &e || (&entry[0] <= p && p <= &entry[255])); count++; } assert(count == 258); hashtable_free(t); return 0; }