Chromium Code Reviews| Index: native_client_sdk/src/examples/trace_events/trace_events.cc |
| diff --git a/native_client_sdk/src/examples/trace_events/trace_events.cc b/native_client_sdk/src/examples/trace_events/trace_events.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..064b2d49751f657b593c350808052e568fcdc6d5 |
| --- /dev/null |
| +++ b/native_client_sdk/src/examples/trace_events/trace_events.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright (c) 2013 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. |
| + |
| +/// @file |
| +/// This example demonstrates using Trace Events in a simple NaCl module. |
| +/// See documentation here: |
| +/// http://www.chromium.org/developers/how-tos/trace-event-profiling-tool/tracing-event-instrumentation |
| +/// |
| +/// Instead of including <base/debug/trace_event.h>, include |
| +/// "ppapi/cpp/dev/trace_event_dev.h" for tracing within PPAPI C++. |
| + |
| +#include <pthread.h> |
| +#include <unistd.h> |
| + |
| +#include <cstdio> |
| +#include <sstream> |
| +#include <string> |
| + |
| +#include "ppapi/cpp/dev/trace_event_dev.h" |
| +#include "ppapi/cpp/instance.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/cpp/var.h" |
| + |
| +namespace trace_event_example { |
|
binji
2013/02/14 22:01:28
we don't usually namespace the examples
elijahtaylor1
2013/02/14 23:38:10
This was cribbed from hello_world_interactive. Re
|
| +/// Method name for running a task on a new thread, as seen by JavaScript code. |
| +const char* const kRunThreadedTask = "runthreadedtask"; |
| + |
| +/// Method name to send an async begin trace, as seen by Javascript code. |
| +const char* const kSendAsyncBegin = "sendasyncbegin"; |
| + |
| +/// Method name to send an async end trace, as seen by Javascript code. |
| +const char* const kSendAsyncEnd = "sendasyncend"; |
| + |
| +/// The Instance class for the Trace Events example. |
| +class TraceEventInstance : public pp::Instance { |
| + int thread_num_; |
|
binji
2013/02/14 22:01:28
move to end of class definition
elijahtaylor1
2013/02/14 23:38:10
Done.
|
| + int async_num_; |
| + public: |
| + explicit TraceEventInstance(PP_Instance instance) : pp::Instance(instance), |
|
binji
2013/02/14 22:01:28
I usually move the initializer list to the next li
elijahtaylor1
2013/02/14 23:38:10
Again, cribbed from hello_world_interactive (where
|
| + thread_num_(0), |
| + async_num_(0) { |
| + printf("TraceEventInstance.\n"); |
| + } |
| + virtual ~TraceEventInstance() {} |
| + |
| + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); |
| + |
| + virtual void HandleMessage(const pp::Var& var_message); |
| +}; |
| + |
| +void* RunTaskOnThread(void* arg) { |
| + int thread_num = reinterpret_cast<int>(arg); |
| + std::ostringstream stream; |
| + stream << "Non-main thread #" << thread_num; |
| + pp::trace_events::SetThreadName(stream.str().c_str()); |
| + // TRACE_EVENTx traces scope, so will automatically close on function end. |
| + TRACE_EVENT0("ExampleCategory", "traceThisSleep"); |
| + sleep(2); |
| + |
| + return NULL; |
| +} |
| + |
| +bool TraceEventInstance::Init(uint32_t argc, |
| + const char* argn[], |
| + const char* argv[]) { |
| + pp::trace_events::SetThreadName("Main Pepper Thread"); |
|
binji
2013/02/14 22:01:28
what is the default name if this is not set?
elijahtaylor1
2013/02/14 23:38:10
Added comment
|
| +} |
| + |
| +void TraceEventInstance::HandleMessage(const pp::Var& var_message) { |
| + if (!var_message.is_string()) { |
| + return; |
| + } |
| + std::string message = var_message.AsString(); |
| + pp::Var return_var; |
| + if (message == kRunThreadedTask) { |
| + pthread_t p; |
| + pthread_create(&p, NULL, |
| + RunTaskOnThread, reinterpret_cast<void*>(thread_num_)); |
| + thread_num_++; |
| + } else if (message == kSendAsyncBegin) { |
| + TRACE_EVENT_ASYNC_BEGIN0("ExampleCategory", "ExampleMessage", async_num_); |
|
binji
2013/02/14 22:01:28
maybe a little more explanation of what this is do
elijahtaylor1
2013/02/14 23:38:10
Done.
|
| + } else if (message == kSendAsyncEnd) { |
| + TRACE_EVENT_ASYNC_END0("ExampleCategory", "ExampleMessage", async_num_); |
| + async_num_++; |
| + } |
| +} |
| + |
| +/// The Module class. |
| +class TraceEventModule : public pp::Module { |
| + public: |
| + TraceEventModule() : pp::Module() { |
| + printf("TraceEventModule.\n"); |
| + } |
| + virtual ~TraceEventModule() {} |
| + |
| + virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| + return new TraceEventInstance(instance); |
| + } |
| +}; |
| +} // namespace trace_event_example |
| + |
| + |
| +namespace pp { |
| +Module* CreateModule() { |
| + return new trace_event_example::TraceEventModule(); |
| +} |
| +} // namespace pp |