Skip to content

Commit 5b738fa

Browse files
authored
Merge pull request #38749 from ahoppen/pr/sourcekitd-test-instruction-counter
[sourcekitd-test] Add option to count number of instructions taken to execute request
2 parents b26c15d + 85869bb commit 5b738fa

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

tools/SourceKit/tools/sourcekitd-test/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ def cancel_on_subsequent_request_EQ : Joined<["-"], "cancel-on-subsequent-reques
134134
def time_request : Flag<["-"], "time-request">,
135135
HelpText<"Print the time taken to process the request">;
136136

137+
def measure_instructions : Flag<["-"], "measure-instructions">,
138+
HelpText<"Measure how many instructions the execution of this request took in the SourceKit process.">;
139+
137140
def repeat_request : Separate<["-"], "repeat-request">,
138141
HelpText<"Repeat the request n times">, MetaVarName<"<n>">;
139142
def repeat_request_EQ : Joined<["-"], "repeat-request=">, Alias<repeat_request>;

tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
382382
timeRequest = true;
383383
break;
384384

385+
case OPT_measure_instructions:
386+
measureInstructions = true;
387+
break;
388+
385389
case OPT_repeat_request:
386390
if (StringRef(InputArg->getValue()).getAsInteger(10, repeatRequest)) {
387391
llvm::errs() << "error: expected integer for 'cancel-on-subsequent-request'\n";

tools/SourceKit/tools/sourcekitd-test/TestOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct TestOptions {
114114
bool CollectActionables = false;
115115
bool isAsyncRequest = false;
116116
bool timeRequest = false;
117+
bool measureInstructions = false;
117118
bool DisableImplicitConcurrencyModuleImport = false;
118119
llvm::Optional<unsigned> CompletionCheckDependencyInterval;
119120
unsigned repeatRequest = 1;

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,32 @@ static void setRefactoringFields(sourcekitd_object_t &Req, TestOptions Opts,
509509
sourcekitd_request_dictionary_set_int64(Req, KeyLength, Opts.Length);
510510
}
511511

512+
/// Returns the number of instructions executed by the SourceKit process since
513+
/// its launch. If SourceKit is running in-process this is the instruction count
514+
/// of the current process. If it's running out-of process it is the instruction
515+
/// count of the XPC process.
516+
int64_t getSourceKitInstructionCount() {
517+
sourcekitd_object_t Req =
518+
sourcekitd_request_dictionary_create(nullptr, nullptr, 0);
519+
sourcekitd_request_dictionary_set_uid(Req, KeyRequest, RequestStatistics);
520+
sourcekitd_response_t Resp = sourcekitd_send_request_sync(Req);
521+
sourcekitd_variant_t Info = sourcekitd_response_get_value(Resp);
522+
sourcekitd_variant_t Results =
523+
sourcekitd_variant_dictionary_get_value(Info, KeyResults);
524+
__block size_t InstructionCount = 0;
525+
sourcekitd_variant_array_apply(
526+
Results, ^bool(size_t index, sourcekitd_variant_t value) {
527+
auto UID = sourcekitd_variant_dictionary_get_uid(value, KeyKind);
528+
if (UID == KindStatInstructionCount) {
529+
InstructionCount =
530+
sourcekitd_variant_dictionary_get_int64(value, KeyValue);
531+
return false;
532+
}
533+
return true;
534+
});
535+
return InstructionCount;
536+
}
537+
512538
static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
513539
if (!Opts.JsonRequestPath.empty())
514540
return handleJsonRequestPath(Opts.JsonRequestPath, Opts);
@@ -1140,8 +1166,19 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
11401166
sourcekitd_request_release(files);
11411167
}
11421168

1169+
int64_t BeforeInstructions;
1170+
if (Opts.measureInstructions)
1171+
BeforeInstructions = getSourceKitInstructionCount();
1172+
11431173
if (!Opts.isAsyncRequest) {
11441174
sourcekitd_response_t Resp = sendRequestSync(Req, Opts);
1175+
1176+
if (Opts.measureInstructions) {
1177+
int64_t AfterInstructions = getSourceKitInstructionCount();
1178+
llvm::errs() << "request instructions: "
1179+
<< (AfterInstructions - BeforeInstructions);
1180+
}
1181+
11451182
sourcekitd_request_release(Req);
11461183
return handleResponse(Resp, Opts, SemaName, std::move(SourceBuf),
11471184
&InitOpts)
@@ -1170,6 +1207,12 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
11701207
"-async not supported when sourcekitd is built without blocks support");
11711208
#endif
11721209

1210+
if (Opts.measureInstructions) {
1211+
int64_t AfterInstructions = getSourceKitInstructionCount();
1212+
llvm::errs() << "request instructions: "
1213+
<< (AfterInstructions - BeforeInstructions);
1214+
}
1215+
11731216
sourcekitd_request_release(Req);
11741217
return 0;
11751218
}

0 commit comments

Comments
 (0)