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 "%13s"
72 #define AVAIL_ARCHVAL "%13iu"
74 #define AVAIL_ARCHSTR "%9s"
75 #define AVAIL_ARCHVAL "%9iu"
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 (__WORDSIZE == 64)
209 if (PutStr("Type Available In-Use Maximum Largest\n") < 0 ||
211 if (PutStr("Type Available In-Use Maximum Largest\n") < 0 ||
213 printm("chip", chip
, 4) < 0 ||
214 printm("fast", fast
, 4) < 0 ||
215 printm("total", total
, 4) < 0)
217 error
= RETURN_ERROR
;
229 if(error
!= RETURN_OK
&& bPrintErr
)
231 PrintFault(IoErr(), "Avail");
238 void fmtlarge(UBYTE
*buf
, IPTR num
)
252 if (num
>= 0x40000000)
254 array
.val
= num
>> 30;
255 d
= ((UQUAD
)num
* 100 + 0x20000000) / 0x40000000;
259 else if (num
>= 0x100000)
261 array
.val
= num
>> 20;
262 d
= ((UQUAD
)num
* 100 + 0x80000) / 0x100000;
266 else if (num
>= 0x400)
268 array
.val
= num
>> 10;
269 d
= (num
* 100 + 0x200) / 0x400;
281 if (!array
.dec
&& (d
> array
.val
* 100))
286 RawDoFmt(array
.dec
? "%iu.%02iu" : "%iu", (RAWARG
)&array
, NULL
, buf
);
287 while (*buf
) { buf
++; }
292 LONG
printm(CONST_STRPTR head
, IPTR
*array
, LONG num
)
300 ULONG len
= 18 - strlen(head
);
301 RawDoFmt(aHuman
? "%%%lds" : "%%%ldiu", (RAWARG
)&len
, NULL
, buf
);
307 fmt
= aHuman
? AVAIL_ARCHSTR
: AVAIL_ARCHVAL
;
314 fmtlarge(buf
, *array
);
323 fmtlarge(tmp
, *array
);
324 res
= Printf(fmt
, tmp
);
328 fmt
= " " AVAIL_ARCHSTR
;
337 res
= VPrintf("%iu", (RAWARG
)array
);
343 res
= VPrintf(fmt
, (RAWARG
)array
);
347 fmt
= " " AVAIL_ARCHVAL
;