void recJointIncrement(CountDist *D, JNode **p, int x, int y) /* Search a node with index pair (x,y). If found increments ** count. If not found add new node to tree. */ { register int cmp; register JNode *xy=*p; if ( xy == 0 ) { /* pair (x,y) not found! */ (*p)=newJNode(D,x,y); } else { if ( (cmp=idxcmp(x,y,xy->x,xy->y)) < 0 ) { /* (x,y) is smaller then (xy->x,xy->y), hence go into left subtree */ recJointIncrement(D,&(xy->left),x,y); } else if ( cmp > 0 ) { /* (x,y) is larger then (xy->x,xy->y), hence go into left subtree */ recJointIncrement(D,&(xy->right),x,y); } else { /* key found! */ xy->count++; D->XYjoint->nSamples++; } } }
static int process(datum_unit_t *du) { struct timeval start, end; state_unit_t *sf = NULL, *work_queue = NULL, *rest_queue = NULL, *sw = NULL, *sr = NULL, *su = NULL, *sv = NULL; match_unit_t *mu = NULL; index_unit_t *iu = NULL, *idx_to_exec[du->nt+1]; double hz, us, pe; //count_t n = 0, rn = 0, zn = 0, ni = du->ni, stat[du->nr+1]; count_t n = 0, rn = 0, zn = 0, ni = du->ni; state_t s = 0; regex_t r = 0, id = 0; table_t t = 0, m = du->nt; input_t c = 0; label_t x = 0, keep[du->np+1], exec[du->nt+1]; size_t q = 0, q_size = 0; int rv; //printf("DEBUG: %s() init\n", __FUNCTION__); iu = (index_unit_t *)malloc((du->nt+1)*sizeof(index_unit_t)); if (iu == NULL) { rv = -31; goto err; } memset(iu, 0, (du->nt+1)*sizeof(index_unit_t)); for (t=0; t<du->nt+1; t++) { idx_to_exec[t] = iu; iu++; } for (t=0; t<du->nt+1; t++) { exec[t] = 0; } //for (r=0; r<du->nr+1; r++) { // stat[r] = 0; //} for (r=0; r<du->np+1; r++) { keep[r] = 1; } work_queue = (state_unit_t *)malloc(QUEUE_SIZE*sizeof(state_unit_t)); if (work_queue == NULL) { rv = -32; goto err; } memset(work_queue, 0, QUEUE_SIZE*sizeof(state_unit_t)); iu = (index_unit_t *)malloc(QUEUE_SIZE*sizeof(index_unit_t)); if (iu == NULL) { rv = -33; goto err; } memset(iu, 0, QUEUE_SIZE*sizeof(index_unit_t)); for (q=1, sw=work_queue; q<QUEUE_SIZE; q++, sw++, iu++) { sw->next = (sw + 1); sw->index = iu; } sw->next = NULL; sw->index = iu; sw = work_queue; for (t=0; t<m; t++) { sw->table = t; sw->state = 0; sw = sw->next; q_size++; } rest_queue = sw; sw--; sw->next = NULL; sf = work_queue; sw = work_queue; sr = rest_queue; su = sw; sv = sr; //printf("DEBUG: %s() start\n", __FUNCTION__); //gettimeofday(&start, NULL); for (;;) { c = POOL[n++]; do { sw->state = DFA[sw->table][sw->state][c]; if (TIE[sw->table][sw->state] == NULL) { if (sw->state != END[sw->table]) { su = sw; sw = sw->next; } else { if (sw == su->next) { su->next = sw->next; } else if (sw->next != NULL) { work_queue = sw->next; su = sw->next; } else { goto out; } sr = sw; sw = sw->next; sr->next = rest_queue; rest_queue = sr; q_size--; } } else { mu = TIE[sw->table][sw->state]; do { id = mu->regex - 1; if (keep[id] && (MRT[id][0] == 0 || idxcmp(MRT[id][0], sw->index))) { if (MRT[id][1] == 0) { //stat[MRT[id][2]]++; } else { if ((t = MRT[id][1] - 1)) { if (t != sw->table) { exec[t] = 1; idxadd(id + 1, idx_to_exec[t]); } else { exec[m] = 1; idxadd(id + 1, idx_to_exec[m]); } } else { if (t == sw->table) { exec[m] = 1; idxadd(id + 1, idx_to_exec[m]); } else { exec[t] = 1; idxadd(id + 1, idx_to_exec[t]); } keep[id] = 0; } } } mu = mu->next; } while (mu != NULL); for (t=0; t<m; t++) { if (exec[t]) { exec[t] = 0; idxcpy(idx_to_exec[t], sr->index); sr->table = t; sr->state = 0; sv = sr; sr = sr->next; if (++q_size >= QUEUE_SIZE) { rv = -34; goto err; } } } if (sr != rest_queue) { sv->next = work_queue; work_queue = rest_queue; rest_queue = sr; if (q_size > QUEUE_WARN) { x = 1; } } if (exec[m]) { exec[m] = 0; idxcat(idx_to_exec[m], sw->index); } su = sw; sw = sw->next; } } while (sw != NULL); if (n < ni) { if (!x) { sw = work_queue; } else { x = 0; for (sw=work_queue; sw!=NULL;) { for (su=sw->next, sv=sw; su!=NULL;) { if (su->state == sw->state && su->table == sw->table) { idxcat(su->index, sw->index); sv->next = su->next; su->next = rest_queue; rest_queue = su; q_size--; } else { sv = su; } su = sv->next; } sw=sw->next; } sw = work_queue; sr = rest_queue; if (q_size > QUEUE_WARN) { QUEUE_WARN = q_size + 4; } } } else { break; } } out: // gettimeofday(&end, NULL); //printf("DEBUG: %s() end\n", __FUNCTION__); /* for (r=0; r<=du->nr; r++) { if (stat[r] > 0) { printf("rule %-5hu matched %8u times\n", r, stat[r]); rn += 1; zn += stat[r]; } } for (t=0; t<du->nt; t++) { s += du->ns[t]; } printf(">> storage efficiency: %u states\n", s); hz = F; us = (end.tv_sec-start.tv_sec)+(end.tv_usec-start.tv_usec)/1000000.0; pe = (hz*1000000000*us)/ni; printf(">> running efficiency: %f cycle/byte (%fs/%ubytes)\n", pe, us, ni); printf(">> # of matched rules: %u\n", rn); printf(">> # of matched times: %u\n", zn); */ if (sf != NULL) { free(sf); } free(POOL); for (r=0; r<du->nr; r++) { if (MRT[r] != NULL) { free(MRT[r]); } } free(MRT); for (t=0; t<du->nt; t++) { if (TIE[t] != NULL) { for (s=0; s<du->ns[t]; s++) { if (TIE[t][s] != NULL) { free(TIE[t][s]); } if (DFA[t][s] != NULL) { free(DFA[t][s]); } } free(TIE[t]); free(DFA[t]); } } return 0; err: printf("ERROR: %s() return %d\n", __FUNCTION__, rv); return rv; }