1 // Copyright (c) 2006-2011 The Chromium Authors. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions
5 // are met:
6 //  * Redistributions of source code must retain the above copyright
7 //    notice, this list of conditions and the following disclaimer.
8 //  * Redistributions in binary form must reproduce the above copyright
9 //    notice, this list of conditions and the following disclaimer in
10 //    the documentation and/or other materials provided with the
11 //    distribution.
12 //  * Neither the name of Google, Inc. nor the names of its contributors
13 //    may be used to endorse or promote products derived from this
14 //    software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 // SUCH DAMAGE.
28 
29 #ifndef TOOLS_PLATFORM_H_
30 #define TOOLS_PLATFORM_H_
31 
32 #include "PlatformMacros.h"
33 
34 #include "BaseProfiler.h"
35 
36 #include "mozilla/Logging.h"
37 #include "mozilla/UniquePtr.h"
38 #include "mozilla/Vector.h"
39 
40 #include <functional>
41 #include <stdint.h>
42 #include <string>
43 
44 namespace mozilla {
45 namespace baseprofiler {
46 bool LogTest(int aLevelToTest);
47 void PrintToConsole(const char* aFmt, ...) MOZ_FORMAT_PRINTF(1, 2);
48 }  // namespace baseprofiler
49 }  // namespace mozilla
50 
51 // These are for MOZ_BASE_PROFILER_LOGGING and above. It's the default logging
52 // level for the profiler, and should be used sparingly.
53 #define LOG_TEST ::mozilla::baseprofiler::LogTest(3)
54 #define LOG(arg, ...)                                           \
55   do {                                                          \
56     if (LOG_TEST) {                                             \
57       ::mozilla::baseprofiler::PrintToConsole(                  \
58           "[I %d/%d] " arg "\n", profiler_current_process_id(), \
59           profiler_current_thread_id(), ##__VA_ARGS__);         \
60     }                                                           \
61   } while (0)
62 
63 // These are for MOZ_BASE_PROFILER_DEBUG_LOGGING. It should be used for logging
64 // that is somewhat more verbose than LOG.
65 #define DEBUG_LOG_TEST ::mozilla::baseprofiler::LogTest(4)
66 #define DEBUG_LOG(arg, ...)                                     \
67   do {                                                          \
68     if (DEBUG_LOG_TEST) {                                       \
69       ::mozilla::baseprofiler::PrintToConsole(                  \
70           "[D %d/%d] " arg "\n", profiler_current_process_id(), \
71           profiler_current_thread_id(), ##__VA_ARGS__);         \
72     }                                                           \
73   } while (0)
74 
75 // These are for MOZ_BASE_PROFILER_VERBOSE_LOGGING. It should be used for
76 // logging that is somewhat more verbose than DEBUG_LOG.
77 #define VERBOSE_LOG_TEST ::mozilla::baseprofiler::LogTest(5)
78 #define VERBOSE_LOG(arg, ...)                                   \
79   do {                                                          \
80     if (VERBOSE_LOG_TEST) {                                     \
81       ::mozilla::baseprofiler::PrintToConsole(                  \
82           "[V %d/%d] " arg "\n", profiler_current_process_id(), \
83           profiler_current_thread_id(), ##__VA_ARGS__);         \
84     }                                                           \
85   } while (0)
86 
87 namespace mozilla {
88 
89 class JSONWriter;
90 
91 namespace baseprofiler {
92 
93 typedef uint8_t* Address;
94 
95 class PlatformData;
96 
97 // We can't new/delete the type safely without defining it
98 // (-Wdelete-incomplete).  Use these to hide the details from clients.
99 struct PlatformDataDestructor {
100   void operator()(PlatformData*);
101 };
102 
103 typedef UniquePtr<PlatformData, PlatformDataDestructor> UniquePlatformData;
104 UniquePlatformData AllocPlatformData(int aThreadId);
105 
106 // Convert the array of strings to a bitfield.
107 uint32_t ParseFeaturesFromStringArray(const char** aFeatures,
108                                       uint32_t aFeatureCount,
109                                       bool aIsStartup = false);
110 
111 void profiler_get_profile_json_into_lazily_allocated_buffer(
112     const std::function<char*(size_t)>& aAllocator, double aSinceTime,
113     bool aIsShuttingDown);
114 
115 // Flags to conveniently track various JS instrumentations.
116 enum class JSInstrumentationFlags {
117   StackSampling = 0x1,
118   TraceLogging = 0x2,
119   Allocations = 0x4,
120 };
121 
122 // Record an exit profile from a child process.
123 void profiler_received_exit_profile(const std::string& aExitProfile);
124 
125 // Extract all received exit profiles that have not yet expired (i.e., they
126 // still intersect with this process' buffer range).
127 Vector<std::string> profiler_move_exit_profiles();
128 
129 }  // namespace baseprofiler
130 }  // namespace mozilla
131 
132 #endif /* ndef TOOLS_PLATFORM_H_ */
133