@@ -509,6 +509,32 @@ static void setRefactoringFields(sourcekitd_object_t &Req, TestOptions Opts,
509
509
sourcekitd_request_dictionary_set_int64 (Req, KeyLength, Opts.Length );
510
510
}
511
511
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
+
512
538
static int handleTestInvocation (TestOptions Opts, TestOptions &InitOpts) {
513
539
if (!Opts.JsonRequestPath .empty ())
514
540
return handleJsonRequestPath (Opts.JsonRequestPath , Opts);
@@ -1140,8 +1166,19 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
1140
1166
sourcekitd_request_release (files);
1141
1167
}
1142
1168
1169
+ int64_t BeforeInstructions;
1170
+ if (Opts.measureInstructions )
1171
+ BeforeInstructions = getSourceKitInstructionCount ();
1172
+
1143
1173
if (!Opts.isAsyncRequest ) {
1144
1174
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
+
1145
1182
sourcekitd_request_release (Req);
1146
1183
return handleResponse (Resp, Opts, SemaName, std::move (SourceBuf),
1147
1184
&InitOpts)
@@ -1170,6 +1207,12 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
1170
1207
" -async not supported when sourcekitd is built without blocks support" );
1171
1208
#endif
1172
1209
1210
+ if (Opts.measureInstructions ) {
1211
+ int64_t AfterInstructions = getSourceKitInstructionCount ();
1212
+ llvm::errs () << " request instructions: "
1213
+ << (AfterInstructions - BeforeInstructions);
1214
+ }
1215
+
1173
1216
sourcekitd_request_release (Req);
1174
1217
return 0 ;
1175
1218
}
0 commit comments