Bug 1319669 - Handle TaskTracer at STS thread and Cache thread. r=honzab
authorThinker K.F. Li <thinker@codemud.net>
Fri, 09 Dec 2016 14:09:00 -0500
changeset 325617 66d47555ad2215eae7bd1f1bcebdae66ab0e8e9a
parent 325616 33044cd4c5dbe20994d10541db6ecbe9180fdc69
child 325618 e83eaf2380c65400dc03c6f3615d4b2cef669af3
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewershonzab
bugs1319669
milestone53.0a1
Bug 1319669 - Handle TaskTracer at STS thread and Cache thread. r=honzab
netwerk/base/nsSocketTransportService2.cpp
netwerk/cache2/CacheIOThread.cpp
netwerk/protocol/http/HttpChannelChild.cpp
netwerk/protocol/http/nsHttpChannel.cpp
netwerk/protocol/http/nsHttpHandler.cpp
tools/profiler/tasktracer/SourceEventTypeMap.h
--- a/netwerk/base/nsSocketTransportService2.cpp
+++ b/netwerk/base/nsSocketTransportService2.cpp
@@ -27,16 +27,20 @@
 #include "nsIFile.h"
 #include "nsIWidget.h"
 #include "mozilla/dom/FlyWebService.h"
 
 #if defined(XP_WIN)
 #include "mozilla/WindowsVersion.h"
 #endif
 
