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 #include <string.h> | |
9 | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "base/threading/thread.h" | |
13 #include "base/trace_event/trace_event.h" | |
14 | |
15 namespace mojo { | |
16 namespace shell { | |
17 | |
18 Tracer::Tracer() | |
19 : tracing_(false), first_chunk_written_(false), trace_file_(nullptr) { | |
20 } | |
21 | |
22 Tracer::~Tracer() { | |
23 } | |
24 | |
25 void Tracer::Start(const std::string& categories) { | |
26 tracing_ = true; | |
27 base::trace_event::CategoryFilter category_filter(categories); | |
28 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
29 category_filter, base::trace_event::TraceLog::RECORDING_MODE, | |
30 base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); | |
31 } | |
32 | |
33 void Tracer::StopAndFlushToFile(const std::string& filename) { | |
34 if (tracing_) | |
35 StopTracingAndFlushToDisk(filename); | |
36 } | |
37 | |
38 void Tracer::EndTraceAndFlush(const std::string& filename, | |
39 const base::Closure& done_callback) { | |
40 trace_file_ = fopen(filename.c_str(), "w+"); | |
41 PCHECK(trace_file_); | |
42 static const char kStart[] = "{\"traceEvents\":["; | |
43 fwrite(kStart, 1, strlen(kStart), trace_file_); | |
44 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
45 base::trace_event::TraceLog::GetInstance()->Flush(base::Bind( | |
46 &Tracer::WriteTraceDataCollected, base::Unretained(this), done_callback)); | |
47 } | |
48 | |
49 void Tracer::WriteTraceDataCollected( | |
50 const base::Closure& done_callback, | |
51 const scoped_refptr<base::RefCountedString>& events_str, | |
52 bool has_more_events) { | |
53 if (events_str->size()) { | |
54 if (!first_chunk_written_) | |
55 fwrite(",", 1, 1, trace_file_); | |
56 | |
57 first_chunk_written_ = true; | |
58 fwrite(events_str->data().c_str(), 1, events_str->data().length(), | |
59 trace_file_); | |
60 } else | |
61 printf("got empty chunk, skipping. has_more_events %d\n", has_more_events); | |
viettrungluu
2015/04/08 00:39:15
I don't think you really want this printf (and if
| |
62 | |
63 if (!has_more_events) { | |
64 static const char kEnd[] = "]}"; | |
65 fwrite(kEnd, 1, strlen(kEnd), trace_file_); | |
66 PCHECK(fclose(trace_file_) == 0); | |
67 trace_file_ = nullptr; | |
68 done_callback.Run(); | |
69 } | |
70 } | |
71 | |
72 void Tracer::StopTracingAndFlushToDisk(const std::string& filename) { | |
73 tracing_ = false; | |
74 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
75 base::WaitableEvent flush_complete_event(false, false); | |
76 // TraceLog::Flush requires a message loop but we've already shut ours down. | |
77 // Spin up a new thread to flush things out. | |
78 base::Thread flush_thread("mojo_shell_trace_event_flush"); | |
79 flush_thread.Start(); | |
80 flush_thread.message_loop()->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&Tracer::EndTraceAndFlush, base::Unretained(this), filename, | |
83 base::Bind(&base::WaitableEvent::Signal, | |
84 base::Unretained(&flush_complete_event)))); | |
85 flush_complete_event.Wait(); | |
86 LOG(INFO) << "Wrote trace data to " << filename; | |
87 } | |
88 | |
89 } // namespace shell | |
90 } // namespace mojo | |
OLD | NEW |