ipc/chromium/src/base/trace_event.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/trace_event.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,155 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/trace_event.h"
     1.9 +
    1.10 +#include "base/file_path.h"
    1.11 +#include "base/file_util.h"
    1.12 +#include "base/path_service.h"
    1.13 +#include "base/platform_thread.h"
    1.14 +#include "base/process_util.h"
    1.15 +#include "base/string_util.h"
    1.16 +#include "base/time.h"
    1.17 +
    1.18 +#define USE_UNRELIABLE_NOW
    1.19 +
    1.20 +namespace base {
    1.21 +
    1.22 +static const char* kEventTypeNames[] = {
    1.23 +  "BEGIN",
    1.24 +  "END",
    1.25 +  "INSTANT"
    1.26 +};
    1.27 +
    1.28 +static const FilePath::CharType* kLogFileName =
    1.29 +    FILE_PATH_LITERAL("trace_%d.log");
    1.30 +
    1.31 +TraceLog::TraceLog() : enabled_(false), log_file_(NULL) {
    1.32 +  base::ProcessHandle proc = base::GetCurrentProcessHandle();
    1.33 +  process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc));
    1.34 +}
    1.35 +
    1.36 +TraceLog::~TraceLog() {
    1.37 +  Stop();
    1.38 +}
    1.39 +
    1.40 +// static
    1.41 +bool TraceLog::IsTracing() {
    1.42 +  TraceLog* trace = Singleton<TraceLog>::get();
    1.43 +  return trace->enabled_;
    1.44 +}
    1.45 +
    1.46 +// static
    1.47 +bool TraceLog::StartTracing() {
    1.48 +  TraceLog* trace = Singleton<TraceLog>::get();
    1.49 +  return trace->Start();
    1.50 +}
    1.51 +
    1.52 +bool TraceLog::Start() {
    1.53 +  if (enabled_)
    1.54 +    return true;
    1.55 +  enabled_ = OpenLogFile();
    1.56 +  if (enabled_) {
    1.57 +    Log("var raw_trace_events = [\n");
    1.58 +    trace_start_time_ = TimeTicks::Now();
    1.59 +    timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat);
    1.60 +  }
    1.61 +  return enabled_;
    1.62 +}
    1.63 +
    1.64 +// static
    1.65 +void TraceLog::StopTracing() {
    1.66 +  TraceLog* trace = Singleton<TraceLog>::get();
    1.67 +  return trace->Stop();
    1.68 +}
    1.69 +
    1.70 +void TraceLog::Stop() {
    1.71 +  if (enabled_) {
    1.72 +    enabled_ = false;
    1.73 +    Log("];\n");
    1.74 +    CloseLogFile();
    1.75 +    timer_.Stop();
    1.76 +  }
    1.77 +}
    1.78 +
    1.79 +void TraceLog::Heartbeat() {
    1.80 +  std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage());
    1.81 +  TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu);
    1.82 +}
    1.83 +
    1.84 +void TraceLog::CloseLogFile() {
    1.85 +  if (log_file_) {
    1.86 +    file_util::CloseFile(log_file_);
    1.87 +  }
    1.88 +}
    1.89 +
    1.90 +bool TraceLog::OpenLogFile() {
    1.91 +  FilePath::StringType pid_filename =
    1.92 +      StringPrintf(kLogFileName, base::GetCurrentProcId());
    1.93 +  FilePath log_file_path;
    1.94 +  if (!PathService::Get(base::DIR_EXE, &log_file_path))
    1.95 +    return false;
    1.96 +  log_file_path = log_file_path.Append(pid_filename);
    1.97 +  log_file_ = file_util::OpenFile(log_file_path, "a");
    1.98 +  if (!log_file_) {
    1.99 +    // try the current directory
   1.100 +    log_file_ = file_util::OpenFile(FilePath(pid_filename), "a");
   1.101 +    if (!log_file_) {
   1.102 +      return false;
   1.103 +    }
   1.104 +  }
   1.105 +  return true;
   1.106 +}
   1.107 +
   1.108 +void TraceLog::Trace(const std::string& name,
   1.109 +                     EventType type,
   1.110 +                     const void* id,
   1.111 +                     const std::wstring& extra,
   1.112 +                     const char* file,
   1.113 +                     int line) {
   1.114 +  if (!enabled_)
   1.115 +    return;
   1.116 +  Trace(name, type, id, WideToUTF8(extra), file, line);
   1.117 +}
   1.118 +
   1.119 +void TraceLog::Trace(const std::string& name,
   1.120 +                     EventType type,
   1.121 +                     const void* id,
   1.122 +                     const std::string& extra,
   1.123 +                     const char* file,
   1.124 +                     int line) {
   1.125 +  if (!enabled_)
   1.126 +    return;
   1.127 +
   1.128 +#ifdef USE_UNRELIABLE_NOW
   1.129 +  TimeTicks tick = TimeTicks::HighResNow();
   1.130 +#else
   1.131 +  TimeTicks tick = TimeTicks::Now();
   1.132 +#endif
   1.133 +  TimeDelta delta = tick - trace_start_time_;
   1.134 +  int64_t usec = delta.InMicroseconds();
   1.135 +  std::string msg =
   1.136 +    StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', "
   1.137 +                 "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', "
   1.138 +                 "'line_number':'%d', 'usec_begin': %I64d},\n",
   1.139 +                 base::GetCurrentProcId(),
   1.140 +                 PlatformThread::CurrentId(),
   1.141 +                 kEventTypeNames[type],
   1.142 +                 name.c_str(),
   1.143 +                 id,
   1.144 +                 extra.c_str(),
   1.145 +                 file,
   1.146 +                 line,
   1.147 +                 usec);
   1.148 +
   1.149 +  Log(msg);
   1.150 +}
   1.151 +
   1.152 +void TraceLog::Log(const std::string& msg) {
   1.153 +  AutoLock lock(file_lock_);
   1.154 +
   1.155 +  fprintf(log_file_, "%s", msg.c_str());
   1.156 +}
   1.157 +
   1.158 +} // namespace base

mercurial