TracingTest.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkImageInfo.h"
  8. #include "include/core/SkPoint.h"
  9. #include "include/core/SkRect.h"
  10. #include "src/core/SkLeanWindows.h"
  11. #include "src/core/SkTraceEvent.h"
  12. #include "tests/Test.h"
  13. #include "tools/flags/CommandLineFlags.h"
  14. static DEFINE_bool(slowTracingTest, false,
  15. "Artificially slow down tracing test to produce nicer JSON");
  16. namespace {
  17. /**
  18. * Helper types for demonstrating usage of TRACE_EVENT_OBJECT_XXX macros.
  19. */
  20. struct TracingShape {
  21. TracingShape() {
  22. TRACE_EVENT_OBJECT_CREATED_WITH_ID("skia.objects", this->typeName(), this);
  23. }
  24. virtual ~TracingShape() {
  25. TRACE_EVENT_OBJECT_DELETED_WITH_ID("skia.objects", this->typeName(), this);
  26. }
  27. void traceSnapshot() {
  28. // The state of an object can be specified at any point with the OBJECT_SNAPSHOT macro.
  29. // This takes the "name" (actually the type name), the ID of the object (typically a
  30. // pointer), and a single (unnnamed) argument, which is the "snapshot" of that object.
  31. //
  32. // Tracing viewer requires that all object macros use the same name and id for creation,
  33. // deletion, and snapshots. However: It's convenient to put creation and deletion in the
  34. // base-class constructor/destructor where the actual type name isn't known yet. That's
  35. // what we're doing here. The JSON for snapshots can therefore include the actual type
  36. // name, and a special tag that refers to the type name originally used at creation time.
  37. // Skia's JSON tracer handles this automatically, so SNAPSHOT macros can simply use the
  38. // derived type name, and the JSON will be formatted correctly to link the events.
  39. TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID("skia.objects", this->typeName(), this,
  40. TRACE_STR_COPY(this->toString().c_str()));
  41. }
  42. virtual const char* typeName() { return "TracingShape"; }
  43. virtual SkString toString() { return SkString("Shape()"); }
  44. };
  45. struct TracingCircle : public TracingShape {
  46. TracingCircle(SkPoint center, SkScalar radius) : fCenter(center), fRadius(radius) {}
  47. const char* typeName() override { return "TracingCircle"; }
  48. SkString toString() override {
  49. return SkStringPrintf("Circle(%f, %f, %f)", fCenter.fX, fCenter.fY, fRadius);
  50. }
  51. SkPoint fCenter;
  52. SkScalar fRadius;
  53. };
  54. struct TracingRect : public TracingShape {
  55. TracingRect(SkRect rect) : fRect(rect) {}
  56. const char* typeName() override { return "TracingRect"; }
  57. SkString toString() override {
  58. return SkStringPrintf("Rect(%f, %f, %f, %f)",
  59. fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom);
  60. }
  61. SkRect fRect;
  62. };
  63. }
  64. static SkScalar gTracingTestWorkSink = 1.0f;
  65. static void do_work(int howMuchWork) {
  66. // Do busy work so the trace marker durations are large enough to be readable in trace viewer
  67. if (FLAGS_slowTracingTest) {
  68. for (int i = 0; i < howMuchWork * 100; ++i) {
  69. gTracingTestWorkSink += SkScalarSin(i);
  70. }
  71. }
  72. }
  73. static void test_trace_simple() {
  74. // Simple event that lasts until the end of the current scope. TRACE_FUNC is an easy way
  75. // to insert the current function name.
  76. TRACE_EVENT0("skia", TRACE_FUNC);
  77. {
  78. // There are versions of the macro that take 1 or 2 named arguments. The arguments
  79. // can be any simple type. Strings need to be static/literal - we just copy pointers.
  80. // Argument names & values are shown when the event is selected in the viewer.
  81. TRACE_EVENT1("skia", "Nested work",
  82. "isBGRA", kN32_SkColorType == kBGRA_8888_SkColorType);
  83. do_work(500);
  84. }
  85. {
  86. // If you must copy a string as an argument value, use the TRACE_STR_COPY macro.
  87. // This will instruct the tracing system (if one is active) to make a copy.
  88. SkString message = SkStringPrintf("%s %s", "Hello", "World");
  89. TRACE_EVENT1("skia", "Dynamic String", "message", TRACE_STR_COPY(message.c_str()));
  90. do_work(500);
  91. }
  92. }
  93. static void test_trace_counters() {
  94. TRACE_EVENT0("skia", TRACE_FUNC);
  95. {
  96. TRACE_EVENT0("skia", "Single Counter");
  97. // Counter macros allow recording a named value (which must be a 32-bit integer).
  98. // The value will be graphed in the viewer.
  99. for (int i = 0; i < 180; ++i) {
  100. SkScalar rad = SkDegreesToRadians(SkIntToScalar(i));
  101. TRACE_COUNTER1("skia", "sin", SkScalarSin(rad) * 1000.0f + 1000.0f);
  102. do_work(10);
  103. }
  104. }
  105. {
  106. TRACE_EVENT0("skia", "Independent Counters");
  107. // Recording multiple counters with separate COUNTER1 macros will make separate graphs.
  108. for (int i = 0; i < 180; ++i) {
  109. SkScalar rad = SkDegreesToRadians(SkIntToScalar(i));
  110. TRACE_COUNTER1("skia", "sin", SkScalarSin(rad) * 1000.0f + 1000.0f);
  111. TRACE_COUNTER1("skia", "cos", SkScalarCos(rad) * 1000.0f + 1000.0f);
  112. do_work(10);
  113. }
  114. }
  115. {
  116. TRACE_EVENT0("skia", "Stacked Counters");
  117. // Two counters can be recorded together with COUNTER2. They will be graphed together,
  118. // as a stacked bar graph. The combined graph needs a name, as does each data series.
  119. for (int i = 0; i < 180; ++i) {
  120. SkScalar rad = SkDegreesToRadians(SkIntToScalar(i));
  121. TRACE_COUNTER2("skia", "trig",
  122. "sin", SkScalarSin(rad) * 1000.0f + 1000.0f,
  123. "cos", SkScalarCos(rad) * 1000.0f + 1000.0f);
  124. do_work(10);
  125. }
  126. }
  127. }
  128. static void test_trace_objects() {
  129. TRACE_EVENT0("skia", TRACE_FUNC);
  130. // Objects can be tracked through time with the TRACE_EVENT_OBJECT_ macros.
  131. // The macros in use (and their idiosyncracies) are commented in the TracingShape class above.
  132. TracingCircle* circle = new TracingCircle(SkPoint::Make(20, 20), 15);
  133. circle->traceSnapshot();
  134. do_work(100);
  135. // Make another object. Objects with the same base type are shown in the same row in the viewer.
  136. TracingRect* rect = new TracingRect(SkRect::MakeWH(100, 50));
  137. rect->traceSnapshot();
  138. do_work(100);
  139. // We can create multiple snapshots of objects to reflect their state over time.
  140. circle->fCenter.offset(10, 10);
  141. circle->traceSnapshot();
  142. {
  143. // Other events (duration or instant) can refer directly to objects. For Skia's JSON
  144. // tracer, having an argument whose name starts with '#' will trigger the creation of JSON
  145. // that links the event to the object (with a direct link to the most recent snapshot).
  146. TRACE_EVENT1("skia", "Processing Shape", "#shape", circle);
  147. do_work(100);
  148. }
  149. delete circle;
  150. delete rect;
  151. }
  152. DEF_TEST(Tracing, reporter) {
  153. test_trace_simple();
  154. test_trace_counters();
  155. test_trace_objects();
  156. }