Add resize test

reviewable/pr8239/r2
Craig Tiller 9 years ago
parent 0b13fcdab2
commit fe4f0012c6
  1. 34
      src/core/lib/iomgr/buffer_pool.c
  2. 8
      test/core/iomgr/buffer_pool_test.c

@ -138,6 +138,10 @@ static void bpstep_sched(grpc_exec_ctx *exec_ctx,
/* returns true if all allocations are completed */
static bool bpalloc(grpc_exec_ctx *exec_ctx, grpc_buffer_pool *buffer_pool) {
if (buffer_pool->free_pool <= 0) {
return false;
}
grpc_buffer_user *buffer_user;
while ((buffer_user =
bulist_pop(buffer_pool, GRPC_BULIST_AWAITING_ALLOCATION))) {
@ -242,6 +246,25 @@ static void bu_post_destructive_reclaimer(grpc_exec_ctx *exec_ctx, void *bu,
bulist_add_tail(buffer_user, GRPC_BULIST_RECLAIMER_DESTRUCTIVE);
}
typedef struct {
int64_t size;
grpc_buffer_pool *buffer_pool;
grpc_closure closure;
} bp_resize_args;
static void bp_resize(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
bp_resize_args *a = args;
int64_t delta = a->size - a->buffer_pool->size;
a->buffer_pool->size += delta;
a->buffer_pool->free_pool += delta;
if (delta < 0 && a->buffer_pool->free_pool < 0) {
bpstep_sched(exec_ctx, a->buffer_pool);
} else if (delta > 0 &&
!bulist_empty(a->buffer_pool, GRPC_BULIST_AWAITING_ALLOCATION)) {
bpstep_sched(exec_ctx, a->buffer_pool);
}
}
/*******************************************************************************
* grpc_buffer_pool api
*/
@ -279,6 +302,17 @@ void grpc_buffer_pool_ref(grpc_buffer_pool *buffer_pool) {
grpc_buffer_pool_internal_ref(buffer_pool);
}
void grpc_buffer_pool_resize(grpc_buffer_pool *buffer_pool, size_t size) {
grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
bp_resize_args *a = gpr_malloc(sizeof(*a));
a->buffer_pool = grpc_buffer_pool_internal_ref(buffer_pool);
a->size = (int64_t)size;
grpc_closure_init(&a->closure, bp_resize, a);
grpc_combiner_execute(&exec_ctx, buffer_pool->combiner, &a->closure,
GRPC_ERROR_NONE);
grpc_exec_ctx_finish(&exec_ctx);
}
/*******************************************************************************
* grpc_buffer_user api
*/

@ -42,10 +42,18 @@ static void test_no_op(void) {
grpc_buffer_pool_unref(grpc_buffer_pool_create());
}
static void test_resize_then_destroy(void) {
gpr_log(GPR_DEBUG, "** test_resize_then_destroy **");
grpc_buffer_pool *p = grpc_buffer_pool_create();
grpc_buffer_pool_resize(p, 1024 * 1024);
grpc_buffer_pool_unref(p);
}
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
grpc_init();
test_no_op();
test_resize_then_destroy();
grpc_shutdown();
return 0;
}

Loading…
Cancel
Save