1 /* 2 * Copyright (C) 2020 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 "src/profiling/memory/log_histogram.h" 18 19 #include <stddef.h> 20 21 #include <cinttypes> 22 #include <utility> 23 #include <vector> 24 25 namespace perfetto { 26 namespace profiling { 27 namespace { 28 29 // Return largest n such that pow(2, n) < value. Log2LessThan(uint64_t value)30size_t Log2LessThan(uint64_t value) { 31 size_t i = 0; 32 while (value) { 33 i++; 34 value >>= 1; 35 } 36 return i; 37 } 38 39 } // namespace 40 41 const uint64_t LogHistogram::kMaxBucket = 0; 42 GetData() const43std::vector<std::pair<uint64_t, uint64_t>> LogHistogram::GetData() const { 44 std::vector<std::pair<uint64_t, uint64_t>> data; 45 data.reserve(kBuckets); 46 for (size_t i = 0; i < kBuckets; ++i) { 47 if (i == kBuckets - 1) 48 data.emplace_back(kMaxBucket, values_[i]); 49 else 50 data.emplace_back(1 << i, values_[i]); 51 } 52 return data; 53 } 54 GetBucket(uint64_t value)55size_t LogHistogram::GetBucket(uint64_t value) { 56 if (value == 0) 57 return 0; 58 59 size_t hibit = Log2LessThan(value); 60 if (hibit >= kBuckets) 61 return kBuckets - 1; 62 return hibit; 63 } 64 65 } // namespace profiling 66 } // namespace perfetto 67