00001
00007
00008
00009 #include <kernel.h>
00010 #include <platform.h>
00011 #include <mips.h>
00012 #include <memory.h>
00013 #include <proc.h>
00014 #include <stdio.h>
00015 #include <string.h>
00016
00017 extern char *maxaddr;
00018 extern void *end;
00019 extern void _start(void);
00020
00030 command xsh_memstat(ushort stdin, ushort stdout, ushort stderr,
00031 ushort nargs, char *args[])
00032 {
00033 struct memblock *mptr;
00034 ulong phys = 0;
00035 ulong resrv = 0;
00036 ulong code = 0;
00037 ulong stack = 0;
00038 ulong heap = 0;
00039 ulong free = 0;
00040 long i;
00041
00042
00043 if (nargs == 2 && strncmp(args[1],"--help",6) == 0)
00044 {
00045 fprintf(stdout, "Usage: memstat\n");
00046 fprintf(stdout, "Displays the current memory usage and prints the");
00047 fprintf(stdout, "free list.\n");
00048 fprintf(stdout, "\t--help\t display this help and exit\n");
00049 return OK;
00050 }
00051
00052
00053 if (nargs > 1)
00054 {
00055 fprintf(stderr, "memstat: too many arguments\n");
00056 fprintf(stderr, "Try 'memstat --help' for more information\n");
00057 return SYSERR;
00058 }
00059
00060
00061 phys = (ulong)platform.maxaddr - (ulong)KSEG0_BASE;
00062
00063
00064 resrv = (ulong)_start - (ulong)KSEG0_BASE;
00065
00066
00067 code = (ulong)&end - (ulong)_start;
00068
00069
00070 for (i=0; i<NPROC; i++)
00071 {
00072 if (proctab[i].state != PRFREE)
00073 {
00074 stack += (ulong)proctab[i].stklen;
00075 }
00076 }
00077
00078
00079 for (mptr = freelist.next; mptr != NULL; mptr = mptr->next)
00080 { free += mptr->length; }
00081
00082
00083 heap = phys - resrv - code - stack - free;
00084
00085
00086 fprintf(stdout,"Current System Memory Usage:\n");
00087 fprintf(stdout,"----------------------------\n");
00088 fprintf(stdout,"%10d bytes system area\n", resrv);
00089 fprintf(stdout,"%10d bytes XINU code\n", code);
00090 fprintf(stdout,"%10d bytes stack space\n", stack);
00091 fprintf(stdout,"%10d bytes heap space\n", heap);
00092 fprintf(stdout,"%10d bytes free space\n", free);
00093 fprintf(stdout,"----------------------------\n");
00094 fprintf(stdout,"%10d bytes physical memory\n", phys);
00095
00096
00097 if ( freelist.length != free )
00098 {
00099 fprintf(stderr, "\nPotential memory leak detected:\n");
00100 fprintf(stderr, "\nComputed free memory: %10d bytes\n", free);
00101 fprintf(stderr, "Maintained free memory: %10d bytes\n\n",
00102 freelist.length);
00103 }
00104
00105
00106 fprintf(stdout,"Free List:\n");
00107 fprintf(stdout,"BLOCK START LENGTH \n");
00108 fprintf(stdout,"----------- --------\n");
00109
00110 for (mptr=freelist.next; mptr != NULL; mptr=mptr->next)
00111 { fprintf(stdout,"0x%08X %8d\n", mptr, mptr->length); }
00112
00113 return OK;
00114 }