LCOV - code coverage report
Current view: top level - core/iomgr - iomgr.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 70 70 100.0 %
Date: 2015-12-10 22:15:08 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "src/core/iomgr/iomgr.h"
      35             : 
      36             : #include <stdlib.h>
      37             : #include <string.h>
      38             : 
      39             : #include <grpc/support/alloc.h>
      40             : #include <grpc/support/log.h>
      41             : #include <grpc/support/string_util.h>
      42             : #include <grpc/support/sync.h>
      43             : #include <grpc/support/thd.h>
      44             : 
      45             : #include "src/core/iomgr/iomgr_internal.h"
      46             : #include "src/core/iomgr/timer_internal.h"
      47             : #include "src/core/support/string.h"
      48             : 
      49             : static gpr_mu g_mu;
      50             : static gpr_cv g_rcv;
      51             : static int g_shutdown;
      52             : static grpc_iomgr_object g_root_object;
      53             : 
      54        3457 : void grpc_iomgr_init(void) {
      55        3457 :   g_shutdown = 0;
      56        3457 :   gpr_mu_init(&g_mu);
      57        3457 :   gpr_cv_init(&g_rcv);
      58        3457 :   grpc_timer_list_init(gpr_now(GPR_CLOCK_MONOTONIC));
      59        3457 :   g_root_object.next = g_root_object.prev = &g_root_object;
      60        3457 :   g_root_object.name = "root";
      61        3457 :   grpc_iomgr_platform_init();
      62        3457 :   grpc_pollset_global_init();
      63        3457 : }
      64             : 
      65          10 : static size_t count_objects(void) {
      66             :   grpc_iomgr_object *obj;
      67          10 :   size_t n = 0;
      68         110 :   for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
      69         100 :     n++;
      70             :   }
      71          10 :   return n;
      72             : }
      73             : 
      74           1 : static void dump_objects(const char *kind) {
      75             :   grpc_iomgr_object *obj;
      76          11 :   for (obj = g_root_object.next; obj != &g_root_object; obj = obj->next) {
      77          10 :     gpr_log(GPR_DEBUG, "%s OBJECT: %s %p", kind, obj->name, obj);
      78             :   }
      79           1 : }
      80             : 
      81        3455 : void grpc_iomgr_shutdown(void) {
      82        3455 :   gpr_timespec shutdown_deadline = gpr_time_add(
      83             :       gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN));
      84        3455 :   gpr_timespec last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
      85        3455 :   grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
      86             : 
      87        3455 :   grpc_iomgr_platform_flush();
      88             : 
      89        3455 :   gpr_mu_lock(&g_mu);
      90        3455 :   g_shutdown = 1;
      91        7283 :   while (g_root_object.next != &g_root_object) {
      92         374 :     if (gpr_time_cmp(
      93             :             gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), last_warning_time),
      94             :             gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) {
      95           9 :       if (g_root_object.next != &g_root_object) {
      96           9 :         gpr_log(GPR_DEBUG, "Waiting for %d iomgr objects to be destroyed",
      97             :                 count_objects());
      98             :       }
      99           9 :       last_warning_time = gpr_now(GPR_CLOCK_REALTIME);
     100             :     }
     101         374 :     if (grpc_timer_check(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC),
     102             :                          NULL)) {
     103         269 :       gpr_mu_unlock(&g_mu);
     104         269 :       grpc_exec_ctx_flush(&exec_ctx);
     105         269 :       gpr_mu_lock(&g_mu);
     106         269 :       continue;
     107             :     }
     108         105 :     if (g_root_object.next != &g_root_object) {
     109         105 :       gpr_timespec short_deadline = gpr_time_add(
     110             :           gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(100, GPR_TIMESPAN));
     111         105 :       if (gpr_cv_wait(&g_rcv, &g_mu, short_deadline)) {
     112         100 :         if (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), shutdown_deadline) > 0) {
     113           1 :           if (g_root_object.next != &g_root_object) {
     114           1 :             gpr_log(GPR_DEBUG,
     115             :                     "Failed to free %d iomgr objects before shutdown deadline: "
     116             :                     "memory leaks are likely",
     117             :                     count_objects());
     118           1 :             dump_objects("LEAKED");
     119             :           }
     120           1 :           break;
     121             :         }
     122             :       }
     123             :     }
     124             :   }
     125        3455 :   gpr_mu_unlock(&g_mu);
     126             : 
     127        3455 :   grpc_timer_list_shutdown(&exec_ctx);
     128        3455 :   grpc_exec_ctx_finish(&exec_ctx);
     129             : 
     130             :   /* ensure all threads have left g_mu */
     131        3455 :   gpr_mu_lock(&g_mu);
     132        3455 :   gpr_mu_unlock(&g_mu);
     133             : 
     134        3455 :   grpc_pollset_global_shutdown();
     135        3455 :   grpc_iomgr_platform_shutdown();
     136        3455 :   gpr_mu_destroy(&g_mu);
     137        3455 :   gpr_cv_destroy(&g_rcv);
     138        3455 : }
     139             : 
     140       24974 : void grpc_iomgr_register_object(grpc_iomgr_object *obj, const char *name) {
     141       24974 :   obj->name = gpr_strdup(name);
     142       24974 :   gpr_mu_lock(&g_mu);
     143       24974 :   obj->next = &g_root_object;
     144       24974 :   obj->prev = g_root_object.prev;
     145       24974 :   obj->next->prev = obj->prev->next = obj;
     146       24974 :   gpr_mu_unlock(&g_mu);
     147       24974 : }
     148             : 
     149       24877 : void grpc_iomgr_unregister_object(grpc_iomgr_object *obj) {
     150       24877 :   gpr_mu_lock(&g_mu);
     151       24877 :   obj->next->prev = obj->prev;
     152       24877 :   obj->prev->next = obj->next;
     153       24877 :   gpr_cv_signal(&g_rcv);
     154       24877 :   gpr_mu_unlock(&g_mu);
     155       24877 :   gpr_free(obj->name);
     156       24877 : }

Generated by: LCOV version 1.11