base/sched/liblxrt/touchall.c

Go to the documentation of this file.
00001 /* 00002 * Copyright (C) Tomasz Motylewski <motyl@stan.chemie.unibas.ch> 00003 * Copyright (C) Pierre Cloutier <pcloutier@poseidoncontrols.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the version 2 of the GNU Lesser 00007 * General Public License as published by the Free Software 00008 * Foundation. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser General Public License for more details. 00014 00015 * You should have received a copy of the GNU Lesser General Public 00016 * License along with this library; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00018 */ 00019 00020 #include <stdio.h> 00021 #include <stdlib.h> 00022 #include <asm/page.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; 00035 volatile int tmp; 00036 00037 for(i=0;i<len;i+=PAGE_SIZE) { 00038 tmp=ptr[i]; 00039 /* printf("R:%p",ptr+i); */ 00040 if(writeable) { 00041 ptr[i]=tmp; 00042 /* printf("W:%p",ptr+i); */ 00043 } 00044 } 00045 } 00046 00047 int touch_all(void) { 00048 FILE *maps; 00049 unsigned long start,end,flags,size; 00050 char perms[STR_SIZE],dev[STR_SIZE]; 00051 char buf[TOUCH_BUFSIZE]; 00052 00053 maps=fopen("/proc/self/maps","r"); 00054 if(!maps) { 00055 perror("touch_all"); 00056 return -1; 00057 } 00058 while(fgets(buf,TOUCH_BUFSIZE-1,maps)) { 00059 if(sscanf(buf,"%8lx-%8lx %15s %lx %15s %lu",&start,&end,perms,&flags,dev,&size)<2) 00060 continue; 00061 if(perms[1] == 'w') 00062 touch_area((void*)start,end-start,1); 00063 else 00064 touch_area((void*)start,end-start,0); 00065 } 00066 fclose(maps); 00067 return 0; 00068 } 00069 00070 int lock_all(int stk, int heap) { 00071 extern void dump_malloc_stats(void); 00072 int err, n, i; 00073 unsigned long *pt; 00074 char stack[stk ? stk : GROW_STACK]; 00075 stack[0] = ' '; 00076 00077 /* 00078 glibc (2.1.3) limits and default values: 00079 00080 dynamically tunable * 00081 mmap threshold 128k * 00082 mmap threshold max 512k 00083 mmap max 1024k * 00084 trim threshold 128k * 00085 top pad 0k * 00086 heap min 32k 00087 heap max 1024k 00088 */ 00089 00090 n = heap / 65536 + 1; 00091 if ( n > (sizeof(stack) / sizeof(int))) { 00092 printf("heap too large\n"); 00093 exit(-1); 00094 } 00095 00096 err = mallopt(M_MMAP_THRESHOLD, 512*1024); 00097 if (!err) { 00098 printf("mallopt(M_MMAP_THRESHOLD, heap) failed\n"); 00099 exit(-1); 00100 } 00101 00102 err = mallopt(M_TOP_PAD, heap ? heap : GROW_HEAP); 00103 if (!err) { 00104 printf("mallopt(M_TOP_PAD, heap) failed\n"); 00105 exit(-1); 00106 } 00107 00108 if(mlockall(MCL_CURRENT|MCL_FUTURE)) { 00109 perror("mlockall"); 00110 exit(-1); 00111 } 00112 00113 touch_all(); 00114 00115 pt = (unsigned long *) stack; 00116 for(i=0; i<n ; i++, pt++) *pt = (unsigned long) malloc(65536); 00117 pt = (unsigned long *) stack; 00118 for(i=0; i<n ; i++, pt++) free((void *) *pt); 00119 00120 return 0; 00121 } 00122 00123 void dump_malloc_stats(void) 00124 { 00125 struct mallinfo mi; 00126 extern int rtai_print_to_screen(const char *fmt, ...); 00127 // memset(&mi, 0, sizeof(mi)); 00128 mi = mallinfo(); 00129 00130 rtai_print_to_screen("\ntotal space allocated from system %d\n", mi.arena); 00131 rtai_print_to_screen("number of non-inuse chunks %d\n", mi.ordblks); 00132 rtai_print_to_screen("number of mmapped regions %d\n", mi.hblks); 00133 rtai_print_to_screen("total space in mmapped regions %d\n", mi.hblkhd); 00134 rtai_print_to_screen("total allocated space %d\n", mi.uordblks); 00135 rtai_print_to_screen("total non-inuse space %d\n", mi.fordblks); 00136 rtai_print_to_screen("top-most, releasable space %d\n", mi.keepcost); 00137 } 00138 00139 /* 00140 int main(int argc, char *argv[]) { 00141 sleep(5); 00142 lock_all(0,0); 00143 dump_malloc_stats(); 00144 sleep(5); 00145 return 0; 00146 } 00147 */

Generated on Thu Nov 20 11:49:52 2008 for RTAI API by doxygen 1.3.8