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 <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
00040
if(writeable) {
00041 ptr[i]=tmp;
00042
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
00079
00080
00081
00082
00083
00084
00085
00086
00087
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
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
00141
00142
00143
00144
00145
00146
00147