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