1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GeckoProfiler.h"
#include "ProfilerStackWalk.h"
#include "mozilla/Maybe.h"
#include "nsPrintfCString.h"
#include "public/GeckoTraceEvent.h"
#include "mozilla/ProfilerState.h"
using namespace mozilla;
using webrtc::trace_event_internal::TraceValueUnion;
void uprofiler_register_thread(const char* name, void* stacktop) {
#ifdef MOZ_GECKO_PROFILER
profiler_register_thread(name, stacktop);
#endif // MOZ_GECKO_PROFILER
}
void uprofiler_unregister_thread() {
#ifdef MOZ_GECKO_PROFILER
profiler_unregister_thread();
#endif // MOZ_GECKO_PROFILER
}
#ifdef MOZ_GECKO_PROFILER
namespace {
Maybe<MarkerTiming> ToTiming(char phase) {
switch (phase) {
case 'B':
return Some(MarkerTiming::IntervalStart());
case 'E':
return Some(MarkerTiming::IntervalEnd());
case 'I':
return Some(MarkerTiming::InstantNow());
default:
return Nothing();
}
}
MarkerCategory ToCategory(const char category) {
switch (category) {
case 'S':
return geckoprofiler::category::SANDBOX;
case 'M':
return geckoprofiler::category::MEDIA_RT;
default:
return geckoprofiler::category::OTHER;
}
}
struct TraceOption {
bool mPassed = false;
ProfilerString8View mName;
Variant<int64_t, bool, double, ProfilerString8View> mValue = AsVariant(false);
};
struct TraceMarker {
static constexpr int MAX_NUM_ARGS = 6;
using OptionsType = std::tuple<TraceOption, TraceOption, TraceOption,
TraceOption, TraceOption, TraceOption>;
static constexpr mozilla::Span<const char> MarkerTypeName() {
return MakeStringSpan("TraceEvent");
}
static void StreamJSONMarkerData(
mozilla::baseprofiler::SpliceableJSONWriter& aWriter,
const OptionsType& aArgs) {
auto writeValue = [&](const auto& aName, const auto& aVariant) {
aVariant.match(
[&](const int64_t& aValue) { aWriter.IntProperty(aName, aValue); },
[&](const bool& aValue) { aWriter.BoolProperty(aName, aValue); },
[&](const double& aValue) { aWriter.DoubleProperty(aName, aValue); },
[&](const ProfilerString8View& aValue) {
aWriter.StringProperty(aName, aValue);
});
};
if (const auto& arg = std::get<0>(aArgs); arg.mPassed) {
aWriter.StringProperty("name1", arg.mName);
writeValue("val1", arg.mValue);
}
if (const auto& arg = std::get<1>(aArgs); arg.mPassed) {
aWriter.StringProperty("name2", arg.mName);
writeValue("val2", arg.mValue);
}
if (const auto& arg = std::get<2>(aArgs); arg.mPassed) {
aWriter.StringProperty("name3", arg.mName);
writeValue("val3", arg.mValue);
}
if (const auto& arg = std::get<3>(aArgs); arg.mPassed) {
aWriter.StringProperty("name4", arg.mName);
writeValue("val4", arg.mValue);
}
if (const auto& arg = std::get<4>(aArgs); arg.mPassed) {
aWriter.StringProperty("name5", arg.mName);
writeValue("val5", arg.mValue);
}
if (const auto& arg = std::get<5>(aArgs); arg.mPassed) {
aWriter.StringProperty("name6", arg.mName);
writeValue("val6", arg.mValue);
}
}
static mozilla::MarkerSchema MarkerTypeDisplay() {
using MS = MarkerSchema;
MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
schema.SetChartLabel("{marker.name}");
schema.SetTableLabel(
"{marker.name} {marker.data.name1} {marker.data.val1} "
"{marker.data.name2} {marker.data.val2}"
"{marker.data.name3} {marker.data.val3}"
"{marker.data.name4} {marker.data.val4}"
"{marker.data.name5} {marker.data.val5}"
"{marker.data.name6} {marker.data.val6}");
schema.AddKeyLabelFormat("name1", "Key 1", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val1", "Value 1", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("name2", "Key 2", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val2", "Value 2", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("name3", "Key 3", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val3", "Value 3", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("name4", "Key 4", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val4", "Value 4", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("name5", "Key 5", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val5", "Value 5", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("name6", "Key 6", MS::Format::String,
MS::PayloadFlags::Searchable);
schema.AddKeyLabelFormat("val6", "Value 6", MS::Format::String,
MS::PayloadFlags::Searchable);
return schema;
}
};
} // namespace
namespace mozilla {
template <>
struct ProfileBufferEntryWriter::Serializer<TraceOption> {
static Length Bytes(const TraceOption& aOption) {
// 1 byte to store passed flag, then object size if passed.
return aOption.mPassed ? (1 + SumBytes(aOption.mName, aOption.mValue)) : 1;
}
static void Write(ProfileBufferEntryWriter& aEW, const TraceOption& aOption) {
// 'T'/'t' is just an arbitrary 1-byte value to distinguish states.
if (aOption.mPassed) {
aEW.WriteObject<char>('T');
// Use the Serializer for the name/value pair.
aEW.WriteObject(aOption.mName);
aEW.WriteObject(aOption.mValue);
} else {
aEW.WriteObject<char>('t');
}
}
};
template <>
struct ProfileBufferEntryReader::Deserializer<TraceOption> {
static void ReadInto(ProfileBufferEntryReader& aER, TraceOption& aOption) {
char c = aER.ReadObject<char>();
if ((aOption.mPassed = (c == 'T'))) {
aER.ReadIntoObject(aOption.mName);
aER.ReadIntoObject(aOption.mValue);
} else {
MOZ_ASSERT(c == 't');
}
}
static TraceOption Read(ProfileBufferEntryReader& aER) {
TraceOption option;
ReadInto(aER, option);
return option;
}
};
} // namespace mozilla
#endif // MOZ_GECKO_PROFILER
void uprofiler_simple_event_marker_internal(
const char* name, const char category, char phase, int num_args,
const char** arg_names, const unsigned char* arg_types,
const unsigned long long* arg_values, bool capture_stack = false,
void* provided_stack = nullptr) {
#ifdef MOZ_GECKO_PROFILER
if (!profiler_thread_is_being_profiled_for_markers()) {
return;
}
Maybe<MarkerTiming> timing = ToTiming(phase);
if (!timing) {
if (getenv("MOZ_LOG_UNKNOWN_TRACE_EVENT_PHASES")) {
fprintf(stderr, "XXX UProfiler: phase not handled: '%c'\n", phase);
}
return;
}
MOZ_ASSERT(num_args <= TraceMarker::MAX_NUM_ARGS);
TraceMarker::OptionsType tuple;
TraceOption* args[TraceMarker::MAX_NUM_ARGS] = {
&std::get<0>(tuple), &std::get<1>(tuple), &std::get<2>(tuple),
&std::get<3>(tuple), &std::get<4>(tuple), &std::get<5>(tuple)};
nsCString strValueContainers[TraceMarker::MAX_NUM_ARGS];
for (int i = 0; i < std::min(num_args, TraceMarker::MAX_NUM_ARGS); ++i) {
auto& arg = *args[i];
arg.mPassed = true;
arg.mName = ProfilerString8View::WrapNullTerminatedString(arg_names[i]);
switch (arg_types[i]) {
case TRACE_VALUE_TYPE_UINT:
MOZ_ASSERT(arg_values[i] <= std::numeric_limits<int64_t>::max());
arg.mValue = AsVariant(static_cast<int64_t>(
reinterpret_cast<const TraceValueUnion*>(&arg_values[i])->as_uint));
break;
case TRACE_VALUE_TYPE_INT:
arg.mValue = AsVariant(static_cast<int64_t>(
reinterpret_cast<const TraceValueUnion*>(&arg_values[i])->as_int));
break;
case TRACE_VALUE_TYPE_BOOL:
arg.mValue = AsVariant(
reinterpret_cast<const TraceValueUnion*>(&arg_values[i])->as_bool);
break;
case TRACE_VALUE_TYPE_DOUBLE:
arg.mValue =
AsVariant(reinterpret_cast<const TraceValueUnion*>(&arg_values[i])
->as_double);
break;
case TRACE_VALUE_TYPE_POINTER:
strValueContainers[i] = nsPrintfCString(
"%p", reinterpret_cast<const TraceValueUnion*>(&arg_values[i])
->as_pointer);
arg.mValue = AsVariant(ProfilerString8View(strValueContainers[i]));
break;
case TRACE_VALUE_TYPE_STRING:
arg.mValue = AsVariant(ProfilerString8View::WrapNullTerminatedString(
reinterpret_cast<const TraceValueUnion*>(&arg_values[i])
->as_string));
break;
case TRACE_VALUE_TYPE_COPY_STRING:
strValueContainers[i] =
reinterpret_cast<const TraceValueUnion*>(&arg_values[i])->as_string;
arg.mValue = AsVariant(ProfilerString8View(strValueContainers[i]));
break;
default:
MOZ_ASSERT_UNREACHABLE("Unexpected trace value type");
strValueContainers[i] =
nsPrintfCString("Unexpected type: %u", arg_types[i]);
arg.mValue = AsVariant(ProfilerString8View(strValueContainers[i]));
break;
}
}
profiler_add_marker(
ProfilerString8View::WrapNullTerminatedString(name), ToCategory(category),
{timing.extract(),
capture_stack
? MarkerStack::Capture(StackCaptureOptions::Full)
: (provided_stack
? MarkerStack::UseBacktrace(
*(static_cast<mozilla::ProfileChunkedBuffer*>(
provided_stack)))
: MarkerStack::Capture(StackCaptureOptions::NoStack))},
TraceMarker{}, tuple);
#endif // MOZ_GECKO_PROFILER
}
void uprofiler_simple_event_marker_capture_stack(
const char* name, const char category, char phase, int num_args,
const char** arg_names, const unsigned char* arg_types,
const unsigned long long* arg_values) {
uprofiler_simple_event_marker_internal(
name, category, phase, num_args, arg_names, arg_types, arg_values, true);
}
void uprofiler_simple_event_marker_with_stack(
const char* name, const char category, char phase, int num_args,
const char** arg_names, const unsigned char* arg_types,
const unsigned long long* arg_values, void* provided_stack) {
MOZ_ASSERT(provided_stack != nullptr);
uprofiler_simple_event_marker_internal(name, category, phase, num_args,
arg_names, arg_types, arg_values,
false, provided_stack);
}
void uprofiler_simple_event_marker(const char* name, const char category,
char phase, int num_args,
const char** arg_names,
const unsigned char* arg_types,
const unsigned long long* arg_values) {
uprofiler_simple_event_marker_internal(name, category, phase, num_args,
arg_names, arg_types, arg_values);
}
bool uprofiler_backtrace_into_buffer(NativeStack* aNativeStack, void* aBuffer) {
#if defined(MOZ_GECKO_PROFILER)
return profiler_backtrace_into_buffer(
*(static_cast<mozilla::ProfileChunkedBuffer*>(aBuffer)), *aNativeStack);
#else
return false;
#endif
}
void uprofiler_native_backtrace(const void* top, NativeStack* nativeStack) {
#if defined(MOZ_GECKO_PROFILER)
DoNativeBacktraceDirect(top, *nativeStack, nullptr);
#endif
}
bool uprofiler_is_active() { return profiler_is_active(); }
bool uprofiler_feature_active(int32_t aFeature) {
return profiler_feature_active(aFeature);
}
bool uprofiler_get(struct UprofilerFuncPtrs* aFuncPtrs) {
if (!aFuncPtrs) {
return false;
}
aFuncPtrs->register_thread = uprofiler_register_thread;
aFuncPtrs->unregister_thread = uprofiler_unregister_thread;
aFuncPtrs->simple_event_marker = uprofiler_simple_event_marker;
aFuncPtrs->simple_event_marker_capture_stack =
uprofiler_simple_event_marker_capture_stack;
aFuncPtrs->simple_event_marker_with_stack =
uprofiler_simple_event_marker_with_stack;
aFuncPtrs->backtrace_into_buffer = uprofiler_backtrace_into_buffer;
aFuncPtrs->native_backtrace = uprofiler_native_backtrace;
aFuncPtrs->is_active = uprofiler_is_active;
aFuncPtrs->feature_active = uprofiler_feature_active;
return true;
}
|