xref: /aosp_15_r20/art/libartbase/base/stats.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTBASE_BASE_STATS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_STATS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <unordered_map>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "globals.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker class VariableIndentationOutputStream;
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker // Simple structure to record tree of statistical values.
30*795d594fSAndroid Build Coastguard Worker class Stats {
31*795d594fSAndroid Build Coastguard Worker  public:
Value()32*795d594fSAndroid Build Coastguard Worker   double Value() const { return value_; }
Count()33*795d594fSAndroid Build Coastguard Worker   size_t Count() const { return count_; }
34*795d594fSAndroid Build Coastguard Worker   Stats& operator[](const char* name) { return children_[name]; }
Children()35*795d594fSAndroid Build Coastguard Worker   const std::unordered_map<const char*, Stats>& Children() const { return children_; }
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker   void AddBytes(double bytes, size_t count = 1) { Add(bytes, count); }
38*795d594fSAndroid Build Coastguard Worker   void AddBits(double bits, size_t count = 1) { Add(bits / kBitsPerByte, count); }
39*795d594fSAndroid Build Coastguard Worker   void AddSeconds(double s, size_t count = 1) { Add(s, count); }
40*795d594fSAndroid Build Coastguard Worker   void AddNanoSeconds(double ns, size_t count = 1) { Add(ns / 1000000000.0, count); }
41*795d594fSAndroid Build Coastguard Worker 
SumChildrenValues()42*795d594fSAndroid Build Coastguard Worker   double SumChildrenValues() const {
43*795d594fSAndroid Build Coastguard Worker     double sum = 0.0;
44*795d594fSAndroid Build Coastguard Worker     for (auto it : children_) {
45*795d594fSAndroid Build Coastguard Worker       sum += it.second.Value();
46*795d594fSAndroid Build Coastguard Worker     }
47*795d594fSAndroid Build Coastguard Worker     return sum;
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker   inline void DumpSizes(VariableIndentationOutputStream& os, std::string_view name) const;
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker  private:
53*795d594fSAndroid Build Coastguard Worker   void Add(double value, size_t count = 1) {
54*795d594fSAndroid Build Coastguard Worker     value_ += value;
55*795d594fSAndroid Build Coastguard Worker     count_ += count;
56*795d594fSAndroid Build Coastguard Worker   }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker   inline void Dump(VariableIndentationOutputStream& os,
59*795d594fSAndroid Build Coastguard Worker                    std::string_view name,
60*795d594fSAndroid Build Coastguard Worker                    double total,
61*795d594fSAndroid Build Coastguard Worker                    double unit_size,
62*795d594fSAndroid Build Coastguard Worker                    const char* unit) const;
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   double value_ = 0.0;  // Commutative sum of the collected statistic in basic units.
65*795d594fSAndroid Build Coastguard Worker   size_t count_ = 0;    // The number of samples for this node.
66*795d594fSAndroid Build Coastguard Worker   std::unordered_map<const char*, Stats> children_;
67*795d594fSAndroid Build Coastguard Worker };
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker }  // namespace art
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_STATS_H_
72