int filter(List *lst1, List *lst2, int (*predicate)(void *)){ int count = 0; if(lst1 != NULL && lst2 != NULL){ if(list_size(lst1) == 0){ count = 0; } }else{ ListElmt el; for(el = list_head(lst1); list_has_next(lst1); el = list_next(el)){ if(*predicate(lst1->data)){ list_ins_next(lst2, list_tail(lst2), list_data(el)); } } } return }
double_struct *merge_partitions(cog *c, long low, long high) { list *list = create_list(); int has_merge_work; gather_partitions(list, c); struct cog *right_replacement = NULL; struct cog *left_replacement = NULL; struct list *list_iter; struct iter_list *tail; struct stack_triple *stack = create_stack(); struct iter_list *head_list = malloc(sizeof(struct iter_list)); tail = head_list; list_iter = list; iterator merge_iter; cog *c1 = malloc(sizeof(struct cog)); while(list_has_next(list_iter)) { list_iter = get_cog_from_list(list_iter, c1); struct extracted_components *components = extract_partitions(c1, low, high); if(components->iter != NULL) { has_merge_work = 1; tail = iter_list_add(tail, components->iter); } if(components->rhs != NULL) { if(right_replacement == NULL) { right_replacement = components->rhs; } else { right_replacement = make_concat(right_replacement, components->rhs); } } if(components->lhs != NULL) { if(left_replacement == NULL) { left_replacement = components->lhs; } else { left_replacement = make_concat(left_replacement, components->lhs); } } free(components); } cleanup_list(list); free(c1); if(has_merge_work != 0) { double_struct *ret = create_double_struct(); merge_iter = iter_merge(head_list); record r = malloc(sizeof(struct record)); while(1) { int i; buffer out = buffer_alloc(MERGE_BLOCK_SIZE); record buf = out->data; for(i = 0; i < MERGE_BLOCK_SIZE && iter_has_next(merge_iter); i++) { iter_next(merge_iter, r); record_set(&buf[i], r->key, r->value); } if(i == 0) { break; } struct cog *buffer = make_sortedarray(0, i, out); fold_append(&stack, buffer, cog_min(buffer)); if(i < MERGE_BLOCK_SIZE) { break; } } cog *root = fold(&stack); if(root == NULL) { root = make_btree(left_replacement, right_replacement, high); } else { ret->iter = scan(root, low, high); if(left_replacement != NULL && cog_length(left_replacement) != 0) { root = make_btree(left_replacement, root, cog_min(root)); } if(right_replacement != NULL && cog_length(right_replacement) != 0) { root = make_btree(root, right_replacement, cog_min(right_replacement)); } } free(r); cleanup_stack(stack); iter_list_cleanup(head_list); iter_cleanup(merge_iter); ret->cog = root; return ret; } else { double_struct *ret; ret = amerge(list->cog, low, high); if(left_replacement != NULL) { ret->cog = make_concat(ret->cog, left_replacement); } if(right_replacement != NULL) { ret->cog = make_concat(ret->cog, right_replacement); } return ret; } }
void *weightedFairScheduler(void *pc) { pktcore_t *pcore = (pktcore_t *)pc; List *keylst; simplequeue_t *nxtq, *thisq; char *nxtkey, *savekey; double minftime, minstime, tweight; int pktsize, npktsize; gpacket_t *in_pkt, *nxt_pkt; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // die as soon as cancelled while (1) { verbose(2, "[weightedFairScheduler]:: Worst-case weighted fair queuing scheduler processing.."); pthread_mutex_lock(&(pcore->qlock)); if (pcore->packetcnt == 0) pthread_cond_wait(&(pcore->schwaiting), &(pcore->qlock)); pthread_mutex_unlock(&(pcore->qlock)); pthread_testcancel(); keylst = map_keys(pcore->queues); while (list_has_next(keylst) == 1) { nxtkey = list_next(keylst); nxtq = map_get(pcore->queues, nxtkey); if (nxtq->cursize == 0) continue; if ((nxtq->stime <= pcore->vclock) && (nxtq->ftime < minftime)) { savekey = nxtkey; minftime = nxtq->ftime; } } list_release(keylst); // if savekey is NULL then release the lock.. if (savekey == NULL) continue; else { thisq = map_get(pcore->queues, savekey); readQueue(thisq, (void **)&in_pkt, &pktsize); writeQueue(pcore->workQ, in_pkt, pktsize); pthread_mutex_lock(&(pcore->qlock)); pcore->packetcnt--; pthread_mutex_unlock(&(pcore->qlock)); peekQueue(thisq, (void **)&nxt_pkt, &npktsize); if (npktsize) { thisq->stime = thisq->ftime; thisq->ftime = thisq->stime + npktsize/thisq->weight; } minstime = thisq->stime; tweight = 0.0; keylst = map_keys(pcore->queues); while (list_has_next(keylst) == 1) { nxtkey = list_next(keylst); nxtq = map_get(pcore->queues, nxtkey); tweight += nxtq->weight; if ((nxtq->cursize > 0) && (nxtq->stime < minstime)) minstime = nxtq->stime; } list_release(keylst); pcore->vclock = max(minstime, (pcore->vclock + ((double)pktsize)/tweight)); } } }
// WCWeightFairQueuer: function called by the classifier to enqueue // the packets.. // TODO: Debug this function... int weightedFairQueuer(pktcore_t *pcore, gpacket_t *in_pkt, int pktsize, char *qkey) { simplequeue_t *thisq, *nxtq; double minftime, minstime, tweight; List *keylst; char *nxtkey, *savekey; verbose(2, "[weightedFairQueuer]:: Worst-case weighted fair queuing scheduler processing.."); pthread_mutex_lock(&(pcore->qlock)); thisq = map_get(pcore->queues, qkey); if (thisq == NULL) { fatal("[weightedFairQueuer]:: Invalid %s key presented for queue addition", qkey); pthread_mutex_unlock(&(pcore->qlock)); return EXIT_FAILURE; // packet dropped.. } printf("Checking the queue size \n"); if (thisq->cursize == 0) { verbose(2, "[weightedFairQueuer]:: inserting the first element.. "); thisq->stime = max(pcore->vclock, thisq->ftime); thisq->ftime = thisq->stime + pktsize/thisq->weight; minstime = thisq->stime; keylst = map_keys(pcore->queues); while (list_has_next(keylst) == 1) { nxtkey = list_next(keylst); nxtq = map_get(pcore->queues, nxtkey); if ((nxtq->cursize > 0) && (nxtq->stime < minstime)) minstime = nxtq->stime; } list_release(keylst); pcore->vclock = max(minstime, pcore->vclock); // insert the packet... and increment variables.. writeQueue(thisq, in_pkt, pktsize); pcore->packetcnt++; // wake up scheduler if it was waiting.. if (pcore->packetcnt == 1) pthread_cond_signal(&(pcore->schwaiting)); pthread_mutex_unlock(&(pcore->qlock)); return EXIT_SUCCESS; } else if (thisq->cursize < thisq->maxsize) { // insert packet and setup variables.. writeQueue(thisq, in_pkt, pktsize); pcore->packetcnt++; pthread_mutex_unlock(&(pcore->qlock)); return EXIT_SUCCESS; } else { verbose(2, "[weightedFairQueuer]:: Packet dropped.. Queue for %s is full ", qkey); pthread_mutex_unlock(&(pcore->qlock)); return EXIT_SUCCESS; } }
void *weightedFairScheduler(void *pc) { pktcore_t *pcore = (pktcore_t *)pc; List *keylst; simplequeue_t *nxtq, *thisq; char *nxtkey, *savekey; double minftime, minstime, tweight; int pktsize, npktsize; gpacket_t *in_pkt, *nxt_pkt; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); // die as soon as cancelled //MOD pcore->vclock = 0.0; //pktsize - used to get the packet size of packet being enqueued now //npktsize - used to determine if there are more packets in current queue // while (1) { verbose(2, "[weightedFairScheduler]:: Worst-case weighted fair queuing scheduler processing.."); pthread_mutex_lock(&(pcore->qlock)); if (pcore->packetcnt == 0) pthread_cond_wait(&(pcore->schwaiting), &(pcore->qlock)); pthread_mutex_unlock(&(pcore->qlock)); pthread_testcancel(); keylst = map_keys(pcore->queues); minftime = MAX_DOUBLE;//MOD savekey = NULL;//MOD while (list_has_next(keylst) == 1) { nxtkey = list_next(keylst); nxtq = map_get(pcore->queues, nxtkey); if (nxtq->cursize == 0) continue; //determines the minftime if ((nxtq->stime <= pcore->vclock) && (nxtq->ftime < minftime)) { savekey = nxtkey; minftime = nxtq->ftime; } } list_release(keylst); // if savekey is NULL then release the lock.. if (savekey == NULL) continue; else { thisq = map_get(pcore->queues, savekey); readQueue(thisq, (void **)&in_pkt, &pktsize); writeQueue(pcore->workQ, in_pkt, pktsize); pthread_mutex_lock(&(pcore->qlock)); pcore->packetcnt--; pthread_mutex_unlock(&(pcore->qlock)); peekQueue(thisq, (void **)&nxt_pkt, &npktsize); //Doing this because we don't change the stime and //ftime unless a new packet comes into an empty queue if (npktsize) { thisq->stime = thisq->ftime; thisq->ftime = thisq->stime + npktsize/thisq->weight; } minstime = thisq->stime; tweight = 0.0; keylst = map_keys(pcore->queues); //determine minstime while (list_has_next(keylst) == 1) { nxtkey = list_next(keylst); nxtq = map_get(pcore->queues, nxtkey); tweight += nxtq->weight; if ((nxtq->cursize > 0) && (nxtq->stime < minstime)) minstime = nxtq->stime; } list_release(keylst); pcore->vclock = max(minstime, (pcore->vclock + ((double)pktsize)/tweight)); } } }