2012-03-02 17:59:36 +05:30
|
|
|
/*
|
|
|
|
* File for parsing top-level /proc entities.
|
|
|
|
* Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
|
|
|
|
* Copyright 1998-2003 Albert Cahalan
|
|
|
|
* June 2003, Fabian Frederick, disk and slab info
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2013-05-09 05:04:44 +05:30
|
|
|
#include <stdbool.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2002-02-02 05:10:38 +05:30
|
|
|
#include <locale.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2011-12-03 04:47:02 +05:30
|
|
|
#include "alloc.h"
|
2002-06-24 09:59:04 +05:30
|
|
|
#include "version.h"
|
|
|
|
#include "sysinfo.h" /* include self to verify prototypes */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#ifndef HZ
|
|
|
|
#include <netinet/in.h> /* htons */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
long smp_num_cpus; /* number of CPUs */
|
2014-07-17 21:41:11 +05:30
|
|
|
long page_bytes; /* this architecture's page size */
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#define BAD_OPEN_MESSAGE \
|
|
|
|
"Error: /proc must be mounted\n" \
|
|
|
|
" To mount /proc at boot you need an /etc/fstab line like:\n" \
|
2006-06-27 08:37:09 +05:30
|
|
|
" proc /proc proc defaults\n" \
|
|
|
|
" In the meantime, run \"mount proc /proc -t proc\"\n"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
#define STAT_FILE "/proc/stat"
|
|
|
|
static int stat_fd = -1;
|
|
|
|
#define UPTIME_FILE "/proc/uptime"
|
|
|
|
static int uptime_fd = -1;
|
|
|
|
#define LOADAVG_FILE "/proc/loadavg"
|
|
|
|
static int loadavg_fd = -1;
|
|
|
|
#define MEMINFO_FILE "/proc/meminfo"
|
|
|
|
static int meminfo_fd = -1;
|
2002-10-06 22:16:06 +05:30
|
|
|
#define VMINFO_FILE "/proc/vmstat"
|
|
|
|
static int vminfo_fd = -1;
|
2014-07-15 22:47:02 +05:30
|
|
|
#define VM_MIN_FREE_FILE "/proc/sys/vm/min_free_kbytes"
|
|
|
|
static int vm_min_free_fd = -1;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2008-03-24 10:11:26 +05:30
|
|
|
// As of 2.6.24 /proc/meminfo seems to need 888 on 64-bit,
|
|
|
|
// and would need 1258 if the obsolete fields were there.
|
2014-07-14 23:14:14 +05:30
|
|
|
// As of 3.13 /proc/vmstat needs 2623,
|
|
|
|
// and /proc/stat needs 3076.
|
2014-07-14 22:37:25 +05:30
|
|
|
static char buf[8192];
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/* This macro opens filename only if necessary and seeks to 0 so
|
|
|
|
* that successive calls to the functions are more efficient.
|
|
|
|
* It also reads the current contents of the file into the global buf.
|
|
|
|
*/
|
|
|
|
#define FILE_TO_BUF(filename, fd) do{ \
|
|
|
|
static int local_n; \
|
|
|
|
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
|
2005-10-31 05:15:47 +05:30
|
|
|
fputs(BAD_OPEN_MESSAGE, stderr); \
|
2002-02-02 04:17:29 +05:30
|
|
|
fflush(NULL); \
|
|
|
|
_exit(102); \
|
|
|
|
} \
|
|
|
|
lseek(fd, 0L, SEEK_SET); \
|
|
|
|
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
|
|
|
|
perror(filename); \
|
|
|
|
fflush(NULL); \
|
|
|
|
_exit(103); \
|
|
|
|
} \
|
|
|
|
buf[local_n] = '\0'; \
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
/* evals 'x' twice */
|
|
|
|
#define SET_IF_DESIRED(x,y) do{ if(x) *(x) = (y); }while(0)
|
|
|
|
|
2014-07-15 22:47:02 +05:30
|
|
|
/* return minimum of two values */
|
|
|
|
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/***********************************************************************/
|
2002-11-25 15:46:33 +05:30
|
|
|
int uptime(double *restrict uptime_secs, double *restrict idle_secs) {
|
2002-02-02 04:17:29 +05:30
|
|
|
double up=0, idle=0;
|
2009-12-18 09:57:21 +05:30
|
|
|
char *savelocale;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
FILE_TO_BUF(UPTIME_FILE,uptime_fd);
|
2009-12-18 09:57:21 +05:30
|
|
|
savelocale = strdup(setlocale(LC_NUMERIC, NULL));
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC,"C");
|
2002-02-02 04:17:29 +05:30
|
|
|
if (sscanf(buf, "%lf %lf", &up, &idle) < 2) {
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC,savelocale);
|
2009-12-18 09:57:21 +05:30
|
|
|
free(savelocale);
|
2005-10-31 05:15:47 +05:30
|
|
|
fputs("bad data in " UPTIME_FILE "\n", stderr);
|
2002-02-02 05:10:38 +05:30
|
|
|
return 0;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC,savelocale);
|
2009-12-18 09:57:21 +05:30
|
|
|
free(savelocale);
|
2002-02-02 04:17:29 +05:30
|
|
|
SET_IF_DESIRED(uptime_secs, up);
|
|
|
|
SET_IF_DESIRED(idle_secs, idle);
|
|
|
|
return up; /* assume never be zero seconds in practice */
|
|
|
|
}
|
|
|
|
|
2010-12-16 15:00:39 +05:30
|
|
|
unsigned long getbtime(void) {
|
|
|
|
static unsigned long btime = 0;
|
2013-05-09 05:04:44 +05:30
|
|
|
bool found_btime = false;
|
2010-12-16 15:00:39 +05:30
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (btime)
|
|
|
|
return btime;
|
|
|
|
|
|
|
|
/* /proc/stat can get very large on multi-CPU systems so we
|
|
|
|
can't use FILE_TO_BUF */
|
|
|
|
if (!(f = fopen(STAT_FILE, "r"))) {
|
|
|
|
fputs(BAD_OPEN_MESSAGE, stderr);
|
|
|
|
fflush(NULL);
|
|
|
|
_exit(102);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((fgets(buf, sizeof buf, f))) {
|
2013-05-09 05:04:44 +05:30
|
|
|
if (sscanf(buf, "btime %lu", &btime) == 1) {
|
|
|
|
found_btime = true;
|
2010-12-16 15:00:39 +05:30
|
|
|
break;
|
2013-05-09 05:04:44 +05:30
|
|
|
}
|
2010-12-16 15:00:39 +05:30
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
2013-05-09 05:04:44 +05:30
|
|
|
if (!found_btime) {
|
2010-12-16 15:00:39 +05:30
|
|
|
fputs("missing btime in " STAT_FILE "\n", stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return btime;
|
|
|
|
}
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
/***********************************************************************
|
|
|
|
* Some values in /proc are expressed in units of 1/HZ seconds, where HZ
|
|
|
|
* is the kernel clock tick rate. One of these units is called a jiffy.
|
|
|
|
* The HZ value used in the kernel may vary according to hacker desire.
|
|
|
|
* According to Linus Torvalds, this is not true. He considers the values
|
2014-08-06 10:30:00 +05:30
|
|
|
* in /proc as being in architecture-dependent units that have no relation
|
2002-02-02 04:17:29 +05:30
|
|
|
* to the kernel clock tick rate. Examination of the kernel source code
|
|
|
|
* reveals that opinion as wishful thinking.
|
|
|
|
*
|
|
|
|
* In any case, we need the HZ constant as used in /proc. (the real HZ value
|
|
|
|
* may differ, but we don't care) There are several ways we could get HZ:
|
|
|
|
*
|
|
|
|
* 1. Include the kernel header file. If it changes, recompile this library.
|
|
|
|
* 2. Use the sysconf() function. When HZ changes, recompile the C library!
|
|
|
|
* 3. Ask the kernel. This is obviously correct...
|
|
|
|
*
|
|
|
|
* Linus Torvalds won't let us ask the kernel, because he thinks we should
|
|
|
|
* not know the HZ value. Oh well, we don't have to listen to him.
|
|
|
|
* Someone smuggled out the HZ value. :-)
|
|
|
|
*
|
|
|
|
* This code should work fine, even if Linus fixes the kernel to match his
|
|
|
|
* stated behavior. The code only fails in case of a partial conversion.
|
|
|
|
*
|
2002-05-28 08:00:40 +05:30
|
|
|
* Recent update: on some architectures, the 2.4 kernel provides an
|
|
|
|
* ELF note to indicate HZ. This may be for ARM or user-mode Linux
|
|
|
|
* support. This ought to be investigated. Note that sysconf() is still
|
|
|
|
* unreliable, because it doesn't return an error code when it is
|
|
|
|
* used with a kernel that doesn't support the ELF note. On some other
|
|
|
|
* architectures there may be a system call or sysctl() that will work.
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2002-10-07 07:18:47 +05:30
|
|
|
|
2002-05-28 09:48:55 +05:30
|
|
|
unsigned long long Hertz;
|
2002-10-07 07:18:47 +05:30
|
|
|
|
2002-10-07 09:07:48 +05:30
|
|
|
static void old_Hertz_hack(void){
|
2011-10-07 09:05:16 +05:30
|
|
|
unsigned long long user_j, nice_j, sys_j, other_j, wait_j, hirq_j, sirq_j, stol_j; /* jiffies (clock ticks) */
|
2002-02-02 04:17:29 +05:30
|
|
|
double up_1, up_2, seconds;
|
2002-05-29 22:10:03 +05:30
|
|
|
unsigned long long jiffies;
|
|
|
|
unsigned h;
|
2009-12-18 09:57:21 +05:30
|
|
|
char *savelocale;
|
2011-01-19 17:20:26 +05:30
|
|
|
long hz;
|
|
|
|
|
|
|
|
#ifdef _SC_CLK_TCK
|
|
|
|
if((hz = sysconf(_SC_CLK_TCK)) > 0){
|
|
|
|
Hertz = hz;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2002-02-02 05:10:38 +05:30
|
|
|
|
2011-10-07 09:05:16 +05:30
|
|
|
wait_j = hirq_j = sirq_j = stol_j = 0;
|
2009-12-18 09:57:21 +05:30
|
|
|
savelocale = strdup(setlocale(LC_NUMERIC, NULL));
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC, "C");
|
2002-02-02 04:17:29 +05:30
|
|
|
do{
|
|
|
|
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1);
|
|
|
|
/* uptime(&up_1, NULL); */
|
|
|
|
FILE_TO_BUF(STAT_FILE,stat_fd);
|
2011-10-07 09:05:16 +05:30
|
|
|
sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j, &other_j, &wait_j, &hirq_j, &sirq_j, &stol_j);
|
2002-02-02 04:17:29 +05:30
|
|
|
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2);
|
|
|
|
/* uptime(&up_2, NULL); */
|
2002-05-28 09:48:55 +05:30
|
|
|
} while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC, savelocale);
|
2009-12-18 09:57:21 +05:30
|
|
|
free(savelocale);
|
2011-10-07 09:05:16 +05:30
|
|
|
jiffies = user_j + nice_j + sys_j + other_j + wait_j + hirq_j + sirq_j + stol_j ;
|
2002-02-02 04:17:29 +05:30
|
|
|
seconds = (up_1 + up_2) / 2;
|
2002-05-29 22:10:03 +05:30
|
|
|
h = (unsigned)( (double)jiffies/seconds/smp_num_cpus );
|
2002-02-02 04:17:29 +05:30
|
|
|
/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
|
|
|
|
switch(h){
|
2002-05-28 08:00:40 +05:30
|
|
|
case 9 ... 11 : Hertz = 10; break; /* S/390 (sometimes) */
|
|
|
|
case 18 ... 22 : Hertz = 20; break; /* user-mode Linux */
|
2002-02-02 04:17:29 +05:30
|
|
|
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
|
|
|
|
case 48 ... 52 : Hertz = 50; break;
|
2002-05-29 22:10:03 +05:30
|
|
|
case 58 ... 61 : Hertz = 60; break;
|
|
|
|
case 62 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
|
2002-02-02 04:17:29 +05:30
|
|
|
case 95 ... 105 : Hertz = 100; break; /* normal Linux */
|
|
|
|
case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
|
|
|
|
case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
|
2006-09-10 11:55:56 +05:30
|
|
|
case 247 ... 252 : Hertz = 250; break;
|
2002-02-02 04:17:29 +05:30
|
|
|
case 253 ... 260 : Hertz = 256; break;
|
|
|
|
case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
|
|
|
|
case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
|
|
|
|
case 990 ... 1010 : Hertz = 1000; break; /* ARM */
|
|
|
|
case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
|
|
|
|
case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
|
|
|
|
default:
|
|
|
|
#ifdef HZ
|
2002-05-28 09:48:55 +05:30
|
|
|
Hertz = (unsigned long long)HZ; /* <asm/param.h> */
|
2002-02-02 04:17:29 +05:30
|
|
|
#else
|
|
|
|
/* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
|
|
|
|
Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
|
|
|
|
#endif
|
2002-05-29 22:10:03 +05:30
|
|
|
fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-10 21:13:15 +05:30
|
|
|
// same as: euid != uid || egid != gid
|
|
|
|
#ifndef AT_SECURE
|
|
|
|
#define AT_SECURE 23 // secure mode boolean (true if setuid, etc.)
|
|
|
|
#endif
|
|
|
|
|
2002-10-07 07:18:47 +05:30
|
|
|
#ifndef AT_CLKTCK
|
2005-06-10 21:13:15 +05:30
|
|
|
#define AT_CLKTCK 17 // frequency of times()
|
2002-10-07 07:18:47 +05:30
|
|
|
#endif
|
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
#define NOTE_NOT_FOUND 42
|
|
|
|
|
2011-06-04 20:20:01 +05:30
|
|
|
extern char** environ;
|
2002-10-07 07:18:47 +05:30
|
|
|
|
|
|
|
/* for ELF executables, notes are pushed before environment and args */
|
|
|
|
static unsigned long find_elf_note(unsigned long findme){
|
|
|
|
unsigned long *ep = (unsigned long *)environ;
|
|
|
|
while(*ep++);
|
|
|
|
while(*ep){
|
2002-10-10 03:02:33 +05:30
|
|
|
if(ep[0]==findme) return ep[1];
|
2002-10-07 07:18:47 +05:30
|
|
|
ep+=2;
|
|
|
|
}
|
2003-06-08 22:58:06 +05:30
|
|
|
return NOTE_NOT_FOUND;
|
2002-10-07 07:18:47 +05:30
|
|
|
}
|
|
|
|
|
2005-06-10 21:13:15 +05:30
|
|
|
int have_privs;
|
|
|
|
|
|
|
|
static int check_for_privs(void){
|
|
|
|
unsigned long rc = find_elf_note(AT_SECURE);
|
|
|
|
if(rc==NOTE_NOT_FOUND){
|
|
|
|
// not valid to run this code after UID or GID change!
|
|
|
|
// (if needed, may use AT_UID and friends instead)
|
|
|
|
rc = geteuid() != getuid() || getegid() != getgid();
|
|
|
|
}
|
|
|
|
return !!rc;
|
|
|
|
}
|
|
|
|
|
2002-10-07 09:07:48 +05:30
|
|
|
static void init_libproc(void) __attribute__((constructor));
|
|
|
|
static void init_libproc(void){
|
2005-06-10 21:13:15 +05:30
|
|
|
have_privs = check_for_privs();
|
2015-06-19 16:30:46 +05:30
|
|
|
int linux_version_code = procps_linux_version();
|
2011-07-14 16:46:02 +05:30
|
|
|
|
|
|
|
cpuinfo();
|
2014-07-17 21:41:11 +05:30
|
|
|
page_bytes = sysconf(_SC_PAGESIZE);
|
2011-07-14 16:46:02 +05:30
|
|
|
|
2011-10-07 04:38:42 +05:30
|
|
|
#ifdef __linux__
|
2013-03-11 11:30:00 +05:30
|
|
|
if(linux_version_code > LINUX_VERSION(2, 4, 0)){
|
2002-10-14 13:23:08 +05:30
|
|
|
Hertz = find_elf_note(AT_CLKTCK);
|
2003-06-08 22:58:06 +05:30
|
|
|
if(Hertz!=NOTE_NOT_FOUND) return;
|
2014-07-16 10:30:00 +05:30
|
|
|
// fputs("2.4+ kernel w/o ELF notes? -- report this\n", stderr);
|
2002-10-14 13:23:08 +05:30
|
|
|
}
|
2011-10-07 04:38:42 +05:30
|
|
|
#endif /* __linux __ */
|
2011-10-07 04:52:24 +05:30
|
|
|
#if defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
|
|
|
|
/* On FreeBSD the Hertz hack is unrelaible, there is no ELF note and
|
|
|
|
* Hertz isn't defined in asm/params.h
|
|
|
|
* See Debian Bug #460331
|
|
|
|
*/
|
|
|
|
Hertz = 100;
|
|
|
|
return;
|
|
|
|
#endif /* __FreeBSD__ */
|
2002-10-14 13:23:08 +05:30
|
|
|
old_Hertz_hack();
|
2002-10-07 07:18:47 +05:30
|
|
|
}
|
|
|
|
|
2003-09-08 07:09:49 +05:30
|
|
|
#if 0
|
2002-02-02 04:17:29 +05:30
|
|
|
/***********************************************************************
|
|
|
|
* The /proc filesystem calculates idle=jiffies-(user+nice+sys) and we
|
2002-10-06 22:16:06 +05:30
|
|
|
* recover jiffies by adding up the 4 or 5 numbers we are given. SMP kernels
|
2002-02-02 04:17:29 +05:30
|
|
|
* (as of pre-2.4 era) can report idle time going backwards, perhaps due
|
|
|
|
* to non-atomic reads and updates. There is no locking for these values.
|
|
|
|
*/
|
|
|
|
#ifndef NAN
|
|
|
|
#define NAN (-0.0)
|
|
|
|
#endif
|
2002-05-28 09:48:55 +05:30
|
|
|
#define JT unsigned long long
|
2006-06-25 07:45:36 +05:30
|
|
|
void eight_cpu_numbers(double *restrict uret, double *restrict nret, double *restrict sret, double *restrict iret, double *restrict wret, double *restrict xret, double *restrict yret, double *restrict zret){
|
|
|
|
double tmp_u, tmp_n, tmp_s, tmp_i, tmp_w, tmp_x, tmp_y, tmp_z;
|
2002-02-02 04:17:29 +05:30
|
|
|
double scale; /* scale values to % */
|
2006-06-25 07:45:36 +05:30
|
|
|
static JT old_u, old_n, old_s, old_i, old_w, old_x, old_y, old_z;
|
|
|
|
JT new_u, new_n, new_s, new_i, new_w, new_x, new_y, new_z;
|
2002-02-02 04:17:29 +05:30
|
|
|
JT ticks_past; /* avoid div-by-0 by not calling too often :-( */
|
2002-10-09 12:41:08 +05:30
|
|
|
|
|
|
|
tmp_w = 0.0;
|
|
|
|
new_w = 0;
|
2003-09-08 07:09:49 +05:30
|
|
|
tmp_x = 0.0;
|
|
|
|
new_x = 0;
|
|
|
|
tmp_y = 0.0;
|
|
|
|
new_y = 0;
|
2006-06-25 07:45:36 +05:30
|
|
|
tmp_z = 0.0;
|
|
|
|
new_z = 0;
|
2013-03-31 10:30:00 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
FILE_TO_BUF(STAT_FILE,stat_fd);
|
2006-06-25 07:45:36 +05:30
|
|
|
sscanf(buf, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &new_u, &new_n, &new_s, &new_i, &new_w, &new_x, &new_y, &new_z);
|
|
|
|
ticks_past = (new_u+new_n+new_s+new_i+new_w+new_x+new_y+new_z)-(old_u+old_n+old_s+old_i+old_w+old_x+old_y+old_z);
|
2002-02-02 04:17:29 +05:30
|
|
|
if(ticks_past){
|
|
|
|
scale = 100.0 / (double)ticks_past;
|
|
|
|
tmp_u = ( (double)new_u - (double)old_u ) * scale;
|
|
|
|
tmp_n = ( (double)new_n - (double)old_n ) * scale;
|
|
|
|
tmp_s = ( (double)new_s - (double)old_s ) * scale;
|
|
|
|
tmp_i = ( (double)new_i - (double)old_i ) * scale;
|
2002-10-09 12:41:08 +05:30
|
|
|
tmp_w = ( (double)new_w - (double)old_w ) * scale;
|
2003-09-08 07:09:49 +05:30
|
|
|
tmp_x = ( (double)new_x - (double)old_x ) * scale;
|
|
|
|
tmp_y = ( (double)new_y - (double)old_y ) * scale;
|
2006-06-25 07:45:36 +05:30
|
|
|
tmp_z = ( (double)new_z - (double)old_z ) * scale;
|
2002-02-02 04:17:29 +05:30
|
|
|
}else{
|
|
|
|
tmp_u = NAN;
|
|
|
|
tmp_n = NAN;
|
|
|
|
tmp_s = NAN;
|
|
|
|
tmp_i = NAN;
|
2002-10-09 12:41:08 +05:30
|
|
|
tmp_w = NAN;
|
2003-09-08 07:09:49 +05:30
|
|
|
tmp_x = NAN;
|
|
|
|
tmp_y = NAN;
|
2006-06-25 07:45:36 +05:30
|
|
|
tmp_z = NAN;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
SET_IF_DESIRED(uret, tmp_u);
|
|
|
|
SET_IF_DESIRED(nret, tmp_n);
|
|
|
|
SET_IF_DESIRED(sret, tmp_s);
|
|
|
|
SET_IF_DESIRED(iret, tmp_i);
|
2002-10-09 12:41:08 +05:30
|
|
|
SET_IF_DESIRED(wret, tmp_w);
|
2006-06-25 07:45:36 +05:30
|
|
|
SET_IF_DESIRED(xret, tmp_x);
|
|
|
|
SET_IF_DESIRED(yret, tmp_y);
|
|
|
|
SET_IF_DESIRED(zret, tmp_z);
|
2002-02-02 04:17:29 +05:30
|
|
|
old_u=new_u;
|
|
|
|
old_n=new_n;
|
|
|
|
old_s=new_s;
|
|
|
|
old_i=new_i;
|
2002-10-09 12:41:08 +05:30
|
|
|
old_w=new_w;
|
2006-06-25 07:45:36 +05:30
|
|
|
old_x=new_x;
|
|
|
|
old_y=new_y;
|
|
|
|
old_z=new_z;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
#undef JT
|
2003-09-08 07:09:49 +05:30
|
|
|
#endif
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
/***********************************************************************/
|
2002-11-25 15:46:33 +05:30
|
|
|
void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) {
|
2002-02-02 04:17:29 +05:30
|
|
|
double avg_1=0, avg_5=0, avg_15=0;
|
2009-12-18 09:57:21 +05:30
|
|
|
char *savelocale;
|
2013-03-31 10:30:00 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
FILE_TO_BUF(LOADAVG_FILE,loadavg_fd);
|
2009-12-18 09:57:21 +05:30
|
|
|
savelocale = strdup(setlocale(LC_NUMERIC, NULL));
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC, "C");
|
2002-02-02 04:17:29 +05:30
|
|
|
if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
|
2005-10-31 05:15:47 +05:30
|
|
|
fputs("bad data in " LOADAVG_FILE "\n", stderr);
|
2009-12-18 09:57:21 +05:30
|
|
|
free(savelocale);
|
2002-05-28 08:00:40 +05:30
|
|
|
exit(1);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-02-02 05:10:38 +05:30
|
|
|
setlocale(LC_NUMERIC, savelocale);
|
2009-12-18 09:57:21 +05:30
|
|
|
free(savelocale);
|
2002-02-02 04:17:29 +05:30
|
|
|
SET_IF_DESIRED(av1, avg_1);
|
|
|
|
SET_IF_DESIRED(av5, avg_5);
|
|
|
|
SET_IF_DESIRED(av15, avg_15);
|
|
|
|
}
|
|
|
|
|
2003-05-31 06:08:55 +05:30
|
|
|
static char buff[BUFFSIZE]; /* used in the procedures */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
static void crash(const char *filename) {
|
|
|
|
perror(filename);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
static void getrunners(unsigned int *restrict running, unsigned int *restrict blocked) {
|
2014-07-06 19:51:01 +05:30
|
|
|
struct dirent *ent;
|
2003-05-31 06:08:55 +05:30
|
|
|
DIR *proc;
|
|
|
|
|
|
|
|
*running=0;
|
|
|
|
*blocked=0;
|
|
|
|
|
|
|
|
if((proc=opendir("/proc"))==NULL) crash("/proc");
|
|
|
|
|
|
|
|
while(( ent=readdir(proc) )) {
|
2004-07-28 20:49:46 +05:30
|
|
|
char tbuf[32];
|
|
|
|
char *cp;
|
2003-05-31 06:08:55 +05:30
|
|
|
int fd;
|
|
|
|
char c;
|
2004-07-28 20:49:46 +05:30
|
|
|
|
2003-05-31 06:08:55 +05:30
|
|
|
if (!isdigit(ent->d_name[0])) continue;
|
2004-07-28 20:49:46 +05:30
|
|
|
sprintf(tbuf, "/proc/%s/stat", ent->d_name);
|
|
|
|
|
|
|
|
fd = open(tbuf, O_RDONLY, 0);
|
2003-05-31 06:08:55 +05:30
|
|
|
if (fd == -1) continue;
|
2004-07-28 20:49:46 +05:30
|
|
|
memset(tbuf, '\0', sizeof tbuf); // didn't feel like checking read()
|
|
|
|
read(fd, tbuf, sizeof tbuf - 1); // need 32 byte buffer at most
|
2003-05-31 06:08:55 +05:30
|
|
|
close(fd);
|
|
|
|
|
2004-07-28 20:49:46 +05:30
|
|
|
cp = strrchr(tbuf, ')');
|
|
|
|
if(!cp) continue;
|
|
|
|
c = cp[2];
|
|
|
|
|
2003-05-31 06:08:55 +05:30
|
|
|
if (c=='R') {
|
|
|
|
(*running)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c=='D') {
|
|
|
|
(*blocked)++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2006-06-25 07:45:36 +05:30
|
|
|
void getstat(jiff *restrict cuse, jiff *restrict cice, jiff *restrict csys, jiff *restrict cide, jiff *restrict ciow, jiff *restrict cxxx, jiff *restrict cyyy, jiff *restrict czzz,
|
2003-05-31 06:08:55 +05:30
|
|
|
unsigned long *restrict pin, unsigned long *restrict pout, unsigned long *restrict s_in, unsigned long *restrict sout,
|
|
|
|
unsigned *restrict intr, unsigned *restrict ctxt,
|
|
|
|
unsigned int *restrict running, unsigned int *restrict blocked,
|
|
|
|
unsigned int *restrict btime, unsigned int *restrict processes) {
|
|
|
|
static int fd;
|
2004-07-26 19:18:59 +05:30
|
|
|
unsigned long long llbuf = 0;
|
2003-05-31 06:08:55 +05:30
|
|
|
int need_vmstat_file = 0;
|
|
|
|
int need_proc_scan = 0;
|
|
|
|
const char* b;
|
|
|
|
buff[BUFFSIZE-1] = 0; /* ensure null termination in buffer */
|
|
|
|
|
|
|
|
if(fd){
|
|
|
|
lseek(fd, 0L, SEEK_SET);
|
|
|
|
}else{
|
|
|
|
fd = open("/proc/stat", O_RDONLY, 0);
|
|
|
|
if(fd == -1) crash("/proc/stat");
|
|
|
|
}
|
|
|
|
read(fd,buff,BUFFSIZE-1);
|
2013-03-11 11:30:00 +05:30
|
|
|
*intr = 0;
|
2003-05-31 06:08:55 +05:30
|
|
|
*ciow = 0; /* not separated out until the 2.5.41 kernel */
|
2003-09-08 07:09:49 +05:30
|
|
|
*cxxx = 0; /* not separated out until the 2.6.0-test4 kernel */
|
|
|
|
*cyyy = 0; /* not separated out until the 2.6.0-test4 kernel */
|
2006-06-25 07:45:36 +05:30
|
|
|
*czzz = 0; /* not separated out until the 2.6.11 kernel */
|
2003-05-31 06:08:55 +05:30
|
|
|
|
|
|
|
b = strstr(buff, "cpu ");
|
2006-06-25 07:45:36 +05:30
|
|
|
if(b) sscanf(b, "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", cuse, cice, csys, cide, ciow, cxxx, cyyy, czzz);
|
2003-05-31 06:08:55 +05:30
|
|
|
|
|
|
|
b = strstr(buff, "page ");
|
|
|
|
if(b) sscanf(b, "page %lu %lu", pin, pout);
|
|
|
|
else need_vmstat_file = 1;
|
|
|
|
|
|
|
|
b = strstr(buff, "swap ");
|
|
|
|
if(b) sscanf(b, "swap %lu %lu", s_in, sout);
|
|
|
|
else need_vmstat_file = 1;
|
|
|
|
|
|
|
|
b = strstr(buff, "intr ");
|
2004-07-26 19:18:59 +05:30
|
|
|
if(b) sscanf(b, "intr %Lu", &llbuf);
|
|
|
|
*intr = llbuf;
|
2003-05-31 06:08:55 +05:30
|
|
|
|
|
|
|
b = strstr(buff, "ctxt ");
|
2004-07-26 19:18:59 +05:30
|
|
|
if(b) sscanf(b, "ctxt %Lu", &llbuf);
|
|
|
|
*ctxt = llbuf;
|
2003-05-31 06:08:55 +05:30
|
|
|
|
|
|
|
b = strstr(buff, "btime ");
|
|
|
|
if(b) sscanf(b, "btime %u", btime);
|
|
|
|
|
|
|
|
b = strstr(buff, "processes ");
|
|
|
|
if(b) sscanf(b, "processes %u", processes);
|
|
|
|
|
|
|
|
b = strstr(buff, "procs_running ");
|
|
|
|
if(b) sscanf(b, "procs_running %u", running);
|
|
|
|
else need_proc_scan = 1;
|
|
|
|
|
|
|
|
b = strstr(buff, "procs_blocked ");
|
|
|
|
if(b) sscanf(b, "procs_blocked %u", blocked);
|
|
|
|
else need_proc_scan = 1;
|
|
|
|
|
|
|
|
if(need_proc_scan){ /* Linux 2.5.46 (approximately) and below */
|
|
|
|
getrunners(running, blocked);
|
|
|
|
}
|
|
|
|
|
2013-06-11 09:39:05 +05:30
|
|
|
if(*running)
|
|
|
|
(*running)--; // exclude vmstat itself
|
2003-05-31 06:08:55 +05:30
|
|
|
|
|
|
|
if(need_vmstat_file){ /* Linux 2.5.40-bk4 and above */
|
|
|
|
vminfo();
|
|
|
|
*pin = vm_pgpgin;
|
|
|
|
*pout = vm_pgpgout;
|
|
|
|
*s_in = vm_pswpin;
|
|
|
|
*sout = vm_pswpout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
/***********************************************************************/
|
|
|
|
/*
|
|
|
|
* Copyright 1999 by Albert Cahalan; all rights reserved.
|
|
|
|
* This file may be used subject to the terms and conditions of the
|
|
|
|
* GNU Library General Public License Version 2, or any later version
|
|
|
|
* at your option, as published by the Free Software Foundation.
|
|
|
|
* 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 Library General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct mem_table_struct {
|
|
|
|
const char *name; /* memory type name */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long *slot; /* slot in return struct */
|
2002-02-02 04:17:29 +05:30
|
|
|
} mem_table_struct;
|
|
|
|
|
|
|
|
static int compare_mem_table_structs(const void *a, const void *b){
|
2002-10-12 09:55:57 +05:30
|
|
|
return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* example data, following junk, with comments added:
|
|
|
|
*
|
|
|
|
* MemTotal: 61768 kB old
|
|
|
|
* MemFree: 1436 kB old
|
|
|
|
* Buffers: 1312 kB old
|
|
|
|
* Cached: 20932 kB old
|
|
|
|
* Active: 12464 kB new
|
|
|
|
* Inact_dirty: 7772 kB new
|
|
|
|
* Inact_clean: 2008 kB new
|
|
|
|
* Inact_target: 0 kB new
|
2002-12-08 05:44:02 +05:30
|
|
|
* Inact_laundry: 0 kB new, and might be missing too
|
2002-02-02 04:17:29 +05:30
|
|
|
* HighTotal: 0 kB
|
|
|
|
* HighFree: 0 kB
|
|
|
|
* LowTotal: 61768 kB
|
|
|
|
* LowFree: 1436 kB
|
|
|
|
* SwapTotal: 122580 kB old
|
|
|
|
* SwapFree: 60352 kB old
|
2002-10-06 22:16:06 +05:30
|
|
|
* Inactive: 20420 kB 2.5.41+
|
|
|
|
* Dirty: 0 kB 2.5.41+
|
|
|
|
* Writeback: 0 kB 2.5.41+
|
|
|
|
* Mapped: 9792 kB 2.5.41+
|
2013-07-24 00:03:00 +05:30
|
|
|
* Shmem: 28 kB 2.6.32+
|
2002-10-06 22:16:06 +05:30
|
|
|
* Slab: 4564 kB 2.5.41+
|
|
|
|
* Committed_AS: 8440 kB 2.5.41+
|
|
|
|
* PageTables: 304 kB 2.5.41+
|
|
|
|
* ReverseMaps: 5738 2.5.41+
|
2003-01-23 11:18:27 +05:30
|
|
|
* SwapCached: 0 kB 2.5.??+
|
|
|
|
* HugePages_Total: 220 2.5.??+
|
|
|
|
* HugePages_Free: 138 2.5.??+
|
|
|
|
* Hugepagesize: 4096 kB 2.5.??+
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
|
|
|
|
library: properly handle memory used by tmpfs
tmpfs has become much more widely used since distributions use it for
/tmp (Fedora 18+). In /proc/meminfo, memory used by tmpfs is accounted
into "Cached" (aka "NR_FILE_PAGES",
http://lxr.free-electrons.com/source/mm/shmem.c#L301 ).
The tools just pass it on, so what top, free and vmstat report as
"cached" is the sum of page cache and tmpfs.
free has the extremely useful "-/+ buffers/cache" output. However, now
that tmpfs is accounted into "cached", those numbers are way off once
you have big files in /tmp.
Fortunately, kernel 2.6.32 introduces "Shmem", which makes tmpfs memory
usage accessible from userspace (
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4b02108ac1b3354a22b0d83c684797692efdc395 ).
This patch substracts Shmem from Cached to get the actual page cache
memory. This makes both issues mentioned above disappear. For older
kernels, Shmem is not available (hence zero) and this patch is no-op.
Additionally:
* Update the man pages of free and vmstat to explain what is happening
* Finally drop "MemShared" from the /proc/meminfo parser, it has been
dead for 10+ years and is only causing confusion ( removed in kernel
2.5.54, see
https://git.kernel.org/cgit/linux/kernel/git/tglx/history.git/commit/?id=fe04e9451e5a159247cf9f03c615a4273ac0c571 )
2014-02-19 02:42:21 +05:30
|
|
|
/* Shmem in 2.6.32+ */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_main_shared;
|
2002-02-02 04:17:29 +05:30
|
|
|
/* old but still kicking -- the important stuff */
|
2014-07-30 00:39:07 +05:30
|
|
|
static unsigned long kb_page_cache;
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_main_buffers;
|
|
|
|
unsigned long kb_main_free;
|
|
|
|
unsigned long kb_main_total;
|
|
|
|
unsigned long kb_swap_free;
|
|
|
|
unsigned long kb_swap_total;
|
2002-02-02 04:17:29 +05:30
|
|
|
/* recently introduced */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_high_free;
|
|
|
|
unsigned long kb_high_total;
|
|
|
|
unsigned long kb_low_free;
|
|
|
|
unsigned long kb_low_total;
|
2014-07-12 02:04:06 +05:30
|
|
|
unsigned long kb_main_available;
|
2002-02-02 04:17:29 +05:30
|
|
|
/* 2.4.xx era */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_active;
|
|
|
|
unsigned long kb_inact_laundry;
|
|
|
|
unsigned long kb_inact_dirty;
|
|
|
|
unsigned long kb_inact_clean;
|
|
|
|
unsigned long kb_inact_target;
|
2004-08-27 19:53:36 +05:30
|
|
|
unsigned long kb_swap_cached; /* late 2.4 and 2.6+ only */
|
2002-02-02 04:17:29 +05:30
|
|
|
/* derived values */
|
2014-07-30 00:39:07 +05:30
|
|
|
unsigned long kb_main_cached;
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_swap_used;
|
|
|
|
unsigned long kb_main_used;
|
2002-10-06 22:16:06 +05:30
|
|
|
/* 2.5.41+ */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long kb_writeback;
|
|
|
|
unsigned long kb_slab;
|
|
|
|
unsigned long nr_reversemaps;
|
|
|
|
unsigned long kb_committed_as;
|
|
|
|
unsigned long kb_dirty;
|
|
|
|
unsigned long kb_inactive;
|
|
|
|
unsigned long kb_mapped;
|
|
|
|
unsigned long kb_pagetables;
|
2004-08-27 19:53:36 +05:30
|
|
|
// seen on a 2.6.x kernel:
|
2004-08-28 01:51:46 +05:30
|
|
|
static unsigned long kb_vmalloc_chunk;
|
|
|
|
static unsigned long kb_vmalloc_total;
|
|
|
|
static unsigned long kb_vmalloc_used;
|
2008-03-24 10:11:26 +05:30
|
|
|
// seen on 2.6.24-rc6-git12
|
|
|
|
static unsigned long kb_anon_pages;
|
|
|
|
static unsigned long kb_bounce;
|
|
|
|
static unsigned long kb_commit_limit;
|
|
|
|
static unsigned long kb_nfs_unstable;
|
2014-07-30 00:26:20 +05:30
|
|
|
// seen on 2.6.18
|
|
|
|
static unsigned long kb_min_free;
|
|
|
|
// 2.6.19+
|
|
|
|
static unsigned long kb_slab_reclaimable;
|
|
|
|
static unsigned long kb_slab_unreclaimable;
|
|
|
|
// 2.6.27+
|
|
|
|
static unsigned long kb_active_file;
|
|
|
|
static unsigned long kb_inactive_file;
|
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
void meminfo(void){
|
2014-08-05 22:45:03 +05:30
|
|
|
char namebuf[32]; /* big enough to hold any row name */
|
2015-06-19 16:30:46 +05:30
|
|
|
int linux_version_code = procps_linux_version();
|
2002-02-02 04:17:29 +05:30
|
|
|
mem_table_struct findme = { namebuf, NULL};
|
|
|
|
mem_table_struct *found;
|
|
|
|
char *head;
|
|
|
|
char *tail;
|
|
|
|
static const mem_table_struct mem_table[] = {
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Active", &kb_active}, // important
|
2014-07-30 00:26:20 +05:30
|
|
|
{"Active(file)", &kb_active_file},
|
2008-03-24 10:11:26 +05:30
|
|
|
{"AnonPages", &kb_anon_pages},
|
|
|
|
{"Bounce", &kb_bounce},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Buffers", &kb_main_buffers}, // important
|
2014-07-30 00:39:07 +05:30
|
|
|
{"Cached", &kb_page_cache}, // important
|
2008-03-24 10:11:26 +05:30
|
|
|
{"CommitLimit", &kb_commit_limit},
|
2002-10-06 22:16:06 +05:30
|
|
|
{"Committed_AS", &kb_committed_as},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty
|
2002-02-02 04:17:29 +05:30
|
|
|
{"HighFree", &kb_high_free},
|
|
|
|
{"HighTotal", &kb_high_total},
|
|
|
|
{"Inact_clean", &kb_inact_clean},
|
|
|
|
{"Inact_dirty", &kb_inact_dirty},
|
2002-12-08 05:44:02 +05:30
|
|
|
{"Inact_laundry",&kb_inact_laundry},
|
2002-02-02 04:17:29 +05:30
|
|
|
{"Inact_target", &kb_inact_target},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Inactive", &kb_inactive}, // important
|
2014-07-30 00:26:20 +05:30
|
|
|
{"Inactive(file)",&kb_inactive_file},
|
2002-02-02 04:17:29 +05:30
|
|
|
{"LowFree", &kb_low_free},
|
|
|
|
{"LowTotal", &kb_low_total},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped
|
2014-07-12 02:04:06 +05:30
|
|
|
{"MemAvailable", &kb_main_available}, // important
|
2003-01-23 11:18:27 +05:30
|
|
|
{"MemFree", &kb_main_free}, // important
|
|
|
|
{"MemTotal", &kb_main_total}, // important
|
2008-03-24 10:11:26 +05:30
|
|
|
{"NFS_Unstable", &kb_nfs_unstable},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"PageTables", &kb_pagetables}, // kB version of vmstat nr_page_table_pages
|
|
|
|
{"ReverseMaps", &nr_reversemaps}, // same as vmstat nr_page_table_pages
|
2014-07-22 17:22:11 +05:30
|
|
|
{"SReclaimable", &kb_slab_reclaimable}, // "slab reclaimable" (dentry and inode structures)
|
|
|
|
{"SUnreclaim", &kb_slab_unreclaimable},
|
library: properly handle memory used by tmpfs
tmpfs has become much more widely used since distributions use it for
/tmp (Fedora 18+). In /proc/meminfo, memory used by tmpfs is accounted
into "Cached" (aka "NR_FILE_PAGES",
http://lxr.free-electrons.com/source/mm/shmem.c#L301 ).
The tools just pass it on, so what top, free and vmstat report as
"cached" is the sum of page cache and tmpfs.
free has the extremely useful "-/+ buffers/cache" output. However, now
that tmpfs is accounted into "cached", those numbers are way off once
you have big files in /tmp.
Fortunately, kernel 2.6.32 introduces "Shmem", which makes tmpfs memory
usage accessible from userspace (
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=4b02108ac1b3354a22b0d83c684797692efdc395 ).
This patch substracts Shmem from Cached to get the actual page cache
memory. This makes both issues mentioned above disappear. For older
kernels, Shmem is not available (hence zero) and this patch is no-op.
Additionally:
* Update the man pages of free and vmstat to explain what is happening
* Finally drop "MemShared" from the /proc/meminfo parser, it has been
dead for 10+ years and is only causing confusion ( removed in kernel
2.5.54, see
https://git.kernel.org/cgit/linux/kernel/git/tglx/history.git/commit/?id=fe04e9451e5a159247cf9f03c615a4273ac0c571 )
2014-02-19 02:42:21 +05:30
|
|
|
{"Shmem", &kb_main_shared}, // kernel 2.6.32 and later
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Slab", &kb_slab}, // kB version of vmstat nr_slab
|
2002-05-28 08:00:40 +05:30
|
|
|
{"SwapCached", &kb_swap_cached},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"SwapFree", &kb_swap_free}, // important
|
|
|
|
{"SwapTotal", &kb_swap_total}, // important
|
2004-08-27 19:53:36 +05:30
|
|
|
{"VmallocChunk", &kb_vmalloc_chunk},
|
|
|
|
{"VmallocTotal", &kb_vmalloc_total},
|
|
|
|
{"VmallocUsed", &kb_vmalloc_used},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"Writeback", &kb_writeback}, // kB version of vmstat nr_writeback
|
2002-02-02 04:17:29 +05:30
|
|
|
};
|
|
|
|
const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
|
2014-07-15 22:47:02 +05:30
|
|
|
unsigned long watermark_low;
|
2015-06-13 05:30:00 +05:30
|
|
|
signed long mem_available, mem_used;
|
2002-02-02 04:17:29 +05:30
|
|
|
|
|
|
|
FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
|
|
|
|
|
2003-01-23 11:18:27 +05:30
|
|
|
kb_inactive = ~0UL;
|
2014-07-17 21:41:11 +05:30
|
|
|
kb_low_total = kb_main_available = 0;
|
2002-10-06 22:16:06 +05:30
|
|
|
|
2002-02-02 04:17:29 +05:30
|
|
|
head = buf;
|
|
|
|
for(;;){
|
|
|
|
tail = strchr(head, ':');
|
|
|
|
if(!tail) break;
|
|
|
|
*tail = '\0';
|
|
|
|
if(strlen(head) >= sizeof(namebuf)){
|
|
|
|
head = tail+1;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
strcpy(namebuf,head);
|
|
|
|
found = bsearch(&findme, mem_table, mem_table_count,
|
|
|
|
sizeof(mem_table_struct), compare_mem_table_structs
|
|
|
|
);
|
|
|
|
head = tail+1;
|
|
|
|
if(!found) goto nextline;
|
2009-11-26 22:25:22 +05:30
|
|
|
*(found->slot) = (unsigned long)strtoull(head,&tail,10);
|
2002-02-02 04:17:29 +05:30
|
|
|
nextline:
|
|
|
|
tail = strchr(head, '\n');
|
|
|
|
if(!tail) break;
|
|
|
|
head = tail+1;
|
|
|
|
}
|
|
|
|
if(!kb_low_total){ /* low==main except with large-memory support */
|
|
|
|
kb_low_total = kb_main_total;
|
|
|
|
kb_low_free = kb_main_free;
|
|
|
|
}
|
2003-01-23 11:18:27 +05:30
|
|
|
if(kb_inactive==~0UL){
|
2002-12-08 05:44:02 +05:30
|
|
|
kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
|
2002-10-06 22:16:06 +05:30
|
|
|
}
|
2015-10-24 08:45:07 +05:30
|
|
|
kb_main_cached = kb_page_cache + kb_slab_reclaimable;
|
2002-02-02 04:17:29 +05:30
|
|
|
kb_swap_used = kb_swap_total - kb_swap_free;
|
2015-06-13 05:30:00 +05:30
|
|
|
|
|
|
|
/* if kb_main_available is greater than kb_main_total or our calculation of
|
|
|
|
mem_used overflows, that's symptomatic of running within a lxc container
|
|
|
|
where such values will be dramatically distorted over those of the host. */
|
|
|
|
if (kb_main_available > kb_main_total)
|
|
|
|
kb_main_available = kb_main_free;
|
|
|
|
mem_used = kb_main_total - kb_main_free - kb_main_cached - kb_main_buffers;
|
|
|
|
if (mem_used < 0)
|
|
|
|
mem_used = kb_main_total - kb_main_free;
|
|
|
|
kb_main_used = (unsigned long)mem_used;
|
2014-07-15 22:47:02 +05:30
|
|
|
|
|
|
|
/* zero? might need fallback for 2.6.27 <= kernel <? 3.14 */
|
2014-07-19 15:14:44 +05:30
|
|
|
if (!kb_main_available) {
|
2014-07-22 21:48:51 +05:30
|
|
|
if (linux_version_code < LINUX_VERSION(2, 6, 27))
|
2014-07-19 15:14:44 +05:30
|
|
|
kb_main_available = kb_main_free;
|
|
|
|
else {
|
2014-07-30 00:26:20 +05:30
|
|
|
FILE_TO_BUF(VM_MIN_FREE_FILE, vm_min_free_fd);
|
|
|
|
kb_min_free = (unsigned long) strtoull(buf,&tail,10);
|
|
|
|
|
|
|
|
watermark_low = kb_min_free * 5 / 4; /* should be equal to sum of all 'low' fields in /proc/zoneinfo */
|
2014-07-19 15:14:44 +05:30
|
|
|
|
2014-07-30 00:26:20 +05:30
|
|
|
mem_available = (signed long)kb_main_free - watermark_low
|
|
|
|
+ kb_inactive_file + kb_active_file - MIN((kb_inactive_file + kb_active_file) / 2, watermark_low)
|
|
|
|
+ kb_slab_reclaimable - MIN(kb_slab_reclaimable / 2, watermark_low);
|
2014-07-19 15:14:44 +05:30
|
|
|
|
|
|
|
if (mem_available < 0) mem_available = 0;
|
2014-07-30 00:26:20 +05:30
|
|
|
kb_main_available = (unsigned long)mem_available;
|
2014-07-19 15:14:44 +05:30
|
|
|
}
|
2014-07-15 22:47:02 +05:30
|
|
|
}
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2002-10-06 22:16:06 +05:30
|
|
|
|
|
|
|
/*****************************************************************/
|
|
|
|
|
|
|
|
/* read /proc/vminfo only for 2.5.41 and above */
|
|
|
|
|
|
|
|
typedef struct vm_table_struct {
|
|
|
|
const char *name; /* VM statistic name */
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long *slot; /* slot in return struct */
|
2002-10-06 22:16:06 +05:30
|
|
|
} vm_table_struct;
|
|
|
|
|
|
|
|
static int compare_vm_table_structs(const void *a, const void *b){
|
2002-10-12 09:55:57 +05:30
|
|
|
return strcmp(((const vm_table_struct*)a)->name,((const vm_table_struct*)b)->name);
|
2002-10-06 22:16:06 +05:30
|
|
|
}
|
|
|
|
|
2003-01-23 11:18:27 +05:30
|
|
|
// see include/linux/page-flags.h and mm/page_alloc.c
|
|
|
|
unsigned long vm_nr_dirty; // dirty writable pages
|
|
|
|
unsigned long vm_nr_writeback; // pages under writeback
|
2003-03-27 21:58:41 +05:30
|
|
|
unsigned long vm_nr_pagecache; // pages in pagecache -- gone in 2.5.66+ kernels
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long vm_nr_page_table_pages;// pages used for pagetables
|
|
|
|
unsigned long vm_nr_reverse_maps; // includes PageDirect
|
|
|
|
unsigned long vm_nr_mapped; // mapped into pagetables
|
|
|
|
unsigned long vm_nr_slab; // in slab
|
2014-07-15 22:47:02 +05:30
|
|
|
unsigned long vm_nr_slab_reclaimable; // 2.6.19+ kernels
|
|
|
|
unsigned long vm_nr_slab_unreclaimable;// 2.6.19+ kernels
|
|
|
|
unsigned long vm_nr_active_file; // 2.6.27+ kernels
|
|
|
|
unsigned long vm_nr_inactive_file; // 2.6.27+ kernels
|
|
|
|
unsigned long vm_nr_free_pages; // 2.6.21+ kernels
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long vm_pgpgin; // kB disk reads (same as 1st num on /proc/stat page line)
|
|
|
|
unsigned long vm_pgpgout; // kB disk writes (same as 2nd num on /proc/stat page line)
|
|
|
|
unsigned long vm_pswpin; // swap reads (same as 1st num on /proc/stat swap line)
|
|
|
|
unsigned long vm_pswpout; // swap writes (same as 2nd num on /proc/stat swap line)
|
|
|
|
unsigned long vm_pgalloc; // page allocations
|
|
|
|
unsigned long vm_pgfree; // page freeings
|
|
|
|
unsigned long vm_pgactivate; // pages moved inactive -> active
|
|
|
|
unsigned long vm_pgdeactivate; // pages moved active -> inactive
|
|
|
|
unsigned long vm_pgfault; // total faults (major+minor)
|
|
|
|
unsigned long vm_pgmajfault; // major faults
|
|
|
|
unsigned long vm_pgscan; // pages scanned by page reclaim
|
|
|
|
unsigned long vm_pgrefill; // inspected by refill_inactive_zone
|
|
|
|
unsigned long vm_pgsteal; // total pages reclaimed
|
|
|
|
unsigned long vm_kswapd_steal; // pages reclaimed by kswapd
|
2002-12-15 12:00:04 +05:30
|
|
|
// next 3 as defined by the 2.5.52 kernel
|
2003-01-23 11:18:27 +05:30
|
|
|
unsigned long vm_pageoutrun; // times kswapd ran page reclaim
|
|
|
|
unsigned long vm_allocstall; // times a page allocator ran direct reclaim
|
|
|
|
unsigned long vm_pgrotated; // pages rotated to the tail of the LRU for immediate reclaim
|
2004-08-28 01:51:46 +05:30
|
|
|
// seen on a 2.6.8-rc1 kernel, apparently replacing old fields
|
2013-03-11 11:30:00 +05:30
|
|
|
static unsigned long vm_pgalloc_dma; //
|
|
|
|
static unsigned long vm_pgalloc_high; //
|
|
|
|
static unsigned long vm_pgalloc_normal; //
|
|
|
|
static unsigned long vm_pgrefill_dma; //
|
|
|
|
static unsigned long vm_pgrefill_high; //
|
|
|
|
static unsigned long vm_pgrefill_normal; //
|
|
|
|
static unsigned long vm_pgscan_direct_dma; //
|
|
|
|
static unsigned long vm_pgscan_direct_high; //
|
|
|
|
static unsigned long vm_pgscan_direct_normal; //
|
|
|
|
static unsigned long vm_pgscan_kswapd_dma; //
|
|
|
|
static unsigned long vm_pgscan_kswapd_high; //
|
|
|
|
static unsigned long vm_pgscan_kswapd_normal; //
|
|
|
|
static unsigned long vm_pgsteal_dma; //
|
|
|
|
static unsigned long vm_pgsteal_high; //
|
|
|
|
static unsigned long vm_pgsteal_normal; //
|
2004-08-28 01:51:46 +05:30
|
|
|
// seen on a 2.6.8-rc1 kernel
|
|
|
|
static unsigned long vm_kswapd_inodesteal; //
|
|
|
|
static unsigned long vm_nr_unstable; //
|
|
|
|
static unsigned long vm_pginodesteal; //
|
|
|
|
static unsigned long vm_slabs_scanned; //
|
2002-10-06 22:16:06 +05:30
|
|
|
|
|
|
|
void vminfo(void){
|
2014-07-14 22:37:25 +05:30
|
|
|
char namebuf[32]; /* big enough to hold any row name */
|
2002-10-06 22:16:06 +05:30
|
|
|
vm_table_struct findme = { namebuf, NULL};
|
|
|
|
vm_table_struct *found;
|
|
|
|
char *head;
|
|
|
|
char *tail;
|
|
|
|
static const vm_table_struct vm_table[] = {
|
|
|
|
{"allocstall", &vm_allocstall},
|
2004-08-28 01:51:46 +05:30
|
|
|
{"kswapd_inodesteal", &vm_kswapd_inodesteal},
|
2002-10-06 22:16:06 +05:30
|
|
|
{"kswapd_steal", &vm_kswapd_steal},
|
2014-07-15 22:47:02 +05:30
|
|
|
{"nr_active_file", &vm_nr_active_file}, // 2.6.27+ kernels
|
2003-01-23 12:38:00 +05:30
|
|
|
{"nr_dirty", &vm_nr_dirty}, // page version of meminfo Dirty
|
2014-07-15 22:47:02 +05:30
|
|
|
{"nr_free_pages", &vm_nr_free_pages}, // 2.6.21+ kernels
|
|
|
|
{"nr_inactive_file", &vm_nr_inactive_file}, // 2.6.27+ kernels
|
2003-01-23 12:38:00 +05:30
|
|
|
{"nr_mapped", &vm_nr_mapped}, // page version of meminfo Mapped
|
|
|
|
{"nr_page_table_pages", &vm_nr_page_table_pages},// same as meminfo PageTables
|
2003-03-27 21:58:41 +05:30
|
|
|
{"nr_pagecache", &vm_nr_pagecache}, // gone in 2.5.66+ kernels
|
2004-08-27 19:53:36 +05:30
|
|
|
{"nr_reverse_maps", &vm_nr_reverse_maps}, // page version of meminfo ReverseMaps GONE
|
2014-07-15 22:47:02 +05:30
|
|
|
{"nr_slab", &vm_nr_slab}, // page version of meminfo Slab (gone in 2.6.19+)
|
|
|
|
{"nr_slab_reclaimable", &vm_nr_slab_reclaimable},// 2.6.19+ kernels
|
|
|
|
{"nr_slab_unreclaimable",&vm_nr_slab_unreclaimable},// 2.6.19+ kernels
|
2004-08-28 01:51:46 +05:30
|
|
|
{"nr_unstable", &vm_nr_unstable},
|
2003-01-23 12:38:00 +05:30
|
|
|
{"nr_writeback", &vm_nr_writeback}, // page version of meminfo Writeback
|
2002-10-06 22:16:06 +05:30
|
|
|
{"pageoutrun", &vm_pageoutrun},
|
|
|
|
{"pgactivate", &vm_pgactivate},
|
2004-08-27 19:53:36 +05:30
|
|
|
{"pgalloc", &vm_pgalloc}, // GONE (now separate dma,high,normal)
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pgalloc_dma", &vm_pgalloc_dma},
|
|
|
|
{"pgalloc_high", &vm_pgalloc_high},
|
|
|
|
{"pgalloc_normal", &vm_pgalloc_normal},
|
2002-10-06 22:16:06 +05:30
|
|
|
{"pgdeactivate", &vm_pgdeactivate},
|
|
|
|
{"pgfault", &vm_pgfault},
|
|
|
|
{"pgfree", &vm_pgfree},
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pginodesteal", &vm_pginodesteal},
|
2002-10-06 22:16:06 +05:30
|
|
|
{"pgmajfault", &vm_pgmajfault},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"pgpgin", &vm_pgpgin}, // important
|
|
|
|
{"pgpgout", &vm_pgpgout}, // important
|
2004-08-27 19:53:36 +05:30
|
|
|
{"pgrefill", &vm_pgrefill}, // GONE (now separate dma,high,normal)
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pgrefill_dma", &vm_pgrefill_dma},
|
|
|
|
{"pgrefill_high", &vm_pgrefill_high},
|
|
|
|
{"pgrefill_normal", &vm_pgrefill_normal},
|
2002-12-15 12:00:04 +05:30
|
|
|
{"pgrotated", &vm_pgrotated},
|
2004-08-27 19:53:36 +05:30
|
|
|
{"pgscan", &vm_pgscan}, // GONE (now separate direct,kswapd and dma,high,normal)
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pgscan_direct_dma", &vm_pgscan_direct_dma},
|
|
|
|
{"pgscan_direct_high", &vm_pgscan_direct_high},
|
|
|
|
{"pgscan_direct_normal",&vm_pgscan_direct_normal},
|
|
|
|
{"pgscan_kswapd_dma", &vm_pgscan_kswapd_dma},
|
|
|
|
{"pgscan_kswapd_high", &vm_pgscan_kswapd_high},
|
|
|
|
{"pgscan_kswapd_normal",&vm_pgscan_kswapd_normal},
|
2004-08-27 19:53:36 +05:30
|
|
|
{"pgsteal", &vm_pgsteal}, // GONE (now separate dma,high,normal)
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pgsteal_dma", &vm_pgsteal_dma},
|
|
|
|
{"pgsteal_high", &vm_pgsteal_high},
|
|
|
|
{"pgsteal_normal", &vm_pgsteal_normal},
|
2003-01-23 11:18:27 +05:30
|
|
|
{"pswpin", &vm_pswpin}, // important
|
2004-08-28 01:51:46 +05:30
|
|
|
{"pswpout", &vm_pswpout}, // important
|
|
|
|
{"slabs_scanned", &vm_slabs_scanned},
|
2002-10-06 22:16:06 +05:30
|
|
|
};
|
|
|
|
const int vm_table_count = sizeof(vm_table)/sizeof(vm_table_struct);
|
|
|
|
|
2011-04-02 23:29:17 +05:30
|
|
|
#if __SIZEOF_LONG__ == 4
|
|
|
|
unsigned long long slotll;
|
|
|
|
#endif
|
|
|
|
|
2004-08-28 01:51:46 +05:30
|
|
|
vm_pgalloc = 0;
|
|
|
|
vm_pgrefill = 0;
|
|
|
|
vm_pgscan = 0;
|
|
|
|
vm_pgsteal = 0;
|
|
|
|
|
2002-10-06 22:16:06 +05:30
|
|
|
FILE_TO_BUF(VMINFO_FILE,vminfo_fd);
|
|
|
|
|
|
|
|
head = buf;
|
|
|
|
for(;;){
|
|
|
|
tail = strchr(head, ' ');
|
|
|
|
if(!tail) break;
|
|
|
|
*tail = '\0';
|
|
|
|
if(strlen(head) >= sizeof(namebuf)){
|
|
|
|
head = tail+1;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
strcpy(namebuf,head);
|
|
|
|
found = bsearch(&findme, vm_table, vm_table_count,
|
|
|
|
sizeof(vm_table_struct), compare_vm_table_structs
|
|
|
|
);
|
|
|
|
head = tail+1;
|
|
|
|
if(!found) goto nextline;
|
2011-04-02 23:29:17 +05:30
|
|
|
#if __SIZEOF_LONG__ == 4
|
|
|
|
// A 32 bit kernel would have already truncated the value, a 64 bit kernel
|
|
|
|
// doesn't need to. Truncate here to let 32 bit programs to continue to get
|
|
|
|
// truncated values. It's that or change the API for a larger data type.
|
|
|
|
slotll = strtoull(head,&tail,10);
|
|
|
|
*(found->slot) = (unsigned long)slotll;
|
|
|
|
#else
|
2002-10-06 22:16:06 +05:30
|
|
|
*(found->slot) = strtoul(head,&tail,10);
|
2011-04-02 23:29:17 +05:30
|
|
|
#endif
|
2002-10-06 22:16:06 +05:30
|
|
|
nextline:
|
|
|
|
|
|
|
|
//if(found) fprintf(stderr,"%s=%d\n",found->name,*(found->slot));
|
|
|
|
//else fprintf(stderr,"%s not found\n",findme.name);
|
|
|
|
|
|
|
|
tail = strchr(head, '\n');
|
|
|
|
if(!tail) break;
|
|
|
|
head = tail+1;
|
|
|
|
}
|
2004-08-28 01:51:46 +05:30
|
|
|
if(!vm_pgalloc)
|
|
|
|
vm_pgalloc = vm_pgalloc_dma + vm_pgalloc_high + vm_pgalloc_normal;
|
|
|
|
if(!vm_pgrefill)
|
|
|
|
vm_pgrefill = vm_pgrefill_dma + vm_pgrefill_high + vm_pgrefill_normal;
|
|
|
|
if(!vm_pgscan)
|
|
|
|
vm_pgscan = vm_pgscan_direct_dma + vm_pgscan_direct_high + vm_pgscan_direct_normal
|
|
|
|
+ vm_pgscan_kswapd_dma + vm_pgscan_kswapd_high + vm_pgscan_kswapd_normal;
|
|
|
|
if(!vm_pgsteal)
|
|
|
|
vm_pgsteal = vm_pgsteal_dma + vm_pgsteal_high + vm_pgsteal_normal;
|
2002-10-06 22:16:06 +05:30
|
|
|
}
|
2003-05-31 06:08:55 +05:30
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// based on Fabian Frederick's /proc/diskstats parser
|
|
|
|
|
2003-08-11 05:10:41 +05:30
|
|
|
|
|
|
|
unsigned int getpartitions_num(struct disk_stat *disks, int ndisks){
|
|
|
|
int i=0;
|
|
|
|
int partitions=0;
|
|
|
|
|
|
|
|
for (i=0;i<ndisks;i++){
|
|
|
|
partitions+=disks[i].partitions;
|
|
|
|
}
|
|
|
|
return partitions;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-27 17:17:17 +05:30
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
static int is_disk(char *dev)
|
|
|
|
{
|
|
|
|
char syspath[32];
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
while ((slash = strchr(dev, '/')))
|
|
|
|
*slash = '!';
|
|
|
|
snprintf(syspath, sizeof(syspath), "/sys/block/%s", dev);
|
|
|
|
return !(access(syspath, F_OK));
|
|
|
|
}
|
|
|
|
|
2003-08-11 05:10:41 +05:30
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
unsigned int getdiskstat(struct disk_stat **disks, struct partition_stat **partitions){
|
|
|
|
FILE* fd;
|
2004-05-05 04:59:40 +05:30
|
|
|
int cDisk = 0;
|
|
|
|
int cPartition = 0;
|
|
|
|
int fields;
|
|
|
|
unsigned dummy;
|
2011-11-27 17:17:17 +05:30
|
|
|
char devname[32];
|
2003-07-14 17:41:43 +05:30
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
*disks = NULL;
|
|
|
|
*partitions = NULL;
|
2013-03-11 11:30:00 +05:30
|
|
|
buff[BUFFSIZE-1] = 0;
|
2003-06-08 22:58:06 +05:30
|
|
|
fd = fopen("/proc/diskstats", "rb");
|
|
|
|
if(!fd) crash("/proc/diskstats");
|
|
|
|
|
2004-05-05 04:59:40 +05:30
|
|
|
for (;;) {
|
2003-06-08 22:58:06 +05:30
|
|
|
if (!fgets(buff,BUFFSIZE-1,fd)){
|
|
|
|
fclose(fd);
|
2004-05-05 04:59:40 +05:30
|
|
|
break;
|
2003-06-08 22:58:06 +05:30
|
|
|
}
|
2011-11-27 17:17:17 +05:30
|
|
|
fields = sscanf(buff, " %*d %*d %15s %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %u", devname, &dummy);
|
|
|
|
if (fields == 2 && is_disk(devname)){
|
2011-12-03 04:47:02 +05:30
|
|
|
(*disks) = xrealloc(*disks, (cDisk+1)*sizeof(struct disk_stat));
|
2004-05-05 04:59:40 +05:30
|
|
|
sscanf(buff, " %*d %*d %15s %u %u %llu %u %u %u %llu %u %u %u %u",
|
|
|
|
//&disk_major,
|
|
|
|
//&disk_minor,
|
2003-06-08 22:58:06 +05:30
|
|
|
(*disks)[cDisk].disk_name,
|
|
|
|
&(*disks)[cDisk].reads,
|
|
|
|
&(*disks)[cDisk].merged_reads,
|
|
|
|
&(*disks)[cDisk].reads_sectors,
|
|
|
|
&(*disks)[cDisk].milli_reading,
|
|
|
|
&(*disks)[cDisk].writes,
|
|
|
|
&(*disks)[cDisk].merged_writes,
|
|
|
|
&(*disks)[cDisk].written_sectors,
|
|
|
|
&(*disks)[cDisk].milli_writing,
|
|
|
|
&(*disks)[cDisk].inprogress_IO,
|
|
|
|
&(*disks)[cDisk].milli_spent_IO,
|
|
|
|
&(*disks)[cDisk].weighted_milli_spent_IO
|
|
|
|
);
|
2003-08-11 05:10:41 +05:30
|
|
|
(*disks)[cDisk].partitions=0;
|
2003-06-08 22:58:06 +05:30
|
|
|
cDisk++;
|
|
|
|
}else{
|
2011-12-03 04:47:02 +05:30
|
|
|
(*partitions) = xrealloc(*partitions, (cPartition+1)*sizeof(struct partition_stat));
|
2003-06-08 22:58:06 +05:30
|
|
|
fflush(stdout);
|
2011-11-27 17:17:17 +05:30
|
|
|
sscanf(buff, (fields == 2)
|
|
|
|
? " %*d %*d %15s %u %*u %llu %*u %u %*u %llu %*u %*u %*u %*u"
|
|
|
|
: " %*d %*d %15s %u %llu %u %llu",
|
2004-05-05 04:59:40 +05:30
|
|
|
//&part_major,
|
|
|
|
//&part_minor,
|
2003-06-08 22:58:06 +05:30
|
|
|
(*partitions)[cPartition].partition_name,
|
|
|
|
&(*partitions)[cPartition].reads,
|
|
|
|
&(*partitions)[cPartition].reads_sectors,
|
|
|
|
&(*partitions)[cPartition].writes,
|
|
|
|
&(*partitions)[cPartition].requested_writes
|
|
|
|
);
|
2014-01-29 16:52:11 +05:30
|
|
|
|
|
|
|
if (cDisk > 0) {
|
|
|
|
(*partitions)[cPartition++].parent_disk = cDisk-1;
|
|
|
|
(*disks)[cDisk-1].partitions++;
|
|
|
|
}
|
2003-06-08 22:58:06 +05:30
|
|
|
}
|
|
|
|
}
|
2004-05-05 04:59:40 +05:30
|
|
|
|
2003-06-08 22:58:06 +05:30
|
|
|
return cDisk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// based on Fabian Frederick's /proc/slabinfo parser
|
|
|
|
|
|
|
|
unsigned int getslabinfo (struct slab_cache **slab){
|
|
|
|
FILE* fd;
|
|
|
|
int cSlab = 0;
|
2013-03-11 11:30:00 +05:30
|
|
|
buff[BUFFSIZE-1] = 0;
|
2003-06-08 22:58:06 +05:30
|
|
|
*slab = NULL;
|
|
|
|
fd = fopen("/proc/slabinfo", "rb");
|
|
|
|
if(!fd) crash("/proc/slabinfo");
|
|
|
|
while (fgets(buff,BUFFSIZE-1,fd)){
|
|
|
|
if(!memcmp("slabinfo - version:",buff,19)) continue; // skip header
|
|
|
|
if(*buff == '#') continue; // skip comments
|
2011-12-03 04:47:02 +05:30
|
|
|
(*slab) = xrealloc(*slab, (cSlab+1)*sizeof(struct slab_cache));
|
2003-06-08 22:58:06 +05:30
|
|
|
sscanf(buff, "%47s %u %u %u %u", // allow 47; max seen is 24
|
|
|
|
(*slab)[cSlab].name,
|
|
|
|
&(*slab)[cSlab].active_objs,
|
|
|
|
&(*slab)[cSlab].num_objs,
|
|
|
|
&(*slab)[cSlab].objsize,
|
|
|
|
&(*slab)[cSlab].objperslab
|
|
|
|
) ;
|
|
|
|
cSlab++;
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
return cSlab;
|
|
|
|
}
|
|
|
|
|
2003-07-03 10:50:19 +05:30
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
unsigned get_pid_digits(void){
|
2003-07-14 17:41:43 +05:30
|
|
|
char pidbuf[24];
|
2003-07-03 10:50:19 +05:30
|
|
|
char *endp;
|
|
|
|
long rc;
|
|
|
|
int fd;
|
|
|
|
static unsigned ret;
|
|
|
|
|
|
|
|
if(ret) goto out;
|
|
|
|
ret = 5;
|
|
|
|
fd = open("/proc/sys/kernel/pid_max", O_RDONLY);
|
|
|
|
if(fd==-1) goto out;
|
2003-07-14 17:41:43 +05:30
|
|
|
rc = read(fd, pidbuf, sizeof pidbuf);
|
2003-07-03 10:50:19 +05:30
|
|
|
close(fd);
|
|
|
|
if(rc<3) goto out;
|
2003-07-14 17:41:43 +05:30
|
|
|
pidbuf[rc] = '\0';
|
|
|
|
rc = strtol(pidbuf,&endp,10);
|
2003-07-03 10:50:19 +05:30
|
|
|
if(rc<42) goto out;
|
|
|
|
if(*endp && *endp!='\n') goto out;
|
|
|
|
rc--; // the pid_max value is really the max PID plus 1
|
|
|
|
ret = 0;
|
|
|
|
while(rc){
|
|
|
|
rc /= 10;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
2011-07-14 16:46:02 +05:30
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void cpuinfo (void) {
|
|
|
|
// ought to count CPUs in /proc/stat instead of relying
|
|
|
|
// on glibc, which foolishly tries to parse /proc/cpuinfo
|
2011-08-01 16:58:46 +05:30
|
|
|
// note: that may have been the case but now /proc/stat
|
|
|
|
// is the default source. parsing of /proc/cpuinfo
|
|
|
|
// only occurs if the open on /proc/stat fails
|
2011-07-14 16:46:02 +05:30
|
|
|
//
|
|
|
|
// SourceForge has an old Alpha running Linux 2.2.20 that
|
|
|
|
// appears to have a non-SMP kernel on a 2-way SMP box.
|
|
|
|
// _SC_NPROCESSORS_CONF returns 2, resulting in HZ=512
|
|
|
|
// _SC_NPROCESSORS_ONLN returns 1, which should work OK
|
|
|
|
|
|
|
|
smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (smp_num_cpus<1) /* SPARC glibc is buggy */
|
|
|
|
smp_num_cpus=1;
|
|
|
|
}
|