Jelajahi Sumber

Put in blocking point annotations at places in the code where we may block for reasons other than synchronization

Vijay Pai 10 tahun lalu
induk
melakukan
ba130550fc

+ 44 - 0
src/core/iomgr/block_annotate.h

@@ -0,0 +1,44 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_INTERNAL_CORE_IOMGR_BLOCK_ANNOTATE_H
+#define GRPC_INTERNAL_CORE_IOMGR_BLOCK_ANNOTATE_H
+
+/* These annotations identify the beginning and end of regions where
+   the code may block for reasons other than synchronization functions.
+   These include poll, epoll, and getaddrinfo. */
+
+#define GRPC_IOMGR_START_BLOCKING_REGION do {} while (0)
+#define GRPC_IOMGR_END_BLOCKING_REGION do {} while (0)
+
+#endif /* GRPC_INTERNAL_CORE_IOMGR_BLOCK_ANNOTATE_H */

+ 5 - 0
src/core/iomgr/pollset_multipoller_with_epoll.c

@@ -41,6 +41,7 @@
 #include <sys/epoll.h>
 #include <unistd.h>
 
+#include "src/core/iomgr/block_annotate.h"
 #include "src/core/iomgr/fd_posix.h"
 #include <grpc/support/alloc.h>
 #include <grpc/support/log.h>
@@ -180,7 +181,9 @@ static void multipoll_with_epoll_pollset_maybe_work(
   pfds[1].events = POLLIN;
   pfds[1].revents = 0;
 
+  GRPC_IOMGR_START_BLOCKING_REGION;
   poll_rv = grpc_poll_function(pfds, 2, timeout_ms);
+  GRPC_IOMGR_END_BLOCKING_REGION;
 
   if (poll_rv < 0) {
     if (errno != EINTR) {
@@ -194,7 +197,9 @@ static void multipoll_with_epoll_pollset_maybe_work(
     }
     if (pfds[1].revents) {
       do {
+        GRPC_IOMGR_START_BLOCKING_REGION;
         ep_rv = epoll_wait(h->epoll_fd, ep_ev, GRPC_EPOLL_MAX_EVENTS, 0);
+        GRPC_IOMGR_END_BLOCKING_REGION;
         if (ep_rv < 0) {
           if (errno != EINTR) {
             gpr_log(GPR_ERROR, "epoll_wait() failed: %s", strerror(errno));

+ 3 - 0
src/core/iomgr/pollset_multipoller_with_poll_posix.c

@@ -42,6 +42,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "src/core/iomgr/block_annotate.h"
 #include "src/core/iomgr/fd_posix.h"
 #include "src/core/iomgr/iomgr_internal.h"
 #include <grpc/support/alloc.h>
@@ -145,7 +146,9 @@ static void multipoll_with_poll_pollset_maybe_work(
                                                POLLOUT, &watchers[i]);
   }
 
+  GRPC_IOMGR_START_BLOCKING_REGION;
   r = grpc_poll_function(pfds, pfd_count, timeout);
+  GRPC_IOMGR_END_BLOCKING_REGION;
 
   for (i = 1; i < pfd_count; i++) {
     grpc_fd_end_poll(&watchers[i], pfds[i].revents & POLLIN,

+ 3 - 0
src/core/iomgr/pollset_posix.c

@@ -43,6 +43,7 @@
 #include <unistd.h>
 
 #include "src/core/iomgr/alarm_internal.h"
+#include "src/core/iomgr/block_annotate.h"
 #include "src/core/iomgr/fd_posix.h"
 #include "src/core/iomgr/iomgr_internal.h"
 #include "src/core/iomgr/socket_utils_posix.h"
@@ -453,7 +454,9 @@ static void basic_pollset_maybe_work(grpc_pollset *pollset,
 
   /* poll fd count (argument 2) is shortened by one if we have no events
      to poll on - such that it only includes the kicker */
+  GRPC_IOMGR_START_BLOCKING_REGION;
   r = grpc_poll_function(pfd, nfds, timeout);
+  GRPC_IOMGR_END_BLOCKING_REGION;
   GRPC_TIMER_MARK(GRPC_PTAG_POLL_FINISHED, r);
 
   if (fd) {

+ 6 - 0
src/core/iomgr/resolve_address_posix.c

@@ -41,6 +41,7 @@
 #include <sys/un.h>
 #include <string.h>
 
+#include "src/core/iomgr/block_annotate.h"
 #include "src/core/iomgr/iomgr_internal.h"
 #include "src/core/iomgr/sockaddr_utils.h"
 #include "src/core/support/string.h"
@@ -102,14 +103,19 @@ grpc_resolved_addresses *grpc_blocking_resolve_address(
   hints.ai_socktype = SOCK_STREAM; /* stream socket */
   hints.ai_flags = AI_PASSIVE;     /* for wildcard IP address */
 
+  GRPC_IOMGR_START_BLOCKING_REGION;
   s = getaddrinfo(host, port, &hints, &result);
+  GRPC_IOMGR_END_BLOCKING_REGION;
+
   if (s != 0) {
     /* Retry if well-known service name is recognized */
     char *svc[][2] = {{"http", "80"}, {"https", "443"}};
     int i;
     for (i = 0; i < (int)(sizeof(svc) / sizeof(svc[0])); i++) {
       if (strcmp(port, svc[i][0]) == 0) {
+        GRPC_IOMGR_START_BLOCKING_REGION;
         s = getaddrinfo(host, svc[i][1], &hints, &result);
+        GRPC_IOMGR_END_BLOCKING_REGION;
         break;
       }
     }

+ 3 - 0
src/core/iomgr/resolve_address_windows.c

@@ -40,6 +40,7 @@
 #include <sys/types.h>
 #include <string.h>
 
+#include "src/core/iomgr/block_annotate.h"
 #include "src/core/iomgr/iomgr_internal.h"
 #include "src/core/iomgr/sockaddr_utils.h"
 #include "src/core/support/string.h"
@@ -88,7 +89,9 @@ grpc_resolved_addresses *grpc_blocking_resolve_address(
   hints.ai_socktype = SOCK_STREAM; /* stream socket */
   hints.ai_flags = AI_PASSIVE;     /* for wildcard IP address */
 
+  GRPC_IOMGR_START_BLOCKING_REGION;
   s = getaddrinfo(host, port, &hints, &result);
+  GRPC_IOMGR_END_BLOCKING_REGION;
   if (s != 0) {
     gpr_log(GPR_ERROR, "getaddrinfo: %s", gai_strerror(s));
     goto done;