Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1461)

Unified Diff: content/browser/tracing/tracing_consumer_win.cc

Issue 171143002: Implements Windows system tracing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@tracing
Patch Set: rebase on ToT Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/tracing/tracing_consumer_win.cc
diff --git a/content/browser/tracing/tracing_consumer_win.cc b/content/browser/tracing/tracing_consumer_win.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d90ac0987fa0c6c2ae5e22c0d1480ed071c989b9
--- /dev/null
+++ b/content/browser/tracing/tracing_consumer_win.cc
@@ -0,0 +1,151 @@
+// Copyright 2014 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/tracing/tracing_consumer_win.h"
+
+#include "base/base64.h"
+#include "base/lazy_instance.h"
+#include "base/json/json_string_value_serializer.h"
+#include "base/strings/stringprintf.h"
+#include "content/public/browser/browser_thread.h"
+
+namespace content {
+
+namespace {
+
+const int kEtwBufferSizeInKBytes = 16;
+const int kEtwBufferFlushTimeoutInSeconds = 1;
+
+std::string GuidToString(const GUID& guid) {
+ return base::StringPrintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
+ guid.Data1, guid.Data2, guid.Data3,
+ guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
+ guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
+}
+
+base::LazyInstance<EtwSystemEventConsumer>::Leaky g_etw_consumer =
+ LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+EtwSystemEventConsumer::EtwSystemEventConsumer()
+ : thread_("EtwConsumerThread") {
+ thread_.Start();
+}
+
+bool EtwSystemEventConsumer::StartKernelSessionTracing() {
nduca 2014/02/20 07:04:54 lets match the class name of this to the file, so
etienneb 2014/02/20 19:08:03 Done.
+ // Enabled flags (tracing facilities).
+ uint32 enabled_flags = EVENT_TRACE_FLAG_IMAGE_LOAD |
+ EVENT_TRACE_FLAG_PROCESS |
+ EVENT_TRACE_FLAG_THREAD |
+ EVENT_TRACE_FLAG_CSWITCH;
+
+ EVENT_TRACE_PROPERTIES& p = *properties_.get();
+ p.LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
+ p.FlushTimer = kEtwBufferFlushTimeoutInSeconds;
+ p.BufferSize = kEtwBufferSizeInKBytes;
+ p.LogFileNameOffset = 0;
+ p.EnableFlags = enabled_flags;
+
+ HRESULT hr = base::win::EtwTraceController::Start(
+ KERNEL_LOGGER_NAME, &properties_, &session_handle_);
+ if (FAILED(hr)) {
+ VLOG(1) << "StartRealtimeSession() failed with " << hr << ".";
+ return false;
+ }
+
+ return true;
+}
+
+bool EtwSystemEventConsumer::StopKernelSessionTracing() {
+ HRESULT hr = base::win::EtwTraceController::Stop(
+ KERNEL_LOGGER_NAME, &properties_);
+ return SUCCEEDED(hr);
+}
+
+void EtwSystemEventConsumer::RequestStartTraceConsuming() {
+ events_.reset(new base::ListValue());
+ thread_.message_loop()->PostTask(
+ FROM_HERE,
+ base::Bind(&EtwSystemEventConsumer::TraceAndConsumeOnThread,
+ base::Unretained(this)));
+}
+
+void EtwSystemEventConsumer::RequestStopTraceConsuming(
+ const OutputCallback& callback) {
+ thread_.message_loop()->PostTask(FROM_HERE,
+ base::Bind(&EtwSystemEventConsumer::StopAndFlushOnThread,
+ base::Unretained(this), callback));
+}
+
+EtwSystemEventConsumer* EtwSystemEventConsumer::Get() {
+ return g_etw_consumer.Pointer();
+}
+
+void EtwSystemEventConsumer::ProcessEvent(EVENT_TRACE* event) {
+ EtwSystemEventConsumer::Get()->AppendEventToBuffer(event);
+}
+
+void EtwSystemEventConsumer::AppendEventToBuffer(EVENT_TRACE* event) {
+ using base::FundamentalValue;
+ using base::StringPrintf;
+
+ scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
+
+ // Add header fields to the event.
+ value->Set("ts", new base::StringValue(
+ StringPrintf("%08X%08X",
+ event->Header.TimeStamp.HighPart,
+ event->Header.TimeStamp.LowPart)));
+
+ value->Set("guid", new base::StringValue(GuidToString(event->Header.Guid)));
+
+ value->Set("op", new FundamentalValue(event->Header.Class.Type));
+ value->Set("ver", new FundamentalValue(event->Header.Class.Version));
+ value->Set("pid",
+ new FundamentalValue(static_cast<int>(event->Header.ProcessId)));
+ value->Set("tid",
+ new FundamentalValue(static_cast<int>(event->Header.ThreadId)));
+ value->Set("cpu", new FundamentalValue(event->BufferContext.ProcessorNumber));
+
+ // Base64 encode the payload bytes.
+ base::StringPiece buffer(static_cast<const char*>(event->MofData),
+ event->MofLength);
+ std::string payload;
+ base::Base64Encode(buffer, &payload);
+ value->Set("payload", new base::StringValue(payload));
+
+ // Append it to the events buffer.
+ events_->Append(value.release());
+}
+
+void EtwSystemEventConsumer::TraceAndConsumeOnThread() {
+ HRESULT hr = OpenRealtimeSession(KERNEL_LOGGER_NAME);
+ if (FAILED(hr))
+ return;
+ Consume();
+ Close();
+}
+
+void EtwSystemEventConsumer::StopAndFlushOnThread(
+ const OutputCallback& callback) {
+ // Add the header information to the stream.
+ scoped_ptr<base::DictionaryValue> header(new base::DictionaryValue());
+ header->Set("name", base::Value::CreateStringValue("ETW"));
+ header->Set("content", events_.release());
+
+ // Serialize the results as a JSon string.
+ std::string output;
+ JSONStringValueSerializer serializer(&output);
+ serializer.set_pretty_print(true);
+ serializer.Serialize(*header.get());
+
+ // Pass the result to the UI Thread.
+ scoped_refptr<base::RefCountedString> result =
+ base::RefCountedString::TakeString(&output);
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, result));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698