blob: 5fa1372432061feffc4ecb6f2ae75c5394e6bb39 [file] [log] [blame]
David Srbecky81b1d782021-03-07 21:33:28 +00001/*
2 * Copyright (C) 2021 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_INL_H_
18#define ART_LIBARTBASE_BASE_STATS_INL_H_
19
20#include <iomanip>
21#include <map>
22
23#include "base/stats.h"
24#include "base/indenter.h"
25
26namespace art {
27 void Stats::DumpSizes(VariableIndentationOutputStream& os, std::string_view name) const {
28 Dump(os, name, Value(), 1000.0, "KB");
29 }
30
31 void Stats::Dump(VariableIndentationOutputStream& os,
32 std::string_view name,
33 double total,
34 double unit_size,
35 const char* unit) const {
36 double percent = total > 0 ? 100.0 * Value() / total : 0;
37 os.Stream()
38 << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
39 << std::setw(8) << Count() << " "
40 << std::setw(12) << std::fixed << std::setprecision(3) << Value() / unit_size << unit
41 << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
42
43 // Sort all children by largest value first, than by name.
44 std::map<std::pair<double, std::string_view>, const Stats&> sorted_children;
45 for (const auto& it : Children()) {
46 sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
47 }
48
49 // Add "other" row to represent any amount not account for by the children.
50 Stats other;
51 other.AddBytes(Value() - SumChildrenValues(), Count());
52 if (other.Value() != 0.0 && !Children().empty()) {
53 sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
54 }
55
56 // Print the data.
57 ScopedIndentation indent1(&os);
58 for (const auto& it : sorted_children) {
59 it.second.Dump(os, it.first.second, total, unit_size, unit);
60 }
61 }
62} // namespace art
63
64#endif // ART_LIBARTBASE_BASE_STATS_INL_H_