Instrument EventHandlers' execution
authorAndre Pinto <aap@fb.com>
Wed, 27 May 2015 02:06:05 +0000 (19:06 -0700)
committerPavlo Kushnir <pavlo@fb.com>
Thu, 28 May 2015 00:53:09 +0000 (17:53 -0700)
Summary:
Add the option to instrument every EventHandler that runs in a
EventBase.

Test Plan:
Unit tests

Reviewed By: davejwatson@fb.com

Subscribers: alikhtarov, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2044546

Tasks: 6958000

Signature: t1:2044546:1432221358:6500d6c1a6631d7a06a18e874d051f2ff3108e10

folly/Makefile.am
folly/experimental/ExecutionObserver.h [new file with mode: 0644]
folly/experimental/fibers/ExecutionObserver.h [deleted file]
folly/experimental/fibers/FiberManager.h
folly/io/async/EventBase.cpp
folly/io/async/EventBase.h
folly/io/async/EventHandler.cpp

index 0602265ec12bbe901856c04f4a3a63e7ccaf4e33..38776bdb2e9c08c3718e1107b1fdeccd8e71fe3b 100644 (file)
@@ -71,6 +71,7 @@ nobase_follyinclude_HEADERS = \
        EvictingCacheMap.h \
        experimental/AutoTimer.h \
        experimental/Bits.h \
+       experimental/ExecutionObserver.h \
        experimental/EliasFanoCoding.h \
        experimental/EventCount.h \
        experimental/fibers/AddTasks.h \
@@ -80,7 +81,6 @@ nobase_follyinclude_HEADERS = \
        experimental/fibers/BoostContextCompatibility.h \
        experimental/fibers/EventBaseLoopController.h \
        experimental/fibers/EventBaseLoopController-inl.h \
-       experimental/fibers/ExecutionObserver.h \
        experimental/fibers/Fiber.h \
        experimental/fibers/Fiber-inl.h \
        experimental/fibers/FiberManager.h \
diff --git a/folly/experimental/ExecutionObserver.h b/folly/experimental/ExecutionObserver.h
new file mode 100644 (file)
index 0000000..1977d0c
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2015 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <cstdint>
+
+namespace folly {
+
+/**
+ * Observes the execution of a task.
+ */
+class ExecutionObserver {
+ public:
+  virtual ~ExecutionObserver() { }
+
+  /**
+   * Called when a task is about to start executing.
+   *
+   * @param id Unique id for the task which is starting.
+   */
+  virtual void starting(uintptr_t id) noexcept = 0;
+
+  /**
+   * Called when a task is ready to run.
+   *
+   * @param id Unique id for the task which is ready to run.
+   */
+  virtual void runnable(uintptr_t id) noexcept = 0;
+
+  /**
+   * Called just after a task stops executing.
+   *
+   * @param id Unique id for the task which stopped.
+   */
+  virtual void stopped(uintptr_t id) noexcept = 0;
+};
+
+} // namespace folly
diff --git a/folly/experimental/fibers/ExecutionObserver.h b/folly/experimental/fibers/ExecutionObserver.h
deleted file mode 100644 (file)
index ef5f8fb..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright 2015 Facebook, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#pragma once
-
-#include <cstdint>
-
-namespace folly { namespace fibers {
-
-/**
- * Observes the execution of a task.
- */
-class ExecutionObserver {
- public:
-  virtual ~ExecutionObserver() { }
-
-  /**
-   * Called when a task is about to start executing.
-   *
-   * @param id Unique id for the fiber which is starting.
-   */
-  virtual void starting(uintptr_t id) noexcept = 0;
-
-  /**
-   * Called when a task is ready to run.
-   *
-   * @param id Unique id for the fiber which is ready to run.
-   */
-  virtual void runnable(uintptr_t id) noexcept = 0;
-
-  /**
-   * Called just after a task stops executing.
-   *
-   * @param id Unique id for the fiber which is stopping.
-   */
-  virtual void stopped(uintptr_t id) noexcept = 0;
-};
-
-}} // namespace folly::fibers
index 468a06da8b955475461019e5a1a52ad732201fdd..ecd1a9000700e89483547ba5cdba7ee58ef73b7c 100644 (file)
@@ -29,8 +29,8 @@
 #include <folly/IntrusiveList.h>
 #include <folly/futures/Try.h>
 
