Now the cache adapt version works
[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+2)))
71 #elif defined GC_LARGESHAREDHEAP2
72 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+2)))
73 #elif defined MGC
74 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*72)) // 72M per core
75 #else
76 #define BAMBOO_NUM_BLOCKS ((unsigned int)((GC_BAMBOO_NUMCORES)*(2+3))) //(15 * 1024) //(64 * 4 * 0.75) //(1024 * 1024 * 3.5)  3G
77 #endif
78 #ifdef GC_LARGEPAGESIZE
79 #define BAMBOO_PAGE_SIZE ((unsigned int)(4 * 1024 * 1024))  // (4096)
80 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
81 #elif defined GC_SMALLPAGESIZE
82 #define BAMBOO_PAGE_SIZE ((unsigned int)(256 * 1024))  // (4096)
83 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
84 #elif defined GC_SMALLPAGESIZE2
85 #define BAMBOO_PAGE_SIZE ((unsigned int)(256 * 1024))  // (4096) 64
86 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
87 #else
88 #define BAMBOO_PAGE_SIZE ((unsigned int)(1024 * 1024))  // (4096)
89 #define BAMBOO_SMEM_SIZE ((unsigned int)(BAMBOO_PAGE_SIZE))
90 #endif // GC_LARGEPAGESIZE
91 #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 
92 #endif // GC_DEBUG
93
94 #ifdef MULTICORE_GC
95 #ifdef GC_SMALLPAGESIZE
96 // memory for globals
97 #define BAMBOO_GLOBAL_DEFS_SIZE (1024 * 1024)
98 #define BAMBOO_GLOBAL_DEFS_PRIM_SIZE (1024 * 512)
99 // memory for thread queue
100 #define BAMBOO_THREAD_QUEUE_SIZE (1024 * 1024)
101 #else
102 // memory for globals
103 #define BAMBOO_GLOBAL_DEFS_SIZE (BAMBOO_SMEM_SIZE)
104 #define BAMBOO_GLOBAL_DEFS_PRIM_SIZE (BAMBOO_SMEM_SIZE/2)
105 // memory for thread queue
106 #define BAMBOO_THREAD_QUEUE_SIZE (BAMBOO_SMEM_SIZE) // (45 * 16 * 1024)
107 #endif // GC_SMALLPAGESIZE
108
109 volatile bool gc_localheap_s;
110
111 typedef enum {
112   SMEMLOCAL = 0x0,// 0x0, using local mem only
113   SMEMFIXED,      // 0x1, use local mem in lower address space(1 block only)
114                   //      and global mem in higher address space
115   SMEMMIXED,      // 0x2, like FIXED mode but use a threshold to control
116   SMEMGLOBAL,     // 0x3, using global mem only
117   SMEMEND
118 } SMEMSTRATEGY;
119
120 SMEMSTRATEGY bamboo_smem_mode; //-DSMEML: LOCAL; -DSMEMF: FIXED;
121                                //-DSMEMM: MIXED; -DSMEMG: GLOBAL;
122
123 struct freeMemItem {
124   unsigned int ptr;
125   int size;
126   int startblock;
127   int endblock;
128   struct freeMemItem * next;
129 };
130
131 struct freeMemList {
132   struct freeMemItem * head;
133   struct freeMemItem * backuplist; // hold removed freeMemItem for reuse;
134                                    // only maintain 1 freemMemItem
135 };
136
137 // Zero out the remaining bamboo_cur_msp. Only zero out the first 4 bytes 
138 // of the remaining memory
139 #define BAMBOO_CLOSE_CUR_MSP() \
140   { \
141     if((bamboo_cur_msp!=0)&&(bamboo_smem_zero_top==bamboo_cur_msp) \
142         &&(bamboo_smem_size>0)) { \
143       *((int *)bamboo_cur_msp) = 0; \
144     } \
145   }
146
147 // table recording the number of allocated bytes on each block
148 // Note: this table resides on the bottom of the shared heap for all cores
149 //       to access
150 volatile unsigned int * bamboo_smemtbl;
151 #ifdef GC_TBL_DEBUG
152 // the length of the bamboo_smemtbl is gcnumblock
153 #endif
154 volatile unsigned int bamboo_free_block;
155 unsigned int bamboo_reserved_smem; // reserved blocks on the top of the shared 
156                                    // heap e.g. 20% of the heap and should not 
157                                                                    // be allocated otherwise gc is invoked
158 volatile unsigned int bamboo_smem_zero_top;
159 #define BAMBOO_SMEM_ZERO_UNIT_SIZE ((unsigned int)(4 * 1024)) // 4KB
160 #else
161 //volatile mspace bamboo_free_msp;
162 unsigned int bamboo_free_smemp;
163 int bamboo_free_smem_size;
164 #endif // MULTICORE_GC
165 volatile bool smemflag;
166 volatile unsigned int bamboo_cur_msp;
167 volatile int bamboo_smem_size;
168
169 #endif // BAMBOO_MULTICORE_MEM_H