-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_utils.cc
387 lines (334 loc) · 14.4 KB
/
metrics_utils.cc
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/metrics_utils.h"
#include <string>
#include <base/time/time.h>
#include "update_engine/common/clock_interface.h"
#include "update_engine/common/constants.h"
#include "update_engine/common/utils.h"
#include "update_engine/system_state.h"
using base::Time;
using base::TimeDelta;
namespace chromeos_update_engine {
namespace metrics_utils {
metrics::AttemptResult GetAttemptResult(ErrorCode code) {
ErrorCode base_code = static_cast<ErrorCode>(
static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
switch (base_code) {
case ErrorCode::kSuccess:
return metrics::AttemptResult::kUpdateSucceeded;
case ErrorCode::kUpdatedButNotActive:
return metrics::AttemptResult::kUpdateSucceededNotActive;
case ErrorCode::kDownloadTransferError:
return metrics::AttemptResult::kPayloadDownloadError;
case ErrorCode::kDownloadInvalidMetadataSize:
case ErrorCode::kDownloadInvalidMetadataMagicString:
case ErrorCode::kDownloadMetadataSignatureError:
case ErrorCode::kDownloadMetadataSignatureVerificationError:
case ErrorCode::kPayloadMismatchedType:
case ErrorCode::kUnsupportedMajorPayloadVersion:
case ErrorCode::kUnsupportedMinorPayloadVersion:
case ErrorCode::kDownloadNewPartitionInfoError:
case ErrorCode::kDownloadSignatureMissingInManifest:
case ErrorCode::kDownloadManifestParseError:
case ErrorCode::kDownloadOperationHashMissingError:
return metrics::AttemptResult::kMetadataMalformed;
case ErrorCode::kDownloadOperationHashMismatch:
case ErrorCode::kDownloadOperationHashVerificationError:
return metrics::AttemptResult::kOperationMalformed;
case ErrorCode::kDownloadOperationExecutionError:
case ErrorCode::kInstallDeviceOpenError:
case ErrorCode::kKernelDeviceOpenError:
case ErrorCode::kDownloadWriteError:
case ErrorCode::kFilesystemCopierError:
case ErrorCode::kFilesystemVerifierError:
return metrics::AttemptResult::kOperationExecutionError;
case ErrorCode::kDownloadMetadataSignatureMismatch:
return metrics::AttemptResult::kMetadataVerificationFailed;
case ErrorCode::kPayloadSizeMismatchError:
case ErrorCode::kPayloadHashMismatchError:
case ErrorCode::kDownloadPayloadVerificationError:
case ErrorCode::kSignedDeltaPayloadExpectedError:
case ErrorCode::kDownloadPayloadPubKeyVerificationError:
case ErrorCode::kPayloadTimestampError:
return metrics::AttemptResult::kPayloadVerificationFailed;
case ErrorCode::kNewRootfsVerificationError:
case ErrorCode::kNewKernelVerificationError:
return metrics::AttemptResult::kVerificationFailed;
case ErrorCode::kPostinstallRunnerError:
case ErrorCode::kPostinstallBootedFromFirmwareB:
case ErrorCode::kPostinstallFirmwareRONotUpdatable:
return metrics::AttemptResult::kPostInstallFailed;
case ErrorCode::kUserCanceled:
return metrics::AttemptResult::kUpdateCanceled;
// We should never get these errors in the update-attempt stage so
// return internal error if this happens.
case ErrorCode::kError:
case ErrorCode::kOmahaRequestXMLParseError:
case ErrorCode::kOmahaRequestError:
case ErrorCode::kOmahaResponseHandlerError:
case ErrorCode::kDownloadStateInitializationError:
case ErrorCode::kOmahaRequestEmptyResponseError:
case ErrorCode::kDownloadInvalidMetadataSignature:
case ErrorCode::kOmahaResponseInvalid:
case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
// TODO(deymo): The next two items belong in their own category; they
// should not be counted as internal errors. b/27112092
case ErrorCode::kOmahaUpdateDeferredPerPolicy:
case ErrorCode::kNonCriticalUpdateInOOBE:
case ErrorCode::kOmahaErrorInHTTPResponse:
case ErrorCode::kDownloadMetadataSignatureMissingError:
case ErrorCode::kOmahaUpdateDeferredForBackoff:
case ErrorCode::kPostinstallPowerwashError:
case ErrorCode::kUpdateCanceledByChannelChange:
case ErrorCode::kOmahaRequestXMLHasEntityDecl:
return metrics::AttemptResult::kInternalError;
// Special flags. These can't happen (we mask them out above) but
// the compiler doesn't know that. Just break out so we can warn and
// return |kInternalError|.
case ErrorCode::kUmaReportedMax:
case ErrorCode::kOmahaRequestHTTPResponseBase:
case ErrorCode::kDevModeFlag:
case ErrorCode::kResumedFlag:
case ErrorCode::kTestImageFlag:
case ErrorCode::kTestOmahaUrlFlag:
case ErrorCode::kSpecialFlags:
break;
}
LOG(ERROR) << "Unexpected error code " << base_code;
return metrics::AttemptResult::kInternalError;
}
metrics::DownloadErrorCode GetDownloadErrorCode(ErrorCode code) {
ErrorCode base_code = static_cast<ErrorCode>(
static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
int http_status =
static_cast<int>(base_code) -
static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase);
if (http_status >= 200 && http_status <= 599) {
return static_cast<metrics::DownloadErrorCode>(
static_cast<int>(metrics::DownloadErrorCode::kHttpStatus200) +
http_status - 200);
} else if (http_status == 0) {
// The code is using HTTP Status 0 for "Unable to get http
// response code."
return metrics::DownloadErrorCode::kDownloadError;
}
LOG(WARNING) << "Unexpected HTTP status code " << http_status;
return metrics::DownloadErrorCode::kHttpStatusOther;
}
switch (base_code) {
// Unfortunately, ErrorCode::kDownloadTransferError is returned for a wide
// variety of errors (proxy errors, host not reachable, timeouts etc.).
//
// For now just map that to kDownloading. See http://crbug.com/355745
// for how we plan to add more detail in the future.
case ErrorCode::kDownloadTransferError:
return metrics::DownloadErrorCode::kDownloadError;
// All of these error codes are not related to downloading so break
// out so we can warn and return InputMalformed.
case ErrorCode::kSuccess:
case ErrorCode::kError:
case ErrorCode::kOmahaRequestError:
case ErrorCode::kOmahaResponseHandlerError:
case ErrorCode::kFilesystemCopierError:
case ErrorCode::kPostinstallRunnerError:
case ErrorCode::kPayloadMismatchedType:
case ErrorCode::kInstallDeviceOpenError:
case ErrorCode::kKernelDeviceOpenError:
case ErrorCode::kPayloadHashMismatchError:
case ErrorCode::kPayloadSizeMismatchError:
case ErrorCode::kDownloadPayloadVerificationError:
case ErrorCode::kDownloadNewPartitionInfoError:
case ErrorCode::kDownloadWriteError:
case ErrorCode::kNewRootfsVerificationError:
case ErrorCode::kNewKernelVerificationError:
case ErrorCode::kSignedDeltaPayloadExpectedError:
case ErrorCode::kDownloadPayloadPubKeyVerificationError:
case ErrorCode::kPostinstallBootedFromFirmwareB:
case ErrorCode::kDownloadStateInitializationError:
case ErrorCode::kDownloadInvalidMetadataMagicString:
case ErrorCode::kDownloadSignatureMissingInManifest:
case ErrorCode::kDownloadManifestParseError:
case ErrorCode::kDownloadMetadataSignatureError:
case ErrorCode::kDownloadMetadataSignatureVerificationError:
case ErrorCode::kDownloadMetadataSignatureMismatch:
case ErrorCode::kDownloadOperationHashVerificationError:
case ErrorCode::kDownloadOperationExecutionError:
case ErrorCode::kDownloadOperationHashMismatch:
case ErrorCode::kOmahaRequestEmptyResponseError:
case ErrorCode::kOmahaRequestXMLParseError:
case ErrorCode::kDownloadInvalidMetadataSize:
case ErrorCode::kDownloadInvalidMetadataSignature:
case ErrorCode::kOmahaResponseInvalid:
case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
case ErrorCode::kOmahaUpdateDeferredPerPolicy:
case ErrorCode::kNonCriticalUpdateInOOBE:
case ErrorCode::kOmahaErrorInHTTPResponse:
case ErrorCode::kDownloadOperationHashMissingError:
case ErrorCode::kDownloadMetadataSignatureMissingError:
case ErrorCode::kOmahaUpdateDeferredForBackoff:
case ErrorCode::kPostinstallPowerwashError:
case ErrorCode::kUpdateCanceledByChannelChange:
case ErrorCode::kPostinstallFirmwareRONotUpdatable:
case ErrorCode::kUnsupportedMajorPayloadVersion:
case ErrorCode::kUnsupportedMinorPayloadVersion:
case ErrorCode::kOmahaRequestXMLHasEntityDecl:
case ErrorCode::kFilesystemVerifierError:
case ErrorCode::kUserCanceled:
case ErrorCode::kPayloadTimestampError:
case ErrorCode::kUpdatedButNotActive:
break;
// Special flags. These can't happen (we mask them out above) but
// the compiler doesn't know that. Just break out so we can warn and
// return |kInputMalformed|.
case ErrorCode::kUmaReportedMax:
case ErrorCode::kOmahaRequestHTTPResponseBase:
case ErrorCode::kDevModeFlag:
case ErrorCode::kResumedFlag:
case ErrorCode::kTestImageFlag:
case ErrorCode::kTestOmahaUrlFlag:
case ErrorCode::kSpecialFlags:
LOG(ERROR) << "Unexpected error code " << base_code;
break;
}
return metrics::DownloadErrorCode::kInputMalformed;
}
metrics::ConnectionType GetConnectionType(ConnectionType type,
ConnectionTethering tethering) {
switch (type) {
case ConnectionType::kUnknown:
return metrics::ConnectionType::kUnknown;
case ConnectionType::kEthernet:
if (tethering == ConnectionTethering::kConfirmed)
return metrics::ConnectionType::kTetheredEthernet;
else
return metrics::ConnectionType::kEthernet;
case ConnectionType::kWifi:
if (tethering == ConnectionTethering::kConfirmed)
return metrics::ConnectionType::kTetheredWifi;
else
return metrics::ConnectionType::kWifi;
case ConnectionType::kWimax:
return metrics::ConnectionType::kWimax;
case ConnectionType::kBluetooth:
return metrics::ConnectionType::kBluetooth;
case ConnectionType::kCellular:
return metrics::ConnectionType::kCellular;
}
LOG(ERROR) << "Unexpected network connection type: type="
<< static_cast<int>(type)
<< ", tethering=" << static_cast<int>(tethering);
return metrics::ConnectionType::kUnknown;
}
bool WallclockDurationHelper(SystemState* system_state,
const std::string& state_variable_key,
TimeDelta* out_duration) {
bool ret = false;
Time now = system_state->clock()->GetWallclockTime();
int64_t stored_value;
if (system_state->prefs()->GetInt64(state_variable_key, &stored_value)) {
Time stored_time = Time::FromInternalValue(stored_value);
if (stored_time > now) {
LOG(ERROR) << "Stored time-stamp used for " << state_variable_key
<< " is in the future.";
} else {
*out_duration = now - stored_time;
ret = true;
}
}
if (!system_state->prefs()->SetInt64(state_variable_key,
now.ToInternalValue())) {
LOG(ERROR) << "Error storing time-stamp in " << state_variable_key;
}
return ret;
}
bool MonotonicDurationHelper(SystemState* system_state,
int64_t* storage,
TimeDelta* out_duration) {
bool ret = false;
Time now = system_state->clock()->GetMonotonicTime();
if (*storage != 0) {
Time stored_time = Time::FromInternalValue(*storage);
*out_duration = now - stored_time;
ret = true;
}
*storage = now.ToInternalValue();
return ret;
}
int64_t GetPersistedValue(const std::string& key, PrefsInterface* prefs) {
CHECK(prefs);
if (!prefs->Exists(key))
return 0;
int64_t stored_value;
if (!prefs->GetInt64(key, &stored_value))
return 0;
if (stored_value < 0) {
LOG(ERROR) << key << ": Invalid value (" << stored_value
<< ") in persisted state. Defaulting to 0";
return 0;
}
return stored_value;
}
void SetNumReboots(int64_t num_reboots, PrefsInterface* prefs) {
CHECK(prefs);
prefs->SetInt64(kPrefsNumReboots, num_reboots);
LOG(INFO) << "Number of Reboots during current update attempt = "
<< num_reboots;
}
void SetPayloadAttemptNumber(int64_t payload_attempt_number,
PrefsInterface* prefs) {
CHECK(prefs);
prefs->SetInt64(kPrefsPayloadAttemptNumber, payload_attempt_number);
LOG(INFO) << "Payload Attempt Number = " << payload_attempt_number;
}
void SetSystemUpdatedMarker(ClockInterface* clock, PrefsInterface* prefs) {
CHECK(prefs);
CHECK(clock);
Time update_finish_time = clock->GetMonotonicTime();
prefs->SetInt64(kPrefsSystemUpdatedMarker,
update_finish_time.ToInternalValue());
LOG(INFO) << "Updated Marker = " << utils::ToString(update_finish_time);
}
void SetUpdateTimestampStart(const Time& update_start_time,
PrefsInterface* prefs) {
CHECK(prefs);
prefs->SetInt64(kPrefsUpdateTimestampStart,
update_start_time.ToInternalValue());
LOG(INFO) << "Update Timestamp Start = "
<< utils::ToString(update_start_time);
}
bool LoadAndReportTimeToReboot(MetricsReporterInterface* metrics_reporter,
PrefsInterface* prefs,
ClockInterface* clock) {
CHECK(prefs);
CHECK(clock);
int64_t stored_value = GetPersistedValue(kPrefsSystemUpdatedMarker, prefs);
if (stored_value == 0)
return false;
Time system_updated_at = Time::FromInternalValue(stored_value);
base::TimeDelta time_to_reboot =
clock->GetMonotonicTime() - system_updated_at;
if (time_to_reboot.ToInternalValue() < 0) {
LOG(ERROR) << "time_to_reboot is negative - system_updated_at: "
<< utils::ToString(system_updated_at);
return false;
}
metrics_reporter->ReportTimeToReboot(time_to_reboot.InMinutes());
return true;
}
} // namespace metrics_utils
} // namespace chromeos_update_engine