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
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350

base / android / jank_metric_uma_recorder.cc [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/jank_metric_uma_recorder.h"

#include <cstdint>

#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "base/trace_event/base_tracing.h"
#include "jank_metric_uma_recorder.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "base/jank_tracker_jni/JankMetricUMARecorder_jni.h"

namespace base::android {

namespace {

// Histogram min, max and no. of buckets.
constexpr int kVsyncCountsMin = 1;
constexpr int kVsyncCountsMax = 50;
constexpr int kVsyncCountsBuckets = 25;

enum class PerScrollHistogramType {
  kPercentage = 0,
  kMax = 1,
  kSum = 2,
};

const char* GetPerScrollHistogramName(JankScenario scenario,
                                      int num_frames,
                                      PerScrollHistogramType type,
                                      bool with_scroll_size_suffix) {
#define HISTOGRAM_NAME(hist_scenario, hist_type, length)     \
  "Android.FrameTimelineJank." #hist_scenario "." #hist_type \
  "."                                                        \
  "PerScroll" #length
  if (scenario == JankScenario::WEBVIEW_SCROLLING) {
    if (type == PerScrollHistogramType::kPercentage) {
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(WebviewScrolling, DelayedFramesPercentage,
                              /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(WebviewScrolling, DelayedFramesPercentage,
                              .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(WebviewScrolling, DelayedFramesPercentage,
                              .Medium);
      } else {
        return HISTOGRAM_NAME(WebviewScrolling, DelayedFramesPercentage,
                              .Large);
      }
    } else if (type == PerScrollHistogramType::kMax) {
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsMax, /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsMax, .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsMax, .Medium);
      } else {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsMax, .Large);
      }
    } else {
      DCHECK_EQ(type, PerScrollHistogramType::kSum);
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsSum, /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsSum, .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsSum, .Medium);
      } else {
        return HISTOGRAM_NAME(WebviewScrolling, MissedVsyncsSum, .Large);
      }
    }
  } else {
    DCHECK_EQ(scenario, JankScenario::FEED_SCROLLING);
    if (type == PerScrollHistogramType::kPercentage) {
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(FeedScrolling, DelayedFramesPercentage,
                              /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(FeedScrolling, DelayedFramesPercentage, .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(FeedScrolling, DelayedFramesPercentage, .Medium);
      } else {
        return HISTOGRAM_NAME(FeedScrolling, DelayedFramesPercentage, .Large);
      }
    } else if (type == PerScrollHistogramType::kMax) {
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsMax, /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsMax, .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsMax, .Medium);
      } else {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsMax, .Large);
      }
    } else {
      DCHECK_EQ(type, PerScrollHistogramType::kSum);
      if (!with_scroll_size_suffix) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsSum, /*no suffix*/);
      }
      if (num_frames <= 16) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsSum, .Small);
      } else if (num_frames <= 64) {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsSum, .Medium);
      } else {
        return HISTOGRAM_NAME(FeedScrolling, MissedVsyncsSum, .Large);
      }
    }
  }
#undef HISTOGRAM_NAME
}

