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

content / browser / cache_storage / cache_storage_histogram_utils.cc [blame]

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

#include "content/browser/cache_storage/cache_storage_histogram_utils.h"

#include <string_view>

#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"

namespace content {

blink::mojom::CacheStorageError MakeErrorStorage(ErrorStorageType type) {
  UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.ErrorStorageType", type);
  return blink::mojom::CacheStorageError::kErrorStorage;
}

namespace {

// Helper macro to return a literal std::string_view.
#define RETURN_LITERAL_STRING_PIECE(target)                \
  do {                                                     \
    static constexpr std::string_view kValue("." target); \
    return kValue;                                         \
  } while (0)

std::string_view UMAToName(CacheStorageSchedulerUMA uma_type) {
  switch (uma_type) {
    case CacheStorageSchedulerUMA::kOperationDuration:
      RETURN_LITERAL_STRING_PIECE("OperationDuration2");
    case CacheStorageSchedulerUMA::kQueueDuration:
      RETURN_LITERAL_STRING_PIECE("QueueDuration2");
    case CacheStorageSchedulerUMA::kQueueLength:
      RETURN_LITERAL_STRING_PIECE("QueueLength");
  }
}

std::string_view ClientToName(CacheStorageSchedulerClient client_type) {
  switch (client_type) {
    case CacheStorageSchedulerClient::kCache:
      RETURN_LITERAL_STRING_PIECE("Cache");
    case CacheStorageSchedulerClient::kStorage:
      RETURN_LITERAL_STRING_PIECE("CacheStorage");
  }
}

bool ShouldRecordOpUMA(CacheStorageSchedulerOp op_type) {
  return op_type != CacheStorageSchedulerOp::kTest;
}

std::string_view OpToName(CacheStorageSchedulerOp op_type) {
  switch (op_type) {
    case CacheStorageSchedulerOp::kClose:
      RETURN_LITERAL_STRING_PIECE("Close");
    case CacheStorageSchedulerOp::kDelete:
      RETURN_LITERAL_STRING_PIECE("Delete");
    case CacheStorageSchedulerOp::kGetAllMatched:
      RETURN_LITERAL_STRING_PIECE("GetAllMatched");
    case CacheStorageSchedulerOp::kHas:
      RETURN_LITERAL_STRING_PIECE("Has");
    case CacheStorageSchedulerOp::kInit:
      RETURN_LITERAL_STRING_PIECE("Init");
    case CacheStorageSchedulerOp::kKeys:
      RETURN_LITERAL_STRING_PIECE("Keys");
    case CacheStorageSchedulerOp::kMatch:
      RETURN_LITERAL_STRING_PIECE("Match");
    case CacheStorageSchedulerOp::kMatchAll:
      RETURN_LITERAL_STRING_PIECE("MatchAll");
    case CacheStorageSchedulerOp::kOpen:
      RETURN_LITERAL_STRING_PIECE("Open");
    case CacheStorageSchedulerOp::kPut:
      RETURN_LITERAL_STRING_PIECE("Put");
    case CacheStorageSchedulerOp::kSize:
      RETURN_LITERAL_STRING_PIECE("Size");
    case CacheStorageSchedulerOp::kSizeThenClose:
      RETURN_LITERAL_STRING_PIECE("SizeThenClose");
    case CacheStorageSchedulerOp::kTest:
      NOTREACHED_IN_MIGRATION();
      return "";
    case CacheStorageSchedulerOp::kWriteIndex:
      RETURN_LITERAL_STRING_PIECE("WriteIndex");
    case CacheStorageSchedulerOp::kWriteSideData:
      RETURN_LITERAL_STRING_PIECE("WriteSideData");
  }
}

std::string GetClientHistogramName(CacheStorageSchedulerUMA uma_type,
                                   CacheStorageSchedulerClient client_type) {
  return base::StrCat({"ServiceWorkerCache", ClientToName(client_type),
                       ".Scheduler", UMAToName(uma_type)});
}

}  // namespace

void RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,
                                    CacheStorageSchedulerClient client_type,
                                    CacheStorageSchedulerOp op_type,
                                    int value) {
  DCHECK(uma_type == CacheStorageSchedulerUMA::kQueueLength);
  std::string histogram_name = GetClientHistogramName(uma_type, client_type);
  base::UmaHistogramCounts10000(histogram_name, value);
  if (!ShouldRecordOpUMA(op_type))
    return;
  histogram_name.append(OpToName(op_type));
  base::UmaHistogramCounts10000(histogram_name, value);
}

void RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,
                                    CacheStorageSchedulerClient client_type,
                                    CacheStorageSchedulerOp op_type,
                                    base::TimeDelta value) {
  DCHECK(uma_type == CacheStorageSchedulerUMA::kOperationDuration ||
         uma_type == CacheStorageSchedulerUMA::kQueueDuration);
  std::string histogram_name = GetClientHistogramName(uma_type, client_type);
  base::UmaHistogramLongTimes(histogram_name, value);
  if (!ShouldRecordOpUMA(op_type))
    return;
  histogram_name.append(OpToName(op_type));
  base::UmaHistogramLongTimes(histogram_name, value);
}

}  // namespace content