File: StatsCollector.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (208 lines) | stat: -rw-r--r-- 7,659 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "StatsCollector.h"
#include "llvm/CAS/TreeSchema.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"

using namespace llvm;
using namespace llvm::cas;

void StatsCollector::visitPOT(ExitOnError &ExitOnErr,
                              ArrayRef<ObjectProxy> TopLevels,
                              ArrayRef<POTItem> POT) {
  for (auto ID : TopLevels)
    Nodes[ID.getRef()].NumPaths = 1;

  // Visit POT in reverse (topological sort), computing Nodes and collecting
  // stats.
  for (const POTItem &Item : llvm::reverse(POT))
    visitPOTItem(ExitOnErr, Item);
}

void StatsCollector::visitPOTItem(ExitOnError &ExitOnErr, const POTItem &Item) {
  cas::ObjectRef ID = Item.ID;
  size_t NumPaths = Nodes.lookup(ID).NumPaths;
  ObjectProxy Object = ExitOnErr(CAS.getProxy(ID));

  auto updateChild = [&](ObjectRef Child) {
    ++Nodes[Child].NumParents;
    Nodes[Child].NumPaths += NumPaths;
  };

  size_t NumParents = Nodes.lookup(ID).NumParents;
  TreeSchema Schema(CAS);
  if (Schema.isNode(Object)) {
    auto &Info = Stats["builtin:tree"];
    ++Info.Count;
    TreeProxy Tree = ExitOnErr(Schema.load(Object));
    Info.NumChildren += Tree.size();
    Info.NumParents += NumParents;
    Info.NumPaths += NumPaths;
    if (!Tree.size())
      Totals.NumPaths += NumPaths; // Count paths to leafs.

    ExitOnErr(Tree.forEachEntry([&](const NamedTreeEntry &Entry) {
      // FIXME: This is copied out of BuiltinCAS.cpp's makeTree.
      Info.DataSize += sizeof(uint64_t) + sizeof(uint32_t) +
                       alignTo(Entry.getName().size() + 1, Align(4));
      updateChild(Entry.getRef());
      return Error::success();
    }));
    return;
  }

  auto addNodeStats = [&](ObjectKindInfo &Info) {
    ++Info.Count;
    Info.NumChildren += Object.getNumReferences();
    Info.NumParents += NumParents;
    Info.NumPaths += NumPaths;
    Info.DataSize += Object.getData().size();
    if (!Object.getNumReferences())
      Totals.NumPaths += NumPaths; // Count paths to leafs.

    ExitOnErr(Object.forEachReference([&](ObjectRef Child) {
      updateChild(Child);
      return Error::success();
    }));
  };

  // Handle nodes not in the schema.
  if (!Item.Schema) {
    addNodeStats(Stats["builtin:node"]);
    return;
  }

  for (auto &S : Schemas) {
    if (Item.Schema != S.first)
      continue;
    S.second(ExitOnErr, addNodeStats, Object);
    return;
  }
  llvm_unreachable("schema not found");
}

void StatsCollector::visitPOTItemMCCASV1(
    ExitOnError &ExitOnErr, llvm::mccasformats::v1::MCSchema &Schema,
    function_ref<void(ObjectKindInfo &Info)> addNodeStats,
    cas::ObjectProxy Node) {
  auto Object = ExitOnErr(Schema.get(Node.getRef()));
  addNodeStats(Stats[Object.getKindString()]);

  if (Object.getNumReferences() == 0 &&
      Object.getData().size() <
          (Object.getID().getHash().size() + sizeof(void *)))
    ++NumTinyObjects;

  if (Object.getKindString() ==
      llvm::mccasformats::v1::SectionRef::KindString) {
    if (!Object.getData().empty())
      SecRefSize += Object.getData().size();
  }
  if (Object.getKindString() == llvm::mccasformats::v1::AtomRef::KindString) {
    if (!Object.getData().empty())
      AtomRefSize += Object.getData().size();
  }
}

