tools/profiler/TracedTaskCommon.h
author Mike Hommey <mh+mozilla@glandium.org>
Wed, 26 Nov 2014 16:04:25 +0900
changeset 217581 5f1b17ceebe48984dc35c0de46f8c325931479ab
parent 215547 c5e61f0f7b3d3e380ce89a90e343ff4b4804f823
child 222858 86924ac6a94a9147bcae518239d7a33b5981062b
permissions -rw-r--r--
Bug 1094565 - Update sccache to e68dfc2. r=mshal

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef TRACED_TASK_COMMON_H
#define TRACED_TASK_COMMON_H

#include "base/task.h"
#include "GeckoTaskTracer.h"
#include "nsCOMPtr.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace tasktracer {

class TracedTaskCommon
{
public:
  TracedTaskCommon();
  virtual ~TracedTaskCommon() {}

protected:
  void Init();

  // Sets up the metadata on the current thread's TraceInfo for this task.
  // After Run(), ClearTraceInfo is called to reset the metadata.
  void SetTraceInfo();
  void ClearTraceInfo();

  // Its own task Id, an unique number base on its thread Id and a last unique
  // task Id stored in its TraceInfo.
  uint64_t mTaskId;

  uint64_t mSourceEventId;
  SourceEventType mSourceEventType;
};

class TracedRunnable : public TracedTaskCommon
                     , public nsRunnable
{
public:
  NS_DECL_NSIRUNNABLE

  TracedRunnable(nsIRunnable* aOriginalObj);

private:
  virtual ~TracedRunnable() {}

  nsCOMPtr<nsIRunnable> mOriginalObj;
};

class TracedTask : public TracedTaskCommon
                 , public Task
{
public:
  TracedTask(Task* aOriginalObj);
  ~TracedTask()
  {
    if (mOriginalObj) {
      delete mOriginalObj;
      mOriginalObj = nullptr;
    }
  }

  virtual void Run();

private:
  Task* mOriginalObj;
};

// FakeTracedTask is for tracking events that are not directly dispatched from
// their parents, e.g. The timer events.
class FakeTracedTask : public TracedTaskCommon
{
public:
  NS_INLINE_DECL_REFCOUNTING(FakeTracedTask)

  FakeTracedTask(int* aVptr);
  void BeginFakeTracedTask();
  void EndFakeTracedTask();
private:
  virtual ~FakeTracedTask() {}

  // No copy allowed.
  FakeTracedTask() MOZ_DELETE;
  FakeTracedTask(const FakeTracedTask& aTask) MOZ_DELETE;
  FakeTracedTask& operator=(const FakeTracedTask& aTask) MOZ_DELETE;
};

class AutoRunFakeTracedTask
{
public:
  AutoRunFakeTracedTask(FakeTracedTask* aFakeTracedTask);
  ~AutoRunFakeTracedTask();
private:
  nsRefPtr<FakeTracedTask> mFakeTracedTask;
};

} // namespace tasktracer
} // namespace mozilla

#endif