xref: /aosp_15_r20/art/libartbase/base/stats-inl.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 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_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_STATS_INL_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <iomanip>
21*795d594fSAndroid Build Coastguard Worker #include <map>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "base/stats.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/indenter.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art {
DumpSizes(VariableIndentationOutputStream & os,std::string_view name)27*795d594fSAndroid Build Coastguard Worker   void Stats::DumpSizes(VariableIndentationOutputStream& os, std::string_view name) const {
28*795d594fSAndroid Build Coastguard Worker     Dump(os, name, Value(), 1000.0, "KB");
29*795d594fSAndroid Build Coastguard Worker   }
30*795d594fSAndroid Build Coastguard Worker 
Dump(VariableIndentationOutputStream & os,std::string_view name,double total,double unit_size,const char * unit)31*795d594fSAndroid Build Coastguard Worker   void Stats::Dump(VariableIndentationOutputStream& os,
32*795d594fSAndroid Build Coastguard Worker                    std::string_view name,
33*795d594fSAndroid Build Coastguard Worker                    double total,
34*795d594fSAndroid Build Coastguard Worker                    double unit_size,
35*795d594fSAndroid Build Coastguard Worker                    const char* unit) const {
36*795d594fSAndroid Build Coastguard Worker     double percent = total > 0 ? 100.0 * Value() / total : 0;
37*795d594fSAndroid Build Coastguard Worker     const size_t name_width = 52 - os.GetIndentation();
38*795d594fSAndroid Build Coastguard Worker     if (name.length() > name_width) {
39*795d594fSAndroid Build Coastguard Worker       // Handle very long names by printing them on their own line.
40*795d594fSAndroid Build Coastguard Worker       os.Stream() << name << " \\\n";
41*795d594fSAndroid Build Coastguard Worker       name = "";
42*795d594fSAndroid Build Coastguard Worker     }
43*795d594fSAndroid Build Coastguard Worker     os.Stream()
44*795d594fSAndroid Build Coastguard Worker         << std::setw(name_width) << std::left << name << " "
45*795d594fSAndroid Build Coastguard Worker         << std::setw(6) << std::right << Count() << " "
46*795d594fSAndroid Build Coastguard Worker         << std::setw(10) << std::fixed << std::setprecision(3) << Value() / unit_size << unit << " "
47*795d594fSAndroid Build Coastguard Worker         << std::setw(6) << std::fixed << std::setprecision(1) << percent << "%\n";
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker     // Sort all children by largest value first, then by name.
50*795d594fSAndroid Build Coastguard Worker     std::map<std::pair<double, std::string_view>, const Stats&> sorted_children;
51*795d594fSAndroid Build Coastguard Worker     for (const auto& it : Children()) {
52*795d594fSAndroid Build Coastguard Worker       sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
53*795d594fSAndroid Build Coastguard Worker     }
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker     // Add "other" row to represent any amount not account for by the children.
56*795d594fSAndroid Build Coastguard Worker     Stats other;
57*795d594fSAndroid Build Coastguard Worker     other.AddBytes(Value() - SumChildrenValues(), Count());
58*795d594fSAndroid Build Coastguard Worker     if (other.Value() != 0.0 && !Children().empty()) {
59*795d594fSAndroid Build Coastguard Worker       sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
60*795d594fSAndroid Build Coastguard Worker     }
61*795d594fSAndroid Build Coastguard Worker 
62*795d594fSAndroid Build Coastguard Worker     // Print the data.
63*795d594fSAndroid Build Coastguard Worker     ScopedIndentation indent1(&os);
64*795d594fSAndroid Build Coastguard Worker     for (const auto& it : sorted_children) {
65*795d594fSAndroid Build Coastguard Worker       it.second.Dump(os, it.first.second, total, unit_size, unit);
66*795d594fSAndroid Build Coastguard Worker     }
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker }  // namespace art
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_STATS_INL_H_
71