Browse code

-faster page pre-faulting (when shm_force_alloc = yes): touch only one word per page

Andrei Pelinescu-Onciul authored on 14/06/2007 16:59:21
Showing 1 changed files
... ...
@@ -55,6 +55,9 @@
55 55
 
56 56
 #endif
57 57
 
58
+#define _ROUND2TYPE(s, type) \
59
+	(((s)+(sizeof(type)-1))&(~(sizeof(type)-1)))
60
+#define _ROUND_LONG(s) _ROUND2TYPE(s, long)
58 61
 
59 62
 
60 63
 #ifndef SHM_MMAP
... ...
@@ -225,11 +228,26 @@ int shm_mem_init_mallocs(void* mempool, unsigned long pool_size)
225 225
 int shm_mem_init(int force_alloc)
226 226
 {
227 227
 	int ret;
228
+	long sz;
229
+	long* p;
230
+	long* end;
228 231
 	
229 232
 	ret=shm_getmem();
230 233
 	if (ret<0) return ret;
231
-	if (force_alloc)
232
-		memset(shm_mempool, 0, shm_mem_size);
234
+	if (force_alloc){
235
+		sz=sysconf(_SC_PAGESIZE);
236
+		DBG("shm_mem_init: %ld bytes/page\n", sz);
237
+		if ((sz<sizeof(*p)) || (_ROUND_LONG(sz)!=sz)){
238
+			LOG(L_WARN, "shm_mem_init: invalid page size %ld, using 4096\n",
239
+					sz);
240
+			sz=4096; /* invalid page size, use 4096 */
241
+		}
242
+		end=shm_mempool+shm_mem_size-sizeof(*p);
243
+		/* touch one word in every page */
244
+		for(p=(long*)_ROUND_LONG((long)shm_mempool); p<=end;
245
+										p=(long*)((char*)p+sz))
246
+			*p=0; 
247
+	}
233 248
 	return shm_mem_init_mallocs(shm_mempool, shm_mem_size);
234 249
 }
235 250