+#include <folly/experimental/ExecutionObserver.h>
 #include <folly/experimental/fibers/BoostContextCompatibility.h>
-#include <folly/experimental/fibers/ExecutionObserver.h>
 #include <folly/experimental/fibers/Fiber.h>
 #include <folly/experimental/fibers/traits.h>
 
index 73a6af57af346823a3b6a0c8b207d4b25a7ee173..a3e5bdc92fd96b6b4bcd133e0f728d908310f069 100644 (file)
@@ -155,7 +155,8 @@ EventBase::EventBase(bool enableTimeMeasurement)
   , latestLoopCnt_(nextLoopCnt_)
   , startWork_(0)
   , observer_(nullptr)
-  , observerSampleCount_(0) {
+  , observerSampleCount_(0)
+  , executionObserver_(nullptr) {
   {
     std::lock_guard<std::mutex> lock(libevent_mutex_);
 
@@ -193,7 +194,8 @@ EventBase::EventBase(event_base* evb, bool enableTimeMeasurement)
   , latestLoopCnt_(nextLoopCnt_)
   , startWork_(0)
   , observer_(nullptr)
-  , observerSampleCount_(0) {
+  , observerSampleCount_(0)
+  , executionObserver_(nullptr) {
   if (UNLIKELY(evb_ == nullptr)) {
     LOG(ERROR) << "EventBase(): Pass nullptr as event base.";
     throw std::invalid_argument("EventBase(): event base cannot be nullptr");
index 3e28d29403c9f8673b97bbd2908724b50ebda7e9..843ab464018bede18daeac0230f3721b7fcea2f8 100644 (file)
@@ -21,6 +21,7 @@
 #include <folly/io/async/TimeoutManager.h>
 #include <folly/io/async/Request.h>
 #include <folly/Executor.h>
+#include <folly/experimental/ExecutionObserver.h>
 #include <folly/futures/DrivableExecutor.h>
 #include <memory>
 #include <stack>
@@ -545,6 +546,23 @@ bool runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn);
     return observer_;
   }
 
+  /**
+   * Setup execution observation/instrumentation for every EventHandler
+   * executed in this EventBase.
+   *
+   * @param executionObserver   EventHandle's execution observer.
+   */
+  void setExecutionObserver(ExecutionObserver* observer) {
+    executionObserver_ = observer;
+  }
+
+  /**
+   * Gets the execution observer associated with this EventBase.
+   */
+  ExecutionObserver* getExecutionObserver() {
+    return executionObserver_;
+  }
+
   /**
    * Set the name of the thread that runs this event base.
    */
@@ -702,6 +720,9 @@ bool runImmediatelyOrRunInEventBaseThreadAndWait(const Cob& fn);
   std::shared_ptr<EventBaseObserver> observer_;
   uint32_t observerSampleCount_;
 
+  // EventHandler's execution observer.
+  ExecutionObserver* executionObserver_;
+
   // Name of the thread running this EventBase
   std::string name_;
 };
index 890bc83cd17bde96add891e84c87f0e7a6ab4704..dcccfafbb047b7e529ddc555f8cade928858d9aa 100644 (file)
@@ -147,10 +147,19 @@ void EventHandler::libeventCallback(int fd, short events, void* arg) {
   EventHandler* handler = reinterpret_cast<EventHandler*>(arg);
   assert(fd == handler->event_.ev_fd);
 
+  auto observer = handler->eventBase_->getExecutionObserver();
+  if (observer) {
+    observer->starting(reinterpret_cast<uintptr_t>(handler));
+  }
+
   // this can't possibly fire if handler->eventBase_ is nullptr
   (void) handler->eventBase_->bumpHandlingTime();
 
   handler->handlerReady(events);
+
+  if (observer) {
+    observer->stopped(reinterpret_cast<uintptr_t>(handler));
+  }
 }
 
 void EventHandler::setEventBase(EventBase* eventBase) {