Parallelize the cache adpation
[IRC.git] / Robust / src / Runtime / bamboo / multicoremem.h
1 #ifndef BAMBOO_MULTICORE_MEM_H
2 #define BAMBOO_MULTICORE_MEM_H
3 #include "multicore.h"
4 #include "Queue.h"
5 #include "SimpleHash.h"
6
7 // data structures for shared memory allocation
8 #ifdef TILERA_BME
9 #ifdef MGC
10 #define BAMBOO_BASE_VA 0x600000  
11 #else 
12 #define BAMBOO_BASE_VA 0xd000000
13 #endif
14 #elif defined TILERA_ZLINUX
15 #ifdef MULTICORE_GC
16 #ifdef MGC
17 #define BAMBOO_BASE_VA 0x600000 
18 #else 
19 #define BAMBOO_BASE_VA 0xd000000
20 #endif
21 #endif // MULTICORE_GC
22 #endif // TILERA_BME
23
24 #ifdef BAMBOO_MEMPROF
25 #define GC_BAMBOO_NUMCORES 56
26 #else
27 #ifdef MGC
28 #define GC_BAMBOO_NUMCORES (NUMCORES)
29 #else
30 #define GC_BAMBOO_NUMCORES 62
31 #endif
32 #endif
33
34 #ifdef GC_DEBUG
35 #include "structdefs.h"
36 #define BAMBOO_NUM_BLOCKS (NUMCORES4GC*(2+1)+3)
37 #define BAMBOO_PAGE_SIZE (64 * 64)
38 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
39 #define BAMBOO_SHARED_MEM_SIZE ((unsigned int)((BAMBOO_SMEM_SIZE) *(BAMBOO_NUM_BLOCKS)))
40
41 #elif defined GC_CACHE_ADAPT
42 #ifdef GC_LARGESHAREDHEAP
43 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+24)))
44 #elif defined MGC
45 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(72))) // 72M per core
46 #else
47 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+14)))
48 #endif
49 #define BAMBOO_PAGE_SIZE ((unsigned int)(64 * 1024)) // 64K
50 #ifdef GC_LARGEPAGESIZE
51 #define BAMBOO_PAGE_SIZE ((unsigned int)(4 * 64 * 1024))
52 #define BAMBOO_SMEM_SIZE ((unsigned int)(4 * (BAMBOO_PAGE_SIZE)))
53 #elif defined GC_SMALLPAGESIZE
54 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
55 #elif defined GC_SMALLPAGESIZE2
56 //#define BAMBOO_PAGE_SIZE ((unsigned int)(16 * 1024))  // (4096)
57 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
58 #elif defined GC_LARGEPAGESIZE2
59 #define BAMBOO_PAGE_SIZE ((unsigned int)(4 * 64 * 1024)) // 64K
60 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
61 #elif defined MGC
62 #define BAMBOO_SMEM_SIZE ((unsigned int)(16*(BAMBOO_PAGE_SIZE)))  // 1M
63 #else
64 #define BAMBOO_SMEM_SIZE ((unsigned int)(4 * (BAMBOO_PAGE_SIZE)))
65 #endif // GC_LARGEPAGESIZE
66 #define BAMBOO_SHARED_MEM_SIZE ((unsigned int)((BAMBOO_SMEM_SIZE) * (BAMBOO_NUM_BLOCKS)))
67
68 #else // GC_DEBUG
69 #ifdef GC_LARGESHAREDHEAP
70 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+5)))
71 #elif defined MGC
72 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*72)) // 72M per core
73 #else
74 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+2))) //(15 * 1024) //(64 * 4 * 0.75) //(1024 * 1024 * 3.5)  3G
75 #endif
76 #ifdef GC_LARGEPAGESIZE
77 #define BAMBOO_PAGE_SIZE ((unsigned int)(4 * 1024 * 1024))  // (4096)
78 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
79 #elif defined GC_SMALLPAGESIZE
80 #define BAMBOO_PAGE_SIZE ((unsigned int)(256 * 1024))  // (4096)
81 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
82 #elif defined GC_SMALLPAGESIZE2
83 #define BAMBOO_PAGE_SIZE ((unsigned int)(256 * 1024))  // (4096) 64
84 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
85 #else
86 #define BAMBOO_PAGE_SIZE ((unsigned int)(1024 * 1024))  // (4096)
87 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
88 #endif // GC_LARGEPAGESIZE
89 #define BAMBOO_SHARED_MEM_SIZE ((unsigned int)((BAMBOO_SMEM_SIZE) * (BAMBOO_NUM_BLOCKS))) //(1024 * 1024 * 240) //((unsigned long long int)(3.0 * 1024 * 1024 * 1024)) // 3G 
90 #endif // GC_DEBUG
91
92 #ifdef MULTICORE_GC
93 #ifdef GC_SMALLPAGESIZE
94 // memory for globals
95 #define BAMBOO_GLOBAL_DEFS_SIZE (1024 * 1024)
96 #define BAMBOO_GLOBAL_DEFS_PRIM_SIZE (1024 * 512)
97 // memory for thread queue
98 #define BAMBOO_THREAD_QUEUE_SIZE (1024 * 1024)
99 #else
100 // memory for globals
101 #define BAMBOO_GLOBAL_DEFS_SIZE (BAMBOO_SMEM_SIZE)
102 #define BAMBOO_GLOBAL_DEFS_PRIM_SIZE (BAMBOO_SMEM_SIZE/2)
103 // memory for thread queue
104 #define BAMBOO_THREAD_QUEUE_SIZE (BAMBOO_SMEM_SIZE) // (45 * 16 * 1024)
105 #endif // GC_SMALLPAGESIZE
106
107 volatile bool gc_localheap_s;
108
109 typedef enum {
110   SMEMLOCAL = 0x0,// 0x0, using local mem only
111   SMEMFIXED,      // 0x1, use local mem in lower address space(1 block only)
112                   //      and global mem in higher address space
113   SMEMMIXED,      // 0x2, like FIXED mode but use a threshold to control
114   SMEMGLOBAL,     // 0x3, using global mem only
115   SMEMEND
116 } SMEMSTRATEGY;
117
118 SMEMSTRATEGY bamboo_smem_mode; //-DSMEML: LOCAL; -DSMEMF: FIXED;
119                                //-DSMEMM: MIXED; -DSMEMG: GLOBAL;
120
121 struct freeMemItem {
122   unsigned int ptr;
123   int size;
124   int startblock;
125   int endblock;
126   struct freeMemItem * next;
127 };
128
129 struct freeMemList {
130   struct freeMemItem * head;
131   struct freeMemItem * backuplist; // hold removed freeMemItem for reuse;
132                                    // only maintain 1 freemMemItem
133 };
134
135 // Zero out the remaining bamboo_cur_msp. Only zero out the first 4 bytes 
136 // of the remaining memory
137 #define BAMBOO_CLOSE_CUR_MSP() \
138   { \
139     if((bamboo_cur_msp!=0)&&(bamboo_smem_zero_top==bamboo_cur_msp) \
140         &&(bamboo_smem_size>0)) { \
141       *((int *)bamboo_cur_msp) = 0; \
142     } \
143   }
144
145 // table recording the number of allocated bytes on each block
146 // Note: this table resides on the bottom of the shared heap for all cores
147 //       to access
148 volatile unsigned int * bamboo_smemtbl;
149 #ifdef GC_TBL_DEBUG
150 // the length of the bamboo_smemtbl is gcnumblock
151 #endif
152 volatile unsigned int bamboo_free_block;
153 unsigned int bamboo_reserved_smem; // reserved blocks on the top of the shared 
154                                    // heap e.g. 20% of the heap and should not 
155                                                                    // be allocated otherwise gc is invoked
156 volatile unsigned int bamboo_smem_zero_top;
157 #define BAMBOO_SMEM_ZERO_UNIT_SIZE ((unsigned int)(4 * 1024)) // 4KB
158 #else
159 //volatile mspace bamboo_free_msp;
160 unsigned int bamboo_free_smemp;
161 int bamboo_free_smem_size;
162 #endif // MULTICORE_GC
163 volatile bool smemflag;
164 volatile unsigned int bamboo_cur_msp;
165 volatile int bamboo_smem_size;
166
167 #endif // BAMBOO_MULTICORE_MEM_H