1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "content/browser/cache_storage/cache_storage_histogram_utils.h"
6 #include "base/metrics/histogram_functions.h"
7 #include "base/metrics/histogram_macros.h"
8 
9 namespace content {
10 
MakeErrorStorage(ErrorStorageType type)11 blink::mojom::CacheStorageError MakeErrorStorage(ErrorStorageType type) {
12   UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.ErrorStorageType", type);
13   return blink::mojom::CacheStorageError::kErrorStorage;
14 }
15 
16 namespace {
17 
18 // Helper macro to return a literal base::StringPiece.  Once
19 // we can use c++17 we can use string view literals instead.
20 #define RETURN_LITERAL_STRING_PIECE(target)                \
21   do {                                                     \
22     static constexpr base::StringPiece kValue("." target); \
23     return kValue;                                         \
24   } while (0)
25 
UMAToName(CacheStorageSchedulerUMA uma_type)26 base::StringPiece UMAToName(CacheStorageSchedulerUMA uma_type) {
27   switch (uma_type) {
28     case CacheStorageSchedulerUMA::kIsOperationSlow:
29       RETURN_LITERAL_STRING_PIECE("IsOperationSlow");
30     case CacheStorageSchedulerUMA::kOperationDuration:
31       RETURN_LITERAL_STRING_PIECE("OperationDuration2");
32     case CacheStorageSchedulerUMA::kQueueDuration:
33       RETURN_LITERAL_STRING_PIECE("QueueDuration2");
34     case CacheStorageSchedulerUMA::kQueueLength:
35       RETURN_LITERAL_STRING_PIECE("QueueLength");
36   }
37 }
38 
ClientToName(CacheStorageSchedulerClient client_type)39 base::StringPiece ClientToName(CacheStorageSchedulerClient client_type) {
40   switch (client_type) {
41     case CacheStorageSchedulerClient::kBackgroundSync:
42       RETURN_LITERAL_STRING_PIECE("BackgroundSyncManager");
43     case CacheStorageSchedulerClient::kCache:
44       RETURN_LITERAL_STRING_PIECE("Cache");
45     case CacheStorageSchedulerClient::kStorage:
46       RETURN_LITERAL_STRING_PIECE("CacheStorage");
47   }
48 }
49 
ShouldRecordOpUMA(CacheStorageSchedulerOp op_type)50 bool ShouldRecordOpUMA(CacheStorageSchedulerOp op_type) {
51   return op_type != CacheStorageSchedulerOp::kBackgroundSync &&
52          op_type != CacheStorageSchedulerOp::kTest;
53 }
54 
OpToName(CacheStorageSchedulerOp op_type)55 base::StringPiece OpToName(CacheStorageSchedulerOp op_type) {
56   switch (op_type) {
57     case CacheStorageSchedulerOp::kBackgroundSync:
58       NOTREACHED();
59       return "";
60     case CacheStorageSchedulerOp::kClose:
61       RETURN_LITERAL_STRING_PIECE("Close");
62     case CacheStorageSchedulerOp::kDelete:
63       RETURN_LITERAL_STRING_PIECE("Delete");
64     case CacheStorageSchedulerOp::kGetAllMatched:
65       RETURN_LITERAL_STRING_PIECE("GetAllMatched");
66     case CacheStorageSchedulerOp::kHas:
67       RETURN_LITERAL_STRING_PIECE("Has");
68     case CacheStorageSchedulerOp::kInit:
69       RETURN_LITERAL_STRING_PIECE("Init");
70     case CacheStorageSchedulerOp::kKeys:
71       RETURN_LITERAL_STRING_PIECE("Keys");
72     case CacheStorageSchedulerOp::kMatch:
73       RETURN_LITERAL_STRING_PIECE("Match");
74     case CacheStorageSchedulerOp::kMatchAll:
75       RETURN_LITERAL_STRING_PIECE("MatchAll");
76     case CacheStorageSchedulerOp::kOpen:
77       RETURN_LITERAL_STRING_PIECE("Open");
78     case CacheStorageSchedulerOp::kPut:
79       RETURN_LITERAL_STRING_PIECE("Put");
80     case CacheStorageSchedulerOp::kSize:
81       RETURN_LITERAL_STRING_PIECE("Size");
82     case CacheStorageSchedulerOp::kSizeThenClose:
83       RETURN_LITERAL_STRING_PIECE("SizeThenClose");
84     case CacheStorageSchedulerOp::kTest:
85       NOTREACHED();
86       return "";
87     case CacheStorageSchedulerOp::kWriteIndex:
88       RETURN_LITERAL_STRING_PIECE("WriteIndex");
89     case CacheStorageSchedulerOp::kWriteSideData:
90       RETURN_LITERAL_STRING_PIECE("WriteSideData");
91   }
92 }
93 
GetClientHistogramName(CacheStorageSchedulerUMA uma_type,CacheStorageSchedulerClient client_type)94 std::string GetClientHistogramName(CacheStorageSchedulerUMA uma_type,
95                                    CacheStorageSchedulerClient client_type) {
96   std::string histogram_name("ServiceWorkerCache");
97   histogram_name.append(ClientToName(client_type).as_string());
98   histogram_name.append(".Scheduler");
99   histogram_name.append(UMAToName(uma_type).as_string());
100   return histogram_name;
101 }
102 
103 }  // namespace
104 
RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,CacheStorageSchedulerClient client_type,CacheStorageSchedulerOp op_type,int value)105 void RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,
106                                     CacheStorageSchedulerClient client_type,
107                                     CacheStorageSchedulerOp op_type,
108                                     int value) {
109   DCHECK(uma_type == CacheStorageSchedulerUMA::kIsOperationSlow ||
110          uma_type == CacheStorageSchedulerUMA::kQueueLength);
111   DCHECK(client_type != CacheStorageSchedulerClient::kBackgroundSync ||
112          op_type == CacheStorageSchedulerOp::kBackgroundSync);
113   std::string histogram_name = GetClientHistogramName(uma_type, client_type);
114   if (uma_type == CacheStorageSchedulerUMA::kIsOperationSlow)
115     base::UmaHistogramBoolean(histogram_name, value);
116   else
117     base::UmaHistogramCounts10000(histogram_name, value);
118   if (!ShouldRecordOpUMA(op_type))
119     return;
120   histogram_name.append(OpToName(op_type).as_string());
121   if (uma_type == CacheStorageSchedulerUMA::kIsOperationSlow)
122     base::UmaHistogramBoolean(histogram_name, value);
123   else
124     base::UmaHistogramCounts10000(histogram_name, value);
125 }
126 
RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,CacheStorageSchedulerClient client_type,CacheStorageSchedulerOp op_type,base::TimeDelta value)127 void RecordCacheStorageSchedulerUMA(CacheStorageSchedulerUMA uma_type,
128                                     CacheStorageSchedulerClient client_type,
129                                     CacheStorageSchedulerOp op_type,
130                                     base::TimeDelta value) {
131   DCHECK(uma_type == CacheStorageSchedulerUMA::kOperationDuration ||
132          uma_type == CacheStorageSchedulerUMA::kQueueDuration);
133   DCHECK(client_type != CacheStorageSchedulerClient::kBackgroundSync ||
134          op_type == CacheStorageSchedulerOp::kBackgroundSync);
135   std::string histogram_name = GetClientHistogramName(uma_type, client_type);
136   base::UmaHistogramLongTimes(histogram_name, value);
137   if (!ShouldRecordOpUMA(op_type))
138     return;
139   histogram_name.append(OpToName(op_type).as_string());
140   base::UmaHistogramLongTimes(histogram_name, value);
141 }
142 
143 }  // namespace content
144