summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_shutdown_profile_dumper.cc
blob: 4330c6141ca755f1005e74d9c55b9fef7aa03e76 (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
140
141
142
143
144
145
146
147
148
// Copyright 2013 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 "content/browser/browser_shutdown_profile_dumper.h"

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_impl.h"
#include "components/tracing/tracing_switches.h"

namespace content {

BrowserShutdownProfileDumper::BrowserShutdownProfileDumper(
    const base::FilePath& dump_file_name)
    : dump_file_name_(dump_file_name),
      blocks_(0),
      dump_file_(NULL) {
}

BrowserShutdownProfileDumper::~BrowserShutdownProfileDumper() {
  WriteTracesToDisc();
}

static float GetTraceBufferPercentFull() {
  base::trace_event::TraceLogStatus status =
      base::trace_event::TraceLog::GetInstance()->GetStatus();
  return 100 * static_cast<float>(static_cast<double>(status.event_count) /
                                  status.event_capacity);
}

void BrowserShutdownProfileDumper::WriteTracesToDisc() {
  // Note: I have seen a usage of 0.000xx% when dumping - which fits easily.
  // Since the tracer stops when the trace buffer is filled, we'd rather save
  // what we have than nothing since we might see from the amount of events
  // that caused the problem.
  DVLOG(1) << "Flushing shutdown traces to disc. The buffer is "
           << GetTraceBufferPercentFull() << "% full.";
  DCHECK(!dump_file_);
  dump_file_ = base::OpenFile(dump_file_name_, "w+");
  if (!IsFileValid()) {
    LOG(ERROR) << "Failed to open performance trace file: "
               << dump_file_name_.value();
    return;
  }
  WriteString("{\"traceEvents\":");
  WriteString("[");

  // TraceLog::Flush() requires the calling thread to have a message loop.
  // As the message loop of the current thread may have quit, start another
  // thread for flushing the trace.
  base::WaitableEvent flush_complete_event(false, false);
  base::Thread flush_thread("browser_shutdown_trace_event_flush");
  flush_thread.Start();
  flush_thread.task_runner()->PostTask(
      FROM_HERE, base::Bind(&BrowserShutdownProfileDumper::EndTraceAndFlush,
                            base::Unretained(this),
                            base::Unretained(&flush_complete_event)));

  bool original_wait_allowed = base::ThreadRestrictions::SetWaitAllowed(true);
  flush_complete_event.Wait();
  base::ThreadRestrictions::SetWaitAllowed(original_wait_allowed);
}

void BrowserShutdownProfileDumper::EndTraceAndFlush(
    base::WaitableEvent* flush_complete_event) {
  base::trace_event::TraceLog::GetInstance()->SetDisabled();
  base::trace_event::TraceLog::GetInstance()->Flush(
      base::Bind(&BrowserShutdownProfileDumper::WriteTraceDataCollected,
                 base::Unretained(this),
                 base::Unretained(flush_complete_event)));
}

// static
base::FilePath BrowserShutdownProfileDumper::GetShutdownProfileFileName() {
  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();
  base::FilePath trace_file =
      command_line.GetSwitchValuePath(switches::kTraceShutdownFile);

  if (!trace_file.empty())
    return trace_file;

  // Default to saving the startup trace into the current dir.
  return base::FilePath().AppendASCII("chrometrace.log");
}

void BrowserShutdownProfileDumper::WriteTraceDataCollected(
    base::WaitableEvent* flush_complete_event,
    const scoped_refptr<base::RefCountedString>& events_str,
    bool has_more_events) {
  if (!IsFileValid()) {
    flush_complete_event->Signal();
    return;
  }
  if (blocks_) {
    // Blocks are not comma separated. Beginning with the second block we
    // start therefore to add one in front of the previous block.
    WriteString(",");
  }
  ++blocks_;
  WriteString(events_str->data());

  if (!has_more_events) {
    WriteString("]");
    WriteString("}");
    CloseFile();
    flush_complete_event->Signal();
  }
}

bool BrowserShutdownProfileDumper::IsFileValid() {
  return dump_file_ && (ferror(dump_file_) == 0);
}

void BrowserShutdownProfileDumper::WriteString(const std::string& string) {
  WriteChars(string.data(), string.size());
}

void BrowserShutdownProfileDumper::WriteChars(const char* chars, size_t size) {
  if (!IsFileValid())
    return;

  size_t written = fwrite(chars, 1, size, dump_file_);
  if (written != size) {
    LOG(ERROR) << "Error " << ferror(dump_file_)
               << " in fwrite() to trace file '" << dump_file_name_.value()
               << "'";
    CloseFile();
  }
}

void BrowserShutdownProfileDumper::CloseFile() {
  if (!dump_file_)
    return;
  base::CloseFile(dump_file_);
  dump_file_ = NULL;
}

}  // namespace content