OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "shell/tracer.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/synchronization/waitable_event.h" | |
11 #include "base/threading/thread.h" | |
12 #include "base/trace_event/trace_event.h" | |
13 | |
14 namespace shell { | |
15 | |
16 Tracer::Tracer() : tracing_(false), blocks_written_(0u), trace_file_(nullptr) { | |
17 } | |
18 Tracer::~Tracer() { | |
viettrungluu
2015/04/07 22:05:44
nit: blank line above?
| |
19 } | |
20 | |
21 void Tracer::Start(std::string categories) { | |
22 tracing_ = true; | |
23 base::trace_event::CategoryFilter category_filter(categories); | |
24 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
25 category_filter, base::trace_event::TraceLog::RECORDING_MODE, | |
26 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); | |
27 } | |
28 | |
29 void Tracer::StopAndFlushToFile(std::string filename) { | |
30 if (tracing_) | |
31 StopTracingAndFlushToDisk(filename); | |
32 } | |
33 | |
34 void Tracer::EndTraceAndFlush(const std::string& filename, | |
35 const base::Closure& done_callback) { | |
36 trace_file_ = fopen(filename.c_str(), "w+"); | |
37 PCHECK(trace_file_); | |
38 static const char kStart[] = "{\"traceEvents\":["; | |
39 fwrite(kStart, 1, strlen(kStart), trace_file_); | |
40 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
41 base::trace_event::TraceLog::GetInstance()->Flush(base::Bind( | |
42 &Tracer::WriteTraceDataCollected, base::Unretained(this), done_callback)); | |
43 } | |
44 | |
45 void Tracer::WriteTraceDataCollected( | |
46 const base::Closure& done_callback, | |
47 const scoped_refptr<base::RefCountedString>& events_str, | |
48 bool has_more_events) { | |
49 if (blocks_written_) { | |
viettrungluu
2015/04/07 22:05:44
nit: no braces? (or braces elsewhere)
| |
50 fwrite(",", 1, 1, trace_file_); | |
51 } | |
52 | |
53 ++blocks_written_; | |
54 fwrite(events_str->data().c_str(), 1, events_str->data().length(), | |
55 trace_file_); | |
56 if (!has_more_events) { | |
57 static const char kEnd[] = "]}"; | |
58 fwrite(kEnd, 1, strlen(kEnd), trace_file_); | |
viettrungluu
2015/04/07 22:05:44
nit: #include <string.h>
| |
59 PCHECK(fclose(trace_file_) == 0); | |
60 trace_file_ = nullptr; | |
61 done_callback.Run(); | |
62 } | |
63 } | |
64 | |
65 void Tracer::StopTracingAndFlushToDisk(const std::string& filename) { | |
66 tracing_ = false; | |
67 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
68 base::WaitableEvent flush_complete_event(false, false); | |
69 // TraceLog::Flush requires a message loop but we've already shut ours down. | |
70 // Spin up a new thread to flush things out. | |
71 base::Thread flush_thread("mojo_shell_trace_event_flush"); | |
72 flush_thread.Start(); | |
73 flush_thread.message_loop()->PostTask( | |
74 FROM_HERE, | |
75 base::Bind(&Tracer::EndTraceAndFlush, base::Unretained(this), filename, | |
76 base::Bind(&base::WaitableEvent::Signal, | |
77 base::Unretained(&flush_complete_event)))); | |
78 flush_complete_event.Wait(); | |
79 LOG(INFO) << "Wrote trace data to " << filename; | |
80 } | |
81 | |
82 } // namespace shell | |
OLD | NEW |