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