Skip to content

Commit 8c32c0d

Browse files
authored
Merge pull request swiftlang#10084 from graydon/process-stats-dir-mangling
[Stats] Mangle a little more information into the file & timer names.
2 parents 904ef73 + e10f483 commit 8c32c0d

File tree

5 files changed

+106
-30
lines changed

5 files changed

+106
-30
lines changed

include/swift/Basic/Statistic.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,16 @@ class UnifiedStatsReporter {
140140
void publishAlwaysOnStatsToLLVM();
141141
void printAlwaysOnStatsAndTimers(llvm::raw_ostream &OS);
142142

143+
UnifiedStatsReporter(StringRef ProgramName,
144+
StringRef AuxName,
145+
StringRef Directory);
143146
public:
144147
UnifiedStatsReporter(StringRef ProgramName,
145-
StringRef TargetName,
148+
StringRef ModuleName,
149+
StringRef InputName,
150+
StringRef TripleName,
151+
StringRef OutputType,
152+
StringRef OptType,
146153
StringRef Directory);
147154
~UnifiedStatsReporter();
148155

lib/Basic/Statistic.cpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ getChildrenMaxResidentSetSize() {
4343
}
4444

4545
static std::string
46-
makeFileName(StringRef ProcessName) {
46+
makeFileName(StringRef ProgramName,
47+
StringRef AuxName) {
4748
std::string tmp;
4849
raw_string_ostream stream(tmp);
4950
auto now = std::chrono::system_clock::now();
5051
stream << "stats"
5152
<< "-" << now.time_since_epoch().count()
52-
<< "-" << ProcessName
53+
<< "-" << ProgramName
54+
<< "-" << AuxName
5355
<< "-" << Process::GetRandomNumber()
5456
<< ".json";
5557
return stream.str();
@@ -58,31 +60,63 @@ makeFileName(StringRef ProcessName) {
5860
// LLVM's statistics-reporting machinery is sensitive to filenames containing
5961
// YAML-quote-requiring characters, which occur surprisingly often in the wild;
6062
// we only need a recognizable and likely-unique name for a target here, not an
61-
// exact filename, so we go with a crude approximation.
63+
// exact filename, so we go with a crude approximation. Furthermore, to avoid
64+
// parse ambiguities when "demangling" counters and filenames we exclude hyphens
65+
// and slashes.
6266
static std::string
63-
cleanTargetName(StringRef TargetName) {
67+
cleanName(StringRef n) {
6468
std::string tmp;
65-
for (auto c : TargetName) {
69+
for (auto c : n) {
6670
if (('a' <= c && c <= 'z') ||
6771
('A' <= c && c <= 'Z') ||
6872
('0' <= c && c <= '9') ||
69-
(c == '-') || (c == '.') || (c == '/'))
73+
(c == '.'))
7074
tmp += c;
7175
else
7276
tmp += '_';
7377
}
7478
return tmp;
7579
}
7680

81+
static std::string
82+
auxName(StringRef ModuleName,
83+
StringRef InputName,
84+
StringRef TripleName,
85+
StringRef OutputType,
86+
StringRef OptType) {
87+
return (cleanName(ModuleName)
88+
+ "-" + cleanName(InputName)
89+
+ "-" + cleanName(TripleName)
90+
+ "-" + cleanName(OutputType)
91+
+ "-" + cleanName(OptType));
92+
}
93+
94+
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
95+
StringRef ModuleName,
96+
StringRef InputName,
97+
StringRef TripleName,
98+
StringRef OutputType,
99+
StringRef OptType,
100+
StringRef Directory)
101+
: UnifiedStatsReporter(ProgramName,
102+
auxName(ModuleName,
103+
InputName,
104+
TripleName,
105+
OutputType,
106+
OptType),
107+
Directory)
108+
{
109+
}
110+
77111
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
78-
StringRef TargetName,
112+
StringRef AuxName,
79113
StringRef Directory)
80114
: Filename(Directory),
81-
Timer(make_unique<NamedRegionTimer>(cleanTargetName(TargetName),
115+
Timer(make_unique<NamedRegionTimer>(AuxName,
82116
"Building Target",
83117
ProgramName, "Running Program"))
84118
{
85-
path::append(Filename, makeFileName(ProgramName));
119+
path::append(Filename, makeFileName(ProgramName, AuxName));
86120
EnableStatistics(/*PrintOnExit=*/false);
87121
SharedTimer::enableCompilationTimers();
88122
}

lib/Driver/Driver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,21 @@ std::unique_ptr<Compilation> Driver::buildCompilation(
558558
std::unique_ptr<UnifiedStatsReporter> StatsReporter;
559559
if (const Arg *A =
560560
ArgList->getLastArgNoClaim(options::OPT_stats_output_dir)) {
561+
StringRef OptType = "Onone";
562+
if (const Arg *OptA = ArgList->getLastArgNoClaim(options::OPT_O_Group)) {
563+
OptType = OptA->getSpelling();
564+
}
565+
StringRef InputName = "all";
566+
if (Inputs.size() == 1) {
567+
InputName = Inputs[0].second->getSpelling();
568+
}
569+
StringRef OutputType = types::getTypeTempSuffix(OI.CompilerOutputType);
561570
StatsReporter = llvm::make_unique<UnifiedStatsReporter>("swift-driver",
562571
OI.ModuleName,
572+
InputName,
573+
DefaultTargetTriple,
574+
OutputType,
575+
OptType,
563576
A->getValue());
564577
}
565578

lib/FrontendTool/FrontendTool.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,18 @@ static bool dumpAPI(ModuleDecl *Mod, StringRef OutDir) {
10321032
return false;
10331033
}
10341034

1035+
static StringRef
1036+
silOptModeArgStr(SILOptions::SILOptMode mode) {
1037+
switch (mode) {
1038+
case SILOptions::SILOptMode::Optimize:
1039+
return "O";
1040+
case SILOptions::SILOptMode::OptimizeUnchecked:
1041+
return "Ounchecked";
1042+
default:
1043+
return "Onone";
1044+
}
1045+
}
1046+
10351047
int swift::performFrontend(ArrayRef<const char *> Args,
10361048
const char *Argv0, void *MainAddr,
10371049
FrontendObserver *observer) {
@@ -1183,16 +1195,29 @@ int swift::performFrontend(ArrayRef<const char *> Args,
11831195
Invocation.getFrontendOptions().StatsOutputDir;
11841196
std::unique_ptr<UnifiedStatsReporter> StatsReporter;
11851197
if (!StatsOutputDir.empty()) {
1186-
auto &opts = Invocation.getFrontendOptions();
1187-
std::string TargetName = opts.ModuleName;
1188-
if (opts.PrimaryInput.hasValue() &&
1189-
opts.PrimaryInput.getValue().isFilename()) {
1190-
auto Index = opts.PrimaryInput.getValue().Index;
1191-
TargetName += ".";
1192-
TargetName += llvm::sys::path::filename(opts.InputFilenames[Index]);
1198+
auto &FEOpts = Invocation.getFrontendOptions();
1199+
auto &LangOpts = Invocation.getLangOptions();
1200+
auto &SILOpts = Invocation.getSILOptions();
1201+
StringRef InputName;
1202+
std::string TargetName = FEOpts.ModuleName;
1203+
if (FEOpts.PrimaryInput.hasValue() &&
1204+
FEOpts.PrimaryInput.getValue().isFilename()) {
1205+
auto Index = FEOpts.PrimaryInput.getValue().Index;
1206+
InputName = FEOpts.InputFilenames[Index];
1207+
}
1208+
StringRef OptType = silOptModeArgStr(SILOpts.Optimization);
1209+
StringRef OutFile = FEOpts.getSingleOutputFilename();
1210+
StringRef OutputType = llvm::sys::path::extension(OutFile);
1211+
if (OutputType.size() > 0 && OutputType.front() == '.') {
1212+
OutputType = OutputType.substr(1);
11931213
}
1214+
std::string TripleName = LangOpts.Target.normalize();
11941215
StatsReporter = llvm::make_unique<UnifiedStatsReporter>("swift-frontend",
1195-
TargetName,
1216+
FEOpts.ModuleName,
1217+
InputName,
1218+
TripleName,
1219+
OutputType,
1220+
OptType,
11961221
StatsOutputDir);
11971222
}
11981223

utils/process-stats-dir.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ def to_lnt_test_obj(self, args):
148148
# Return an array of JobStats objects
149149
def load_stats_dir(path):
150150
jobstats = []
151-
fpat = r"^stats-(?P<start>\d+)-swift-(?P<kind>\w+)-(?P<pid>\d+).json$"
151+
auxpat = (r"(?P<module>[^-]+)-(?P<input>[^-]+)-(?P<triple>[^-]+)" +
152+
r"-(?P<out>[^-]+)-(?P<opt>[^-]+)")
153+
fpat = (r"^stats-(?P<start>\d+)-swift-(?P<kind>\w+)-" +
154+
auxpat +
155+
r"-(?P<pid>\d+)(-.*)?.json$")
152156
for root, dirs, files in os.walk(path):
153157
for f in files:
154158
m = re.match(fpat, f)
@@ -158,13 +162,13 @@ def load_stats_dir(path):
158162
jobkind = mg['kind']
159163
jobid = int(mg['pid'])
160164
start_usec = int(mg['start'])
165+
module = mg["module"]
166+
jobargs = [mg["input"], mg["triple"], mg["out"], mg["opt"]]
161167

162168
j = json.load(open(os.path.join(root, f)))
163169
dur_usec = 1
164-
jobargs = None
165-
module = "module"
166-
patstr = (r"time\.swift-" + jobkind +
167-
r"\.(?P<module>[^\.]+)(?P<filename>.*)\.wall$")
170+
patstr = (r"time\.swift-" + jobkind + r"\." + auxpat +
171+
r"\.wall$")
168172
pat = re.compile(patstr)
169173
stats = dict()
170174
for (k, v) in j.items():
@@ -173,14 +177,7 @@ def load_stats_dir(path):
173177
stats[k] = v
174178
tm = re.match(pat, k)
175179
if tm:
176-
tmg = tm.groupdict()
177180
dur_usec = v
178-
module = tmg['module']
179-
if 'filename' in tmg:
180-
ff = tmg['filename']
181-
if ff.startswith('.'):
182-
ff = ff[1:]
183-
jobargs = [ff]
184181

185182
e = JobStats(jobkind=jobkind, jobid=jobid,
186183
module=module, start_usec=start_usec,

0 commit comments

Comments
 (0)