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