tools/profiler/TracedTaskCommon.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "GeckoTaskTracerImpl.h"
michael@0 8 #include "TracedTaskCommon.h"
michael@0 9
michael@0 10 namespace mozilla {
michael@0 11 namespace tasktracer {
michael@0 12
michael@0 13 TracedTaskCommon::TracedTaskCommon()
michael@0 14 : mSourceEventId(0)
michael@0 15 , mSourceEventType(SourceEventType::UNKNOWN)
michael@0 16 {
michael@0 17 Init();
michael@0 18 }
michael@0 19
michael@0 20 void
michael@0 21 TracedTaskCommon::Init()
michael@0 22 {
michael@0 23 TraceInfo* info = GetOrCreateTraceInfo();
michael@0 24 NS_ENSURE_TRUE_VOID(info);
michael@0 25
michael@0 26 mTaskId = GenNewUniqueTaskId();
michael@0 27 mSourceEventId = info->mCurTraceSourceId;
michael@0 28 mSourceEventType = info->mCurTraceSourceType;
michael@0 29
michael@0 30 LogDispatch(mTaskId, info->mCurTaskId, mSourceEventId, mSourceEventType);
michael@0 31 }
michael@0 32
michael@0 33 void
michael@0 34 TracedTaskCommon::SetTraceInfo()
michael@0 35 {
michael@0 36 TraceInfo* info = GetOrCreateTraceInfo();
michael@0 37 NS_ENSURE_TRUE_VOID(info);
michael@0 38
michael@0 39 info->mCurTraceSourceId = mSourceEventId;
michael@0 40 info->mCurTraceSourceType = mSourceEventType;
michael@0 41 info->mCurTaskId = mTaskId;
michael@0 42 }
michael@0 43
michael@0 44 void
michael@0 45 TracedTaskCommon::ClearTraceInfo()
michael@0 46 {
michael@0 47 TraceInfo* info = GetOrCreateTraceInfo();
michael@0 48 NS_ENSURE_TRUE_VOID(info);
michael@0 49
michael@0 50 info->mCurTraceSourceId = 0;
michael@0 51 info->mCurTraceSourceType = SourceEventType::UNKNOWN;
michael@0 52 info->mCurTaskId = 0;
michael@0 53 }
michael@0 54
michael@0 55 /**
michael@0 56 * Implementation of class TracedRunnable.
michael@0 57 */
michael@0 58 TracedRunnable::TracedRunnable(nsIRunnable* aOriginalObj)
michael@0 59 : TracedTaskCommon()
michael@0 60 , mOriginalObj(aOriginalObj)
michael@0 61 {
michael@0 62 LogVirtualTablePtr(mTaskId, mSourceEventId, *(int**)(aOriginalObj));
michael@0 63 }
michael@0 64
michael@0 65 NS_IMETHODIMP
michael@0 66 TracedRunnable::Run()
michael@0 67 {
michael@0 68 LogBegin(mTaskId, mSourceEventId);
michael@0 69
michael@0 70 SetTraceInfo();
michael@0 71 nsresult rv = mOriginalObj->Run();
michael@0 72 ClearTraceInfo();
michael@0 73
michael@0 74 LogEnd(mTaskId, mSourceEventId);
michael@0 75 return rv;
michael@0 76 }
michael@0 77
michael@0 78 /**
michael@0 79 * Implementation of class TracedTask.
michael@0 80 */
michael@0 81 TracedTask::TracedTask(Task* aOriginalObj)
michael@0 82 : TracedTaskCommon()
michael@0 83 , mOriginalObj(aOriginalObj)
michael@0 84 {
michael@0 85 LogVirtualTablePtr(mTaskId, mSourceEventId, *(int**)(aOriginalObj));
michael@0 86 }
michael@0 87
michael@0 88 void
michael@0 89 TracedTask::Run()
michael@0 90 {
michael@0 91 LogBegin(mTaskId, mSourceEventId);
michael@0 92
michael@0 93 SetTraceInfo();
michael@0 94 mOriginalObj->Run();
michael@0 95 ClearTraceInfo();
michael@0 96
michael@0 97 LogEnd(mTaskId, mSourceEventId);
michael@0 98 }
michael@0 99
michael@0 100 FakeTracedTask::FakeTracedTask(int* aVptr)
michael@0 101 : TracedTaskCommon()
michael@0 102 {
michael@0 103 LogVirtualTablePtr(mTaskId, mSourceEventId, aVptr);
michael@0 104 }
michael@0 105
michael@0 106 FakeTracedTask::FakeTracedTask(const FakeTracedTask& aTask)
michael@0 107 {
michael@0 108 mTaskId = aTask.mTaskId;
michael@0 109 mSourceEventId = aTask.mSourceEventId;
michael@0 110 mSourceEventType = aTask.mSourceEventType;
michael@0 111 }
michael@0 112
michael@0 113 void
michael@0 114 FakeTracedTask::BeginFakeTracedTask()
michael@0 115 {
michael@0 116 LogBegin(mTaskId, mSourceEventId);
michael@0 117 SetTraceInfo();
michael@0 118 }
michael@0 119
michael@0 120 void
michael@0 121 FakeTracedTask::EndFakeTracedTask()
michael@0 122 {
michael@0 123 ClearTraceInfo();
michael@0 124 LogEnd(mTaskId, mSourceEventId);
michael@0 125 }
michael@0 126
michael@0 127 AutoRunFakeTracedTask::AutoRunFakeTracedTask(FakeTracedTask* aFakeTracedTask)
michael@0 128 : mInitialized(false)
michael@0 129 {
michael@0 130 if (aFakeTracedTask) {
michael@0 131 mInitialized = true;
michael@0 132 mFakeTracedTask = *aFakeTracedTask;
michael@0 133 mFakeTracedTask.BeginFakeTracedTask();
michael@0 134 }
michael@0 135 }
michael@0 136
michael@0 137 AutoRunFakeTracedTask::~AutoRunFakeTracedTask()
michael@0 138 {
michael@0 139 if (mInitialized) {
michael@0 140 mFakeTracedTask.EndFakeTracedTask();
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 /**
michael@0 145 * CreateTracedRunnable() returns a TracedRunnable wrapping the original
michael@0 146 * nsIRunnable object, aRunnable.
michael@0 147 */
michael@0 148 already_AddRefed<nsIRunnable>
michael@0 149 CreateTracedRunnable(nsIRunnable* aRunnable)
michael@0 150 {
michael@0 151 nsCOMPtr<nsIRunnable> runnable = new TracedRunnable(aRunnable);
michael@0 152 return runnable.forget();
michael@0 153 }
michael@0 154
michael@0 155 /**
michael@0 156 * CreateTracedTask() returns a TracedTask wrapping the original Task object,
michael@0 157 * aTask.
michael@0 158 */
michael@0 159 Task*
michael@0 160 CreateTracedTask(Task* aTask)
michael@0 161 {
michael@0 162 Task* task = new TracedTask(aTask);
michael@0 163 return task;
michael@0 164 }
michael@0 165
michael@0 166 /**
michael@0 167 * CreateFakeTracedTask() returns a FakeTracedTask tracking the event which is
michael@0 168 * not dispatched from its parent task directly, such as timer events.
michael@0 169 */
michael@0 170 FakeTracedTask*
michael@0 171 CreateFakeTracedTask(int* aVptr)
michael@0 172 {
michael@0 173 nsAutoPtr<FakeTracedTask> task(new FakeTracedTask(aVptr));
michael@0 174 return task.forget();
michael@0 175 }
michael@0 176
michael@0 177 } // namespace tasktracer
michael@0 178 } // namespace mozilla

mercurial