Skip to content

Commit cc7eec2

Browse files
authored
Adding Stats (#473)
* Adding stats * Fixing
1 parent f44b67d commit cc7eec2

File tree

9 files changed

+152
-6
lines changed

9 files changed

+152
-6
lines changed

clang/include/clang/3C/3C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ class _3CInterface {
104104
const std::vector<std::string> &SourceFileList,
105105
clang::tooling::CompilationDatabase *CompDB);
106106

107+
virtual ~_3CInterface() {
108+
GlobalProgramInfo.getPerfStats().printPerformanceStats(llvm::errs());
109+
}
110+
107111
// Constraint Building.
108112

109113
// Create ConstraintVariables to hold constraints

clang/include/clang/3C/ProgramInfo.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,54 @@
2323
#include "clang/Frontend/FrontendAction.h"
2424
#include "clang/Tooling/Tooling.h"
2525

26+
class PerformanceStats {
27+
public:
28+
double CompileTime;
29+
double ConstraintBuilderTime;
30+
double ConstraintSolverTime;
31+
double ArrayBoundsInferenceTime;
32+
double RewritingTime;
33+
double TotalTime;
34+
35+
PerformanceStats() {
36+
CompileTime = ConstraintBuilderTime = 0;
37+
ConstraintSolverTime = ArrayBoundsInferenceTime = 0;
38+
RewritingTime = TotalTime = 0;
39+
40+
CompileTimeSt = ConstraintBuilderTimeSt = 0;
41+
ConstraintSolverTimeSt = ArrayBoundsInferenceTimeSt = 0;
42+
RewritingTimeSt = TotalTimeSt = 0;
43+
}
44+
45+
void startCompileTime();
46+
void endCompileTime();
47+
48+
void startConstraintBuilderTime();
49+
void endConstraintBuilderTime();
50+
51+
void startConstraintSolverTime();
52+
void endConstraintSolverTime();
53+
54+
void startArrayBoundsInferenceTime();
55+
void endArrayBoundsInferenceTime();
56+
57+
void startRewritingTime();
58+
void endRewritingTime();
59+
60+
void startTotalTime();
61+
void endTotalTime();
62+
63+
void printPerformanceStats(raw_ostream &O);
64+
65+
private:
66+
clock_t CompileTimeSt;
67+
clock_t ConstraintBuilderTimeSt;
68+
clock_t ConstraintSolverTimeSt;
69+
clock_t ArrayBoundsInferenceTimeSt;
70+
clock_t RewritingTimeSt;
71+
clock_t TotalTimeSt;
72+
73+
};
2674
class ProgramVariableAdder {
2775
public:
2876
virtual void addVariable(clang::DeclaratorDecl *D,
@@ -95,7 +143,11 @@ class ProgramInfo : public ProgramVariableAdder {
95143
Constraints &getConstraints() { return CS; }
96144
AVarBoundsInfo &getABoundsInfo() { return ArrBInfo; }
97145

98-
ConstraintsInfo &getInterimConstraintState() { return CState; }
146+
PerformanceStats &getPerfStats() { return PerfS; }
147+
148+
ConstraintsInfo &getInterimConstraintState() {
149+
return CState;
150+
}
99151
bool computeInterimConstraintState(const std::set<std::string> &FilePaths);
100152

101153
const ExternalFunctionMapType &getExternFuncDefFVMap() const {
@@ -148,6 +200,9 @@ class ProgramInfo : public ProgramVariableAdder {
148200
// placement, the variables are stored in this separate map.
149201
std::map<PersistentSourceLoc, CVarSet> ImplicitCastConstraintVars;
150202

203+
//Performance stats
204+
PerformanceStats PerfS;
205+
151206
// Constraint system.
152207
Constraints CS;
153208
// Is the ProgramInfo persisted? Only tested in asserts. Starts at true.

clang/lib/3C/3C.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ _3CInterface::_3CInterface(const struct _3COptions &CCopt,
343343
}
344344

345345
CurrCompDB = CompDB;
346+
347+
GlobalProgramInfo.getPerfStats().startTotalTime();
346348
}
347349

348350
bool _3CInterface::addVariables() {
@@ -403,7 +405,11 @@ bool _3CInterface::solveConstraints() {
403405
if (DumpIntermediate)
404406
GlobalProgramInfo.dump();
405407

408+
auto &PStats = GlobalProgramInfo.getPerfStats();
409+
410+
PStats.startConstraintSolverTime();
406411
runSolver(GlobalProgramInfo, FilePaths);
412+
PStats.endConstraintSolverTime();
407413

408414
if (Verbose)
409415
errs() << "Constraints solved\n";
@@ -487,8 +493,10 @@ bool _3CInterface::solveConstraints() {
487493

488494
bool _3CInterface::writeConvertedFileToDisk(const std::string &FilePath) {
489495
std::lock_guard<std::mutex> Lock(InterfaceMutex);
496+
bool RetVal = false;
490497
if (std::find(SourceFiles.begin(), SourceFiles.end(), FilePath) !=
491498
SourceFiles.end()) {
499+
RetVal = true;
492500
std::vector<std::string> SourceFiles;
493501
SourceFiles.clear();
494502
SourceFiles.push_back(FilePath);
@@ -502,11 +510,12 @@ bool _3CInterface::writeConvertedFileToDisk(const std::string &FilePath) {
502510
if (RewriteTool) {
503511
int ToolExitCode = Tool.run(RewriteTool.get());
504512
if (ToolExitCode != 0)
505-
return false;
513+
RetVal = false;
506514
}
507-
return true;
508515
}
509-
return false;
516+
GlobalProgramInfo.getPerfStats().endTotalTime();
517+
GlobalProgramInfo.getPerfStats().startTotalTime();
518+
return RetVal;
510519
}
511520

512521
bool _3CInterface::writeAllConvertedFilesToDisk() {
@@ -525,6 +534,8 @@ bool _3CInterface::writeAllConvertedFilesToDisk() {
525534
} else
526535
llvm_unreachable("No action");
527536

537+
GlobalProgramInfo.getPerfStats().endTotalTime();
538+
GlobalProgramInfo.getPerfStats().startTotalTime();
528539
return true;
529540
}
530541

clang/lib/3C/AVarBoundsInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ bool AVarBoundsInfo::isValidBoundVariable(clang::Decl *D) {
6868
}
6969
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
7070
return !VD->getNameAsString().empty();
71-
}
72-
if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
71+
} else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
7372
return !FD->getNameAsString().empty();
7473
}
7574
return false;
@@ -1168,6 +1167,9 @@ void AVarBoundsInfo::getBoundsNeededArrPointers(
11681167
// predecessors have bounds.
11691168
bool AVarBoundsInfo::performFlowAnalysis(ProgramInfo *PI) {
11701169
bool RetVal = false;
1170+
auto &PStats = PI->getPerfStats();
1171+
1172+
PStats.startArrayBoundsInferenceTime();
11711173
AvarBoundsInference ABI(this);
11721174
// First get all the pointer vars which are ARRs
11731175
std::set<BoundsKey> ArrPointers;
@@ -1249,6 +1251,7 @@ bool AVarBoundsInfo::performFlowAnalysis(ProgramInfo *PI) {
12491251
ArrNeededBounds = ArrNeededBoundsNew;
12501252
}
12511253

1254+
PStats.endArrayBoundsInferenceTime();
12521255
return RetVal;
12531256
}
12541257

clang/lib/3C/ArrayBoundsInferenceConsumer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ bool isExpressionStructField(Expr *ToCheck, FieldDecl **TargetDecl) {
337337

338338
void AllocBasedBoundsInference::HandleTranslationUnit(ASTContext &Context) {
339339
Info.enterCompilationUnit(Context);
340+
Info.getPerfStats().startArrayBoundsInferenceTime();
340341
handleArrayVariablesBoundsDetection(&Context, Info, false);
342+
Info.getPerfStats().endArrayBoundsInferenceTime();
341343
Info.exitCompilationUnit();
342344
}
343345

clang/lib/3C/ConstraintBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,9 @@ void ConstraintBuilderConsumer::HandleTranslationUnit(ASTContext &C) {
643643
errs() << "Analyzing\n";
644644
}
645645

646+
auto &PStats = Info.getPerfStats();
647+
648+
PStats.startConstraintBuilderTime();
646649

647650
TypeVarVisitor TV = TypeVarVisitor(&C, Info);
648651
ConstraintResolver CSResolver(Info, &C);
@@ -668,6 +671,8 @@ void ConstraintBuilderConsumer::HandleTranslationUnit(ASTContext &C) {
668671
if (Verbose)
669672
errs() << "Done analyzing\n";
670673

674+
PStats.endConstraintBuilderTime();
675+
671676
Info.exitCompilationUnit();
672677
return;
673678
}

clang/lib/3C/IntermediateToolHook.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ static cl::opt<bool>
2323

2424
void IntermediateToolHook::HandleTranslationUnit(ASTContext &Context) {
2525
Info.enterCompilationUnit(Context);
26+
Info.getPerfStats().startArrayBoundsInferenceTime();
2627
handleArrayVariablesBoundsDetection(&Context, Info, !DisableArrH);
28+
Info.getPerfStats().endArrayBoundsInferenceTime();
2729
Info.exitCompilationUnit();
2830
}

clang/lib/3C/ProgramInfo.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,63 @@
1717

1818
using namespace clang;
1919

20+
void PerformanceStats::startCompileTime() {
21+
CompileTimeSt = clock();
22+
}
23+
24+
void PerformanceStats::endCompileTime() {
25+
CompileTime += getTimeSpentInSeconds(CompileTimeSt);
26+
}
27+
28+
void PerformanceStats::startConstraintBuilderTime() {
29+
ConstraintBuilderTimeSt = clock();
30+
}
31+
32+
void PerformanceStats::endConstraintBuilderTime() {
33+
ConstraintBuilderTime += getTimeSpentInSeconds(ConstraintBuilderTimeSt);
34+
}
35+
36+
void PerformanceStats::startConstraintSolverTime() {
37+
ConstraintSolverTimeSt = clock();
38+
}
39+
40+
void PerformanceStats::endConstraintSolverTime() {
41+
ConstraintSolverTime += getTimeSpentInSeconds(ConstraintSolverTimeSt);
42+
}
43+
44+
void PerformanceStats::startArrayBoundsInferenceTime() {
45+
ArrayBoundsInferenceTimeSt = clock();
46+
}
47+
48+
void PerformanceStats::endArrayBoundsInferenceTime() {
49+
ArrayBoundsInferenceTime += getTimeSpentInSeconds(ArrayBoundsInferenceTimeSt);
50+
}
51+
52+
void PerformanceStats::startRewritingTime() {
53+
RewritingTimeSt = clock();
54+
}
55+
56+
void PerformanceStats::endRewritingTime() {
57+
RewritingTime += getTimeSpentInSeconds(RewritingTimeSt);
58+
}
59+
60+
void PerformanceStats::startTotalTime() {
61+
TotalTimeSt = clock();
62+
}
63+
64+
void PerformanceStats::endTotalTime() {
65+
TotalTime += getTimeSpentInSeconds(TotalTimeSt);
66+
}
67+
68+
void PerformanceStats::printPerformanceStats(raw_ostream &O) {
69+
O << "{\"TotalTime\":" << TotalTime;
70+
O << ", \"ConstraintBuilderTime\":" << ConstraintBuilderTime;
71+
O << ", \"ConstraintSolverTime\":" << ConstraintSolverTime;
72+
O << ", \"ArrayBoundsInferenceTime\":" << ArrayBoundsInferenceTime;
73+
O << ", \"RewritingTime\":" << RewritingTime;
74+
O << "}";
75+
}
76+
2077
ProgramInfo::ProgramInfo() : Persisted(true) {
2178
ExternalFunctionFVCons.clear();
2279
StaticFunctionFVCons.clear();
@@ -274,6 +331,9 @@ void ProgramInfo::printStats(const std::set<std::string> &F, raw_ostream &O,
274331
}
275332

276333
if (JsonFormat) {
334+
O << ",";
335+
O << "\"TimingStats\":";
336+
PerfS.printPerformanceStats(O);
277337
O << "}}";
278338
}
279339
}

clang/lib/3C/RewriteUtils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ void RewriteConsumer::emitRootCauseDiagnostics(ASTContext &Context) {
546546
void RewriteConsumer::HandleTranslationUnit(ASTContext &Context) {
547547
Info.enterCompilationUnit(Context);
548548

549+
Info.getPerfStats().startRewritingTime();
550+
549551
if (WarnRootCause)
550552
emitRootCauseDiagnostics(Context);
551553

@@ -585,6 +587,8 @@ void RewriteConsumer::HandleTranslationUnit(ASTContext &Context) {
585587
// Output files.
586588
emit(R, Context);
587589

590+
Info.getPerfStats().endRewritingTime();
591+
588592
Info.exitCompilationUnit();
589593
return;
590594
}

0 commit comments

Comments
 (0)