OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 /// @file |
| 6 /// This example demonstrates using Trace Events in a simple NaCl module. |
| 7 /// See documentation here: |
| 8 /// http://www.chromium.org/developers/how-tos/trace-event-profiling-tool/tracin
g-event-instrumentation |
| 9 /// |
| 10 /// Instead of including <base/debug/trace_event.h>, include |
| 11 /// "ppapi/cpp/dev/trace_event_dev.h" for tracing within PPAPI C++. |
| 12 |
| 13 #include <pthread.h> |
| 14 #include <unistd.h> |
| 15 |
| 16 #include <cstdio> |
| 17 #include <sstream> |
| 18 #include <string> |
| 19 |
| 20 #include "ppapi/cpp/dev/trace_event_dev.h" |
| 21 #include "ppapi/cpp/instance.h" |
| 22 #include "ppapi/cpp/module.h" |
| 23 #include "ppapi/cpp/var.h" |
| 24 |
| 25 /// Method name for running a task on a new thread, as seen by JavaScript code. |
| 26 const char* const kRunThreadedTask = "runthreadedtask"; |
| 27 |
| 28 /// Method name to send an async begin trace, as seen by Javascript code. |
| 29 const char* const kSendAsyncBegin = "sendasyncbegin"; |
| 30 |
| 31 /// Method name to send an async end trace, as seen by Javascript code. |
| 32 const char* const kSendAsyncEnd = "sendasyncend"; |
| 33 |
| 34 /// The Instance class for the Trace Events example. |
| 35 class TraceEventInstance : public pp::Instance { |
| 36 public: |
| 37 explicit TraceEventInstance(PP_Instance instance) : |
| 38 pp::Instance(instance), |
| 39 thread_num_(0), |
| 40 async_num_(0) { |
| 41 printf("TraceEventInstance.\n"); |
| 42 } |
| 43 virtual ~TraceEventInstance() {} |
| 44 |
| 45 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); |
| 46 |
| 47 virtual void HandleMessage(const pp::Var& var_message); |
| 48 |
| 49 private: |
| 50 int thread_num_; |
| 51 int async_num_; |
| 52 }; |
| 53 |
| 54 void* RunTaskOnThread(void* arg) { |
| 55 int thread_num = *static_cast<int*>(arg); |
| 56 std::ostringstream stream; |
| 57 stream << "Non-main thread #" << thread_num; |
| 58 pp::trace_events::SetThreadName(stream.str().c_str()); |
| 59 // TRACE_EVENTx traces scope, so will automatically close on function end. |
| 60 // The '0' variation means that this macro takes zero additional parameters. |
| 61 TRACE_EVENT0("ExampleCategory", "traceThisSleep"); |
| 62 sleep(2); |
| 63 |
| 64 return NULL; |
| 65 } |
| 66 |
| 67 bool TraceEventInstance::Init(uint32_t argc, |
| 68 const char* argn[], |
| 69 const char* argv[]) { |
| 70 // Set a thread name for the main thread, otherwise it shows up as |
| 71 // a non-descriptive thread-id. |
| 72 pp::trace_events::SetThreadName("Main Pepper Thread"); |
| 73 return PP_TRUE; |
| 74 } |
| 75 |
| 76 void TraceEventInstance::HandleMessage(const pp::Var& var_message) { |
| 77 if (!var_message.is_string()) { |
| 78 return; |
| 79 } |
| 80 std::string message = var_message.AsString(); |
| 81 pp::Var return_var; |
| 82 if (message == kRunThreadedTask) { |
| 83 pthread_t p; |
| 84 pthread_create(&p, NULL, |
| 85 RunTaskOnThread, new int(thread_num_)); |
| 86 thread_num_++; |
| 87 } else if (message == kSendAsyncBegin) { |
| 88 // TRACE_EVENT_ASYNC_BEGIN marks the beginning point for a trace event that |
| 89 // does not end with the scope of the marked location. It must be ended |
| 90 // with a matching TRACE_EVENT_ASYNC_END call (with matching parameter: |
| 91 // here, 'async_num_'). |
| 92 // The '0' variation means that this macro takes zero additional parameters. |
| 93 TRACE_EVENT_ASYNC_BEGIN0("ExampleCategory", "ExampleMessage", async_num_); |
| 94 } else if (message == kSendAsyncEnd) { |
| 95 // Here is the matching call to a previous BEGIN, note that async_num_ is |
| 96 // incremented after the END call to set up the next BEGIN/END pair. |
| 97 TRACE_EVENT_ASYNC_END0("ExampleCategory", "ExampleMessage", async_num_); |
| 98 async_num_++; |
| 99 } |
| 100 } |
| 101 |
| 102 /// The Module class. |
| 103 class TraceEventModule : public pp::Module { |
| 104 public: |
| 105 TraceEventModule() : pp::Module() { |
| 106 printf("TraceEventModule.\n"); |
| 107 } |
| 108 virtual ~TraceEventModule() {} |
| 109 |
| 110 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 111 return new TraceEventInstance(instance); |
| 112 } |
| 113 }; |
| 114 |
| 115 |
| 116 namespace pp { |
| 117 Module* CreateModule() { |
| 118 return new TraceEventModule(); |
| 119 } |
| 120 } // namespace pp |
OLD | NEW |