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 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
| |
26 /// Method name for running a task on a new thread, as seen by JavaScript code. | |
27 const char* const kRunThreadedTask = "runthreadedtask"; | |
28 | |
29 /// Method name to send an async begin trace, as seen by Javascript code. | |
30 const char* const kSendAsyncBegin = "sendasyncbegin"; | |
31 | |
32 /// Method name to send an async end trace, as seen by Javascript code. | |
33 const char* const kSendAsyncEnd = "sendasyncend"; | |
34 | |
35 /// The Instance class for the Trace Events example. | |
36 class TraceEventInstance : public pp::Instance { | |
37 int thread_num_; | |
binji
2013/02/14 22:01:28
move to end of class definition
elijahtaylor1
2013/02/14 23:38:10
Done.
| |
38 int async_num_; | |
39 public: | |
40 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
| |
41 thread_num_(0), | |
42 async_num_(0) { | |
43 printf("TraceEventInstance.\n"); | |
44 } | |
45 virtual ~TraceEventInstance() {} | |
46 | |
47 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); | |
48 | |
49 virtual void HandleMessage(const pp::Var& var_message); | |
50 }; | |
51 | |
52 void* RunTaskOnThread(void* arg) { | |
53 int thread_num = reinterpret_cast<int>(arg); | |
54 std::ostringstream stream; | |
55 stream << "Non-main thread #" << thread_num; | |
56 pp::trace_events::SetThreadName(stream.str().c_str()); | |
57 // TRACE_EVENTx traces scope, so will automatically close on function end. | |
58 TRACE_EVENT0("ExampleCategory", "traceThisSleep"); | |
59 sleep(2); | |
60 | |
61 return NULL; | |
62 } | |
63 | |
64 bool TraceEventInstance::Init(uint32_t argc, | |
65 const char* argn[], | |
66 const char* argv[]) { | |
67 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
| |
68 } | |
69 | |
70 void TraceEventInstance::HandleMessage(const pp::Var& var_message) { | |
71 if (!var_message.is_string()) { | |
72 return; | |
73 } | |
74 std::string message = var_message.AsString(); | |
75 pp::Var return_var; | |
76 if (message == kRunThreadedTask) { | |
77 pthread_t p; | |
78 pthread_create(&p, NULL, | |
79 RunTaskOnThread, reinterpret_cast<void*>(thread_num_)); | |
80 thread_num_++; | |
81 } else if (message == kSendAsyncBegin) { | |
82 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.
| |
83 } else if (message == kSendAsyncEnd) { | |
84 TRACE_EVENT_ASYNC_END0("ExampleCategory", "ExampleMessage", async_num_); | |
85 async_num_++; | |
86 } | |
87 } | |
88 | |
89 /// The Module class. | |
90 class TraceEventModule : public pp::Module { | |
91 public: | |
92 TraceEventModule() : pp::Module() { | |
93 printf("TraceEventModule.\n"); | |
94 } | |
95 virtual ~TraceEventModule() {} | |
96 | |
97 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
98 return new TraceEventInstance(instance); | |
99 } | |
100 }; | |
101 } // namespace trace_event_example | |
102 | |
103 | |
104 namespace pp { | |
105 Module* CreateModule() { | |
106 return new trace_event_example::TraceEventModule(); | |
107 } | |
108 } // namespace pp | |
OLD | NEW |