1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "uploader/bn_metricsd_impl.h" 18 19 #include <base/metrics/histogram.h> 20 #include <base/metrics/sparse_histogram.h> 21 #include <base/metrics/statistics_recorder.h> 22 #include <utils/Errors.h> 23 #include <utils/String16.h> 24 #include <utils/String8.h> 25 26 using android::binder::Status; 27 using android::String16; 28 29 static const char16_t kCrashTypeKernel[] = u"kernel"; 30 static const char16_t kCrashTypeUncleanShutdown[] = u"uncleanshutdown"; 31 static const char16_t kCrashTypeUser[] = u"user"; 32 33 BnMetricsdImpl::BnMetricsdImpl(const std::shared_ptr<CrashCounters>& counters) 34 : counters_(counters) { 35 CHECK(counters_) << "Invalid counters argument to constructor"; 36 } 37 38 Status BnMetricsdImpl::recordHistogram( 39 const String16& name, int sample, int min, int max, int nbuckets) { 40 base::HistogramBase* histogram = base::Histogram::FactoryGet( 41 android::String8(name).string(), min, max, nbuckets, 42 base::Histogram::kUmaTargetedHistogramFlag); 43 // |histogram| may be null if a client reports two contradicting histograms 44 // with the same name but different limits. 45 // FactoryGet will print a useful message if that is the case. 46 if (histogram) { 47 histogram->Add(sample); 48 } 49 return Status::ok(); 50 } 51 52 Status BnMetricsdImpl::recordLinearHistogram(const String16& name, 53 int sample, 54 int max) { 55 base::HistogramBase* histogram = base::LinearHistogram::FactoryGet( 56 android::String8(name).string(), 1, max, max + 1, 57 base::Histogram::kUmaTargetedHistogramFlag); 58 // |histogram| may be null if a client reports two contradicting histograms 59 // with the same name but different limits. 60 // FactoryGet will print a useful message if that is the case. 61 if (histogram) { 62 histogram->Add(sample); 63 } 64 return Status::ok(); 65 } 66 67 Status BnMetricsdImpl::recordSparseHistogram(const String16& name, int sample) { 68 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( 69 android::String8(name).string(), 70 base::Histogram::kUmaTargetedHistogramFlag); 71 // |histogram| may be null if a client reports two contradicting histograms 72 // with the same name but different limits. 73 // FactoryGet will print a useful message if that is the case. 74 if (histogram) { 75 histogram->Add(sample); 76 } 77 return Status::ok(); 78 } 79 80 Status BnMetricsdImpl::recordCrash(const String16& type) { 81 if (type == kCrashTypeUser) { 82 counters_->IncrementUserCrashCount(); 83 } else if (type == kCrashTypeKernel) { 84 counters_->IncrementKernelCrashCount(); 85 } else if (type == kCrashTypeUncleanShutdown) { 86 counters_->IncrementUncleanShutdownCount(); 87 } else { 88 LOG(ERROR) << "Unknown crash type received: " << type; 89 } 90 return Status::ok(); 91 } 92 93 Status BnMetricsdImpl::getHistogramsDump(String16* dump) { 94 std::string str_dump; 95 base::StatisticsRecorder::WriteGraph(std::string(), &str_dump); 96 *dump = String16(str_dump.c_str()); 97 return Status::ok(); 98 } 99