1 /* 2 * Copyright (C) 2018 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 #ifndef ART_LIBARTBASE_BASE_STATS_H_ 18 #define ART_LIBARTBASE_BASE_STATS_H_ 19 20 #include <unordered_map> 21 #include <string> 22 23 #include "globals.h" 24 25 namespace art { 26 27 class VariableIndentationOutputStream; 28 29 // Simple structure to record tree of statistical values. 30 class Stats { 31 public: 32 double Value() const { return value_; } 33 size_t Count() const { return count_; } 34 Stats& operator[](const char* name) { return children_[name]; } 35 const std::unordered_map<const char*, Stats>& Children() const { return children_; } 36 37 void AddBytes(double bytes, size_t count = 1) { Add(bytes, count); } 38 void AddBits(double bits, size_t count = 1) { Add(bits / kBitsPerByte, count); } 39 void AddSeconds(double s, size_t count = 1) { Add(s, count); } 40 void AddNanoSeconds(double ns, size_t count = 1) { Add(ns / 1000000000.0, count); } 41 42 double SumChildrenValues() const { 43 double sum = 0.0; 44 for (auto it : children_) { 45 sum += it.second.Value(); 46 } 47 return sum; 48 } 49 50 inline void DumpSizes(VariableIndentationOutputStream& os, std::string_view name) const; 51 52 private: 53 void Add(double value, size_t count = 1) { 54 value_ += value; 55 count_ += count; 56 } 57 58 inline void Dump(VariableIndentationOutputStream& os, 59 std::string_view name, 60 double total, 61 double unit_size, 62 const char* unit) const; 63 64 double value_ = 0.0; // Commutative sum of the collected statistic in basic units. 65 size_t count_ = 0; // The number of samples for this node. 66 std::unordered_map<const char*, Stats> children_; 67 }; 68 69 } // namespace art 70 71 #endif // ART_LIBARTBASE_BASE_STATS_H_ 72