-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsim.c
1739 lines (1466 loc) · 60.1 KB
/
sim.c
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
/*--------------------------------------------------------------------*/
/*--- Cache simulation. ---*/
/*--- sim.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Callgrind, a Valgrind tool for call graph
profiling programs.
Copyright (C) 2003-2017, Josef Weidendorfer ([email protected])
This tool is derived from and contains code from Cachegrind
Copyright (C) 2002-2017 Nicholas Nethercote ([email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
The GNU General Public License is contained in the file COPYING.
*/
#include "global.h"
/* Notes:
- simulates a write-allocate cache
- (block --> set) hash function uses simple bit selection
- handling of references straddling two cache blocks:
- counts as only one cache access (not two)
- both blocks hit --> one hit
- one block hits, the other misses --> one miss
- both blocks miss --> one miss (not two)
*/
/* Cache configuration */
#include "cg_arch.c"
/* additional structures for cache use info, separated
* according usage frequency:
* - line_loaded : pointer to cost center of instruction
* which loaded the line into cache.
* Needed to increment counters when line is evicted.
* - line_use : updated on every access
*/
typedef struct {
UInt count;
UInt mask; /* e.g. for 64Byte line size 1bit/2Byte */
} line_use;
typedef struct {
Addr memline, iaddr;
line_use* dep_use; /* point to higher-level cacheblock for this memline */
ULong* use_base;
} line_loaded;
/* Cache state */
typedef struct {
const HChar* name;
int size; /* bytes */
int assoc;
int line_size; /* bytes */
Bool sectored; /* prefetch nearside cacheline on read */
int sets;
int sets_min_1;
int line_size_bits;
int tag_shift;
UWord tag_mask;
HChar desc_line[128]; // large enough
UWord* tags;
/* for cache use */
int line_size_mask;
int* line_start_mask;
int* line_end_mask;
line_loaded* loaded;
line_use* use;
} cache_t2;
/*
* States of flat caches in our model.
* We use a 2-level hierarchy,
*/
static cache_t2 I1, D1, LL;
/* Lower bits of cache tags are used as flags for a cache line */
#define CACHELINE_FLAGMASK (MIN_LINE_SIZE-1)
#define CACHELINE_DIRTY 1
/* Cache simulator Options */
static Bool clo_simulate_writeback = False;
static Bool clo_simulate_hwpref = False;
static Bool clo_simulate_sectors = False;
static Bool clo_collect_cacheuse = False;
/* Following global vars are setup before by setup_bbcc():
*
* - Addr CLG_(bb_base) (instruction start address of original BB)
* - ULong* CLG_(cost_base) (start of cost array for BB)
*/
Addr CLG_(bb_base);
ULong* CLG_(cost_base);
static InstrInfo* current_ii;
/* Cache use offsets */
/* The offsets are only correct because all per-instruction event sets get
* the "Use" set added first !
*/
static Int off_I1_AcCost = 0;
static Int off_I1_SpLoss = 1;
static Int off_D1_AcCost = 0;
static Int off_D1_SpLoss = 1;
static Int off_LL_AcCost = 2;
static Int off_LL_SpLoss = 3;
/* Cache access types */
typedef enum { Read = 0, Write = CACHELINE_DIRTY } RefType;
/* Result of a reference into a flat cache */
typedef enum { Hit = 0, Miss, MissDirty } CacheResult;
/* Result of a reference into a hierarchical cache model */
typedef enum {
L1_Hit,
LL_Hit,
MemAccess,
WriteBackMemAccess } CacheModelResult;
typedef CacheModelResult (*simcall_type)(Addr, UChar);
static struct {
simcall_type I1_Read;
simcall_type D1_Read;
simcall_type D1_Write;
} simulator;
/*------------------------------------------------------------*/
/*--- Cache Simulator Initialization ---*/
/*------------------------------------------------------------*/
static void cachesim_clearcache(cache_t2* c)
{
Int i;
for (i = 0; i < c->sets * c->assoc; i++)
c->tags[i] = 0;
if (c->use) {
for (i = 0; i < c->sets * c->assoc; i++) {
c->loaded[i].memline = 0;
c->loaded[i].use_base = 0;
c->loaded[i].dep_use = 0;
c->loaded[i].iaddr = 0;
c->use[i].mask = 0;
c->use[i].count = 0;
c->tags[i] = i % c->assoc; /* init lower bits as pointer */
}
}
}
static void cacheuse_initcache(cache_t2* c);
/* By this point, the size/assoc/line_size has been checked. */
static void cachesim_initcache(cache_t config, cache_t2* c)
{
c->size = config.size;
c->assoc = config.assoc;
c->line_size = config.line_size;
c->sectored = False; // FIXME
c->sets = (c->size / c->line_size) / c->assoc;
c->sets_min_1 = c->sets - 1;
c->line_size_bits = VG_(log2)(c->line_size);
c->tag_shift = c->line_size_bits + VG_(log2)(c->sets);
c->tag_mask = ~((1u<<c->tag_shift)-1);
/* Can bits in tag entries be used for flags?
* Should be always true as MIN_LINE_SIZE >= 16 */
CLG_ASSERT( (c->tag_mask & CACHELINE_FLAGMASK) == 0);
if (c->assoc == 1) {
VG_(sprintf)(c->desc_line, "%d B, %d B, direct-mapped%s",
c->size, c->line_size,
c->sectored ? ", sectored":"");
} else {
VG_(sprintf)(c->desc_line, "%d B, %d B, %d-way associative%s",
c->size, c->line_size, c->assoc,
c->sectored ? ", sectored":"");
}
c->tags = (UWord*) CLG_MALLOC("cl.sim.cs_ic.1",
sizeof(UWord) * c->sets * c->assoc);
if (clo_collect_cacheuse)
cacheuse_initcache(c);
else
c->use = 0;
cachesim_clearcache(c);
}
#if 0
static void print_cache(cache_t2* c)
{
UInt set, way, i;
/* Note initialisation and update of 'i'. */
for (i = 0, set = 0; set < c->sets; set++) {
for (way = 0; way < c->assoc; way++, i++) {
VG_(printf)("%8x ", c->tags[i]);
}
VG_(printf)("\n");
}
}
#endif
/*------------------------------------------------------------*/
/*--- Simple Cache Simulation ---*/
/*------------------------------------------------------------*/
/*
* Model: single inclusive, 2-level cache hierarchy (L1/LL)
* with write-allocate
*
* For simple cache hit/miss counts, we do not have to
* maintain the dirty state of lines (no need to distinguish
* read/write references), and the resulting counts are the
* same for write-through and write-back caches.
*
* Simulator functions:
* CacheModelResult cachesim_I1_ref(Addr a, UChar size)
* CacheModelResult cachesim_D1_ref(Addr a, UChar size)
*/
__attribute__((always_inline))
static __inline__
CacheResult cachesim_setref(cache_t2* c, UInt set_no, UWord tag)
{
int i, j;
UWord *set;
set = &(c->tags[set_no * c->assoc]);
/* This loop is unrolled for just the first case, which is the most */
/* common. We can't unroll any further because it would screw up */
/* if we have a direct-mapped (1-way) cache. */
if (tag == set[0])
return Hit;
/* If the tag is one other than the MRU, move it into the MRU spot */
/* and shuffle the rest down. */
for (i = 1; i < c->assoc; i++) {
if (tag == set[i]) {
for (j = i; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tag;
return Hit;
}
}
/* A miss; install this tag as MRU, shuffle rest down. */
for (j = c->assoc - 1; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tag;
return Miss;
}
__attribute__((always_inline))
static __inline__
CacheResult cachesim_ref(cache_t2* c, Addr a, UChar size)
{
UWord block1 = a >> c->line_size_bits;
UWord block2 = (a+size-1) >> c->line_size_bits;
UInt set1 = block1 & c->sets_min_1;
/* the tag does not need to include bits specifying the set,
* but it can, and this saves instructions */
UWord tag1 = block1;
/* Access entirely within line. */
if (block1 == block2)
return cachesim_setref(c, set1, tag1);
/* Access straddles two lines. */
else if (block1 + 1 == block2) {
UInt set2 = block2 & c->sets_min_1;
UWord tag2 = block2;
/* the call updates cache structures as side effect */
CacheResult res1 = cachesim_setref(c, set1, tag1);
CacheResult res2 = cachesim_setref(c, set2, tag2);
return ((res1 == Miss) || (res2 == Miss)) ? Miss : Hit;
} else {
VG_(printf)("addr: %lx size: %u blocks: %lu %lu",
a, size, block1, block2);
VG_(tool_panic)("item straddles more than two cache sets");
}
return Hit;
}
static
CacheModelResult cachesim_I1_ref(Addr a, UChar size)
{
if ( cachesim_ref( &I1, a, size) == Hit ) return L1_Hit;
if ( cachesim_ref( &LL, a, size) == Hit ) return LL_Hit;
return MemAccess;
}
static
CacheModelResult cachesim_D1_ref(Addr a, UChar size)
{
if ( cachesim_ref( &D1, a, size) == Hit ) return L1_Hit;
if ( cachesim_ref( &LL, a, size) == Hit ) return LL_Hit;
return MemAccess;
}
/*------------------------------------------------------------*/
/*--- Write Back Cache Simulation ---*/
/*------------------------------------------------------------*/
/*
* More complex model: L1 Write-through, LL Write-back
* This needs to distinguish among read and write references.
*
* Simulator functions:
* CacheModelResult cachesim_I1_Read(Addr a, UChar size)
* CacheModelResult cachesim_D1_Read(Addr a, UChar size)
* CacheModelResult cachesim_D1_Write(Addr a, UChar size)
*/
/*
* With write-back, result can be a miss evicting a dirty line
* The dirty state of a cache line is stored in Bit0 of the tag for
* this cache line (CACHELINE_DIRTY = 1). By OR'ing the reference
* type (Read/Write), the line gets dirty on a write.
*/
__attribute__((always_inline))
static __inline__
CacheResult cachesim_setref_wb(cache_t2* c, RefType ref, UInt set_no, UWord tag)
{
int i, j;
UWord *set, tmp_tag;
set = &(c->tags[set_no * c->assoc]);
/* This loop is unrolled for just the first case, which is the most */
/* common. We can't unroll any further because it would screw up */
/* if we have a direct-mapped (1-way) cache. */
if (tag == (set[0] & ~CACHELINE_DIRTY)) {
set[0] |= ref;
return Hit;
}
/* If the tag is one other than the MRU, move it into the MRU spot */
/* and shuffle the rest down. */
for (i = 1; i < c->assoc; i++) {
if (tag == (set[i] & ~CACHELINE_DIRTY)) {
tmp_tag = set[i] | ref; // update dirty flag
for (j = i; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tmp_tag;
return Hit;
}
}
/* A miss; install this tag as MRU, shuffle rest down. */
tmp_tag = set[c->assoc - 1];
for (j = c->assoc - 1; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tag | ref;
return (tmp_tag & CACHELINE_DIRTY) ? MissDirty : Miss;
}
__attribute__((always_inline))
static __inline__
CacheResult cachesim_ref_wb(cache_t2* c, RefType ref, Addr a, UChar size)
{
UInt set1 = ( a >> c->line_size_bits) & (c->sets_min_1);
UInt set2 = ((a+size-1) >> c->line_size_bits) & (c->sets_min_1);
UWord tag = a & c->tag_mask;
/* Access entirely within line. */
if (set1 == set2)
return cachesim_setref_wb(c, ref, set1, tag);
/* Access straddles two lines. */
/* Nb: this is a fast way of doing ((set1+1) % c->sets) */
else if (((set1 + 1) & (c->sets_min_1)) == set2) {
UWord tag2 = (a+size-1) & c->tag_mask;
/* the call updates cache structures as side effect */
CacheResult res1 = cachesim_setref_wb(c, ref, set1, tag);
CacheResult res2 = cachesim_setref_wb(c, ref, set2, tag2);
if ((res1 == MissDirty) || (res2 == MissDirty)) return MissDirty;
return ((res1 == Miss) || (res2 == Miss)) ? Miss : Hit;
} else {
VG_(printf)("addr: %lx size: %u sets: %u %u", a, size, set1, set2);
VG_(tool_panic)("item straddles more than two cache sets");
}
return Hit;
}
static
CacheModelResult cachesim_I1_Read(Addr a, UChar size)
{
if ( cachesim_ref( &I1, a, size) == Hit ) return L1_Hit;
switch( cachesim_ref_wb( &LL, Read, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
static
CacheModelResult cachesim_D1_Read(Addr a, UChar size)
{
if ( cachesim_ref( &D1, a, size) == Hit ) return L1_Hit;
switch( cachesim_ref_wb( &LL, Read, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
static
CacheModelResult cachesim_D1_Write(Addr a, UChar size)
{
if ( cachesim_ref( &D1, a, size) == Hit ) {
/* Even for a L1 hit, the write-trough L1 passes
* the write to the LL to make the LL line dirty.
* But this causes no latency, so return the hit.
*/
cachesim_ref_wb( &LL, Write, a, size);
return L1_Hit;
}
switch( cachesim_ref_wb( &LL, Write, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
/*------------------------------------------------------------*/
/*--- Hardware Prefetch Simulation ---*/
/*------------------------------------------------------------*/
static ULong prefetch_up = 0;
static ULong prefetch_down = 0;
#define PF_STREAMS 8
#define PF_PAGEBITS 12
static UInt pf_lastblock[PF_STREAMS];
static Int pf_seqblocks[PF_STREAMS];
static
void prefetch_clear(void)
{
int i;
for(i=0;i<PF_STREAMS;i++)
pf_lastblock[i] = pf_seqblocks[i] = 0;
}
/*
* HW Prefetch emulation
* Start prefetching when detecting sequential access to 3 memory blocks.
* One stream can be detected per 4k page.
*/
static __inline__
void prefetch_LL_doref(Addr a)
{
UInt stream = (a >> PF_PAGEBITS) % PF_STREAMS;
UInt block = ( a >> LL.line_size_bits);
if (block != pf_lastblock[stream]) {
if (pf_seqblocks[stream] == 0) {
if (pf_lastblock[stream] +1 == block) pf_seqblocks[stream]++;
else if (pf_lastblock[stream] -1 == block) pf_seqblocks[stream]--;
}
else if (pf_seqblocks[stream] >0) {
if (pf_lastblock[stream] +1 == block) {
pf_seqblocks[stream]++;
if (pf_seqblocks[stream] >= 2) {
prefetch_up++;
cachesim_ref(&LL, a + 5 * LL.line_size,1);
}
}
else pf_seqblocks[stream] = 0;
}
else if (pf_seqblocks[stream] <0) {
if (pf_lastblock[stream] -1 == block) {
pf_seqblocks[stream]--;
if (pf_seqblocks[stream] <= -2) {
prefetch_down++;
cachesim_ref(&LL, a - 5 * LL.line_size,1);
}
}
else pf_seqblocks[stream] = 0;
}
pf_lastblock[stream] = block;
}
}
/* simple model with hardware prefetch */
static
CacheModelResult prefetch_I1_ref(Addr a, UChar size)
{
if ( cachesim_ref( &I1, a, size) == Hit ) return L1_Hit;
prefetch_LL_doref(a);
if ( cachesim_ref( &LL, a, size) == Hit ) return LL_Hit;
return MemAccess;
}
static
CacheModelResult prefetch_D1_ref(Addr a, UChar size)
{
if ( cachesim_ref( &D1, a, size) == Hit ) return L1_Hit;
prefetch_LL_doref(a);
if ( cachesim_ref( &LL, a, size) == Hit ) return LL_Hit;
return MemAccess;
}
/* complex model with hardware prefetch */
static
CacheModelResult prefetch_I1_Read(Addr a, UChar size)
{
if ( cachesim_ref( &I1, a, size) == Hit ) return L1_Hit;
prefetch_LL_doref(a);
switch( cachesim_ref_wb( &LL, Read, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
static
CacheModelResult prefetch_D1_Read(Addr a, UChar size)
{
if ( cachesim_ref( &D1, a, size) == Hit ) return L1_Hit;
prefetch_LL_doref(a);
switch( cachesim_ref_wb( &LL, Read, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
static
CacheModelResult prefetch_D1_Write(Addr a, UChar size)
{
prefetch_LL_doref(a);
if ( cachesim_ref( &D1, a, size) == Hit ) {
/* Even for a L1 hit, the write-trough L1 passes
* the write to the LL to make the LL line dirty.
* But this causes no latency, so return the hit.
*/
cachesim_ref_wb( &LL, Write, a, size);
return L1_Hit;
}
switch( cachesim_ref_wb( &LL, Write, a, size) ) {
case Hit: return LL_Hit;
case Miss: return MemAccess;
default: break;
}
return WriteBackMemAccess;
}
/*------------------------------------------------------------*/
/*--- Cache Simulation with use metric collection ---*/
/*------------------------------------------------------------*/
/* can not be combined with write-back or prefetch */
static
void cacheuse_initcache(cache_t2* c)
{
int i;
unsigned int start_mask, start_val;
unsigned int end_mask, end_val;
c->use = CLG_MALLOC("cl.sim.cu_ic.1",
sizeof(line_use) * c->sets * c->assoc);
c->loaded = CLG_MALLOC("cl.sim.cu_ic.2",
sizeof(line_loaded) * c->sets * c->assoc);
c->line_start_mask = CLG_MALLOC("cl.sim.cu_ic.3",
sizeof(int) * c->line_size);
c->line_end_mask = CLG_MALLOC("cl.sim.cu_ic.4",
sizeof(int) * c->line_size);
c->line_size_mask = c->line_size-1;
/* Meaning of line_start_mask/line_end_mask
* Example: for a given cache line, you get an access starting at
* byte offset 5, length 4, byte 5 - 8 was touched. For a cache
* line size of 32, you have 1 bit per byte in the mask:
*
* bit31 bit8 bit5 bit 0
* | | | |
* 11..111111100000 line_start_mask[5]
* 00..000111111111 line_end_mask[(5+4)-1]
*
* use_mask |= line_start_mask[5] && line_end_mask[8]
*
*/
start_val = end_val = ~0;
if (c->line_size < 32) {
int bits_per_byte = 32/c->line_size;
start_mask = (1<<bits_per_byte)-1;
end_mask = start_mask << (32-bits_per_byte);
for(i=0;i<c->line_size;i++) {
c->line_start_mask[i] = start_val;
start_val = start_val & ~start_mask;
start_mask = start_mask << bits_per_byte;
c->line_end_mask[c->line_size-i-1] = end_val;
end_val = end_val & ~end_mask;
end_mask = end_mask >> bits_per_byte;
}
}
else {
int bytes_per_bit = c->line_size/32;
start_mask = 1;
end_mask = 1u << 31;
for(i=0;i<c->line_size;i++) {
c->line_start_mask[i] = start_val;
c->line_end_mask[c->line_size-i-1] = end_val;
if ( ((i+1)%bytes_per_bit) == 0) {
start_val &= ~start_mask;
end_val &= ~end_mask;
start_mask <<= 1;
end_mask >>= 1;
}
}
}
CLG_DEBUG(6, "Config %s:\n", c->desc_line);
for(i=0;i<c->line_size;i++) {
CLG_DEBUG(6, " [%2d]: start mask %8x, end mask %8x\n",
i, (UInt)c->line_start_mask[i], (UInt)c->line_end_mask[i]);
}
/* We use lower tag bits as offset pointers to cache use info.
* I.e. some cache parameters don't work.
*/
if ( (1<<c->tag_shift) < c->assoc) {
VG_(message)(Vg_DebugMsg,
"error: Use associativity < %d for cache use statistics!\n",
(1<<c->tag_shift) );
VG_(tool_panic)("Unsupported cache configuration");
}
}
/* for I1/D1 caches */
#define CACHEUSE(L) \
\
static CacheModelResult cacheuse##_##L##_doRead(Addr a, UChar size) \
{ \
UInt set1 = ( a >> L.line_size_bits) & (L.sets_min_1); \
UInt set2 = ((a+size-1) >> L.line_size_bits) & (L.sets_min_1); \
UWord tag = a & L.tag_mask; \
UWord tag2; \
int i, j, idx; \
UWord *set, tmp_tag; \
UInt use_mask; \
\
CLG_DEBUG(6,"%s.Acc(Addr %#lx, size %d): Sets [%u/%u]\n", \
L.name, a, size, set1, set2); \
\
/* First case: word entirely within line. */ \
if (set1 == set2) { \
\
set = &(L.tags[set1 * L.assoc]); \
use_mask = L.line_start_mask[a & L.line_size_mask] & \
L.line_end_mask[(a+size-1) & L.line_size_mask]; \
\
/* This loop is unrolled for just the first case, which is the most */\
/* common. We can't unroll any further because it would screw up */\
/* if we have a direct-mapped (1-way) cache. */\
if (tag == (set[0] & L.tag_mask)) { \
idx = (set1 * L.assoc) + (set[0] & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit0 [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
return L1_Hit; \
} \
/* If the tag is one other than the MRU, move it into the MRU spot */\
/* and shuffle the rest down. */\
for (i = 1; i < L.assoc; i++) { \
if (tag == (set[i] & L.tag_mask)) { \
tmp_tag = set[i]; \
for (j = i; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tmp_tag; \
idx = (set1 * L.assoc) + (tmp_tag & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit%d [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
i, idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
return L1_Hit; \
} \
} \
\
/* A miss; install this tag as MRU, shuffle rest down. */ \
tmp_tag = set[L.assoc - 1] & ~L.tag_mask; \
for (j = L.assoc - 1; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tag | tmp_tag; \
idx = (set1 * L.assoc) + tmp_tag; \
return update_##L##_use(&L, idx, \
use_mask, a &~ L.line_size_mask); \
\
/* Second case: word straddles two lines. */ \
/* Nb: this is a fast way of doing ((set1+1) % L.sets) */ \
} else if (((set1 + 1) & (L.sets_min_1)) == set2) { \
Int miss1=0, miss2=0; /* 0: L1 hit, 1:L1 miss, 2:LL miss */ \
set = &(L.tags[set1 * L.assoc]); \
use_mask = L.line_start_mask[a & L.line_size_mask]; \
if (tag == (set[0] & L.tag_mask)) { \
idx = (set1 * L.assoc) + (set[0] & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit0 [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
goto block2; \
} \
for (i = 1; i < L.assoc; i++) { \
if (tag == (set[i] & L.tag_mask)) { \
tmp_tag = set[i]; \
for (j = i; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tmp_tag; \
idx = (set1 * L.assoc) + (tmp_tag & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit%d [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
i, idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
goto block2; \
} \
} \
tmp_tag = set[L.assoc - 1] & ~L.tag_mask; \
for (j = L.assoc - 1; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tag | tmp_tag; \
idx = (set1 * L.assoc) + tmp_tag; \
miss1 = update_##L##_use(&L, idx, \
use_mask, a &~ L.line_size_mask); \
block2: \
set = &(L.tags[set2 * L.assoc]); \
use_mask = L.line_end_mask[(a+size-1) & L.line_size_mask]; \
tag2 = (a+size-1) & L.tag_mask; \
if (tag2 == (set[0] & L.tag_mask)) { \
idx = (set2 * L.assoc) + (set[0] & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit0 [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
return miss1; \
} \
for (i = 1; i < L.assoc; i++) { \
if (tag2 == (set[i] & L.tag_mask)) { \
tmp_tag = set[i]; \
for (j = i; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tmp_tag; \
idx = (set2 * L.assoc) + (tmp_tag & ~L.tag_mask); \
L.use[idx].count ++; \
L.use[idx].mask |= use_mask; \
CLG_DEBUG(6," Hit%d [idx %d] (line %#lx from %#lx): %x => %08x, count %u\n",\
i, idx, L.loaded[idx].memline, L.loaded[idx].iaddr, \
use_mask, L.use[idx].mask, L.use[idx].count); \
return miss1; \
} \
} \
tmp_tag = set[L.assoc - 1] & ~L.tag_mask; \
for (j = L.assoc - 1; j > 0; j--) { \
set[j] = set[j - 1]; \
} \
set[0] = tag2 | tmp_tag; \
idx = (set2 * L.assoc) + tmp_tag; \
miss2 = update_##L##_use(&L, idx, \
use_mask, (a+size-1) &~ L.line_size_mask); \
return (miss1==MemAccess || miss2==MemAccess) ? MemAccess:LL_Hit; \
\
} else { \
VG_(printf)("addr: %#lx size: %u sets: %u %u", a, size, set1, set2); \
VG_(tool_panic)("item straddles more than two cache sets"); \
} \
return 0; \
}
/* logarithmic bitcounting algorithm, see
* http://graphics.stanford.edu/~seander/bithacks.html
*/
static __inline__ unsigned int countBits(unsigned int bits)
{
unsigned int c; // store the total here
const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
c = bits;
c = ((c >> S[0]) & B[0]) + (c & B[0]);
c = ((c >> S[1]) & B[1]) + (c & B[1]);
c = ((c >> S[2]) & B[2]) + (c & B[2]);
c = ((c >> S[3]) & B[3]) + (c & B[3]);
c = ((c >> S[4]) & B[4]) + (c & B[4]);
return c;
}
static void update_LL_use(int idx, Addr memline)
{
line_loaded* loaded = &(LL.loaded[idx]);
line_use* use = &(LL.use[idx]);
int i = ((32 - countBits(use->mask)) * LL.line_size)>>5;
CLG_DEBUG(2, " LL.miss [%d]: at %#lx accessing memline %#lx\n",
idx, CLG_(bb_base) + current_ii->instr_offset, memline);
if (use->count>0) {
CLG_DEBUG(2, " old: used %u, loss bits %d (%08x) [line %#lx from %#lx]\n",
use->count, i, use->mask, loaded->memline, loaded->iaddr);
CLG_DEBUG(2, " collect: %d, use_base %p\n",
CLG_(current_state).collect, loaded->use_base);
if (CLG_(current_state).collect && loaded->use_base) {
(loaded->use_base)[off_LL_AcCost] += 1000 / use->count;
(loaded->use_base)[off_LL_SpLoss] += i;
}
}
use->count = 0;
use->mask = 0;
loaded->memline = memline;
loaded->iaddr = CLG_(bb_base) + current_ii->instr_offset;
loaded->use_base = (CLG_(current_state).nonskipped) ?
CLG_(current_state).nonskipped->skipped :
CLG_(cost_base) + current_ii->cost_offset;
}
static
CacheModelResult cacheuse_LL_access(Addr memline, line_loaded* l1_loaded)
{
UInt setNo = (memline >> LL.line_size_bits) & (LL.sets_min_1);
UWord* set = &(LL.tags[setNo * LL.assoc]);
UWord tag = memline & LL.tag_mask;
int i, j, idx;
UWord tmp_tag;
CLG_DEBUG(6,"LL.Acc(Memline %#lx): Set %u\n", memline, setNo);
if (tag == (set[0] & LL.tag_mask)) {
idx = (setNo * LL.assoc) + (set[0] & ~LL.tag_mask);
l1_loaded->dep_use = &(LL.use[idx]);
CLG_DEBUG(6," Hit0 [idx %d] (line %#lx from %#lx): => %08x, count %u\n",
idx, LL.loaded[idx].memline, LL.loaded[idx].iaddr,
LL.use[idx].mask, LL.use[idx].count);
return LL_Hit;
}
for (i = 1; i < LL.assoc; i++) {
if (tag == (set[i] & LL.tag_mask)) {
tmp_tag = set[i];
for (j = i; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tmp_tag;
idx = (setNo * LL.assoc) + (tmp_tag & ~LL.tag_mask);
l1_loaded->dep_use = &(LL.use[idx]);
CLG_DEBUG(6," Hit%d [idx %d] (line %#lx from %#lx): => %08x, count %u\n",
i, idx, LL.loaded[idx].memline, LL.loaded[idx].iaddr,
LL.use[idx].mask, LL.use[idx].count);
return LL_Hit;
}
}
/* A miss; install this tag as MRU, shuffle rest down. */
tmp_tag = set[LL.assoc - 1] & ~LL.tag_mask;
for (j = LL.assoc - 1; j > 0; j--) {
set[j] = set[j - 1];
}
set[0] = tag | tmp_tag;
idx = (setNo * LL.assoc) + tmp_tag;
l1_loaded->dep_use = &(LL.use[idx]);
update_LL_use(idx, memline);
return MemAccess;
}
#define UPDATE_USE(L) \
\
static CacheModelResult update##_##L##_use(cache_t2* cache, int idx, \
UInt mask, Addr memline) \
{ \
line_loaded* loaded = &(cache->loaded[idx]); \
line_use* use = &(cache->use[idx]); \
int c = ((32 - countBits(use->mask)) * cache->line_size)>>5; \
\
CLG_DEBUG(2, " %s.miss [%d]: at %#lx accessing memline %#lx (mask %08x)\n", \
cache->name, idx, CLG_(bb_base) + current_ii->instr_offset, memline, mask); \
if (use->count>0) { \
CLG_DEBUG(2, " old: used %u, loss bits %d (%08x) [line %#lx from %#lx]\n",\
use->count, c, use->mask, loaded->memline, loaded->iaddr); \
CLG_DEBUG(2, " collect: %d, use_base %p\n", \
CLG_(current_state).collect, loaded->use_base); \
\
if (CLG_(current_state).collect && loaded->use_base) { \
(loaded->use_base)[off_##L##_AcCost] += 1000 / use->count; \
(loaded->use_base)[off_##L##_SpLoss] += c; \
\
/* FIXME (?): L1/LL line sizes must be equal ! */ \
loaded->dep_use->mask |= use->mask; \
loaded->dep_use->count += use->count; \
} \
} \
\
use->count = 1; \
use->mask = mask; \
loaded->memline = memline; \
loaded->iaddr = CLG_(bb_base) + current_ii->instr_offset; \
loaded->use_base = (CLG_(current_state).nonskipped) ? \
CLG_(current_state).nonskipped->skipped : \
CLG_(cost_base) + current_ii->cost_offset; \
\
if (memline == 0) return LL_Hit; \
return cacheuse_LL_access(memline, loaded); \
}
UPDATE_USE(I1);
UPDATE_USE(D1);
CACHEUSE(I1);
CACHEUSE(D1);
static
void cacheuse_finish(void)
{
int i;
InstrInfo ii = { 0,0,0,0 };
if (!CLG_(current_state).collect) return;
CLG_(bb_base) = 0;
current_ii = ⅈ /* needs to be set for update_XX_use */
CLG_(cost_base) = 0;
/* update usage counters */
if (I1.use)
for (i = 0; i < I1.sets * I1.assoc; i++)
if (I1.loaded[i].use_base)
update_I1_use( &I1, i, 0,0);
if (D1.use)
for (i = 0; i < D1.sets * D1.assoc; i++)