// Emits trace event for all scenarios and per scroll histograms for webview and
// feed scrolling scenarios.
void EmitMetrics(JankScenario scenario,
                 int janky_frame_count,
                 int missed_vsyncs_max,
                 int missed_vsyncs_sum,
                 int num_presented_frames,
                 int64_t reporting_interval_start_time,
                 int64_t reporting_interval_duration) {
  DCHECK_GT(num_presented_frames, 0);
  int delayed_frames_percentage =
      (100 * janky_frame_count) / num_presented_frames;
  if (reporting_interval_start_time > 0) {
    // The following code does nothing if base tracing is disabled.
    [[maybe_unused]] int non_janky_frame_count =
        num_presented_frames - janky_frame_count;
    [[maybe_unused]] auto t = perfetto::Track(static_cast<uint64_t>(
        reporting_interval_start_time + static_cast<int>(scenario)));
    TRACE_EVENT_BEGIN(
        "android_webview.timeline,android.ui.jank",
        "JankMetricsReportingInterval", t,
        base::TimeTicks::FromUptimeMillis(reporting_interval_start_time),
        "janky_frames", janky_frame_count, "non_janky_frames",
        non_janky_frame_count, "scenario", static_cast<int>(scenario),
        "delayed_frames_percentage", delayed_frames_percentage,
        "missed_vsyns_max", missed_vsyncs_max, "missed_vsyncs_sum",
        missed_vsyncs_sum);
    TRACE_EVENT_END(
        "android_webview.timeline,android.ui.jank", t,
        base::TimeTicks::FromUptimeMillis(
            (reporting_interval_start_time + reporting_interval_duration)));
  }

  if (scenario != JankScenario::WEBVIEW_SCROLLING &&
      scenario != JankScenario::FEED_SCROLLING) {
    return;
  }
  // Emit non-bucketed per scroll metrics.
  base::UmaHistogramPercentage(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kPercentage,
                                /*with_scroll_size_suffix=*/false),
      delayed_frames_percentage);
  base::UmaHistogramCustomCounts(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kMax,
                                /*with_scroll_size_suffix=*/false),
      missed_vsyncs_max, kVsyncCountsMin, kVsyncCountsMax, kVsyncCountsBuckets);
  base::UmaHistogramCustomCounts(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kSum,
                                /*with_scroll_size_suffix=*/false),
      missed_vsyncs_sum, kVsyncCountsMin, kVsyncCountsMax, kVsyncCountsBuckets);

  // Emit bucketed per scroll metrics where scrolls are divided into three
  // buckets Small, Medium, Large.
  base::UmaHistogramPercentage(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kPercentage,
                                /*with_scroll_size_suffix=*/true),
      delayed_frames_percentage);
  base::UmaHistogramCustomCounts(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kMax,
                                /*with_scroll_size_suffix=*/true),
      missed_vsyncs_max, kVsyncCountsMin, kVsyncCountsMax, kVsyncCountsBuckets);
  base::UmaHistogramCustomCounts(
      GetPerScrollHistogramName(scenario, num_presented_frames,
                                PerScrollHistogramType::kSum,
                                /*with_scroll_size_suffix=*/true),
      missed_vsyncs_sum, kVsyncCountsMin, kVsyncCountsMax, kVsyncCountsBuckets);
}

}  // namespace

const char* GetAndroidFrameTimelineJankHistogramName(JankScenario scenario) {
#define HISTOGRAM_NAME(x) "Android.FrameTimelineJank.FrameJankStatus." #x
  switch (scenario) {
    case JankScenario::PERIODIC_REPORTING:
      return HISTOGRAM_NAME(Total);
    case JankScenario::OMNIBOX_FOCUS:
      return HISTOGRAM_NAME(OmniboxFocus);
    case JankScenario::NEW_TAB_PAGE:
      return HISTOGRAM_NAME(NewTabPage);
    case JankScenario::STARTUP:
      return HISTOGRAM_NAME(Startup);
    case JankScenario::TAB_SWITCHER:
      return HISTOGRAM_NAME(TabSwitcher);
    case JankScenario::OPEN_LINK_IN_NEW_TAB:
      return HISTOGRAM_NAME(OpenLinkInNewTab);
    case JankScenario::START_SURFACE_HOMEPAGE:
      return HISTOGRAM_NAME(StartSurfaceHomepage);
    case JankScenario::START_SURFACE_TAB_SWITCHER:
      return HISTOGRAM_NAME(StartSurfaceTabSwitcher);
    case JankScenario::FEED_SCROLLING:
      return HISTOGRAM_NAME(FeedScrolling);
    case JankScenario::WEBVIEW_SCROLLING:
      return HISTOGRAM_NAME(WebviewScrolling);
    case JankScenario::COMBINED_WEBVIEW_SCROLLING:
      // Emit per frame metrics for combined scrolling scenario with same
      // histogram name as webview scrolling. This is fine since we don't emit
      // per frame metrics for |WEBVIEW_SCROLLING| scenario.
      return HISTOGRAM_NAME(WebviewScrolling);
    default:
      NOTREACHED();
  }
#undef HISTOGRAM_NAME
}

