-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
virtualcallstub.cpp
3811 lines (3228 loc) · 147 KB
/
virtualcallstub.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// File: VirtualCallStub.CPP
//
// This file contains the virtual call stub manager and caches
//
//
//
// ============================================================================
#include "common.h"
#include "array.h"
#ifdef FEATURE_PERFMAP
#include "perfmap.h"
#endif
#ifndef DACCESS_COMPILE
//@TODO: make these conditional on whether logs are being produced
//instrumentation counters
UINT32 g_site_counter = 0; //# of call sites
UINT32 g_site_write = 0; //# of call site backpatch writes
UINT32 g_site_write_poly = 0; //# of call site backpatch writes to point to resolve stubs
UINT32 g_site_write_mono = 0; //# of call site backpatch writes to point to dispatch stubs
UINT32 g_stub_lookup_counter = 0; //# of lookup stubs
UINT32 g_stub_mono_counter = 0; //# of dispatch stubs
UINT32 g_stub_poly_counter = 0; //# of resolve stubs
UINT32 g_stub_vtable_counter = 0; //# of vtable call stubs
UINT32 g_stub_space = 0; //# of bytes of stubs
UINT32 g_reclaim_counter = 0; //# of times a ReclaimAll was performed
UINT32 g_worker_call = 0; //# of calls into ResolveWorker
UINT32 g_worker_call_no_patch = 0;
UINT32 g_worker_collide_to_mono = 0; //# of times we converted a poly stub to a mono stub instead of writing the cache entry
UINT32 g_external_call = 0; //# of calls into GetTarget(token, pMT)
UINT32 g_external_call_no_patch = 0;
UINT32 g_insert_cache_external = 0; //# of times Insert was called for IK_EXTERNAL
UINT32 g_insert_cache_shared = 0; //# of times Insert was called for IK_SHARED
UINT32 g_insert_cache_dispatch = 0; //# of times Insert was called for IK_DISPATCH
UINT32 g_insert_cache_resolve = 0; //# of times Insert was called for IK_RESOLVE
UINT32 g_insert_cache_hit = 0; //# of times Insert found an empty cache entry
UINT32 g_insert_cache_miss = 0; //# of times Insert already had a matching cache entry
UINT32 g_insert_cache_collide = 0; //# of times Insert found a used cache entry
UINT32 g_insert_cache_write = 0; //# of times Insert wrote a cache entry
UINT32 g_cache_entry_counter = 0; //# of cache structs
UINT32 g_cache_entry_space = 0; //# of bytes used by cache lookup structs
UINT32 g_call_lookup_counter = 0; //# of times lookup stubs entered
UINT32 g_mono_call_counter = 0; //# of time dispatch stubs entered
UINT32 g_mono_miss_counter = 0; //# of times expected MT did not match actual MT (dispatch stubs)
UINT32 g_poly_call_counter = 0; //# of times resolve stubs entered
UINT32 g_poly_miss_counter = 0; //# of times cache missed (resolve stub)
UINT32 g_chained_lookup_call_counter = 0; //# of hits in a chained lookup
UINT32 g_chained_lookup_miss_counter = 0; //# of misses in a chained lookup
UINT32 g_chained_lookup_external_call_counter = 0; //# of hits in an external chained lookup
UINT32 g_chained_lookup_external_miss_counter = 0; //# of misses in an external chained lookup
UINT32 g_chained_entry_promoted = 0; //# of times a cache entry is promoted to the start of the chain
UINT32 g_bucket_space = 0; //# of bytes in caches and tables, not including the stubs themselves
UINT32 g_bucket_space_dead = 0; //# of bytes of abandoned buckets not yet recycled
#endif // !DACCESS_COMPILE
// This is the number of times a successful chain lookup will occur before the
// entry is promoted to the front of the chain. This is declared as extern because
// the default value (CALL_STUB_CACHE_INITIAL_SUCCESS_COUNT) is defined in the header.
#ifdef TARGET_ARM64
extern "C" size_t g_dispatch_cache_chain_success_counter;
#else
extern size_t g_dispatch_cache_chain_success_counter;
#endif
#define DECLARE_DATA
#include "virtualcallstub.h"
#undef DECLARE_DATA
#include "profilepriv.h"
#include "contractimpl.h"
#include "dynamicinterfacecastable.h"
SPTR_IMPL_INIT(VirtualCallStubManagerManager, VirtualCallStubManagerManager, g_pManager, NULL);
#ifndef DACCESS_COMPILE
#ifdef STUB_LOGGING
UINT32 STUB_MISS_COUNT_VALUE = 100;
UINT32 STUB_COLLIDE_WRITE_PCT = 100;
UINT32 STUB_COLLIDE_MONO_PCT = 0;
#endif // STUB_LOGGING
FastTable::NumCallStubs_t FastTable::NumCallStubs;
FastTable* BucketTable::dead = NULL; //linked list of the abandoned buckets
DispatchCache *g_resolveCache = NULL; //cache of dispatch stubs for in line lookup by resolve stubs.
size_t g_dispatch_cache_chain_success_counter = CALL_STUB_CACHE_INITIAL_SUCCESS_COUNT;
#ifdef STUB_LOGGING
UINT32 g_resetCacheCounter;
UINT32 g_resetCacheIncr;
UINT32 g_dumpLogCounter;
UINT32 g_dumpLogIncr;
#endif // STUB_LOGGING
//@TODO: use the existing logging mechanisms. for now we write to a file.
HANDLE g_hStubLogFile;
void VirtualCallStubManager::StartupLogging()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
FORBID_FAULT;
}
CONTRACTL_END
GCX_PREEMP();
EX_TRY
{
FAULT_NOT_FATAL(); // We handle filecreation problems locally
SString str;
str.Printf("StubLog_%d.log", GetCurrentProcessId());
g_hStubLogFile = WszCreateFile (str.GetUnicode(),
GENERIC_WRITE,
0,
0,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0);
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions)
if (g_hStubLogFile == INVALID_HANDLE_VALUE) {
g_hStubLogFile = NULL;
}
}
#define OUTPUT_FORMAT_INT "\t%-30s %d\r\n"
#define OUTPUT_FORMAT_SIZE "\t%-30s %zu\r\n"
#define OUTPUT_FORMAT_PCT "\t%-30s %#5.2f%%\r\n"
#define OUTPUT_FORMAT_INT_PCT "\t%-30s %5d (%#5.2f%%)\r\n"
void VirtualCallStubManager::LoggingDump()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
FORBID_FAULT;
}
CONTRACTL_END
VirtualCallStubManagerIterator it =
VirtualCallStubManagerManager::GlobalManager()->IterateVirtualCallStubManagers();
while (it.Next())
{
it.Current()->LogStats();
}
g_resolveCache->LogStats();
// Temp space to use for formatting the output.
static const int FMT_STR_SIZE = 160;
char szPrintStr[FMT_STR_SIZE];
DWORD dwWriteByte;
if(g_hStubLogFile)
{
#ifdef STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub tuning parameters\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d (0x%02x)\r\n", "STUB_MISS_COUNT_VALUE",
STUB_MISS_COUNT_VALUE, STUB_MISS_COUNT_VALUE);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_WRITE_PCT",
STUB_COLLIDE_WRITE_PCT, STUB_COLLIDE_WRITE_PCT);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "STUB_COLLIDE_MONO_PCT",
STUB_COLLIDE_MONO_PCT, STUB_COLLIDE_MONO_PCT);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogCounter",
g_dumpLogCounter, g_dumpLogCounter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "DumpLogIncr",
g_dumpLogCounter, g_dumpLogIncr);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheCounter",
g_resetCacheCounter, g_resetCacheCounter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\t%-30s %3d%% (0x%02x)\r\n", "ResetCacheIncr",
g_resetCacheCounter, g_resetCacheIncr);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nsite data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
//output counters
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_counter", g_site_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write", g_site_write);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_mono", g_site_write_mono);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "site_write_poly", g_site_write_poly);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "reclaim_counter", g_reclaim_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_lookup_counter", g_stub_lookup_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_mono_counter", g_stub_mono_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_poly_counter", g_stub_poly_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_vtable_counter", g_stub_vtable_counter);
WriteFile(g_hStubLogFile, szPrintStr, (DWORD)strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "stub_space", g_stub_space);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#ifdef STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nlookup stub data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
UINT32 total_calls = g_mono_call_counter + g_poly_call_counter;
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "lookup_call_counter", g_call_lookup_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "total stub dispatch calls", total_calls);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "mono stub data",
100.0 * double(g_mono_call_counter)/double(total_calls));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "mono_call_counter", g_mono_call_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "mono_miss_counter", g_mono_miss_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent",
100.0 * double(g_mono_miss_counter)/double(g_mono_call_counter));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "poly stub data",
100.0 * double(g_poly_call_counter)/double(total_calls));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "poly_call_counter", g_poly_call_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "poly_miss_counter", g_poly_miss_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent",
100.0 * double(g_poly_miss_counter)/double(g_poly_call_counter));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // STUB_LOGGING
#ifdef CHAIN_LOOKUP
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nchain lookup data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#ifdef STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_call_counter", g_chained_lookup_call_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_miss_counter", g_chained_lookup_miss_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent",
100.0 * double(g_chained_lookup_miss_counter)/double(g_chained_lookup_call_counter));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_external_call_counter", g_chained_lookup_external_call_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_lookup_external_miss_counter", g_chained_lookup_external_miss_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_PCT, "miss percent",
100.0 * double(g_chained_lookup_external_miss_counter)/double(g_chained_lookup_external_call_counter));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "chained_entry_promoted", g_chained_entry_promoted);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // CHAIN_LOOKUP
#ifdef STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %#5.2f%%\r\n", "worker (slow resolver) data",
100.0 * double(g_worker_call)/double(total_calls));
#else // !STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nworker (slow resolver) data\r\n");
#endif // !STUB_LOGGING
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_call", g_worker_call);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_call_no_patch", g_worker_call_no_patch);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "external_call", g_external_call);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "external_call_no_patch", g_external_call_no_patch);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "worker_collide_to_mono", g_worker_collide_to_mono);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
UINT32 total_inserts = g_insert_cache_external
+ g_insert_cache_shared
+ g_insert_cache_dispatch
+ g_insert_cache_resolve;
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n%-30s %d\r\n", "insert cache data", total_inserts);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_external", g_insert_cache_external,
100.0 * double(g_insert_cache_external)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_shared", g_insert_cache_shared,
100.0 * double(g_insert_cache_shared)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_dispatch", g_insert_cache_dispatch,
100.0 * double(g_insert_cache_dispatch)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_resolve", g_insert_cache_resolve,
100.0 * double(g_insert_cache_resolve)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_hit", g_insert_cache_hit,
100.0 * double(g_insert_cache_hit)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_miss", g_insert_cache_miss,
100.0 * double(g_insert_cache_miss)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_collide", g_insert_cache_collide,
100.0 * double(g_insert_cache_collide)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT_PCT, "insert_cache_write", g_insert_cache_write,
100.0 * double(g_insert_cache_write)/double(total_inserts));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
size_t total, used;
g_resolveCache->GetLoadFactor(&total, &used);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_SIZE, "cache_entry_used", used);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_counter", g_cache_entry_counter);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "cache_entry_space", g_cache_entry_space);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\nstub hash table data\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "bucket_space", g_bucket_space);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), OUTPUT_FORMAT_INT, "bucket_space_dead", g_bucket_space_dead);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache_load:\t%zu used, %zu total, utilization %#5.2f%%\r\n",
used, total, 100.0 * double(used) / double(total));
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#ifdef STUB_LOGGING
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\ncache entry write counts\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
DispatchCache::CacheEntryData *rgCacheData = g_resolveCache->cacheData;
for (UINT16 i = 0; i < CALL_STUB_CACHE_SIZE; i++)
{
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), " %4d", rgCacheData[i]);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
if (i % 16 == 15)
{
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
}
}
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "\r\n");
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // STUB_LOGGING
#if 0
for (unsigned i = 0; i < ContractImplMap::max_delta_count; i++)
{
if (ContractImplMap::deltasDescs[i] != 0)
{
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "deltasDescs[%d]\t%d\r\n", i, ContractImplMap::deltasDescs[i]);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
}
}
for (unsigned i = 0; i < ContractImplMap::max_delta_count; i++)
{
if (ContractImplMap::deltasSlots[i] != 0)
{
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "deltasSlots[%d]\t%d\r\n", i, ContractImplMap::deltasSlots[i]);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
}
}
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "cout of maps:\t%d\r\n", ContractImplMap::countMaps);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "count of interfaces:\t%d\r\n", ContractImplMap::countInterfaces);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "count of deltas:\t%d\r\n", ContractImplMap::countDelta);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "total delta for descs:\t%d\r\n", ContractImplMap::totalDeltaDescs);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
sprintf_s(szPrintStr, ARRAY_SIZE(szPrintStr), "total delta for slots:\t%d\r\n", ContractImplMap::totalDeltaSlots);
WriteFile (g_hStubLogFile, szPrintStr, (DWORD) strlen(szPrintStr), &dwWriteByte, NULL);
#endif // 0
}
}
void VirtualCallStubManager::FinishLogging()
{
LoggingDump();
if(g_hStubLogFile)
{
CloseHandle(g_hStubLogFile);
}
g_hStubLogFile = NULL;
}
void VirtualCallStubManager::ResetCache()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
FORBID_FAULT;
}
CONTRACTL_END
g_resolveCache->LogStats();
g_insert_cache_external = 0;
g_insert_cache_shared = 0;
g_insert_cache_dispatch = 0;
g_insert_cache_resolve = 0;
g_insert_cache_hit = 0;
g_insert_cache_miss = 0;
g_insert_cache_collide = 0;
g_insert_cache_write = 0;
// Go through each cache entry and if the cache element there is in
// the cache entry heap of the manager being deleted, then we just
// set the cache entry to empty.
DispatchCache::Iterator it(g_resolveCache);
while (it.IsValid())
{
it.UnlinkEntry();
}
}
void VirtualCallStubManager::Init(LoaderAllocator *pLoaderAllocator)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
INJECT_FAULT(COMPlusThrowOM(););
} CONTRACTL_END;
m_loaderAllocator = pLoaderAllocator;
//
// Init critical sections
//
m_indCellLock.Init(CrstVSDIndirectionCellLock, CRST_UNSAFE_ANYMODE);
//
// Now allocate all BucketTables
//
NewHolder<BucketTable> resolvers_holder(new BucketTable(CALL_STUB_MIN_BUCKETS));
NewHolder<BucketTable> dispatchers_holder(new BucketTable(CALL_STUB_MIN_BUCKETS*2));
NewHolder<BucketTable> lookups_holder(new BucketTable(CALL_STUB_MIN_BUCKETS));
NewHolder<BucketTable> vtableCallers_holder(new BucketTable(CALL_STUB_MIN_BUCKETS));
NewHolder<BucketTable> cache_entries_holder(new BucketTable(CALL_STUB_MIN_BUCKETS));
//
// Now allocate our LoaderHeaps
//
//
// First do some calculation to determine how many pages that we
// will need to commit and reserve for each out our loader heaps
//
DWORD indcell_heap_reserve_size;
DWORD indcell_heap_commit_size;
DWORD cache_entry_heap_reserve_size;
DWORD cache_entry_heap_commit_size;
//
// Setup an expected number of items to commit and reserve
//
// The commit number is not that important as we always commit at least one page worth of items
// The reserve number should be high enough to cover a typical lare application,
// in order to minimize the fragmentation of our rangelists
//
indcell_heap_commit_size = 16; indcell_heap_reserve_size = 2000;
cache_entry_heap_commit_size = 16; cache_entry_heap_reserve_size = 800;
//
// Convert the number of items into a size in bytes to commit and reserve
//
indcell_heap_reserve_size *= sizeof(void *);
indcell_heap_commit_size *= sizeof(void *);
cache_entry_heap_reserve_size *= sizeof(ResolveCacheElem);
cache_entry_heap_commit_size *= sizeof(ResolveCacheElem);
//
// Align up all of the commit and reserve sizes
//
indcell_heap_reserve_size = (DWORD) ALIGN_UP(indcell_heap_reserve_size, GetOsPageSize());
indcell_heap_commit_size = (DWORD) ALIGN_UP(indcell_heap_commit_size, GetOsPageSize());
cache_entry_heap_reserve_size = (DWORD) ALIGN_UP(cache_entry_heap_reserve_size, GetOsPageSize());
cache_entry_heap_commit_size = (DWORD) ALIGN_UP(cache_entry_heap_commit_size, GetOsPageSize());
BYTE * initReservedMem = NULL;
if (!m_loaderAllocator->IsCollectible())
{
DWORD dwTotalReserveMemSizeCalc = indcell_heap_reserve_size +
cache_entry_heap_reserve_size;
DWORD dwTotalReserveMemSize = (DWORD) ALIGN_UP(dwTotalReserveMemSizeCalc, VIRTUAL_ALLOC_RESERVE_GRANULARITY);
// If there's wasted reserved memory, we hand this out to the heaps to avoid waste.
{
DWORD dwWastedReserveMemSize = dwTotalReserveMemSize - dwTotalReserveMemSizeCalc;
if (dwWastedReserveMemSize != 0)
{
DWORD cWastedPages = dwWastedReserveMemSize / GetOsPageSize();
// Split the wasted pages over the 2 LoaderHeaps that we allocate as part of a VirtualCallStubManager
DWORD cPagesPerHeap = cWastedPages / 2;
DWORD cPagesRemainder = cWastedPages % 2; // We'll throw this at the cache entry heap
indcell_heap_reserve_size += cPagesPerHeap * GetOsPageSize();
cache_entry_heap_reserve_size += (cPagesPerHeap + cPagesRemainder) * GetOsPageSize();
}
CONSISTENCY_CHECK((indcell_heap_reserve_size +
cache_entry_heap_reserve_size)==
dwTotalReserveMemSize);
}
initReservedMem = (BYTE*)ExecutableAllocator::Instance()->Reserve(dwTotalReserveMemSize);
m_initialReservedMemForHeaps = (BYTE *) initReservedMem;
if (initReservedMem == NULL)
COMPlusThrowOM();
}
else
{
indcell_heap_reserve_size = GetOsPageSize();
indcell_heap_commit_size = GetOsPageSize();
cache_entry_heap_reserve_size = GetOsPageSize();
cache_entry_heap_commit_size = GetOsPageSize();
#ifdef _DEBUG
DWORD dwTotalReserveMemSizeCalc = indcell_heap_reserve_size +
cache_entry_heap_reserve_size;
#endif
DWORD dwActualVSDSize = 0;
initReservedMem = pLoaderAllocator->GetVSDHeapInitialBlock(&dwActualVSDSize);
_ASSERTE(dwActualVSDSize == dwTotalReserveMemSizeCalc);
m_initialReservedMemForHeaps = (BYTE *) initReservedMem;
if (initReservedMem == NULL)
COMPlusThrowOM();
}
// Hot memory, Writable, No-Execute, infrequent writes
NewHolder<LoaderHeap> indcell_heap_holder(
new LoaderHeap(indcell_heap_reserve_size, indcell_heap_commit_size,
initReservedMem, indcell_heap_reserve_size,
NULL, UnlockedLoaderHeap::HeapKind::Data));
initReservedMem += indcell_heap_reserve_size;
// Hot memory, Writable, No-Execute, infrequent writes
NewHolder<LoaderHeap> cache_entry_heap_holder(
new LoaderHeap(cache_entry_heap_reserve_size, cache_entry_heap_commit_size,
initReservedMem, cache_entry_heap_reserve_size,
&cache_entry_rangeList, UnlockedLoaderHeap::HeapKind::Data));
initReservedMem += cache_entry_heap_reserve_size;
// Warm memory, Writable, Execute, write exactly once
NewHolder<CodeFragmentHeap> lookup_heap_holder(
new CodeFragmentHeap(pLoaderAllocator, STUB_CODE_BLOCK_VSD_LOOKUP_STUB));
// Hot memory, Writable, Execute, write exactly once
NewHolder<CodeFragmentHeap> dispatch_heap_holder(
new CodeFragmentHeap(pLoaderAllocator, STUB_CODE_BLOCK_VSD_DISPATCH_STUB));
// Hot memory, Writable, Execute, write exactly once
NewHolder<CodeFragmentHeap> resolve_heap_holder(
new CodeFragmentHeap(pLoaderAllocator, STUB_CODE_BLOCK_VSD_RESOLVE_STUB));
// Hot memory, Writable, Execute, write exactly once
NewHolder<CodeFragmentHeap> vtable_heap_holder(
new CodeFragmentHeap(pLoaderAllocator, STUB_CODE_BLOCK_VSD_VTABLE_STUB));
// Allocate the initial counter block
NewHolder<counter_block> m_counters_holder(new counter_block);
//
// On success of every allocation, assign the objects and suppress the release
//
indcell_heap = indcell_heap_holder; indcell_heap_holder.SuppressRelease();
lookup_heap = lookup_heap_holder; lookup_heap_holder.SuppressRelease();
dispatch_heap = dispatch_heap_holder; dispatch_heap_holder.SuppressRelease();
resolve_heap = resolve_heap_holder; resolve_heap_holder.SuppressRelease();
vtable_heap = vtable_heap_holder; vtable_heap_holder.SuppressRelease();
cache_entry_heap = cache_entry_heap_holder; cache_entry_heap_holder.SuppressRelease();
resolvers = resolvers_holder; resolvers_holder.SuppressRelease();
dispatchers = dispatchers_holder; dispatchers_holder.SuppressRelease();
lookups = lookups_holder; lookups_holder.SuppressRelease();
vtableCallers = vtableCallers_holder; vtableCallers_holder.SuppressRelease();
cache_entries = cache_entries_holder; cache_entries_holder.SuppressRelease();
m_counters = m_counters_holder; m_counters_holder.SuppressRelease();
// Create the initial failure counter block
m_counters->next = NULL;
m_counters->used = 0;
m_cur_counter_block = m_counters;
m_cur_counter_block_for_reclaim = m_counters;
m_cur_counter_block_for_reclaim_index = 0;
// Keep track of all of our managers
VirtualCallStubManagerManager::GlobalManager()->AddStubManager(this);
}
void VirtualCallStubManager::Uninit()
{
WRAPPER_NO_CONTRACT;
// Keep track of all our managers
VirtualCallStubManagerManager::GlobalManager()->RemoveStubManager(this);
}
VirtualCallStubManager::~VirtualCallStubManager()
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
} CONTRACTL_END;
LogStats();
// Go through each cache entry and if the cache element there is in
// the cache entry heap of the manager being deleted, then we just
// set the cache entry to empty.
DispatchCache::Iterator it(g_resolveCache);
while (it.IsValid())
{
// Using UnlinkEntry performs an implicit call to Next (see comment for UnlinkEntry).
// Thus, we need to avoid calling Next when we delete an entry so
// that we don't accidentally skip entries.
while (it.IsValid() && cache_entry_rangeList.IsInRange((TADDR)it.Entry()))
{
it.UnlinkEntry();
}
it.Next();
}
if (indcell_heap) { delete indcell_heap; indcell_heap = NULL;}
if (lookup_heap) { delete lookup_heap; lookup_heap = NULL;}
if (dispatch_heap) { delete dispatch_heap; dispatch_heap = NULL;}
if (resolve_heap) { delete resolve_heap; resolve_heap = NULL;}
if (vtable_heap) { delete vtable_heap; vtable_heap = NULL;}
if (cache_entry_heap) { delete cache_entry_heap; cache_entry_heap = NULL;}
if (resolvers) { delete resolvers; resolvers = NULL;}
if (dispatchers) { delete dispatchers; dispatchers = NULL;}
if (lookups) { delete lookups; lookups = NULL;}
if (vtableCallers) { delete vtableCallers; vtableCallers = NULL;}
if (cache_entries) { delete cache_entries; cache_entries = NULL;}
// Now get rid of the memory taken by the counter_blocks
while (m_counters != NULL)
{
counter_block *del = m_counters;
m_counters = m_counters->next;
delete del;
}
// This was the block reserved by Init for the heaps.
// For the collectible case, the VSD logic does not allocate the memory.
if (m_initialReservedMemForHeaps && !m_loaderAllocator->IsCollectible())
ClrVirtualFree (m_initialReservedMemForHeaps, 0, MEM_RELEASE);
// Free critical section
m_indCellLock.Destroy();
}
// Initialize static structures, and start up logging if necessary
void VirtualCallStubManager::InitStatic()
{
STANDARD_VM_CONTRACT;
#ifdef STUB_LOGGING
// Note if you change these values using environment variables then you must use hex values :-(
STUB_MISS_COUNT_VALUE = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubMissCount);
STUB_COLLIDE_WRITE_PCT = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubCollideWritePct);
STUB_COLLIDE_MONO_PCT = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubCollideMonoPct);
g_dumpLogCounter = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubDumpLogCounter);
g_dumpLogIncr = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubDumpLogIncr);
g_resetCacheCounter = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubResetCacheCounter);
g_resetCacheIncr = (INT32) CLRConfig::GetConfigValue(CLRConfig::INTERNAL_VirtualCallStubResetCacheIncr);
#endif // STUB_LOGGING
#ifndef STUB_DISPATCH_PORTABLE
DispatchHolder::InitializeStatic();
ResolveHolder::InitializeStatic();
#endif // !STUB_DISPATCH_PORTABLE
LookupHolder::InitializeStatic();
g_resolveCache = new DispatchCache();
if(CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_VirtualCallStubLogging))
StartupLogging();
VirtualCallStubManagerManager::InitStatic();
}
void VirtualCallStubManager::LogFinalStats()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
FORBID_FAULT;
}
CONTRACTL_END
if (g_hStubLogFile != NULL)
{
VirtualCallStubManagerIterator it =
VirtualCallStubManagerManager::GlobalManager()->IterateVirtualCallStubManagers();
while (it.Next())
{
it.Current()->LogStats();
}
g_resolveCache->LogStats();
FinishLogging();
}
}
/* reclaim/rearrange any structures that can only be done during a gc sync point
i.e. need to be serialized and non-concurrant. */
void VirtualCallStubManager::ReclaimAll()
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
/* @todo: if/when app domain unloading is supported,
and when we have app domain specific stub heaps, we can complete the unloading
of an app domain stub heap at this point, and make any patches to existing stubs that are
not being unload so that they nolonger refer to any of the unloaded app domains code or types
*/
//reclaim space of abandoned buckets
BucketTable::Reclaim();
VirtualCallStubManagerIterator it =
VirtualCallStubManagerManager::GlobalManager()->IterateVirtualCallStubManagers();
while (it.Next())
{
it.Current()->Reclaim();
}
g_reclaim_counter++;
}
const UINT32 VirtualCallStubManager::counter_block::MAX_COUNTER_ENTRIES;
/* reclaim/rearrange any structures that can only be done during a gc sync point
i.e. need to be serialized and non-concurrant. */
void VirtualCallStubManager::Reclaim()
{
LIMITED_METHOD_CONTRACT;
UINT32 limit = min(counter_block::MAX_COUNTER_ENTRIES,
m_cur_counter_block_for_reclaim->used);
limit = min(m_cur_counter_block_for_reclaim_index + 16, limit);
for (UINT32 i = m_cur_counter_block_for_reclaim_index; i < limit; i++)
{
m_cur_counter_block_for_reclaim->block[i] += (STUB_MISS_COUNT_VALUE/10)+1;
}
// Increment the index by the number we processed
m_cur_counter_block_for_reclaim_index = limit;
// If we ran to the end of the block, go to the next
if (m_cur_counter_block_for_reclaim_index == m_cur_counter_block->used)
{
m_cur_counter_block_for_reclaim = m_cur_counter_block_for_reclaim->next;
m_cur_counter_block_for_reclaim_index = 0;
// If this was the last block in the chain, go back to the beginning
if (m_cur_counter_block_for_reclaim == NULL)
m_cur_counter_block_for_reclaim = m_counters;
}
}
#endif // !DACCESS_COMPILE
//----------------------------------------------------------------------------
/* static */
VirtualCallStubManager *VirtualCallStubManager::FindStubManager(PCODE stubAddress, StubCodeBlockKind* wbStubKind)
{
CONTRACTL {
NOTHROW;
GC_NOTRIGGER;
FORBID_FAULT;
SUPPORTS_DAC;
} CONTRACTL_END
StubCodeBlockKind unusedStubKind;
if (wbStubKind == NULL)
{
wbStubKind = &unusedStubKind;
}
*wbStubKind = STUB_CODE_BLOCK_UNKNOWN;
RangeSection * pRS = ExecutionManager::FindCodeRange(stubAddress, ExecutionManager::ScanReaderLock);
if (pRS == NULL)
return NULL;
StubCodeBlockKind kind = pRS->_pjit->GetStubCodeBlockKind(pRS, stubAddress);
switch (kind)
{
case STUB_CODE_BLOCK_VSD_DISPATCH_STUB:
case STUB_CODE_BLOCK_VSD_RESOLVE_STUB:
case STUB_CODE_BLOCK_VSD_LOOKUP_STUB:
case STUB_CODE_BLOCK_VSD_VTABLE_STUB:
// This is a VSD stub, using the RangeSection identify which LoaderAllocator this is from
_ASSERTE(pRS->_flags & RangeSection::RANGE_SECTION_CODEHEAP);
*wbStubKind = kind;
return pRS->_pHeapList->pLoaderAllocator->GetVirtualCallStubManager();
default:
return NULL;
}
}
/* for use by debugger.
*/
BOOL VirtualCallStubManager::CheckIsStub_Internal(PCODE stubStartAddress)
{
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FORBID_FAULT;
SUPPORTS_DAC;
// Forwarded to from RangeSectionStubManager
return FALSE;
}
/* for use by debugger.
*/
extern "C" void STDCALL StubDispatchFixupPatchLabel();
BOOL VirtualCallStubManager::DoTraceStub(PCODE stubStartAddress, TraceDestination *trace)
{
LIMITED_METHOD_CONTRACT;
LOG((LF_CORDB, LL_EVERYTHING, "VirtualCallStubManager::DoTraceStub called\n"));
// @workaround: Well, we really need the context to figure out where we're going, so
// we'll do a TRACE_MGR_PUSH so that TraceManager gets called and we can use
// the provided context to figure out where we're going.
trace->InitForManagerPush(stubStartAddress, this);
return TRUE;
}
//----------------------------------------------------------------------------
BOOL VirtualCallStubManager::TraceManager(Thread *thread,
TraceDestination *trace,
T_CONTEXT *pContext,
BYTE **pRetAddr)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
INJECT_FAULT(COMPlusThrowOM(););
}
CONTRACTL_END
TADDR pStub = GetIP(pContext);
// The return address should be on the top of the stack
*pRetAddr = (BYTE *)StubManagerHelpers::GetReturnAddress(pContext);
// Get the token from the stub
DispatchToken token(GetTokenFromStub(pStub));
// Get the this object from ECX
Object *pObj = StubManagerHelpers::GetThisPtr(pContext);
// Call common trace code.
return (TraceResolver(pObj, token, trace));
}
#ifndef DACCESS_COMPILE
PCODE VirtualCallStubManager::GetCallStub(TypeHandle ownerType, MethodDesc *pMD)
{
CONTRACTL {
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(CheckPointer(pMD));
PRECONDITION(!pMD->IsInterface() || ownerType.GetMethodTable()->HasSameTypeDefAs(pMD->GetMethodTable()));
INJECT_FAULT(COMPlusThrowOM(););
} CONTRACTL_END;
return GetCallStub(ownerType, pMD->GetSlot());
}
//find or create a stub
PCODE VirtualCallStubManager::GetCallStub(TypeHandle ownerType, DWORD slot)
{
CONTRACT (PCODE) {
THROWS;
GC_TRIGGERS;
MODE_ANY;
INJECT_FAULT(COMPlusThrowOM(););
POSTCONDITION(RETVAL != NULL);
} CONTRACT_END;
GCX_COOP(); // This is necessary for BucketTable synchronization
MethodTable * pMT = ownerType.GetMethodTable();
pMT->GetRestoredSlot(slot);
DispatchToken token;
if (pMT->IsInterface())
token = pMT->GetLoaderAllocator()->GetDispatchToken(pMT->GetTypeID(), slot);
else
token = DispatchToken::CreateDispatchToken(slot);
//get a stub from lookups, make if necessary
PCODE stub = CALL_STUB_EMPTY_ENTRY;
PCODE addrOfResolver = GetEEFuncEntryPoint(ResolveWorkerAsmStub);
LookupEntry entryL;
Prober probeL(&entryL);
if (lookups->SetUpProber(token.To_SIZE_T(), 0, &probeL))
{