ubase/free.c

73 lines
1.4 KiB
C
Raw Normal View History

2013-08-15 15:02:53 +00:00
/* See LICENSE file for copyright and license details. */
#include <sys/sysinfo.h>
2014-06-30 18:03:41 +00:00
2013-08-15 15:02:53 +00:00
#include <stdio.h>
#include <stdlib.h>
2014-06-30 18:03:41 +00:00
2013-08-15 15:02:53 +00:00
#include "util.h"
static unsigned int mem_unit = 1;
static unsigned int unit_shift;
static unsigned long long
scale(unsigned long long v)
{
return (v * mem_unit) >> unit_shift;
}
static void
usage(void)
{
eprintf("usage: %s [-bkmg]\n", argv0);
}
2013-08-15 15:02:53 +00:00
int
main(int argc, char *argv[])
{
struct sysinfo info;
2013-08-16 14:51:37 +00:00
if (sysinfo(&info) < 0)
eprintf("sysinfo:");
2013-08-15 15:02:53 +00:00
mem_unit = info.mem_unit ? info.mem_unit : 1;
ARGBEGIN {
case 'b':
unit_shift = 0;
break;
case 'k':
unit_shift = 10;
break;
case 'm':
unit_shift = 20;
break;
case 'g':
unit_shift = 30;
break;
default:
usage();
} ARGEND;
printf(" %13s%13s%13s%13s%13s\n",
"total",
"used",
"free",
"shared", "buffers");
printf("Mem: ");
printf("%13llu%13llu%13llu%13llu%13llu\n",
scale(info.totalram),
scale(info.totalram - info.freeram),
scale(info.freeram),
scale(info.sharedram),
scale(info.bufferram));
printf("-/+ buffers/cache:");
printf("%13llu%13llu\n",
scale(info.totalram - info.freeram - info.bufferram),
scale(info.freeram + info.bufferram));
printf("Swap:");
printf("%13llu%13llu%13llu\n",
scale(info.totalswap),
scale(info.totalswap - info.freeswap),
scale(info.freeswap));
2014-10-02 22:45:25 +00:00
return 0;
2013-08-15 15:02:53 +00:00
}