From acd57662f16df9becd183f6ccc6328c9877953c2 Mon Sep 17 00:00:00 2001 From: Dotan Barak Date: Wed, 17 Jan 2007 11:39:01 +0200 Subject: [PATCH] Add resource cleanup at end of pingpong tests Clean up all IB resources at the end of pingpong examples. Ack CQ events when using events to all CQ to be destroyed. Signed-off-by: Dotan Barak Signed-off-by: Roland Dreier --- examples/rc_pingpong.c | 53 ++++++++++++++++++++++++++++++++++- examples/srq_pingpong.c | 62 ++++++++++++++++++++++++++++++++++++++++- examples/uc_pingpong.c | 53 ++++++++++++++++++++++++++++++++++- examples/ud_pingpong.c | 58 +++++++++++++++++++++++++++++++++++++- 4 files changed, 222 insertions(+), 4 deletions(-) diff --git a/examples/rc_pingpong.c b/examples/rc_pingpong.c index 0e17cfb..82623db 100644 --- a/examples/rc_pingpong.c +++ b/examples/rc_pingpong.c @@ -326,7 +326,7 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE); if (!ctx->mr) { - fprintf(stderr, "Couldn't allocate MR\n"); + fprintf(stderr, "Couldn't register MR\n"); return NULL; } @@ -378,6 +378,46 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, return ctx; } +int pp_close_ctx(struct pingpong_context *ctx) +{ + if (ibv_destroy_qp(ctx->qp)) { + fprintf(stderr, "Couldn't destroy QP\n"); + return 1; + } + + if (ibv_destroy_cq(ctx->cq)) { + fprintf(stderr, "Couldn't destroy CQ\n"); + return 1; + } + + if (ibv_dereg_mr(ctx->mr)) { + fprintf(stderr, "Couldn't deregister MR\n"); + return 1; + } + + if (ibv_dealloc_pd(ctx->pd)) { + fprintf(stderr, "Couldn't deallocate PD\n"); + return 1; + } + + if (ctx->channel) { + if (ibv_destroy_comp_channel(ctx->channel)) { + fprintf(stderr, "Couldn't destroy completion channel\n"); + return 1; + } + } + + if (ibv_close_device(ctx->context)) { + fprintf(stderr, "Couldn't release context\n"); + return 1; + } + + free(ctx->buf); + free(ctx); + + return 0; +} + static int pp_post_recv(struct pingpong_context *ctx, int n) { struct ibv_sge list = { @@ -455,6 +495,7 @@ int main(int argc, char *argv[]) int use_event = 0; int routs; int rcnt, scnt; + int num_cq_events = 0; srand48(getpid() * time(NULL)); @@ -626,6 +667,8 @@ int main(int argc, char *argv[]) return 1; } + ++num_cq_events; + if (ev_cq != ctx->cq) { fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq); return 1; @@ -710,5 +753,13 @@ int main(int argc, char *argv[]) iters, usec / 1000000., usec / iters); } + ibv_ack_cq_events(ctx->cq, num_cq_events); + + if (pp_close_ctx(ctx)) + return 1; + + ibv_free_device_list(dev_list); + free(rem_dest); + return 0; } diff --git a/examples/srq_pingpong.c b/examples/srq_pingpong.c index 30dad47..cc57365 100644 --- a/examples/srq_pingpong.c +++ b/examples/srq_pingpong.c @@ -362,7 +362,7 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE); if (!ctx->mr) { - fprintf(stderr, "Couldn't allocate MR\n"); + fprintf(stderr, "Couldn't register MR\n"); return NULL; } @@ -428,6 +428,55 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, return ctx; } +int pp_close_ctx(struct pingpong_context *ctx, int num_qp) +{ + int i; + + for (i = 0; i < num_qp; ++i) { + if (ibv_destroy_qp(ctx->qp[i])) { + fprintf(stderr, "Couldn't destroy QP[%d]\n", i); + return 1; + } + } + + if (ibv_destroy_srq(ctx->srq)) { + fprintf(stderr, "Couldn't destroy SRQ\n"); + return 1; + } + + if (ibv_destroy_cq(ctx->cq)) { + fprintf(stderr, "Couldn't destroy CQ\n"); + return 1; + } + + if (ibv_dereg_mr(ctx->mr)) { + fprintf(stderr, "Couldn't deregister MR\n"); + return 1; + } + + if (ibv_dealloc_pd(ctx->pd)) { + fprintf(stderr, "Couldn't deallocate PD\n"); + return 1; + } + + if (ctx->channel) { + if (ibv_destroy_comp_channel(ctx->channel)) { + fprintf(stderr, "Couldn't destroy completion channel\n"); + return 1; + } + } + + if (ibv_close_device(ctx->context)) { + fprintf(stderr, "Couldn't release context\n"); + return 1; + } + + free(ctx->buf); + free(ctx); + + return 0; +} + static int pp_post_recv(struct pingpong_context *ctx, int n) { struct ibv_sge list = { @@ -521,6 +570,7 @@ int main(int argc, char *argv[]) int rcnt, scnt; int num_wc; int i; + int num_cq_events = 0; srand48(getpid() * time(NULL)); @@ -714,6 +764,8 @@ int main(int argc, char *argv[]) return 1; } + ++num_cq_events; + if (ev_cq != ctx->cq) { fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq); return 1; @@ -805,5 +857,13 @@ int main(int argc, char *argv[]) iters, usec / 1000000., usec / iters); } + ibv_ack_cq_events(ctx->cq, num_cq_events); + + if (pp_close_ctx(ctx, num_qp)) + return 1; + + ibv_free_device_list(dev_list); + free(rem_dest); + return 0; } diff --git a/examples/uc_pingpong.c b/examples/uc_pingpong.c index 55460d7..1b2b1b4 100644 --- a/examples/uc_pingpong.c +++ b/examples/uc_pingpong.c @@ -314,7 +314,7 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size, IBV_ACCESS_LOCAL_WRITE); if (!ctx->mr) { - fprintf(stderr, "Couldn't allocate MR\n"); + fprintf(stderr, "Couldn't register MR\n"); return NULL; } @@ -366,6 +366,46 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, return ctx; } +int pp_close_ctx(struct pingpong_context *ctx) +{ + if (ibv_destroy_qp(ctx->qp)) { + fprintf(stderr, "Couldn't destroy QP\n"); + return 1; + } + + if (ibv_destroy_cq(ctx->cq)) { + fprintf(stderr, "Couldn't destroy CQ\n"); + return 1; + } + + if (ibv_dereg_mr(ctx->mr)) { + fprintf(stderr, "Couldn't deregister MR\n"); + return 1; + } + + if (ibv_dealloc_pd(ctx->pd)) { + fprintf(stderr, "Couldn't deallocate PD\n"); + return 1; + } + + if (ctx->channel) { + if (ibv_destroy_comp_channel(ctx->channel)) { + fprintf(stderr, "Couldn't destroy completion channel\n"); + return 1; + } + } + + if (ibv_close_device(ctx->context)) { + fprintf(stderr, "Couldn't release context\n"); + return 1; + } + + free(ctx->buf); + free(ctx); + + return 0; +} + static int pp_post_recv(struct pingpong_context *ctx, int n) { struct ibv_sge list = { @@ -443,6 +483,7 @@ int main(int argc, char *argv[]) int use_event = 0; int routs; int rcnt, scnt; + int num_cq_events = 0; srand48(getpid() * time(NULL)); @@ -614,6 +655,8 @@ int main(int argc, char *argv[]) return 1; } + ++num_cq_events; + if (ev_cq != ctx->cq) { fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq); return 1; @@ -698,5 +741,13 @@ int main(int argc, char *argv[]) iters, usec / 1000000., usec / iters); } + ibv_ack_cq_events(ctx->cq, num_cq_events); + + if (pp_close_ctx(ctx)) + return 1; + + ibv_free_device_list(dev_list); + free(rem_dest); + return 0; } diff --git a/examples/ud_pingpong.c b/examples/ud_pingpong.c index 50a91df..d22ce8b 100644 --- a/examples/ud_pingpong.c +++ b/examples/ud_pingpong.c @@ -315,7 +315,7 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size + 40, IBV_ACCESS_LOCAL_WRITE); if (!ctx->mr) { - fprintf(stderr, "Couldn't allocate MR\n"); + fprintf(stderr, "Couldn't register MR\n"); return NULL; } @@ -367,6 +367,51 @@ static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size, return ctx; } +int pp_close_ctx(struct pingpong_context *ctx) +{ + if (ibv_destroy_qp(ctx->qp)) { + fprintf(stderr, "Couldn't destroy QP\n"); + return 1; + } + + if (ibv_destroy_cq(ctx->cq)) { + fprintf(stderr, "Couldn't destroy CQ\n"); + return 1; + } + + if (ibv_dereg_mr(ctx->mr)) { + fprintf(stderr, "Couldn't deregister MR\n"); + return 1; + } + + if (ibv_destroy_ah(ctx->ah)) { + fprintf(stderr, "Couldn't destroy AH\n"); + return 1; + } + + if (ibv_dealloc_pd(ctx->pd)) { + fprintf(stderr, "Couldn't deallocate PD\n"); + return 1; + } + + if (ctx->channel) { + if (ibv_destroy_comp_channel(ctx->channel)) { + fprintf(stderr, "Couldn't destroy completion channel\n"); + return 1; + } + } + + if (ibv_close_device(ctx->context)) { + fprintf(stderr, "Couldn't release context\n"); + return 1; + } + + free(ctx->buf); + free(ctx); + + return 0; +} + static int pp_post_recv(struct pingpong_context *ctx, int n) { struct ibv_sge list = { @@ -449,6 +494,7 @@ int main(int argc, char *argv[]) int use_event = 0; int routs; int rcnt, scnt; + int num_cq_events = 0; srand48(getpid() * time(NULL)); @@ -612,6 +658,8 @@ int main(int argc, char *argv[]) return 1; } + ++num_cq_events; + if (ev_cq != ctx->cq) { fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq); return 1; @@ -696,5 +744,13 @@ int main(int argc, char *argv[]) iters, usec / 1000000., usec / iters); } + ibv_ack_cq_events(ctx->cq, num_cq_events); + + if (pp_close_ctx(ctx)) + return 1; + + ibv_free_device_list(dev_list); + free(rem_dest); + return 0; } -- 2.46.0