2012-03-11 10:57:13 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
any later version.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
more details.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
2016-02-28 18:40:20 -05:00
|
|
|
memstat.c
|
2013-07-10 09:26:01 -04:00
|
|
|
Prints system memory usage information.
|
2012-03-11 10:57:13 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2015-06-10 17:24:25 -04:00
|
|
|
#include <err.h>
|
2011-11-26 14:14:57 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-28 18:40:20 -05:00
|
|
|
#define BYTES 0
|
|
|
|
#define KIBI 1
|
|
|
|
#define MEBI 2
|
|
|
|
#define GIBI 3
|
|
|
|
#define TEBI 4
|
|
|
|
#define PEBI 5
|
|
|
|
#define EXBI 6
|
|
|
|
|
2011-11-26 14:14:57 -05:00
|
|
|
void printbytes(unsigned long long bytes)
|
|
|
|
{
|
|
|
|
unsigned unit = BYTES;
|
2012-09-08 14:55:43 -04:00
|
|
|
if ( (bytes >> 10ULL) & 1023ULL ) { unit = KIBI; }
|
|
|
|
if ( (bytes >> 20ULL) & 1023ULL ) { unit = MEBI; }
|
|
|
|
if ( (bytes >> 30ULL) & 1023ULL ) { unit = GIBI; }
|
|
|
|
if ( (bytes >> 40ULL) & 1023ULL ) { unit = TEBI; }
|
|
|
|
if ( (bytes >> 50ULL) & 1023ULL ) { unit = PEBI; }
|
|
|
|
if ( (bytes >> 60ULL) & 1023ULL ) { unit = EXBI; }
|
2011-11-26 14:14:57 -05:00
|
|
|
|
|
|
|
switch ( unit )
|
|
|
|
{
|
|
|
|
case EXBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu ZiB ", (bytes >> 60ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
case PEBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu PiB ", (bytes >> 50ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
case TEBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu TiB ", (bytes >> 40ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
case GIBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu GiB ", (bytes >> 30ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
case MEBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu MiB ", (bytes >> 20ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
case KIBI:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu KiB", (bytes >> 10ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
break;
|
|
|
|
case BYTES:
|
2012-09-08 14:55:43 -04:00
|
|
|
printf("%llu B", (bytes >> 0ULL) & 1023ULL);
|
2011-11-26 14:14:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-28 18:40:20 -05:00
|
|
|
int main(void)
|
2011-11-26 14:14:57 -05:00
|
|
|
{
|
|
|
|
size_t memused = 0;
|
|
|
|
size_t memtotal = 0;
|
2015-06-10 17:24:25 -04:00
|
|
|
if ( memstat(&memused, &memtotal) )
|
|
|
|
err(1, "memstat");
|
2011-11-26 14:14:57 -05:00
|
|
|
|
|
|
|
printf("memory usage: ");
|
|
|
|
printbytes(memused);
|
2011-11-26 15:10:08 -05:00
|
|
|
printf(" used / ");
|
2011-11-26 14:14:57 -05:00
|
|
|
printbytes(memtotal);
|
|
|
|
unsigned percent = ((unsigned long long) memused * 100ULL ) / memtotal;
|
|
|
|
printf(" total (%u%s)\n", percent, "%");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|