summaryrefslogtreecommitdiff
path: root/chromium/content/browser/tracing/power_tracing_agent.cc
blob: 6bf47e66bd2993cf1bb01be41b184d878adb8772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/lazy_instance.h"
#include "base/memory/singleton.h"
#include "base/trace_event/trace_event_impl.h"
#include "content/browser/tracing/battor_power_trace_provider.h"
#include "content/browser/tracing/power_tracing_agent.h"
#include "content/public/browser/browser_thread.h"

namespace content {

namespace {

const char kPowerTracingAgentName[] = "battor";
const char kPowerTraceLabel[] = "powerTraceAsString";

}  // namespace

// static
PowerTracingAgent* PowerTracingAgent::GetInstance() {
  return base::Singleton<PowerTracingAgent>::get();
}

PowerTracingAgent::PowerTracingAgent() : thread_("PowerTracingAgentThread") {
  battor_trace_provider_.reset(new BattorPowerTraceProvider());
}

PowerTracingAgent::~PowerTracingAgent() {}

std::string PowerTracingAgent::GetTracingAgentName() {
  return kPowerTracingAgentName;
}

std::string PowerTracingAgent::GetTraceEventLabel() {
  return kPowerTraceLabel;
}

bool PowerTracingAgent::StartAgentTracing(
    const base::trace_event::TraceConfig& trace_config) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // TODO(charliea): When system tracing is enabled in about://tracing, it will
  // trigger power tracing. We need a way of checking if BattOr is connected.
  // Currently, IsConnected() always returns false, so that we do not include
  // BattOr trace until it is hooked up.
  if (!battor_trace_provider_->IsConnected())
    return false;

  thread_.Start();

  thread_.task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&PowerTracingAgent::TraceOnThread, base::Unretained(this)));
  return true;
}

void PowerTracingAgent::StopAgentTracing(
    const StopAgentTracingCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(thread_.IsRunning());

  thread_.task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&PowerTracingAgent::FlushOnThread, base::Unretained(this),
                 callback));
}

void PowerTracingAgent::OnStopTracingDone(
    const StopAgentTracingCallback& callback,
    const scoped_refptr<base::RefCountedString>& result) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);

  // Pass the serialized events.
  callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result);

  // Stop the power tracing agent thread on file thread.
  BrowserThread::PostTask(
      BrowserThread::FILE, FROM_HERE,
      base::Bind(&base::Thread::Stop, base::Unretained(&thread_)));
}

void PowerTracingAgent::TraceOnThread() {
  DCHECK(thread_.task_runner()->BelongsToCurrentThread());
  battor_trace_provider_->StartTracing();
}

void PowerTracingAgent::FlushOnThread(
    const StopAgentTracingCallback& callback) {
  DCHECK(thread_.task_runner()->BelongsToCurrentThread());

  battor_trace_provider_->StopTracing();
  std::string battor_logs;
  battor_trace_provider_->GetLog(&battor_logs);
  scoped_refptr<base::RefCountedString> result =
      base::RefCountedString::TakeString(&battor_logs);
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&PowerTracingAgent::OnStopTracingDone,
                 base::Unretained(this),
                 callback,
                 result));
}

bool PowerTracingAgent::SupportsExplicitClockSync() {
  return true;
}

void PowerTracingAgent::RecordClockSyncMarker(
    int sync_id,
    const RecordClockSyncMarkerCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(SupportsExplicitClockSync());

  thread_.task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&PowerTracingAgent::RecordClockSyncMarkerOnThread,
                 base::Unretained(this),
                 sync_id,
                 callback));
}

void PowerTracingAgent::RecordClockSyncMarkerOnThread(
    int sync_id,
    const RecordClockSyncMarkerCallback& callback) {
  DCHECK(thread_.task_runner()->BelongsToCurrentThread());
  DCHECK(SupportsExplicitClockSync());

  base::TimeTicks issue_ts = base::TimeTicks::Now();
  battor_trace_provider_->RecordClockSyncMarker(sync_id);
  base::TimeTicks issue_end_ts = base::TimeTicks::Now();

  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(callback, sync_id, issue_ts, issue_end_ts));
}

}  // namespace content