+#ifdef MOZ_TASK_TRACER
+#include "GeckoTaskTracer.h"
+#endif
+
 namespace mozilla {
 namespace net {
 
 LazyLogModule gSocketTransportLog("nsSocketTransport");
 LazyLogModule gUDPSocketLog("UDPSocket");
 LazyLogModule gTCPSocketLog("TCPSocket");
 
 nsSocketTransportService *gSocketTransportService = nullptr;
@@ -231,18 +235,23 @@ nsSocketTransportService::CanAttachSocke
 
 nsresult
 nsSocketTransportService::DetachSocket(SocketContext *listHead, SocketContext *sock)
 {
     SOCKET_LOG(("nsSocketTransportService::DetachSocket [handler=%p]\n", sock->mHandler));
     MOZ_ASSERT((listHead == mActiveList) || (listHead == mIdleList),
                "DetachSocket invalid head");
 
-    // inform the handler that this socket is going away
-    sock->mHandler->OnSocketDetached(sock->mFD);
+    {
+#ifdef MOZ_TASK_TRACER
+	tasktracer::AutoSourceEvent taskTracerEvent(tasktracer::SourceEventType::SocketIO);
+#endif
+        // inform the handler that this socket is going away
+        sock->mHandler->OnSocketDetached(sock->mFD);
+    }
     mSentBytesCount += sock->mHandler->ByteCountSent();
     mReceivedBytesCount += sock->mHandler->ByteCountReceived();
 
     // cleanup
     sock->mFD = nullptr;
     NS_RELEASE(sock->mHandler);
 
     if (listHead == mActiveList)
@@ -1126,16 +1135,19 @@ nsSocketTransportService::DoPollIteratio
         //
         // service "active" sockets...
         //
         uint32_t numberOfOnSocketReadyCalls = 0;
         for (i=0; i<int32_t(mActiveCount); ++i) {
             PRPollDesc &desc = mPollList[i+1];
             SocketContext &s = mActiveList[i];
             if (n > 0 && desc.out_flags != 0) {
+#ifdef MOZ_TASK_TRACER
+		tasktracer::AutoSourceEvent taskTracerEvent(tasktracer::SourceEventType::SocketIO);
+#endif
                 s.mElapsedTime = 0;
                 s.mHandler->OnSocketReady(desc.fd, desc.out_flags);
                 numberOfOnSocketReadyCalls++;
             }
             // check for timeout errors unless disabled...
             else if (s.mHandler->mPollTimeout != UINT16_MAX) {
                 // update elapsed time counter
                 // (NOTE: We explicitly cast UINT16_MAX to be an unsigned value
@@ -1145,16 +1157,19 @@ nsSocketTransportService::DoPollIteratio
                 if (MOZ_UNLIKELY(pollInterval >
                                 static_cast<uint32_t>(UINT16_MAX) -
                                 s.mElapsedTime))
                     s.mElapsedTime = UINT16_MAX;
                 else
                     s.mElapsedTime += uint16_t(pollInterval);
                 // check for timeout expiration 
                 if (s.mElapsedTime >= s.mHandler->mPollTimeout) {
+#ifdef MOZ_TASK_TRACER
+		    tasktracer::AutoSourceEvent taskTracerEvent(tasktracer::SourceEventType::SocketIO);
+#endif
                     s.mElapsedTime = 0;
                     s.mHandler->OnSocketReady(desc.fd, -1);
                     numberOfOnSocketReadyCalls++;
                 }
             }
         }
         if (mTelemetryEnabledPref) {
             Telemetry::Accumulate(
@@ -1315,16 +1330,19 @@ nsSocketTransportService::NotifyKeepaliv
 {
     MOZ_ASSERT(sock, "SocketContext cannot be null!");
     MOZ_ASSERT(sock->mHandler, "SocketContext does not have a handler!");
 
     if (!sock || !sock->mHandler) {
         return;
     }
 
+#ifdef MOZ_TASK_TRACER
+    tasktracer::AutoSourceEvent taskTracerEvent(tasktracer::SourceEventType::SocketIO);
+#endif
     sock->mHandler->OnKeepaliveEnabledPrefChange(mKeepaliveEnabledPref);
 }
 
 NS_IMETHODIMP
 nsSocketTransportService::Observe(nsISupports *subject,
                                   const char *topic,
                                   const char16_t *data)
 {
--- a/netwerk/cache2/CacheIOThread.cpp
+++ b/netwerk/cache2/CacheIOThread.cpp
@@ -10,16 +10,21 @@
 #include "nsPrintfCString.h"
 #include "nsThreadUtils.h"
 #include "mozilla/IOInterposer.h"
 
 #ifdef XP_WIN
 #include <windows.h>
 #endif
 
+#ifdef MOZ_TASK_TRACER
+#include "GeckoTaskTracer.h"
+#include "TracedTaskCommon.h"
+#endif
+
 namespace mozilla {
 namespace net {
 
 namespace { // anon
 
 class CacheIOTelemetry
 {
 public:
@@ -315,16 +320,20 @@ nsresult CacheIOThread::DispatchAfterPen
 
   return DispatchInternal(do_AddRef(aRunnable), OPEN_PRIORITY);
 }
 
 nsresult CacheIOThread::DispatchInternal(already_AddRefed<nsIRunnable> aRunnable,
 					 uint32_t aLevel)
 {
   nsCOMPtr<nsIRunnable> runnable(aRunnable);
+#ifdef MOZ_TASK_TRACER
+  runnable = tasktracer::CreateTracedRunnable(runnable.forget());
+  (static_cast<tasktracer::TracedRunnable*>(runnable.get()))->DispatchTask();
+#endif
 
   if (NS_WARN_IF(!runnable))
     return NS_ERROR_NULL_POINTER;
 
   mMonitor.AssertCurrentThreadOwns();
 
   ++mQueueLength[aLevel];
   mEventQueue[aLevel].AppendElement(runnable.forget());
--- a/netwerk/protocol/http/HttpChannelChild.cpp
+++ b/netwerk/protocol/http/HttpChannelChild.cpp
@@ -39,16 +39,20 @@
 #include "nsIDeprecationWarner.h"
 #include "nsICompressConvStats.h"
 #include "nsStreamUtils.h"
 
 #ifdef OS_POSIX
 #include "chrome/common/file_descriptor_set_posix.h"
 #endif
 
+#ifdef MOZ_TASK_TRACER
+#include "GeckoTaskTracer.h"
+#endif
+
 using namespace mozilla::dom;
 using namespace mozilla::ipc;
 
 namespace mozilla {
 namespace net {
 
 extern bool
 WillRedirect(nsHttpResponseHead * response);
@@ -1910,16 +1914,24 @@ HttpChannelChild::AsyncOpen(nsIStreamLis
     return mStatus;
 
   NS_ENSURE_TRUE(gNeckoChild != nullptr, NS_ERROR_FAILURE);
   NS_ENSURE_ARG_POINTER(listener);
   NS_ENSURE_TRUE(!mIsPending, NS_ERROR_IN_PROGRESS);
   NS_ENSURE_TRUE(!mWasOpened, NS_ERROR_ALREADY_OPENED);
 
   mAsyncOpenTime = TimeStamp::Now();
+#ifdef MOZ_TASK_TRACER
+  nsCOMPtr<nsIURI> uri;
+  GetURI(getter_AddRefs(uri));
+  nsAutoCString urispec;
+  uri->GetSpec(urispec);
+  tasktracer::AddLabel("HttpChannelChild::AsyncOpen %s", urispec.get());
+#endif
+  
 
   // Port checked in parent, but duplicate here so we can return with error
   // immediately
   nsresult rv;
   rv = NS_CheckPortSafety(mURI);
   if (NS_FAILED(rv))
     return rv;
 
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -98,16 +98,20 @@
 #include "nsCORSListenerProxy.h"
 #include "nsISocketProvider.h"
 #include "mozilla/net/Predictor.h"
 #include "CacheControlParser.h"
 #include "nsMixedContentBlocker.h"
 #include "HSTSPrimerListener.h"
 #include "CacheStorageService.h"
 
+#ifdef MOZ_TASK_TRACER
+#include "GeckoTaskTracer.h"
+#endif
+
 namespace mozilla { namespace net {
 
 namespace {
 
 // Monotonically increasing ID for generating unique cache entries per
 // intercepted channel.
 static uint64_t gNumIntercepted = 0;
 
@@ -5685,16 +5689,28 @@ nsHttpChannel::AsyncOpen(nsIStreamListen
     MOZ_ASSERT(!mLoadInfo ||
                mLoadInfo->GetSecurityMode() == 0 ||
                mLoadInfo->GetInitialSecurityCheckDone() ||
                (mLoadInfo->GetSecurityMode() == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL &&
                 nsContentUtils::IsSystemPrincipal(mLoadInfo->LoadingPrincipal())),
                "security flags in loadInfo but asyncOpen2() not called");
 
     LOG(("nsHttpChannel::AsyncOpen [this=%p]\n", this));
+#ifdef MOZ_TASK_TRACER
+    {
+        uint64_t sourceEventId, parentTaskId;
+        tasktracer::SourceEventType sourceEventType;
+        GetCurTraceInfo(&sourceEventId, &parentTaskId, &sourceEventType);
+        nsCOMPtr<nsIURI> uri;
+        GetURI(getter_AddRefs(uri));
+        nsAutoCString urispec;
+        uri->GetSpec(urispec);
+        tasktracer::AddLabel("nsHttpChannel::AsyncOpen %s", urispec.get());
+    }
+#endif
 
     NS_CompareLoadInfoAndLoadContext(this);
 
 #ifdef DEBUG
     AssertPrivateBrowsingId();
 #endif
 
     NS_ENSURE_ARG_POINTER(listener);
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -71,16 +71,20 @@
 #include <windows.h>
 #endif
 
 #if defined(XP_MACOSX)
 #include <CoreServices/CoreServices.h>
 #include "nsCocoaFeatures.h"
 #endif
 
+#ifdef MOZ_TASK_TRACER
+#include "GeckoTaskTracer.h"
+#endif
+
 //-----------------------------------------------------------------------------
 #include "mozilla/net/HttpChannelChild.h"
 
 
 #define UA_PREF_PREFIX          "general.useragent."
 #ifdef XP_WIN
 #define UA_SPARE_PLATFORM
 #endif
@@ -1992,16 +1996,24 @@ nsHttpHandler::NewProxiedChannel2(nsIURI
                                   nsILoadInfo* aLoadInfo,
                                   nsIChannel** result)
 {
     RefPtr<HttpBaseChannel> httpChannel;
 
     LOG(("nsHttpHandler::NewProxiedChannel [proxyInfo=%p]\n",
         givenProxyInfo));
 
+#ifdef MOZ_TASK_TRACER
+    {
+        nsAutoCString urispec;
+        uri->GetSpec(urispec);
+        tasktracer::AddLabel("nsHttpHandler::NewProxiedChannel2 %s", urispec.get());
+    }
+#endif
+
     nsCOMPtr<nsProxyInfo> proxyInfo;
     if (givenProxyInfo) {
         proxyInfo = do_QueryInterface(givenProxyInfo);
         NS_ENSURE_ARG(proxyInfo);
     }
 
     bool https;
     nsresult rv = uri->SchemeIs("https", &https);
--- a/tools/profiler/tasktracer/SourceEventTypeMap.h
+++ b/tools/profiler/tasktracer/SourceEventTypeMap.h
@@ -8,8 +8,9 @@
 
 SOURCE_EVENT_NAME(Unknown)
 SOURCE_EVENT_NAME(Touch)
 SOURCE_EVENT_NAME(Mouse)
 SOURCE_EVENT_NAME(Key)
 SOURCE_EVENT_NAME(Bluetooth)
 SOURCE_EVENT_NAME(Unixsocket)
 SOURCE_EVENT_NAME(Wifi)
+SOURCE_EVENT_NAME(SocketIO)