FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
systrace.h
Go to the documentation of this file.
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_SYSTRACE_H
16 #define FPLBASE_SYSTRACE_H
17 
18 /// @file fplbase/systrace.h
19 /// @brief Functions for creating systrace log events, for Android.
20 ///
21 /// To enable, \#define FPLBASE_ENABLE_SYSTRACE 1.
22 /// @addtogroup fplbase_systrace
23 /// @{
24 
25 #if FPLBASE_ENABLE_SYSTRACE
26 #ifndef __ANDROID__
27 #error Systrace is only suppported for Android Builds
28 #endif
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #endif // FPLBASE_ENABLE_SYSTRACE
34 
35 #define MAX_SYSTRACE_LEN 256
36 int trace_marker = -1;
37 
38 /// @brief Initializes the settings for systrace.
39 ///
40 /// This needs to be called before any other systrace call.
41 void SystraceInit() {
42 #if FPLBASE_ENABLE_SYSTRACE
43  trace_marker = open("/sys/kernel/debug/tracing/trace_marker", O_WRONLY);
44  // Check that it didn't fail:
45  assert(trace_marker == -1);
46 #endif
47 }
48 
49 /// @brief Start a block, with the supplied name.
50 ///
51 /// This will last until SystraceEnd is called. Nesting is supported!
52 ///
53 /// @param name The name of the block, which will appear in the trace.
54 inline void SystraceBegin(const char *name) {
55  (void)name;
56 #if FPLBASE_ENABLE_SYSTRACE
57  char buf[MAX_SYSTRACE_LEN];
58  int len = snprintf(buf, MAX_SYSTRACE_LEN, "B|%d|%s", getpid(), name);
59  write(trace_marker, buf, len);
60 #endif
61 }
62 
63 /// @brief Ends the most recently begun block.
64 inline void SystraceEnd() {
65 #if FPLBASE_ENABLE_SYSTRACE
66  char c = 'E';
67  write(trace_marker, &c, 1);
68 #endif
69 }
70 
71 /// @brief Logs a value.
72 ///
73 /// This will be displayed on a graph in the systrace.
74 ///
75 /// @param name The name that will be logged.
76 /// @param value The value to be logged with the given name.
77 inline void SystraceCounter(const char *name, const int value) {
78  (void)name;
79  (void)value;
80 #if FPLBASE_ENABLE_SYSTRACE
81  char buf[MAX_SYSTRACE_LEN];
82  int len =
83  snprintf(buf, MAX_SYSTRACE_LEN, "C|%d|%s|%i", getpid(), name, value);
84  write(trace_marker, buf, len);
85 #endif
86 }
87 
88 /// @brief Begins an asynchronous block.
89 ///
90 /// Name/Cookie need to be unique per block.
91 ///
92 /// @param name The name of the block, which will appear in the trace.
93 /// @param cookie Part of the unique identifier to note the block.
94 inline void SystraceAsyncBegin(const char *name, const int32_t cookie) {
95  (void)name;
96  (void)cookie;
97 #if FPLBASE_ENABLE_SYSTRACE
98  char buf[MAX_SYSTRACE_LEN];
99  int len =
100  snprintf(buf, MAX_SYSTRACE_LEN, "S|%d|%s|%i", getpid(), name, cookie);
101  write(trace_marker, buf, len);
102 #endif
103 }
104 
105 /// @brief Ends an asynchronous block.
106 ///
107 /// @param name The name of the block, which needs to match the one used to
108 /// begin the block.
109 /// @param cookie The unique identifier of the block, which needs to match the
110 /// one used to begin the block.
111 inline void SystraceAsyncEnd(const char *name, const int32_t cookie) {
112  (void)name;
113  (void)cookie;
114 #if FPLBASE_ENABLE_SYSTRACE
115  char buf[MAX_SYSTRACE_LEN];
116  int len =
117  snprintf(buf, MAX_SYSTRACE_LEN, "F|%d|%s|%i", getpid(), name, cookie);
118  write(trace_marker, buf, len);
119 #endif
120 }
121 
122 /// @}
123 #endif // FPLBASE_SYSTRACE_H
void SystraceEnd()
Ends the most recently begun block.
Definition: systrace.h:64
void SystraceAsyncEnd(const char *name, const int32_t cookie)
Ends an asynchronous block.
Definition: systrace.h:111
void SystraceBegin(const char *name)
Start a block, with the supplied name.
Definition: systrace.h:54
void SystraceInit()
Initializes the settings for systrace.
Definition: systrace.h:41
void SystraceAsyncBegin(const char *name, const int32_t cookie)
Begins an asynchronous block.
Definition: systrace.h:94
void SystraceCounter(const char *name, const int value)
Logs a value.
Definition: systrace.h:77