|
| 1 | +//===--- Statistic.cpp - Swift unified stats reporting --------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/Basic/Statistic.h" |
| 14 | +#include "swift/Driver/DependencyGraph.h" |
| 15 | +#include "swift/SIL/SILModule.h" |
| 16 | +#include "llvm/Support/FileSystem.h" |
| 17 | +#include "llvm/Support/Path.h" |
| 18 | +#include "llvm/Support/Process.h" |
| 19 | +#include "llvm/Support/raw_ostream.h" |
| 20 | +#include <chrono> |
| 21 | + |
| 22 | +namespace swift { |
| 23 | +using namespace llvm; |
| 24 | +using namespace llvm::sys; |
| 25 | + |
| 26 | +static std::string |
| 27 | +makeFileName(StringRef ProcessName) { |
| 28 | + std::string tmp; |
| 29 | + raw_string_ostream stream(tmp); |
| 30 | + auto now = std::chrono::system_clock::now(); |
| 31 | + stream << "stats" |
| 32 | + << "-" << now.time_since_epoch().count() |
| 33 | + << "-" << ProcessName |
| 34 | + << "-" << Process::GetRandomNumber() |
| 35 | + << ".json"; |
| 36 | + return stream.str(); |
| 37 | +} |
| 38 | + |
| 39 | +UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName, |
| 40 | + StringRef TargetName, |
| 41 | + StringRef Directory) |
| 42 | + : Filename(Directory), |
| 43 | + Timer(make_unique<NamedRegionTimer>(TargetName, "Building Target", |
| 44 | + ProgramName, "Running Program")) |
| 45 | +{ |
| 46 | + path::append(Filename, makeFileName(ProgramName)); |
| 47 | + EnableStatistics(/*PrintOnExit=*/false); |
| 48 | + SharedTimer::enableCompilationTimers(); |
| 49 | +} |
| 50 | + |
| 51 | +UnifiedStatsReporter::AlwaysOnDriverCounters & |
| 52 | +UnifiedStatsReporter::getDriverCounters() |
| 53 | +{ |
| 54 | + if (!DriverCounters) |
| 55 | + DriverCounters = make_unique<AlwaysOnDriverCounters>(); |
| 56 | + return *DriverCounters; |
| 57 | +} |
| 58 | + |
| 59 | +UnifiedStatsReporter::AlwaysOnFrontendCounters & |
| 60 | +UnifiedStatsReporter::getFrontendCounters() |
| 61 | +{ |
| 62 | + if (!FrontendCounters) |
| 63 | + FrontendCounters = make_unique<AlwaysOnFrontendCounters>(); |
| 64 | + return *FrontendCounters; |
| 65 | +} |
| 66 | + |
| 67 | +#define PUBLISH_STAT(C,TY,NAME) \ |
| 68 | + do { \ |
| 69 | + static Statistic Stat = {TY, #NAME, #NAME, {0}, false}; \ |
| 70 | + Stat += (C).NAME; \ |
| 71 | + } while(0) |
| 72 | + |
| 73 | +void |
| 74 | +UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() { |
| 75 | + if (FrontendCounters) { |
| 76 | + auto &C = getFrontendCounters(); |
| 77 | + PUBLISH_STAT(C, "SILModule", NumSILGenFunctions); |
| 78 | + PUBLISH_STAT(C, "SILModule", NumSILGenVtables); |
| 79 | + PUBLISH_STAT(C, "SILModule", NumSILGenWitnessTables); |
| 80 | + PUBLISH_STAT(C, "SILModule", NumSILGenDefaultWitnessTables); |
| 81 | + PUBLISH_STAT(C, "SILModule", NumSILGenGlobalVariables); |
| 82 | + |
| 83 | + PUBLISH_STAT(C, "SILModule", NumSILOptFunctions); |
| 84 | + PUBLISH_STAT(C, "SILModule", NumSILOptVtables); |
| 85 | + PUBLISH_STAT(C, "SILModule", NumSILOptWitnessTables); |
| 86 | + PUBLISH_STAT(C, "SILModule", NumSILOptDefaultWitnessTables); |
| 87 | + PUBLISH_STAT(C, "SILModule", NumSILOptGlobalVariables); |
| 88 | + } |
| 89 | + if (DriverCounters) { |
| 90 | + auto &C = getDriverCounters(); |
| 91 | + PUBLISH_STAT(C, "Driver", NumDriverJobsRun); |
| 92 | + PUBLISH_STAT(C, "Driver", NumDriverJobsSkipped); |
| 93 | + |
| 94 | + PUBLISH_STAT(C, "Driver", DriverDepCascadingTopLevel); |
| 95 | + PUBLISH_STAT(C, "Driver", DriverDepCascadingDynamic); |
| 96 | + PUBLISH_STAT(C, "Driver", DriverDepCascadingNominal); |
| 97 | + PUBLISH_STAT(C, "Driver", DriverDepCascadingMember); |
| 98 | + PUBLISH_STAT(C, "Driver", DriverDepCascadingExternal); |
| 99 | + |
| 100 | + PUBLISH_STAT(C, "Driver", DriverDepTopLevel); |
| 101 | + PUBLISH_STAT(C, "Driver", DriverDepDynamic); |
| 102 | + PUBLISH_STAT(C, "Driver", DriverDepNominal); |
| 103 | + PUBLISH_STAT(C, "Driver", DriverDepMember); |
| 104 | + PUBLISH_STAT(C, "Driver", DriverDepExternal); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#define PRINT_STAT(OS,DELIM,C,TY,NAME) \ |
| 109 | + do { \ |
| 110 | + OS << DELIM << "\t\"" TY "." #NAME "\": " << C.NAME; \ |
| 111 | + delim = ",\n"; \ |
| 112 | + } while(0) |
| 113 | + |
| 114 | +void |
| 115 | +UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) { |
| 116 | + // Adapted from llvm::PrintStatisticsJSON |
| 117 | + OS << "{\n"; |
| 118 | + const char *delim = ""; |
| 119 | + if (FrontendCounters) { |
| 120 | + auto &C = getFrontendCounters(); |
| 121 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILGenFunctions); |
| 122 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILGenVtables); |
| 123 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILGenWitnessTables); |
| 124 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILGenDefaultWitnessTables); |
| 125 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILGenGlobalVariables); |
| 126 | + |
| 127 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILOptFunctions); |
| 128 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILOptVtables); |
| 129 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILOptWitnessTables); |
| 130 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILOptDefaultWitnessTables); |
| 131 | + PRINT_STAT(OS, delim, C, "SILModule", NumSILOptGlobalVariables); |
| 132 | + } |
| 133 | + if (DriverCounters) { |
| 134 | + auto &C = getDriverCounters(); |
| 135 | + PRINT_STAT(OS, delim, C, "Driver", NumDriverJobsRun); |
| 136 | + PRINT_STAT(OS, delim, C, "Driver", NumDriverJobsSkipped); |
| 137 | + |
| 138 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepCascadingTopLevel); |
| 139 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepCascadingDynamic); |
| 140 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepCascadingNominal); |
| 141 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepCascadingMember); |
| 142 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepCascadingExternal); |
| 143 | + |
| 144 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepTopLevel); |
| 145 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepDynamic); |
| 146 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepNominal); |
| 147 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepMember); |
| 148 | + PRINT_STAT(OS, delim, C, "Driver", DriverDepExternal); |
| 149 | + } |
| 150 | + // Print timers. |
| 151 | + TimerGroup::printAllJSONValues(OS, delim); |
| 152 | + OS << "\n}\n"; |
| 153 | + OS.flush(); |
| 154 | +} |
| 155 | + |
| 156 | +UnifiedStatsReporter::~UnifiedStatsReporter() |
| 157 | +{ |
| 158 | + // NB: Timer needs to be Optional<> because it needs to be destructed early; |
| 159 | + // LLVM will complain about double-stopping a timer if you tear down a |
| 160 | + // NamedRegionTimer after printing all timers. The printing routines were |
| 161 | + // designed with more of a global-scope, run-at-process-exit in mind, which |
| 162 | + // we're repurposing a bit here. |
| 163 | + Timer.reset(); |
| 164 | + |
| 165 | + std::error_code EC; |
| 166 | + raw_fd_ostream ostream(Filename, EC, fs::F_Append | fs::F_Text); |
| 167 | + if (EC) |
| 168 | + return; |
| 169 | + |
| 170 | + // We change behaviour here depending on whether -DLLVM_ENABLE_STATS and/or |
| 171 | + // assertions were on in this build; this is somewhat subtle, but turning on |
| 172 | + // all stats for all of LLVM and clang is a bit more expensive and intrusive |
| 173 | + // than we want to be in release builds. |
| 174 | + // |
| 175 | + // - If enabled: we copy all of our "always-on" local stats into LLVM's |
| 176 | + // global statistics list, and ask LLVM to manage the printing of them. |
| 177 | + // |
| 178 | + // - If disabled: we still have our "always-on" local stats to write, and |
| 179 | + // LLVM's global _timers_ were still enabled (they're runtime-enabled, not |
| 180 | + // compile-time) so we sequence printing our own stats and LLVM's timers |
| 181 | + // manually. |
| 182 | + |
| 183 | +#if !defined(NDEBUG) || defined(LLVM_ENABLE_STATS) |
| 184 | + publishAlwaysOnStatsToLLVM(); |
| 185 | + PrintStatisticsJSON(ostream); |
| 186 | +#else |
| 187 | + printAlwaysOnStatsAndTimers(ostream); |
| 188 | +#endif |
| 189 | +} |
| 190 | + |
| 191 | +} // namespace swift |
0 commit comments