@@ -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+
512538static 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