2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
5 Desc: Avail CLI command
9 /******************************************************************************
14 Avail [CHIP | FAST | TOTAL | FLUSH] [H | HUMAN]
18 CHIP/S, FAST/S, TOTAL/S, FLUSH/S, H=HUMAN/S
26 Give a summary of the memory usage and availability in the system.
27 To free up unused memory that still may be allocated (libraries,
28 devices, fonts and such present in memory but whcih are currently
29 not in use), use the FLUSH option.
33 CHIP -- show only "chip" memory
34 FAST -- show only "fast" memory
35 TOTAL -- show information on memory regardless of type
36 FLUSH -- remove unnecessary things residing in memory
37 HUMAN -- display more human-readable values (gigabytes as "G",
38 megabytes as "M", kilobytes as "K")
44 "Chip" and "fast" memory are associated with the Amiga computer
45 and may not be applicable on your hardware platform.
57 ******************************************************************************/
59 #include <exec/execbase.h>
60 #include <exec/memory.h>
61 #include <proto/exec.h>
63 #include <proto/dos.h>
64 #include <utility/tagitem.h>
67 const TEXT version
[] = "$VER: Avail 42.1 (8.12.2007)\n";
70 #define ARG_TEMPLATE "CHIP/S,FAST/S,TOTAL/S,FLUSH/S,H=HUMAN/S"
82 LONG
printm(CONST_STRPTR head
, ULONG
*array
, LONG num
);
84 int __nocommandline
= 1;
88 /* Allocate all memory (even for >2G systems), then free it.
89 * This will force all expungable items out of memory
91 static void FlushMem(struct ExecBase
*SysBase
)
95 Mem
= AllocMem(0x7ffffff0, MEMF_PUBLIC
);
98 FreeMem(Mem
, 0x7ffffff0);
104 IPTR args
[NOOFARGS
] = { (IPTR
)FALSE
,
111 BOOL bPrintErr
= TRUE
;
112 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
117 BOOL aChip
= (BOOL
)args
[ARG_CHIP
];
118 BOOL aFast
= (BOOL
)args
[ARG_FAST
];
119 BOOL aTotal
= (BOOL
)args
[ARG_TOTAL
];
120 BOOL aFlush
= (BOOL
)args
[ARG_FLUSH
];
121 aHuman
= (BOOL
)args
[ARG_HUMAN
];
136 ULONG chip
[4], fast
[4], total
[4];
140 FPuts(Output(), "Only one of CHIP, FAST or TOTAL allowed\n");
156 chip
[0] = AvailMem(MEMF_CHIP
);
158 if (printm(NULL
, chip
, 1) < 0)
160 error
= RETURN_ERROR
;
165 fast
[0] = AvailMem(MEMF_FAST
);
167 if (printm(NULL
, fast
, 1) < 0)
169 error
= RETURN_ERROR
;
174 total
[0] = AvailMem(MEMF_ANY
);
176 if (printm(NULL
, total
, 1) < 0)
178 error
= RETURN_ERROR
;
185 chip
[0] = AvailMem(MEMF_CHIP
);
186 chip
[2] = AvailMem(MEMF_CHIP
| MEMF_TOTAL
);
187 chip
[3] = AvailMem(MEMF_CHIP
| MEMF_LARGEST
);
188 chip
[1] = chip
[2] - chip
[0];
189 fast
[0] = AvailMem(MEMF_FAST
);
190 fast
[2] = AvailMem(MEMF_FAST
| MEMF_TOTAL
);
191 fast
[3] = AvailMem(MEMF_FAST
| MEMF_LARGEST
);
192 fast
[1] = fast
[2] - fast
[0];
193 total
[0] = AvailMem(MEMF_ANY
);
194 total
[2] = AvailMem(MEMF_ANY
| MEMF_TOTAL
);
195 total
[3] = AvailMem(MEMF_ANY
| MEMF_LARGEST
);
196 total
[1] = total
[2] - total
[0];
200 if (PutStr("Type Available In-Use Maximum Largest\n") < 0 ||
201 printm("chip", chip
, 4) < 0 ||
202 printm("fast", fast
, 4) < 0 ||
203 printm("total", total
, 4) < 0)
205 error
= RETURN_ERROR
;
217 if(error
!= RETURN_OK
&& bPrintErr
)
219 PrintFault(IoErr(), "Avail");
226 void fmtlarge(UBYTE
*buf
, ULONG num
)
240 if (num
>= 1073741824)
242 array
.val
= num
>> 30;
243 d
= ((UQUAD
)num
* 10 + 536870912) / 1073741824;
247 else if (num
>= 1048576)
249 array
.val
= num
>> 20;
250 d
= ((UQUAD
)num
* 10 + 524288) / 1048576;
254 else if (num
>= 1024)
256 array
.val
= num
>> 10;
257 d
= (num
* 10 + 512) / 1024;
269 if (!array
.dec
&& (d
> array
.val
* 10))
274 RawDoFmt(array
.dec
? "%lu.%lu" : "%lu", &array
, NULL
, buf
);
275 while (*buf
) { buf
++; }
280 LONG
printm(CONST_STRPTR head
, ULONG
*array
, LONG num
)
288 LONG len
= 16 - strlen(head
);
289 RawDoFmt(aHuman
? "%%%lds" : "%%%ldlu", &len
, NULL
, buf
);
295 fmt
= aHuman
? "%9s" : "%9lu";
302 fmtlarge(buf
, *array
);
311 fmtlarge(tmp
, *array
);
312 res
= Printf(fmt
, (IPTR
) tmp
);
325 res
= VPrintf("%lu", (IPTR
*)array
);
331 res
= VPrintf(fmt
, (IPTR
*)array
);