00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <sys/user.h>
00024 #include <sys/mman.h>
00025 #include <malloc.h>
00026
00027 #define TOUCH_BUFSIZE 256
00028 #define GROW_STACK (64*1024)
00029 #define GROW_HEAP (64*1024)
00030 #define STR_SIZE 16
00031
00032 void touch_area(void *begin, size_t len, int writeable) {
00033 volatile char *ptr = begin;
00034 int i, page_size;
00035 volatile int tmp;
00036
00037 page_size = getpagesize();
00038 for(i=0;i<len;i+=page_size) {
00039 tmp=ptr[i];
00040
00041 if(writeable) {
00042 ptr[i]=tmp;
00043
00044 }
00045 }
00046 }
00047
00048 int touch_all(void) {
00049 FILE *maps;
00050 unsigned long start,end,flags,size;
00051 char perms[STR_SIZE],dev[STR_SIZE];
00052 char buf[TOUCH_BUFSIZE];
00053
00054 maps=fopen("/proc/self/maps","r");
00055 if(!maps) {
00056 perror("touch_all");
00057 return -1;
00058 }
00059 while(fgets(buf,TOUCH_BUFSIZE-1,maps)) {
00060 if(sscanf(buf,"%8lx-%8lx %15s %lx %15s %lu",&start,&end,perms,&flags,dev,&size)<2)
00061 continue;
00062 if(perms[1] == 'w')
00063 touch_area((void*)start,end-start,1);
00064 else
00065 touch_area((void*)start,end-start,0);
00066 }
00067 fclose(maps);
00068 return 0;
00069 }
00070
00071 int lock_all(int stk, int heap) {
00072 extern void dump_malloc_stats(void);
00073 int err, n, i;
00074 unsigned long *pt;
00075 char stack[stk ? stk : GROW_STACK];
00076 stack[0] = ' ';
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 n = heap / 65536 + 1;
00092 if ( n > (sizeof(stack) / sizeof(int))) {
00093 printf("heap too large\n");
00094 exit(-1);
00095 }
00096
00097 err = mallopt(M_MMAP_THRESHOLD, 512*1024);
00098 if (!err) {
00099 printf("mallopt(M_MMAP_THRESHOLD, heap) failed\n");
00100 exit(-1);
00101 }
00102
00103 err = mallopt(M_TOP_PAD, heap ? heap : GROW_HEAP);
00104 if (!err) {
00105 printf("mallopt(M_TOP_PAD, heap) failed\n");
00106 exit(-1);
00107 }
00108
00109 if(mlockall(MCL_CURRENT|MCL_FUTURE)) {
00110 perror("mlockall");
00111 exit(-1);
00112 }
00113
00114 touch_all();
00115
00116 pt = (unsigned long *) stack;
00117 for(i=0; i<n ; i++, pt++) *pt = (unsigned long) malloc(65536);
00118 pt = (unsigned long *) stack;
00119 for(i=0; i<n ; i++, pt++) free((void *) *pt);
00120
00121 return 0;
00122 }
00123
00124 void dump_malloc_stats(void)
00125 {
00126 struct mallinfo mi;
00127 extern int rtai_print_to_screen(const char *fmt, ...);
00128
00129 mi = mallinfo();
00130
00131 rtai_print_to_screen("\ntotal space allocated from system %d\n", mi.arena);
00132 rtai_print_to_screen("number of non-inuse chunks %d\n", mi.ordblks);
00133 rtai_print_to_screen("number of mmapped regions %d\n", mi.hblks);
00134 rtai_print_to_screen("total space in mmapped regions %d\n", mi.hblkhd);
00135 rtai_print_to_screen("total allocated space %d\n", mi.uordblks);
00136 rtai_print_to_screen("total non-inuse space %d\n", mi.fordblks);
00137 rtai_print_to_screen("top-most, releasable space %d\n", mi.keepcost);
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148