const char* GetAndroidFrameTimelineDurationHistogramName(
    JankScenario scenario) {
#define HISTOGRAM_NAME(x) "Android.FrameTimelineJank.Duration." #x
  switch (scenario) {
    case JankScenario::PERIODIC_REPORTING:
      return HISTOGRAM_NAME(Total);
    case JankScenario::OMNIBOX_FOCUS:
      return HISTOGRAM_NAME(OmniboxFocus);
    case JankScenario::NEW_TAB_PAGE:
      return HISTOGRAM_NAME(NewTabPage);
    case JankScenario::STARTUP:
      return HISTOGRAM_NAME(Startup);
    case JankScenario::TAB_SWITCHER:
      return HISTOGRAM_NAME(TabSwitcher);
    case JankScenario::OPEN_LINK_IN_NEW_TAB:
      return HISTOGRAM_NAME(OpenLinkInNewTab);
    case JankScenario::START_SURFACE_HOMEPAGE:
      return HISTOGRAM_NAME(StartSurfaceHomepage);
    case JankScenario::START_SURFACE_TAB_SWITCHER:
      return HISTOGRAM_NAME(StartSurfaceTabSwitcher);
    case JankScenario::FEED_SCROLLING:
      return HISTOGRAM_NAME(FeedScrolling);
    case JankScenario::WEBVIEW_SCROLLING:
      return HISTOGRAM_NAME(WebviewScrolling);
    case JankScenario::COMBINED_WEBVIEW_SCROLLING:
      // Emit per frame metrics for combined scrolling scenario with same
      // histogram name as webview scrolling. This is fine since we don't emit
      // per frame metrics for |WEBVIEW_SCROLLING| scenario.
      return HISTOGRAM_NAME(WebviewScrolling);
    default:
      NOTREACHED();
  }
#undef HISTOGRAM_NAME
}

// This function is called from Java with JNI, it's declared in
// base/base_jni/JankMetricUMARecorder_jni.h which is an autogenerated
// header. The actual implementation is in RecordJankMetrics for simpler
// testing.
void JNI_JankMetricUMARecorder_RecordJankMetrics(
    JNIEnv* env,
    const base::android::JavaParamRef<jlongArray>& java_durations_ns,
    const base::android::JavaParamRef<jintArray>& java_missed_vsyncs,
    jlong java_reporting_interval_start_time,
    jlong java_reporting_interval_duration,
    jint java_scenario_enum) {
  RecordJankMetrics(env, java_durations_ns, java_missed_vsyncs,
                    java_reporting_interval_start_time,
                    java_reporting_interval_duration, java_scenario_enum);
}

void RecordJankMetrics(
    JNIEnv* env,
    const base::android::JavaParamRef<jlongArray>& java_durations_ns,
    const base::android::JavaParamRef<jintArray>& java_missed_vsyncs,
    jlong java_reporting_interval_start_time,
    jlong java_reporting_interval_duration,
    jint java_scenario_enum) {
  std::vector<int64_t> durations_ns;
  JavaLongArrayToInt64Vector(env, java_durations_ns, &durations_ns);

  std::vector<int> missed_vsyncs;
  JavaIntArrayToIntVector(env, java_missed_vsyncs, &missed_vsyncs);

  JankScenario scenario = static_cast<JankScenario>(java_scenario_enum);

  const char* frame_duration_histogram_name =
      GetAndroidFrameTimelineDurationHistogramName(scenario);
  const char* janky_frames_per_scenario_histogram_name =
      GetAndroidFrameTimelineJankHistogramName(scenario);

  // We don't want to emit per frame metircs for WEBVIEW SCROLLING scenario
  // which tracks individual scrolls differentiated by gesture_scroll_id.
  // Scroll related per frame metrics are emitted from
  // COMBINED_WEBVIEW_SCROLLING scenario to avoid emitting duplicate metrics for
  // overlapping scrolls.
  const bool emit_per_frame_metrics =
      scenario != JankScenario::WEBVIEW_SCROLLING;

  if (emit_per_frame_metrics) {
    for (const int64_t frame_duration_ns : durations_ns) {
      base::UmaHistogramTimes(frame_duration_histogram_name,
                              base::Nanoseconds(frame_duration_ns));
    }
  }

  int janky_frame_count = 0;
  int missed_vsyncs_max = 0;
  int missed_vsyncs_sum = 0;
  const int num_presented_frames = static_cast<int>(missed_vsyncs.size());

  for (int curr_frame_missed_vsyncs : missed_vsyncs) {
    bool is_janky = curr_frame_missed_vsyncs > 0;
    if (curr_frame_missed_vsyncs > missed_vsyncs_max) {
      missed_vsyncs_max = curr_frame_missed_vsyncs;
    }
    missed_vsyncs_sum += curr_frame_missed_vsyncs;

    if (emit_per_frame_metrics) {
      base::UmaHistogramEnumeration(
          janky_frames_per_scenario_histogram_name,
          is_janky ? FrameJankStatus::kJanky : FrameJankStatus::kNonJanky);
    }
    if (is_janky) {
      ++janky_frame_count;
    }
  }

  if (num_presented_frames > 0) {
    EmitMetrics(scenario, janky_frame_count, missed_vsyncs_max,
                missed_vsyncs_sum, num_presented_frames,
                java_reporting_interval_start_time,
                java_reporting_interval_duration);
  }
}

}  // namespace base::android