-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.c
1214 lines (1109 loc) · 34.2 KB
/
misc.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
/*
* Miscellaneous functions for Dinero IV.
* Written by Jan Edler and Mark D. Hill
*
* Copyright (C) 1997 NEC Research Institute, Inc. and Mark D. Hill.
* All rights reserved.
* Copyright (C) 1985, 1989 Mark D. Hill. All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and
* its associated documentation for non-commercial purposes is hereby
* granted (for commercial purposes see below), provided that the above
* copyright notice appears in all copies, derivative works or modified
* versions of the software and any portions thereof, and that both the
* copyright notice and this permission notice appear in the documentation.
* NEC Research Institute Inc. and Mark D. Hill shall be given a copy of
* any such derivative work or modified version of the software and NEC
* Research Institute Inc. and any of its affiliated companies (collectively
* referred to as NECI) and Mark D. Hill shall be granted permission to use,
* copy, modify, and distribute the software for internal use and research.
* The name of NEC Research Institute Inc. and its affiliated companies
* shall not be used in advertising or publicity related to the distribution
* of the software, without the prior written consent of NECI. All copies,
* derivative works, or modified versions of the software shall be exported
* or reexported in accordance with applicable laws and regulations relating
* to export control. This software is experimental. NECI and Mark D. Hill
* make no representations regarding the suitability of this software for
* any purpose and neither NECI nor Mark D. Hill will support the software.
*
* Use of this software for commercial purposes is also possible, but only
* if, in addition to the above requirements for non-commercial use, written
* permission for such use is obtained by the commercial user from NECI or
* Mark D. Hill prior to the fabrication and distribution of the software.
*
* THE SOFTWARE IS PROVIDED AS IS. NECI AND MARK D. HILL DO NOT MAKE
* ANY WARRANTEES EITHER EXPRESS OR IMPLIED WITH REGARD TO THE SOFTWARE.
* NECI AND MARK D. HILL ALSO DISCLAIM ANY WARRANTY THAT THE SOFTWARE IS
* FREE OF INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS OF OTHERS.
* NO OTHER LICENSE EXPRESS OR IMPLIED IS HEREBY GRANTED. NECI AND MARK
* D. HILL SHALL NOT BE LIABLE FOR ANY DAMAGES, INCLUDING GENERAL, SPECIAL,
* INCIDENTAL, OR CONSEQUENTIAL DAMAGES, ARISING OUT OF THE USE OR INABILITY
* TO USE THE SOFTWARE.
*
* $Header: /home/edler/dinero/d4/RCS/misc.c,v 1.7 1997/12/11 08:07:58 edler Exp $
*/
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <assert.h>
#include "d4.h"
/*
* Global variable definitions
*/
struct d4_stackhash_struct d4stackhash;
d4stacknode d4freelist;
int d4nnodes;
d4pendstack *d4pendfree;
d4cache *d4_allcaches;
/*
* Private prototypes for this file
*/
extern void d4_invblock (d4cache *, int stacknum, d4stacknode *);
extern void d4_invinfcache (d4cache *, const d4memref *);
/*
* Create a new cache
* The new cache sits "above" the indicated larger cache in the
* memory hierarchy, with memory at the bottom and processors at the top.
*/
d4cache *
d4new (d4cache *larger)
{
static int nextcacheid = 1;
d4cache *c = calloc (1, sizeof(d4cache));
if (c == NULL)
return NULL;
c->cacheid = nextcacheid++;
c->downstream = larger;
c->ref = d4ref; /* may get altered for custom version */
if (larger == NULL) { /* simulated memory */
c->flags = D4F_MEM;
c->assoc = 1; /* not used, but helps avoid compiler warnings */
}
c->link = d4_allcaches;
d4_allcaches = c; /* d4customize depends on this LIFO order */
return c;
}
/*
* Check all caches, set up internal data structures.
* Must be called exactly once, after all calls to d4new
* and all necessary direct initialization of d4cache structures.
* The call to d4setup must occur before any calls to d4ref.
* The return value is zero for success.
*/
int
d4setup()
{
int i, nnodes;
int r = 0;
d4cache *c, *cc;
d4stacknode *nodes = NULL, *ptr;
for (c = d4_allcaches; c != NULL; c = c->link) {
/* Check some stuff the user shouldn't muck with */
if (c->stack != NULL || c->pending != NULL ||
c->cacheid < 1 ||
(c->link == NULL && c->cacheid != 1) ||
(c->flags != D4F_MEM && c->downstream == NULL) ||
c->numsets != 0 ||
c->ranges != NULL || c->nranges != 0 || c->maxranges != 0)
goto fail1;
/*
* If customization has been done,
* check that the same values have been set in the d4cache structure.
* NOTE: The order of the checks here
* must match the code in d4customize.
*/
if (d4custom) {
int problem = 0;
if (c->cacheid > d4_ncustom)
problem |= 0x1;
else {
if (d4_cust_vals[c->cacheid][0] != c->flags)
problem |= 0x2;
if (d4_cust_vals[c->cacheid][1] != c->lg2blocksize)
problem |= 0x4;
if (d4_cust_vals[c->cacheid][2] != c->lg2subblocksize)
problem |= 0x8;
if (d4_cust_vals[c->cacheid][3] != c->lg2size)
problem |= 0x10;
if (d4_cust_vals[c->cacheid][4] != c->assoc)
problem |= 0x20;
if (d4_cust_vals[c->cacheid][5] != c->prefetch_abortpercent)
problem |= 0x40;
if (d4_cust_vals[c->cacheid][6] != ((c->replacementf==d4rep_lru)?'l':
(c->replacementf==d4rep_fifo)?'f':
(c->replacementf==d4rep_random)?'r':0))
problem |= 0x80;
if (d4_cust_vals[c->cacheid][7] != ((c->prefetchf==d4prefetch_none)?'n':
(c->prefetchf==d4prefetch_always)?'a':
(c->prefetchf==d4prefetch_loadforw)?'l':
(c->prefetchf==d4prefetch_subblock)?'s':
(c->prefetchf==d4prefetch_miss)?'m':
(c->prefetchf==d4prefetch_tagged)?'t':0))
problem |= 0x100;
if (d4_cust_vals[c->cacheid][8] != ((c->wallocf==d4walloc_always)?'a':
(c->wallocf==d4walloc_never)?'n':
(c->wallocf==d4walloc_nofetch)?'f':0))
problem |= 0x200;
if (d4_cust_vals[c->cacheid][9] != ((c->wbackf==d4wback_always)?'a':
(c->wbackf==d4wback_never)?'n':
(c->wbackf==d4wback_nofetch)?'f':0))
problem |= 0x400;
}
if (problem) {
fprintf (stderr, "Dinero IV: custom values "
"are inconsistent, problem=0x%x\n",
problem);
exit (9);
}
}
if ((c->flags & D4F_MEM) != 0)
c->numsets = 1; /* not used, but helps avoid compiler warnings */
else {
/* check the things the user should have set */
if (c->lg2blocksize < 0)
goto fail2;
if (c->lg2subblocksize < 0 || c->lg2subblocksize > c->lg2blocksize)
goto fail3;
if (c->lg2size < c->lg2blocksize)
goto fail4;
if (c->assoc <= 0)
goto fail5;
if (c->replacementf == NULL || c->name_replacement == NULL)
goto fail6;
if (c->prefetchf == NULL || c->name_prefetch == NULL)
goto fail7;
if (c->wallocf == NULL || c->name_walloc == NULL)
goto fail8;
if (c->wbackf == NULL || c->name_wback == NULL)
goto fail9;
c->vc = calloc(1, sizeof(d4stackhead));
if(c->vc==NULL)
goto fail10;
nnodes=c->vc_size; // TODO: Modify number of blocks in victim cache
d4stacknode *vc_nodes = calloc(nnodes, sizeof(d4stacknode));
vc_nodes->victim = 1; // Set indicator for victim cache
if(vc_nodes == NULL)
goto fail11;
d4stacknode *vc_ptr;
vc_ptr=vc_nodes;
c->vc->top = vc_ptr;
c->vc->n = nnodes;
for(i = 1; i < nnodes - 1; i++) {
vc_ptr[i].onstack= -1;
vc_ptr[i].down=&vc_ptr[i+1];
vc_ptr[i].up=&vc_ptr[i-1];
}
vc_ptr[0].up=&vc_ptr[nnodes-1];
vc_ptr[0].down=&vc_ptr[1];
vc_ptr[0].onstack=-1;
vc_ptr[nnodes-1].down=&vc_ptr[0];
vc_ptr[nnodes-1].up=&vc_ptr[nnodes-2];
vc_ptr[nnodes-1].onstack=-1;
/* we don't try to check per-policy cache state */
/* it looks ok, now initialize */
c->numsets = (1<<c->lg2size) / ((1<<c->lg2blocksize) * c->assoc);
c->stack = calloc (c->numsets+((c->flags&D4F_CCC)!=0),
sizeof(d4stackhead));
if (c->stack == NULL)
goto fail10;
nnodes = c->numsets * (1 + c->assoc) +
(c->numsets * c->assoc + 1) * ((c->flags&D4F_CCC)!=0);
nodes = calloc (nnodes, sizeof(d4stacknode));
if (nodes == NULL)
goto fail11;
nodes->victim = 0;
for (i = 0; i < nnodes; i++)
nodes[i].cachep = c;
ptr = nodes;
/* set up circular list for each stack */
for (i = 0; i < c->numsets+((c->flags&D4F_CCC)!=0); i++) {
int j, n;
n = 1 + c->assoc * ((i < c->numsets) ? 1 : c->numsets);
c->stack[i].top = ptr;
c->stack[i].n = n;
for (j = 1; j < n-1; j++) {
ptr[j].onstack = i;
ptr[j].down = &ptr[j+1];
ptr[j].up = &ptr[j-1];
}
ptr[0].onstack = i;
ptr[0].down = &ptr[1];
ptr[0].up = &ptr[n-1];
ptr[n-1].onstack = i;
ptr[n-1].down = &ptr[0];
ptr[n-1].up = &ptr[n-2];
ptr += n;
}
assert (ptr - nodes == nnodes);
#if D4_HASHSIZE == 0
d4stackhash.size += c->numsets * c->assoc;
#endif
d4nnodes += nnodes;
}
/* make a printable name if the user didn't pick one */
if (c->name == NULL) {
c->name = malloc (30);
if (c->name == NULL)
goto fail12;
sprintf (c->name, "%s%d",
(c->flags&D4F_MEM)!=0 ? "memory" : "cache", c->cacheid);
}
}
#if D4_HASHSIZE > 0
d4stackhash.size = D4_HASHSIZE;
#endif
d4stackhash.table = calloc (d4stackhash.size, sizeof(d4stacknode*));
if (d4stackhash.table == NULL)
goto fail13;
return 0;
/* Try to undo stuff so (in principle) the user could try again */
fail13: r++;
/* don't bother trying to deallocate c->name */
fail12: r++;
free (nodes);
fail11: r++;
free (c->stack);
fail10: r++;
fail9: r++;
fail8: r++;
fail7: r++;
fail6: r++;
fail5: r++;
fail4: r++;
fail3: r++;
fail2: r++;
fail1: r++;
for (cc = d4_allcaches; cc != c; cc = cc->link) {
/* don't bother trying to deallocate c->name */
free (c->stack[0].top);
free (c->stack);
c->stack = NULL;
c->numsets = 0;
}
d4nnodes = 0;
return r;
}
/*
* Initialization routines for built-in policies
* These exist for convenience of use only.
*/
void
d4init_rep_lru (d4cache *c)
{
c->replacementf = d4rep_lru;
c->name_replacement = "LRU";
}
void
d4init_rep_fifo (d4cache *c)
{
c->replacementf = d4rep_fifo;
c->name_replacement = "FIFO";
}
void
d4init_rep_random (d4cache *c)
{
c->replacementf = d4rep_random;
c->name_replacement = "random";
}
void
d4init_prefetch_none (d4cache *c)
{
c->prefetchf = d4prefetch_none;
c->name_prefetch = "none";
}
void
d4init_prefetch_always (d4cache *c, int dist, int abortpct)
{
c->prefetchf = d4prefetch_always;
c->name_prefetch = "always";
c->prefetch_distance = dist;
c->prefetch_abortpercent = abortpct;
}
void
d4init_prefetch_loadforw (d4cache *c, int dist, int abortpct)
{
c->prefetchf = d4prefetch_loadforw;
c->name_prefetch = "load-forward";
c->prefetch_distance = dist;
c->prefetch_abortpercent = abortpct;
}
void
d4init_prefetch_subblock (d4cache *c, int dist, int abortpct)
{
c->prefetchf = d4prefetch_subblock;
c->name_prefetch = "subblock";
c->prefetch_distance = dist;
c->prefetch_abortpercent = abortpct;
}
void
d4init_prefetch_miss (d4cache *c, int dist, int abortpct)
{
c->prefetchf = d4prefetch_miss;
c->name_prefetch = "miss";
c->prefetch_distance = dist;
c->prefetch_abortpercent = abortpct;
}
void
d4init_prefetch_tagged (d4cache *c, int dist, int abortpct)
{
c->prefetchf = d4prefetch_tagged;
c->name_prefetch = "tagged";
c->prefetch_distance = dist;
c->prefetch_abortpercent = abortpct;
}
void
d4init_walloc_always (d4cache *c)
{
c->wallocf = d4walloc_always;
c->name_walloc = "always";
}
void
d4init_walloc_never (d4cache *c)
{
c->wallocf = d4walloc_never;
c->name_walloc = "never";
}
void
d4init_walloc_nofetch (d4cache *c)
{
c->wallocf = d4walloc_nofetch;
c->name_walloc = "nofetch";
}
void
d4init_wback_always (d4cache *c)
{
c->wbackf = d4wback_always;
c->name_wback = "always";
}
void
d4init_wback_never (d4cache *c)
{
c->wbackf = d4wback_never;
c->name_wback = "never";
}
void
d4init_wback_nofetch (d4cache *c)
{
c->wbackf = d4wback_nofetch;
c->name_wback = "nofetch";
}
/* this is for the walloc policy of an icache */
int
d4walloc_impossible (d4cache *c, d4memref m)
{
fprintf (stderr, "Dinero IV: impossible walloc policy routine called for %s!!!\n",
c->name);
exit (9);
return 0; /* can't really get here, but some compilers get upset if we don't have a return value */
}
/* this is for the wback policy of an icache */
int
d4wback_impossible (d4cache *c, d4memref m, int setnumber, d4stacknode *ptr, int walloc)
{
fprintf (stderr, "Dinero IV: impossible wback policy routine called for %s!!!\n",
c->name);
exit (9);
return 0; /* can't really get here, but some compilers get upset if we don't have a return value */
}
#if 0 /* not used normally, but can be useful for debugging */
/*
* Perform a consistency check on a priority stack
*/
void d4checkstack (d4cache *, int, char *); /* prototype avoids warnings */
void
d4checkstack (d4cache *c, int stacknum, char *msg)
{
d4stacknode *sp, *top;
int i, ii;
static int tentimes = 10;
top = c->stack[stacknum].top;
i = 0;
sp = top;
do {
i++;
sp = sp->down;
} while (sp != top);
if (i != c->stack[stacknum].n) {
if (tentimes-- < 0)
return;
fprintf (stderr, "%s: cache %d stack %d actual forward count=%d, shouldbe=%d\n",
msg, c->cacheid, stacknum, i, c->stack[stacknum].n);
}
ii = 0;
sp = top;
do {
ii++;
sp = sp->up;
} while (sp != top);
if (ii != i) {
if (tentimes-- < 0)
return;
fprintf (stderr, "%s: cache %d stack %d actual forward count=%d, actual reverse count=%d\n",
msg, c->cacheid, stacknum, i, ii);
}
}
#endif
/*
* Find address in stack.
*/
d4stacknode *
d4_find (d4cache *c, int stacknum, d4addr blockaddr)
{
d4stacknode *ptr, *vc;
if (c->stack[stacknum].n > D4HASH_THRESH) {
int buck = D4HASH (blockaddr, stacknum, c->cacheid);
for (ptr = d4stackhash.table[buck];
ptr!=NULL && (ptr->blockaddr!=blockaddr || ptr->cachep!=c || ptr->onstack != stacknum);
ptr = ptr->bucket)
assert (ptr->valid != 0);
return ptr;
}
/*
* Don't hash, search the stack linearly.
* The search will terminate,
* because the last node is guaranteed to have valid==0.
*/
for (ptr = c->stack[stacknum].top;
ptr->blockaddr != blockaddr && ptr->valid != 0;
ptr = ptr->down)
continue;
if (ptr->valid != 0) {
/* printf("Found in L1 cache\n\n"); */
return ptr;
}
vc=c->vc->top;
d4stacknode *temp = vc;
if(temp == NULL)
return NULL;
/* for (int i = 0; i < c->vc->n; ++i) { */
/* printf("block: %ld\n", vc->blockaddr); */
/* if(vc->blockaddr == blockaddr && vc->valid != 0) { */
/* printf("Found you bucko!\n"); */
/* return vc; */
/* } */
/* vc = vc->down; */
/* } */
/* for (vc = c->vc->top; */
/* vc->blockaddr != blockaddr && vc->valid != 0 && temp->down != vc; */
/* vc = vc->down) { */
/* continue; */
/* } */
int count = 0;
if(vc->blockaddr != 0) {
while(temp->down != vc && count < c->vc_size) {
if(temp->blockaddr == blockaddr && temp->valid != 0 && temp->blockaddr != 0) {
break;
}
count++;
temp = temp->down;
}
if(temp->valid != 0 && temp->blockaddr == blockaddr) {
// Sanity check :)
#ifdef DEBUG
printf("Searching for %ld and Found %ld in Victim Cache!!\n", blockaddr, temp->blockaddr);
#endif
return temp;
}
}
return NULL; /* not found */
}
/* find the nth element from the top (1 origin) */
d4stacknode *
d4findnth (d4cache *c, int stacknum, int n)
{
d4stacknode *p;
int i, stacksize;
stacksize = c->stack[stacknum].n;
assert (n <= stacksize);
/* go in the shortest direction to find node */
p = c->stack[stacknum].top;
if (n <= stacksize/2) /* search from front */
for (i = 1; i < n; i++)
p = p->down;
else /* search from rear */
for (i = stacksize+1; i > n; i--)
p = p->up;
return p;
}
/* Move node to top (most recently used position) of stack */
void
d4movetotop (d4cache *c, int stacknum, d4stacknode *ptr)
{
d4stacknode *top = c->stack[stacknum].top;
d4stacknode *bot;
/* nothing to do if node is already at top */
if (ptr != top) {
bot = top->up;
if (bot != ptr) { /* general case */
if( ptr->victim == 1){
bot->blockaddr = ptr->blockaddr;
bot->valid = ptr->valid;
bot->referenced = ptr->referenced;
bot->dirty = ptr->dirty;
c->vc->top = ptr->down;
ptr = bot;
bot->up->valid = 0;
}
else {
ptr->down->up = ptr->up; /* remove */
ptr->up->down = ptr->down;
ptr->up = bot; /* insert between top & bot */
ptr->down = top;
bot->down = ptr;
top->up = ptr;
}
}
c->stack[stacknum].top = ptr;
}
}
/* Move node to bottom (least recently used, actually spare) position */
void
d4movetobot (d4cache *c, int stacknum, d4stacknode *ptr)
{
d4stacknode *top = c->stack[stacknum].top;
d4stacknode *bot = top->up;
/* nothing to do if node is already at bottom */
if (ptr != bot) {
if (top == ptr) /* common and favorable: move from top to bot */
c->stack[stacknum].top = top->down;
else {
ptr->down->up = ptr->up; /* remove */
ptr->up->down = ptr->down;
ptr->up = bot; /* insert between top & bot */
ptr->down = top;
bot->down = ptr;
top->up = ptr;
}
}
}
/* Insert the indicated node into the hash table */
void
d4hash (d4cache *c, int stacknum, d4stacknode *s)
{
int buck = D4HASH (s->blockaddr, stacknum, s->cachep->cacheid);
assert (c->stack[stacknum].n > D4HASH_THRESH);
s->bucket = d4stackhash.table[buck];
d4stackhash.table[buck] = s;
}
/* Remove the indicated node from the hash table */
void
d4_unhash (d4cache *c, int stacknum, d4stacknode *s)
{
int buck = D4HASH (s->blockaddr, stacknum, c->cacheid);
d4stacknode *p = d4stackhash.table[buck];
assert (c->stack[stacknum].n > D4HASH_THRESH);
if (p == s)
d4stackhash.table[buck] = s->bucket;
else {
while (p->bucket != s) {
assert (p->bucket != NULL);
p = p->bucket;
}
p->bucket = s->bucket;
}
}
/* Allocate a structure describing a pending memory reference */
d4pendstack *
d4get_mref()
{
d4pendstack *m;
m = d4pendfree;
if (m != NULL) {
d4pendfree = m->next;
return m;
}
m = malloc (sizeof(*m)); /* no need to get too fancy here */
if (m != NULL)
return m;
fprintf (stderr, "DineroIV ***error: no memory for pending mref\n");
exit (9);
return NULL; /* can't really get here, but some compilers get upset if we don't have a return value */
}
/* Deallocate the structure used to describe a pending memory reference */
void
d4put_mref (d4pendstack *m)
{
m->next = d4pendfree;
d4pendfree = m;
}
/*
* Make recursive calls for pending references
* to own cache or towards memory
*/
void
d4_dopending (d4cache *c, d4pendstack *newm)
{
do {
c->pending = newm->next;
if ((newm->m.accesstype & D4PREFETCH) != 0)
c->ref (c, newm->m);
else if ((newm->m.accesstype & D4_MULTIBLOCK) != 0) {
newm->m.accesstype &= ~D4_MULTIBLOCK;
c->ref (c, newm->m);
}
else {
switch (D4BASIC_ATYPE(newm->m.accesstype)) {
default: fprintf (stderr, "Dinero IV: missing case %d in d4_dopending\n",
D4BASIC_ATYPE(newm->m.accesstype));
exit (9);
case D4XMISC:
case D4XREAD:
case D4XINSTRN: c->bytes_read += newm->m.size;
break;
case D4XWRITE: c->bytes_written += newm->m.size;
break;
case D4XCOPYB:
case D4XINVAL: /* don't count these */
break;
}
c->downstream->ref (c->downstream, newm->m);
}
d4put_mref(newm);
} while ((newm = c->pending) != NULL);
}
/*
* Initiate write back for the dirty parts of a block.
* Each contiguous bunch of subblocks is written in one operation.
*/
void
d4_wbblock (d4cache *c, d4stacknode *ptr, const int lg2sbsize)
{
d4addr a;
unsigned int b, dbits;
d4pendstack *newm;
const int sbsize = 1 << lg2sbsize;
b = 1;
dbits = ptr->valid & ptr->dirty;
a = ptr->blockaddr;
do {
newm = d4get_mref();
newm->m.accesstype = D4XWRITE;
for (; (dbits&b) == 0; b<<=1)
a += sbsize;
newm->m.address = a;
for (; (dbits&b) != 0; b<<=1) {
a += sbsize;
dbits &= ~b;
}
newm->m.size = a - newm->m.address;
newm->next = c->pending;
c->pending = newm;
} while (dbits != 0);
ptr->dirty = 0;
}
/* invalidate and deallocate a block, as indicated by ptr */
void
d4_invblock (d4cache *c, int stacknum, d4stacknode *ptr)
{
assert (ptr->valid != 0);
ptr->valid = 0;
d4movetobot (c, stacknum, ptr);
if (c->stack[stacknum].n > D4HASH_THRESH)
d4_unhash (c, stacknum, ptr);
}
/*
* Copy any dirty stuff from the cache back towards memory. The operation
* affects the whole cache or just 1 block, depending on m:
* if m == NULL or m->size == 0, it affects the whole cache.
* If prop is true, the same copyback operation will be propagated
* to other caches towards memory.
* This function can be invoked directly by the subroutine-calling user,
* or indirectly by passing a D4XCOPYB access type to d4ref
* (in which case d4ref passes prop == 1).
*
* NOTE: this function does not invalidate!
*/
void
d4copyback (d4cache *c, const d4memref *m, int prop)
{
int stacknum;
d4stacknode *ptr;
d4pendstack *newm;
if (m != NULL)
assert (m->accesstype == D4XCOPYB);
if (prop) {
newm = d4get_mref();
if (m != NULL)
newm->m = *m;
else {
newm->m.accesstype = D4XCOPYB;
newm->m.address = 0;
newm->m.size = 0; /* affect the whole cache */
}
newm->next = c->pending;
c->pending = newm;
}
if (m != NULL && m->size > 0) { /* copy back just 1 block */
ptr = d4_find (c, D4ADDR2SET (c, m->address), D4ADDR2BLOCK (c, m->address));
if (ptr != NULL && (ptr->dirty & ptr->valid) != 0)
d4_wbblock (c, ptr, c->lg2subblocksize);
}
else for (stacknum=0; stacknum < c->numsets; stacknum++) {
d4stacknode *top = c->stack[stacknum].top;
assert (top->up->valid == 0); /* this loop skips the bottom node */
for (ptr = top; ptr->down != top; ptr = ptr->down)
if ((ptr->dirty & ptr->valid) != 0)
d4_wbblock (c, ptr, c->lg2subblocksize);
}
if ((newm = c->pending) != NULL)
d4_dopending (c, newm);
}
/*
* Invalidate cache contents. The operation affects the whole cache
* or just 1 block, depending on m:
* if m == NULL or m->size == 0, it affects the whole cache.
* If prop is true, the same invalidate operation will be propagated
* to other caches towards memory.
* This function can be invoked directly by the subroutine-calling user,
* or indirectly by passing a D4XINVAL access type to d4ref
* (in which case d4ref passes prop == 1).
*
* NOTE: this does not copy back dirty stuff!
* you have to call d4copyback first for that.
*/
void
d4invalidate (d4cache *c, const d4memref *m, int prop)
{
int stacknum;
d4stacknode *ptr;
d4pendstack *newm;
if (m != NULL)
assert (m->accesstype == D4XINVAL);
if (prop) {
newm = d4get_mref();
if (m != NULL)
newm->m = *m;
else {
newm->m.accesstype = D4XINVAL;
newm->m.address = 0;
newm->m.size = 0; /* affect the whole cache */
}
newm->next = c->pending;
c->pending = newm;
}
if (m != NULL && m->size > 0) { /* invalidate just one block */
d4addr blockaddr = D4ADDR2BLOCK (c, m->address);
stacknum = D4ADDR2SET (c, m->address);
ptr = d4_find (c, stacknum, blockaddr);
if (ptr != NULL)
d4_invblock (c, stacknum, ptr);
if ((c->flags & D4F_CCC) != 0 && /* fully assoc cache */
(ptr = d4_find (c, c->numsets, blockaddr)) != NULL)
d4_invblock (c, c->numsets, ptr);
}
else for (stacknum=0; stacknum < c->numsets + ((c->flags & D4F_CCC) != 0); stacknum++) {
d4stacknode *top = c->stack[stacknum].top;
assert (top->up->valid == 0); /* all invalid nodes are at bottom; at least 1 */
for (ptr = top; ptr->down != top; ptr = ptr->down) {
if (ptr->valid == 0)
break;
d4_invblock (c, stacknum, ptr);
}
}
if ((c->flags & D4F_CCC) != 0)
d4_invinfcache (c, m);
if ((newm = c->pending) != NULL)
d4_dopending (c, newm);
}
/*
* Handle invalidation for infinite cache
*/
void
d4_invinfcache (d4cache *c, const d4memref *m)
{
int i;
if (m == NULL || m->size == 0) { /* invalidate whole cache */
for (i = 0; i < c->nranges; i++) {
assert (c->ranges[i].bitmap != NULL);
free (c->ranges[i].bitmap);
c->ranges[i].bitmap = NULL;
}
c->nranges = 0;
c->maxranges = 0;
}
else { /* invalidate just one block */
const unsigned int sbsize = 1 << D4VAL (c, lg2subblocksize);
const unsigned int baddr = D4ADDR2BLOCK (c, m->address);
unsigned int bitoff; /* offset of bit in bitmap */
int hi, lo, nsb;
bitoff = (baddr & (D4_BITMAP_RSIZE-1)) / sbsize;
/* binary search for range containing our address */
hi = c->nranges-1;
lo = 0;
while (lo <= hi) {
i = lo + (hi-lo)/2;
if (c->ranges[i].addr + D4_BITMAP_RSIZE <= baddr)
lo = i + 1; /* need to look higher */
else if (c->ranges[i].addr > baddr)
hi = i - 1; /* need to look lower */
else { /* found the right range */
for (nsb = c->lg2blocksize - c->lg2subblocksize;
nsb-- > 0;
bitoff++)
c->ranges[i].bitmap[bitoff/CHAR_BIT] &=
~(1<<(bitoff%CHAR_BIT));
break;
}
}
}
}
/*
* Create custom code for d4ref and some routines it calls.
* For each cache, we output macro definitions to establish constant
* alternatives for key fields within d4cache, rename customizable
* functions, and output an #include "ref.c".
* The expectation is that the user will compile this output file
* and link it with the simulator.
*/
void
d4customize (FILE *f)
{
d4cache *c;
int i, n;
fprintf (f, "#if !D4CUSTOM\n"
"#error \"D4CUSTOM not set\"\n"
"#endif\n"
"#include \"ref.c\"\n\n"); /* get one-time stuff */
/*
* define d4_custom[] and d4_ncustom,
* which can be used to find the proper customized d4ref function
* when the customized program is started up.
*/
if (d4_allcaches == NULL) {
fprintf (stderr, "Dinero IV: d4customize called before d4new\n");
exit (9);
}
n = d4_allcaches->cacheid;
for (i = 1; i <= n; i++)
fprintf (f, "extern void d4_%dref (d4cache *, d4memref);\n", i);
fprintf (f, "void (*d4_custom[])(d4cache *, d4memref) = {\n\t");
for (i = 1; i <= n; i++)
fprintf (f, "d4_%dref%s", i, (i<n) ? ", " : "");
fprintf (f, "\n};\n");
fprintf (f, "int d4_ncustom = %d;\n", n);
/*
* Now Generate the customized stuff for each cache.
* Only the policy functions provided with Dinero IV are
* customized; user-supplied policy functions are unaltered.
* If you add new policy functions to ref.c, you need to
* add code here too!
*/
for (c = d4_allcaches; c != NULL; c = c->link) {
int cid = c->cacheid;
fprintf (f, "\n");
/* Define helper used by D4VAL macro */
fprintf (f, "#undef D4_CACHEID\n"
"#define D4_CACHEID %d\n", cid);
/*
* Define a cache-specific macro
* for the value of each field to be constantized,
* and a "trigger" macro to allow its use in D4VAL.
* In a few cases, we may #undef the trigger later
* if we find we can't use it.