static inline void __wheap_swap(ctl_wheap_t h, size_t i, size_t j) { /* swap priority data */ array_swap(h->cells, i, j); /* swap colours too */ array_swap(h->colours, i, j); return; }
void stats_swap(struct stats *st) { if (!stats_enabled) { return; } if (st->aggregate == 1) { log_debug(LOG_PVERB, "skip swap of current %p shadow %p as aggregator " "is busy", st->current.elem, st->shadow.elem); return; } if (st->updated == 0) { log_debug(LOG_PVERB, "skip swap of current %p shadow %p as there is " "nothing new", st->current.elem, st->shadow.elem); return; } log_debug(LOG_PVERB, "swap stats current %p shadow %p", st->current.elem, st->shadow.elem); array_swap(&st->current, &st->shadow); /* * Reset current (a) stats before giving it back to generator to keep * stats addition idempotent */ stats_pool_reset(&st->current); st->updated = 0; st->aggregate = 1; }
//把current数据拷贝到shadow,然后在stats_aggregate进行合并 //只要有读写事件则更新统计信息,然后拷贝到shadow,最终在stats_aggregate进行合并 void stats_swap(struct stats *st) { if (!stats_enabled) { return; } if (st->aggregate == 1) { /* 客户端 */ log_debug(LOG_PVERB, "skip swap of current %p shadow %p as aggregator " "is busy", st->current.elem, st->shadow.elem); return; } if (st->updated == 0) {/* 在客户端通过22222两次获取统计信息这段时间内必须要有相关参数的更新,这里才会置1 */ log_debug(LOG_PVERB, "skip swap of current %p shadow %p as there is " "nothing new", st->current.elem, st->shadow.elem); return; } log_debug(LOG_PVERB, "swap stats current %p shadow %p", st->current.elem, st->shadow.elem); array_swap(&st->current, &st->shadow); //把current数据拷贝到shadow,然后在stats_aggregate进行合并 /* * Reset current (a) stats before giving it back to generator to keep * stats addition idempotent */ stats_pool_reset(&st->current); st->updated = 0; st->aggregate = 1; }
void heap_percolate_down(heap_t *heap, int index) { void *left, *right, *cur; int l, r, c; c = index; for (;;) { l = heap_left(c); r = heap_right(c); left = array_get(&heap->items, l); right = array_get(&heap->items, r); cur = array_get(&heap->items, c); if (left != NULL && right != NULL) { if (heap->compare(left, right) < 0) { goto left; } else { goto right; } } else if (left != NULL) { goto left; } else if (right != NULL) { goto right; } else { return; } left: if (heap->compare(left, cur) < 0) { array_swap(&heap->items, c, l); c = l; } else { return; } continue; right: if (heap->compare(right, cur) < 0) { array_swap(&heap->items, c, r); c = r; } else { return; } continue; } }
void* heap_pop(heap_t *heap) { int n = array_size(&heap->items); if (n == 0) { return NULL; } if (n == 1) { return array_pop(&heap->items); } array_swap(&heap->items, 0, n-1); void *result = array_pop(&heap->items); heap_percolate_down(heap, 0); return result; }
void array_bubble_sort(void *base, size_t nelm, size_t width, int (*cmp)(const void *a, const void *b)) { size_t i_start, i_end; for (i_end = nelm - 1; i_end > 0; i_end--) { for (i_start = 0; i_start < i_end; i_start++) { if (cmp(N2PTR(i_start), N2PTR(i_start + 1)) > 0) { array_swap(N2PTR(i_start), N2PTR(i_start + 1), width); } } } }
void heap_percolate_up(heap_t *heap, int index) { int i = index; int p; for (;;) { p = heap_parent(i); if (p < 0) { return; } if (heap->compare(array_get(&heap->items, i), array_get(&heap->items, p)) < 0) { array_swap(&heap->items, i, p); i = p; } else { return; } } }
void array_select_sort(void *base, size_t nelm, size_t width, int (*cmp)(const void *a, const void *b)) { size_t i, j, index; for (i = 0; i < nelm - 1; i++) { index = i; for (j = i + 1; j < nelm; j++) { if (cmp(N2PTR(index), N2PTR(j)) > 0) { index = j; } } if (index != i) { array_swap(N2PTR(index), N2PTR(i), width); } } }