XINU
xsh_memstat.c
Go to the documentation of this file.
1 /* xsh_memstat.c - xsh_memstat */
2 
3 #include <xinu.h>
4 #include <stdio.h>
5 #include <string.h>
6 
7 static void printMemUse(void);
8 static void printFreeList(void);
9 
10 /*------------------------------------------------------------------------
11  * xsh_memstat - Print statistics about memory use and dump the free list
12  *------------------------------------------------------------------------
13  */
14 shellcmd xsh_memstat(int nargs, char *args[])
15 {
16 
17  /* For argument '--help', emit help about the 'memstat' command */
18 
19  if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
20  printf("use: %s \n\n", args[0]);
21  printf("Description:\n");
22  printf("\tDisplays the current memory use and prints the\n");
23  printf("\tfree list.\n");
24  printf("Options:\n");
25  printf("\t--help\t\tdisplay this help and exit\n");
26  return 0;
27  }
28 
29  /* Check for valid number of arguments */
30 
31  if (nargs > 1) {
32  fprintf(stderr, "%s: too many arguments\n", args[0]);
33  fprintf(stderr, "Try '%s --help' for more information\n",
34  args[0]);
35  return 1;
36  }
37 
38  printMemUse();
39  printFreeList();
40 
41  return 0;
42 }
43 
44 
45 /*------------------------------------------------------------------------
46  * printFreeList - Walk the list of free memory blocks and print the
47  * location and size of each
48  *------------------------------------------------------------------------
49  */
50 static void printFreeList(void)
51 {
52  struct memblk *block;
53 
54  /* Output a heading for the free list */
55 
56  printf("Free List:\n");
57  printf("Block address Length (dec) Length (hex)\n");
58  printf("------------- ------------ ------------\n");
59 
60  for (block = memlist.mnext; block != NULL; block = block->mnext) {
61  printf(" 0x%08x %9d 0x%08x\n", block,
62  block->mlength, block->mlength);
63  }
64  printf("\n");
65 }
66 
67 extern void start(void);
68 extern void *_end;
69 
70 /*------------------------------------------------------------------------
71  * printMemUse - Print statistics about memory use
72  *------------------------------------------------------------------------
73  */
74 static void printMemUse(void)
75 {
76  int i; /* Index into process table */
77  uint32 code = 0; /* Total Xinu code memory */
78  uint32 stack = 0; /* Total used stack memory */
79  uint32 kheap = 0; /* Free kernel heap memory */
80  uint32 kfree = 0; /* Total free memory */
81  struct memblk *block; /* Ptr to memory block */
82 
83  /* Calculate amount of text memory */
84 
85  code = (uint32)&_end - (uint32)start;
86 
87  /* Calculate amount of allocated stack memory */
88  /* Skip the NULL process since it has a private stack */
89 
90  for (i = 1; i < NPROC; i++) {
91  if (proctab[i].prstate != PR_FREE) {
92  stack += (uint32)proctab[i].prstklen;
93  }
94  }
95 
96  /* Calculate the amount of memory on the free list */
97 
98  for (block = memlist.mnext; block != NULL; block = block->mnext) {
99  kfree += block->mlength;
100  }
101 
102  /* Calculate the amount of free kernel heap memory */
103 
104  kheap = kfree - stack;
105 
106  /* Output statistics on current memory use */
107 
108  printf("Current system memory statistics:\n");
109  printf("---------------------------------\n");
110  printf("%10d bytes (0x%08x) of Xinu code\n", code, code);
111  printf("%10d bytes (0x%08x) of allocated stack space\n", stack, stack);
112  printf("%10d bytes (0x%08x) of available kernel heap space\n\n", kheap, kheap);
113 }
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
メモリブロックを管理するための構造体。
Definition: memory.h:53
int32 strncmp(const char *, const char *, int32)
static void printMemUse(void)
Definition: xsh_memstat.c:74
全てのシステムヘッダファイルをインクルードする。
static void printFreeList(void)
Definition: xsh_memstat.c:50
#define stderr
Definition: stdio.h:17
struct memblk * mnext
次のフリーメモリブロックへのポインタ
Definition: memory.h:56
int32 printf(const char *,...)
Definition: printf.c:13
struct memblk memlist
フリーメモリリストの先頭
Definition: initialize.c:27
#define NPROC
Definition: conf.h:79
void * _end
void start(void)
#define PR_FREE
プロセステーブルエントリが使用されていない状態。
Definition: process.h:35
uint32 mlength
memblk構造体のサイズを含むブロックサイズ
Definition: memory.h:58
struct procent proctab[]
プロセステーブル。
Definition: initialize.c:23
int32 shellcmd
シェルコール関数 返り値の型
Definition: kernel.h:51
int32 fprintf(int, char *,...)
Definition: fprintf.c:14
shellcmd xsh_memstat(int nargs, char *args[])
Definition: xsh_memstat.c:14
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15