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