2 Copyright © 1995-2016, 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 <aros/cpu.h> // for __WORDSIZE
60 #include <exec/execbase.h>
61 #include <exec/memory.h>
62 #include <proto/exec.h>
64 #include <proto/dos.h>
65 #include <utility/tagitem.h>
68 const TEXT version
[] = "$VER: Avail 42.2 (24.2.2016)\n";
70 #if (__WORDSIZE == 64)
71 #define AVAIL_ARCHSTR "%9s"
72 #define AVAIL_ARCHVAL "%9iu"
74 #define AVAIL_ARCHSTR "%12s"
75 #define AVAIL_ARCHVAL "%12iu"
78 #define ARG_TEMPLATE "CHIP/S,FAST/S,TOTAL/S,FLUSH/S,H=HUMAN/S"
90 LONG
printm(CONST_STRPTR head
, IPTR
*array
, LONG num
);
92 int __nocommandline
= 1;
96 /* Allocate all memory (even for >2G systems), then free it.
97 * This will force all expungable items out of memory
99 static void FlushMem(struct ExecBase
*SysBase
)
103 Mem
= AllocMem(0x7ffffff0, MEMF_PUBLIC
);
106 FreeMem(Mem
, 0x7ffffff0);
112 IPTR args
[NOOFARGS
] = { (IPTR
)FALSE
,
119 BOOL bPrintErr
= TRUE
;
120 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
125 BOOL aChip
= (BOOL
)args
[ARG_CHIP
];
126 BOOL aFast
= (BOOL
)args
[ARG_FAST
];
127 BOOL aTotal
= (BOOL
)args
[ARG_TOTAL
];
128 BOOL aFlush
= (BOOL
)args
[ARG_FLUSH
];
129 aHuman
= (BOOL
)args
[ARG_HUMAN
];
144 IPTR chip
[4], fast
[4], total
[4];
148 FPuts(Output(), "Only one of CHIP, FAST or TOTAL allowed\n");
164 chip
[0] = AvailMem(MEMF_CHIP
);
166 if (printm(NULL
, chip
, 1) < 0)
168 error
= RETURN_ERROR
;
173 fast
[0] = AvailMem(MEMF_FAST
);
175 if (printm(NULL
, fast
, 1) < 0)
177 error
= RETURN_ERROR
;
182 total
[0] = AvailMem(MEMF_ANY
);
184 if (printm(NULL
, total
, 1) < 0)
186 error
= RETURN_ERROR
;
193 chip
[0] = AvailMem(MEMF_CHIP
);
194 chip
[2] = AvailMem(MEMF_CHIP
| MEMF_TOTAL
);
195 chip
[3] = AvailMem(MEMF_CHIP
| MEMF_LARGEST
);
196 chip
[1] = chip
[2] - chip
[0];
197 fast
[0] = AvailMem(MEMF_FAST
);
198 fast
[2] = AvailMem(MEMF_FAST
| MEMF_TOTAL
);
199 fast
[3] = AvailMem(MEMF_FAST
| MEMF_LARGEST
);
200 fast
[1] = fast
[2] - fast
[0];
201 total
[0] = AvailMem(MEMF_ANY
);
202 total
[2] = AvailMem(MEMF_ANY
| MEMF_TOTAL
);
203 total
[3] = AvailMem(MEMF_ANY
| MEMF_LARGEST
);
204 total
[1] = total
[2] - total
[0];
208 if (PutStr("Type Available In-Use Maximum Largest\n") < 0 ||
209 printm("chip", chip
, 4) < 0 ||
210 printm("fast", fast
, 4) < 0 ||
211 printm("total", total
, 4) < 0)
213 error
= RETURN_ERROR
;
225 if(error
!= RETURN_OK
&& bPrintErr
)
227 PrintFault(IoErr(), "Avail");
234 void fmtlarge(UBYTE
*buf
, IPTR num
)
248 if (num
>= 0x40000000)
250 array
.val
= num
>> 30;
251 d
= ((UQUAD
)num
* 100 + 0x20000000) / 0x40000000;
255 else if (num
>= 0x100000)
257 array
.val
= num
>> 20;
258 d
= ((UQUAD
)num
* 100 + 0x80000) / 0x100000;
262 else if (num
>= 0x400)
264 array
.val
= num
>> 10;
265 d
= (num
* 100 + 0x200) / 0x400;
277 if (!array
.dec
&& (d
> array
.val
* 100))
282 RawDoFmt(array
.dec
? "%iu.%02iu" : "%iu", (RAWARG
)&array
, NULL
, buf
);
283 while (*buf
) { buf
++; }
288 LONG
printm(CONST_STRPTR head
, IPTR
*array
, LONG num
)
296 ULONG len
= 16 - strlen(head
);
297 RawDoFmt(aHuman
? "%%%lds" : "%%%ldiu", (RAWARG
)&len
, NULL
, buf
);
303 fmt
= aHuman
? AVAIL_ARCHSTR
: AVAIL_ARCHVAL
;
310 fmtlarge(buf
, *array
);
319 fmtlarge(tmp
, *array
);
320 res
= Printf(fmt
, tmp
);
324 fmt
= " " AVAIL_ARCHSTR
;
333 res
= VPrintf("%iu", (RAWARG
)array
);
339 res
= VPrintf(fmt
, (RAWARG
)array
);
343 fmt
= " " AVAIL_ARCHVAL
;