]> git.openfabrics.org - ~shefty/libibverbs.git/commitdiff
Add resource cleanup at end of pingpong tests
authorDotan Barak <dotanb@dev.mellanox.co.il>
Wed, 17 Jan 2007 09:39:01 +0000 (11:39 +0200)
committerRoland Dreier <rolandd@cisco.com>
Tue, 23 Jan 2007 15:04:29 +0000 (07:04 -0800)
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 <dotanb@mellanox.co.il>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
examples/rc_pingpong.c
examples/srq_pingpong.c
examples/uc_pingpong.c
examples/ud_pingpong.c

index 0e17cfb04b310ef1272af31ff39b4f5daf0af976..82623dba6e5d355e52b5bb1fcedd9317ed3f0b0c 100644 (file)
@@ -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;
 }
index 30dad47dd042ab9f97bbb5cc17f3550d7c70ea72..cc5736597c153477f04996846a713b52b6fa568c 100644 (file)
@@ -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;
 }
index 55460d73506336c9b8eab7339d259214000b2ee9..1b2b1b44b182646f508c2cda626004c50ec69a88 100644 (file)
@@ -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;
 }
index 50a91dfb3b3e32609643cda2d57ec176586054aa..d22ce8b9b6f1dcc1830198299a80c1cf42ea9663 100644 (file)
@@ -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;
 }