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 <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     /*  printf("R:%p",ptr+i); */
00041         if(writeable) {
00042             ptr[i]=tmp;
00043     /*      printf("W:%p",ptr+i); */
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     glibc (2.1.3) limits and default values:
00080 
00081     dynamically tunable      *
00082     mmap threshold      128k *
00083     mmap threshold max  512k  
00084     mmap max           1024k *
00085     trim threshold      128k *
00086     top pad               0k *  
00087     heap min             32k
00088     heap max           1024k
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 //  memset(&mi, 0, sizeof(mi)); 
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 int main(int argc, char *argv[]) {
00142     sleep(5);
00143     lock_all(0,0);
00144     dump_malloc_stats();
00145     sleep(5);
00146     return 0;
00147 }
00148 */

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7