void StatsCollector::printToOuts(ArrayRef<ObjectProxy> TopLevels,
                                 raw_ostream &StatOS) {

  SmallVector<StringRef> Kinds;
  auto addToTotal = [&Totals = this->Totals](ObjectKindInfo Info) {
    Totals.Count += Info.Count;
    Totals.NumChildren += Info.NumChildren;
    Totals.NumParents += Info.NumParents;
    Totals.DataSize += Info.DataSize;
  };
  for (auto &I : Stats) {
    Kinds.push_back(I.first());
    addToTotal(I.second);
  }
  llvm::sort(Kinds);

  CASID FirstID = TopLevels.front().getID();
  size_t NumHashBytes = FirstID.getHash().size();
  if (ObjectStatsFormat == FormatType::Pretty) {
    StatOS
        << "  => Note: 'Parents' counts incoming edges\n"
        << "  => Note: 'Children' counts outgoing edges (to sub-objects)\n"
        << "  => Note: HashSize = " << NumHashBytes << "B\n"
        << "  => Note: PtrSize  = " << sizeof(void *) << "B\n"
        << "  => Note: Cost     = Count*HashSize + PtrSize*Children + Data\n";
  }
  StringLiteral HeaderFormatPretty =
      "{0,-22} {1,+10} {2,+7} {3,+10} {4,+7} {5,+10} "
      "{6,+7} {7,+10} {8,+7} {9,+10} {10,+7}\n";
  StringLiteral FormatPretty =
      "{0,-22} {1,+10:N} {2,+7:P} {3,+10:N} {4,+7:P} {5,+10:N} "
      "{6,+7:P} {7,+10:N} {8,+7:P} {9,+10:N} {10,+7:P}\n";
  StringLiteral FormatCSV = "{0}, {1}, {3}, {5}, {7}, {9}\n";

  StringLiteral HeaderFormat =
      ObjectStatsFormat == FormatType::Pretty ? HeaderFormatPretty : FormatCSV;
  StringLiteral Format =
      ObjectStatsFormat == FormatType::Pretty ? FormatPretty : FormatCSV;

  StatOS << llvm::formatv(HeaderFormat.begin(), "Kind", "Count", "", "Parents",
                          "", "Children", "", "Data (B)", "", "Cost (B)", "");
  if (ObjectStatsFormat == FormatType::Pretty) {
    StatOS << llvm::formatv(HeaderFormat.begin(), "====", "=====", "",
                            "=======", "", "========", "", "========", "",
                            "========", "");
  }

  auto printInfo = [&](StringRef Kind, ObjectKindInfo Info) {
    if (!Info.Count)
      return;
    auto getPercent = [](double N, double D) { return D ? N / D : 0.0; };
    size_t Size = Info.getTotalSize(NumHashBytes);
    StatOS << llvm::formatv(
        Format.begin(), Kind, Info.Count, getPercent(Info.Count, Totals.Count),
        Info.NumParents, getPercent(Info.NumParents, Totals.NumParents),
        Info.NumChildren, getPercent(Info.NumChildren, Totals.NumChildren),
        Info.DataSize, getPercent(Info.DataSize, Totals.DataSize), Size,
        getPercent(Size, Totals.getTotalSize(NumHashBytes)));
  };
  for (StringRef Kind : Kinds)
    printInfo(Kind, Stats.lookup(Kind));
  printInfo("TOTAL", Totals);

  StringLiteral OtherStatsPretty = "{0,-22} {1,+10}\n";
  StringLiteral OtherStatsCSV = "{0}, {1}\n";

  StringLiteral OtherStats = ObjectStatsFormat == FormatType::Pretty
                                 ? OtherStatsPretty
                                 : OtherStatsCSV;
  // Other stats.
  bool HasPrinted = false;
  auto printIfNotZero = [&](StringRef Name, size_t Num) {
    if (!Num)
      return;
    if (!HasPrinted)
      StatOS << "\n";
    StatOS << llvm::formatv(OtherStats.begin(), Name, Num);
    HasPrinted = true;
  };

  printIfNotZero("num-generated-names", GeneratedNames.size());
  printIfNotZero("num-section-names", SectionNames.size());
  printIfNotZero("num-symbol-names",
                 SymbolNames.size() + UndefinedSymbols.size());
  printIfNotZero("num-undefined-symbols", UndefinedSymbols.size());
  printIfNotZero("num-anonymous-symbols", NumAnonymousSymbols);
  printIfNotZero("num-template-symbols", NumTemplateSymbols);
  printIfNotZero("num-template-targets", NumTemplateTargets);
  printIfNotZero("num-zero-fill-blocks", NumZeroFillBlocks);
  printIfNotZero("num-1-target-blocks", Num2TargetBlocks);
  printIfNotZero("num-2-target-blocks", Num1TargetBlocks);
  printIfNotZero("num-tiny-objects", NumTinyObjects);
  printIfNotZero("sec-ref-size", SecRefSize);
  printIfNotZero("atom-ref-size", AtomRefSize);
}