library: more mem and stat fixes

Make distcheck now succeeds.
Changed some of the binaries to use the new API.
This commit is contained in:
Craig Small 2015-06-24 22:16:16 +10:00
parent 05efbebb66
commit cbf25b93e3
11 changed files with 118 additions and 260 deletions

View File

@ -1,6 +1,5 @@
LIBPROCPS_0 { LIBPROCPS_0 {
global: global:
Hertz;
closeproc; closeproc;
cpuinfo; cpuinfo;
dev_to_tty; dev_to_tty;
@ -61,6 +60,7 @@ global:
uptime; uptime;
sprint_uptime; sprint_uptime;
sprint_uptime_short; sprint_uptime_short;
procps_hertz_get;
procps_linux_version; procps_linux_version;
procps_meminfo_new; procps_meminfo_new;
procps_meminfo_read; procps_meminfo_read;

View File

@ -25,16 +25,15 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <locale.h> #include <locale.h>
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "alloc.h" #include "alloc.h"
#include "version.h" #include "version.h"
#include "sysinfo.h" /* include self to verify prototypes */ #include "sysinfo.h" /* include self to verify prototypes */
#include "procps-private.h"
#ifndef HZ
#include <netinet/in.h> /* htons */
#endif
long smp_num_cpus; /* number of CPUs */ long smp_num_cpus; /* number of CPUs */
long page_bytes; /* this architecture's page size */ long page_bytes; /* this architecture's page size */
@ -122,243 +121,68 @@ unsigned long getbtime(void) {
return btime; return btime;
} }
/*********************************************************************** /*
* procps_hertz_get:
*
*
* Some values in /proc are expressed in units of 1/HZ seconds, where HZ * 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. * 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. * 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
* in /proc as being in architecture-dependent units that have no relation
* 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 * On some architectures, the kernel provides an ELF note to indicate
* may differ, but we don't care) There are several ways we could get HZ: * HZ.
* *
* 1. Include the kernel header file. If it changes, recompile this library. * Returns:
* 2. Use the sysconf() function. When HZ changes, recompile the C library! * The discovered or assumed hertz value
* 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.
*
* 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.
*/ */
PROCPS_EXPORT long procps_hertz_get(void)
unsigned long long Hertz; {
long hz;
#if 0
static void old_Hertz_hack(void){
unsigned long long user_j, nice_j, sys_j, other_j, wait_j, hirq_j, sirq_j, stol_j; /* jiffies (clock ticks) */
double up_1, up_2, seconds;
unsigned long long jiffies;
unsigned h;
char *savelocale;
long hz;
#ifdef _SC_CLK_TCK #ifdef _SC_CLK_TCK
if((hz = sysconf(_SC_CLK_TCK)) > 0){ if ((hz = sysconf(_SC_CLK_TCK)) > 0)
Hertz = hz; return hz;
return;
}
#endif #endif
wait_j = hirq_j = sirq_j = stol_j = 0;
savelocale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C");
do{
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_1);
/* uptime(&up_1, NULL); */
FILE_TO_BUF(STAT_FILE,stat_fd);
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);
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2);
/* uptime(&up_2, NULL); */
} while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
setlocale(LC_NUMERIC, savelocale);
free(savelocale);
jiffies = user_j + nice_j + sys_j + other_j + wait_j + hirq_j + sirq_j + stol_j ;
seconds = (up_1 + up_2) / 2;
h = (unsigned)( (double)jiffies/seconds/smp_num_cpus );
/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
switch(h){
case 9 ... 11 : Hertz = 10; break; /* S/390 (sometimes) */
case 18 ... 22 : Hertz = 20; break; /* user-mode Linux */
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
case 48 ... 52 : Hertz = 50; break;
case 58 ... 61 : Hertz = 60; break;
case 62 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
case 95 ... 105 : Hertz = 100; break; /* normal Linux */
case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
case 247 ... 252 : Hertz = 250; break;
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 #ifdef HZ
Hertz = (unsigned long long)HZ; /* <asm/param.h> */ return(HZ);
#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 #endif
fprintf(stderr, "Unknown HZ value! (%d) Assume %Ld.\n", h, Hertz); /* Last resort, assume 100 */
} return 100;
} }
#endif
// same as: euid != uid || egid != gid // same as: euid != uid || egid != gid
#ifndef AT_SECURE #ifndef AT_SECURE
#define AT_SECURE 23 // secure mode boolean (true if setuid, etc.) #define AT_SECURE 23 // secure mode boolean (true if setuid, etc.)
#endif #endif
#ifndef AT_CLKTCK
#define AT_CLKTCK 17 // frequency of times()
#endif
#define NOTE_NOT_FOUND 42
extern char** environ;
/* 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){
if(ep[0]==findme) return ep[1];
ep+=2;
}
return NOTE_NOT_FOUND;
}
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;
}
static void init_libproc(void) __attribute__((constructor)); static void init_libproc(void) __attribute__((constructor));
static void init_libproc(void){ static void init_libproc(void){
have_privs = check_for_privs();
int linux_version_code = procps_linux_version();
cpuinfo(); cpuinfo();
page_bytes = sysconf(_SC_PAGESIZE); page_bytes = sysconf(_SC_PAGESIZE);
#ifdef __linux__
if(linux_version_code > LINUX_VERSION(2, 4, 0)){
Hertz = find_elf_note(AT_CLKTCK);
if(Hertz!=NOTE_NOT_FOUND) return;
// fputs("2.4+ kernel w/o ELF notes? -- report this\n", stderr);
}
#endif /* __linux __ */
Hertz = 100;
return;
} }
#if 0
/***********************************************************************
* The /proc filesystem calculates idle=jiffies-(user+nice+sys) and we
* recover jiffies by adding up the 4 or 5 numbers we are given. SMP kernels
* (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
#define JT unsigned long long
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;
double scale; /* scale values to % */
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;
JT ticks_past; /* avoid div-by-0 by not calling too often :-( */
tmp_w = 0.0;
new_w = 0;
tmp_x = 0.0;
new_x = 0;
tmp_y = 0.0;
new_y = 0;
tmp_z = 0.0;
new_z = 0;
FILE_TO_BUF(STAT_FILE,stat_fd);
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);
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;
tmp_w = ( (double)new_w - (double)old_w ) * scale;
tmp_x = ( (double)new_x - (double)old_x ) * scale;
tmp_y = ( (double)new_y - (double)old_y ) * scale;
tmp_z = ( (double)new_z - (double)old_z ) * scale;
}else{
tmp_u = NAN;
tmp_n = NAN;
tmp_s = NAN;
tmp_i = NAN;
tmp_w = NAN;
tmp_x = NAN;
tmp_y = NAN;
tmp_z = NAN;
}
SET_IF_DESIRED(uret, tmp_u);
SET_IF_DESIRED(nret, tmp_n);
SET_IF_DESIRED(sret, tmp_s);
SET_IF_DESIRED(iret, tmp_i);
SET_IF_DESIRED(wret, tmp_w);
SET_IF_DESIRED(xret, tmp_x);
SET_IF_DESIRED(yret, tmp_y);
SET_IF_DESIRED(zret, tmp_z);
old_u=new_u;
old_n=new_n;
old_s=new_s;
old_i=new_i;
old_w=new_w;
old_x=new_x;
old_y=new_y;
old_z=new_z;
}
#undef JT
#endif
/***********************************************************************/ /***********************************************************************/
void loadavg(double *restrict av1, double *restrict av5, double *restrict av15) { PROCPS_EXPORT int loadavg(double *restrict av1, double *restrict av5, double *restrict av15)
{
double avg_1=0, avg_5=0, avg_15=0; double avg_1=0, avg_5=0, avg_15=0;
char *savelocale; char *savelocale;
int retval=0;
FILE_TO_BUF(LOADAVG_FILE,loadavg_fd); FILE_TO_BUF(LOADAVG_FILE,loadavg_fd);
savelocale = strdup(setlocale(LC_NUMERIC, NULL)); savelocale = strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC, "C"); setlocale(LC_NUMERIC, "C");
if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) { if (sscanf(buf, "%lf %lf %lf", &avg_1, &avg_5, &avg_15) < 3) {
fputs("bad data in " LOADAVG_FILE "\n", stderr); retval = -ERANGE;
free(savelocale);
exit(1);
} }
setlocale(LC_NUMERIC, savelocale); setlocale(LC_NUMERIC, savelocale);
free(savelocale); free(savelocale);
SET_IF_DESIRED(av1, avg_1); SET_IF_DESIRED(av1, avg_1);
SET_IF_DESIRED(av5, avg_5); SET_IF_DESIRED(av5, avg_5);
SET_IF_DESIRED(av15, avg_15); SET_IF_DESIRED(av15, avg_15);
return retval;
} }
static char buff[BUFFSIZE]; /* used in the procedures */ static char buff[BUFFSIZE]; /* used in the procedures */

View File

@ -6,14 +6,14 @@
__BEGIN_DECLS __BEGIN_DECLS
extern unsigned long long Hertz; /* clock tick frequency */
extern long smp_num_cpus; /* number of CPUs */ extern long smp_num_cpus; /* number of CPUs */
extern int have_privs; /* boolean, true if setuid or similar */ extern int have_privs; /* boolean, true if setuid or similar */
extern long page_bytes; /* this architecture's bytes per page */ extern long page_bytes; /* this architecture's bytes per page */
extern int uptime (double *uptime_secs, double *idle_secs); extern int uptime (double *uptime_secs, double *idle_secs);
extern unsigned long getbtime(void); extern unsigned long getbtime(void);
extern void loadavg(double *av1, double *av5, double *av15); int loadavg(double *av1, double *av5, double *av15);
long procps_hertz_get(void);
/* Shmem in 2.6.32+ */ /* Shmem in 2.6.32+ */
extern unsigned long kb_main_shared; extern unsigned long kb_main_shared;

View File

@ -111,16 +111,14 @@ PROCPS_EXPORT char *sprint_uptime(void)
if (time(&realseconds) < 0) if (time(&realseconds) < 0)
return upbuf; return upbuf;
realtime = localtime(&realseconds); realtime = localtime(&realseconds);
pos = sprintf(upbuf, " %02d:%02d:%02d ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec);
if (uptime(&uptime_secs, &idle_secs) < 0) if (uptime(&uptime_secs, &idle_secs) < 0)
return upbuf; return upbuf;
updays = ((int) uptime_secs / (60*60*24)); updays = ((int) uptime_secs / (60*60*24));
uphours = ((int) uptime_secs / (60*24)) % 24; uphours = ((int) uptime_secs / (60*60)) % 24;
upminutes = ((int) uptime_secs / (60)) % 60; upminutes = ((int) uptime_secs / (60)) % 60;
pos = sprintf(upbuf, "%02d:%02d:%02d up %d %s, ", pos = sprintf(upbuf, " %02d:%02d:%02d up %d %s, ",
realtime->tm_hour, realtime->tm_min, realtime->tm_sec, realtime->tm_hour, realtime->tm_min, realtime->tm_sec,
updays, (updays != 1) ? "days" : "day"); updays, (updays != 1) ? "days" : "day");
if (uphours) if (uphours)

View File

@ -45,6 +45,7 @@
#endif #endif
char *myname; char *myname;
long Hertz;
/* just reports a crash */ /* just reports a crash */
static void signal_handler(int signo){ static void signal_handler(int signo){
@ -323,6 +324,7 @@ static int want_this_proc_pcpu(proc_t *buf){
unsigned long pcpu = 0; unsigned long pcpu = 0;
unsigned long long seconds; unsigned long long seconds;
if(!want_this_proc(buf)) return 0; if(!want_this_proc(buf)) return 0;
used_jiffies = buf->utime + buf->stime; used_jiffies = buf->utime + buf->stime;
@ -611,6 +613,7 @@ int main(int argc, char *argv[]){
atexit(close_stdout); atexit(close_stdout);
myname = strrchr(*argv, '/'); myname = strrchr(*argv, '/');
if (myname) ++myname; else myname = *argv; if (myname) ++myname; else myname = *argv;
Hertz = procps_hertz_get();
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain(PACKAGE, LOCALEDIR);

View File

@ -455,7 +455,6 @@ static const char archdefs[] =
/*********** spew variables ***********/ /*********** spew variables ***********/
void self_info(void){ void self_info(void){
int linux_version_code = procps_linux_version();
fprintf(stderr, fprintf(stderr,
"BSD j %s\n" "BSD j %s\n"
"BSD l %s\n" "BSD l %s\n"
@ -479,11 +478,6 @@ void self_info(void){
); );
fprintf(stderr, "%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION); fprintf(stderr, "%s version %s\n", PACKAGE_NAME, PACKAGE_VERSION);
fprintf(stderr, "Linux version %d.%d.%d\n",
LINUX_VERSION_MAJOR(linux_version_code),
LINUX_VERSION_MINOR(linux_version_code),
LINUX_VERSION_PATCH(linux_version_code)
);
/* __libc_print_version(); */ /* how can we get the run-time version? */ /* __libc_print_version(); */ /* how can we get the run-time version? */
fprintf(stderr, "Compiled with: glibc %d.%d, gcc %d.%d\n\n", fprintf(stderr, "Compiled with: glibc %d.%d, gcc %d.%d\n\n",
__GLIBC__, __GLIBC_MINOR__, __GNUC__, __GNUC_MINOR__ __GLIBC__, __GLIBC_MINOR__, __GNUC__, __GNUC_MINOR__
@ -499,9 +493,9 @@ void self_info(void){
fprintf(stderr, fprintf(stderr,
"personality=0x%08x (from \"%s\")\n" "personality=0x%08x (from \"%s\")\n"
"EUID=%d TTY=%d,%d Hertz=%lld page_size=%d\n", "EUID=%d TTY=%d,%d page_size=%d\n",
personality, saved_personality_text, personality, saved_personality_text,
cached_euid, (int)major(cached_tty), (int)minor(cached_tty), Hertz, cached_euid, (int)major(cached_tty), (int)minor(cached_tty),
(int)(page_size) (int)(page_size)
); );

View File

@ -62,12 +62,15 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/types.h> #include <sys/types.h>
#include "c.h"
#include "../proc/readproc.h" #include "../proc/readproc.h"
#include "../proc/sysinfo.h" #include "../proc/sysinfo.h"
#include "../proc/wchan.h" #include "../proc/wchan.h"
#include "../proc/procps.h" #include "../proc/procps.h"
#include "../proc/devname.h" #include "../proc/devname.h"
#include "../proc/escape.h" #include "../proc/escape.h"
#include <proc/readstat.h>
#include <proc/meminfo.h>
#include "common.h" #include "common.h"
@ -87,6 +90,37 @@ static int wide_signals; /* true if we have room */
static time_t seconds_since_1970; static time_t seconds_since_1970;
static unsigned long page_shift; static unsigned long page_shift;
static unsigned int boot_time;
static unsigned long memory_total;
extern long Hertz;
static void get_boot_time(void)
{
struct procps_stat_info *sys_info;
if (procps_stat_new(&sys_info) < 0)
xerrx(EXIT_FAILURE,
_("Unable to create system stat structure"));
if (procps_stat_read(sys_info,0) < 0)
xerrx(EXIT_FAILURE,
_("Unable to read system stat information"));
boot_time = procps_stat_get(sys_info, PROCPS_STAT_BTIME);
procps_stat_unref(sys_info);
}
static void get_memory_total()
{
struct procps_meminfo *mem_info;
if (procps_meminfo_new(&mem_info) < 0)
xerrx(EXIT_FAILURE,
_("Unable to create meminfo structure"));
if (procps_meminfo_read(mem_info) < 0)
xerrx(EXIT_FAILURE,
_("Unable to read meminfo information"));
memory_total = procps_meminfo_get(mem_info, PROCPS_MEM_TOTAL);
procps_meminfo_unref(mem_info);
}
/*************************************************************************/ /*************************************************************************/
/************ Lots of sort functions, starting with the NOP **************/ /************ Lots of sort functions, starting with the NOP **************/
@ -857,7 +891,7 @@ static int pr_bsdtime(char *restrict const outbuf, const proc_t *restrict const
static int pr_bsdstart(char *restrict const outbuf, const proc_t *restrict const pp){ static int pr_bsdstart(char *restrict const outbuf, const proc_t *restrict const pp){
time_t start; time_t start;
time_t seconds_ago; time_t seconds_ago;
start = getbtime() + pp->start_time / Hertz; start = boot_time + pp->start_time / Hertz;
seconds_ago = seconds_since_1970 - start; seconds_ago = seconds_since_1970 - start;
if(seconds_ago < 0) seconds_ago=0; if(seconds_ago < 0) seconds_ago=0;
if(seconds_ago > 3600*24) strcpy(outbuf, ctime(&start)+4); if(seconds_ago > 3600*24) strcpy(outbuf, ctime(&start)+4);
@ -964,14 +998,14 @@ static int pr_rss(char *restrict const outbuf, const proc_t *restrict const pp){
/* pp->vm_rss * 1000 would overflow on 32-bit systems with 64 GB memory */ /* pp->vm_rss * 1000 would overflow on 32-bit systems with 64 GB memory */
static int pr_pmem(char *restrict const outbuf, const proc_t *restrict const pp){ static int pr_pmem(char *restrict const outbuf, const proc_t *restrict const pp){
unsigned long pmem = 0; unsigned long pmem = 0;
pmem = pp->vm_rss * 1000ULL / kb_main_total; pmem = pp->vm_rss * 1000ULL / memory_total;
if (pmem > 999) pmem = 999; if (pmem > 999) pmem = 999;
return snprintf(outbuf, COLWID, "%2u.%u", (unsigned)(pmem/10), (unsigned)(pmem%10)); return snprintf(outbuf, COLWID, "%2u.%u", (unsigned)(pmem/10), (unsigned)(pmem%10));
} }
static int pr_lstart(char *restrict const outbuf, const proc_t *restrict const pp){ static int pr_lstart(char *restrict const outbuf, const proc_t *restrict const pp){
time_t t; time_t t;
t = getbtime() + pp->start_time / Hertz; t = boot_time + pp->start_time / Hertz;
return snprintf(outbuf, COLWID, "%24.24s", ctime(&t)); return snprintf(outbuf, COLWID, "%24.24s", ctime(&t));
} }
@ -994,7 +1028,7 @@ static int pr_stime(char *restrict const outbuf, const proc_t *restrict const pp
our_time = localtime(&seconds_since_1970); /* not reentrant */ our_time = localtime(&seconds_since_1970); /* not reentrant */
tm_year = our_time->tm_year; tm_year = our_time->tm_year;
tm_yday = our_time->tm_yday; tm_yday = our_time->tm_yday;
t = getbtime() + pp->start_time / Hertz; t = boot_time + pp->start_time / Hertz;
proc_time = localtime(&t); /* not reentrant, this corrupts our_time */ proc_time = localtime(&t); /* not reentrant, this corrupts our_time */
fmt = "%H:%M"; /* 03:02 23:59 */ fmt = "%H:%M"; /* 03:02 23:59 */
if(tm_yday != proc_time->tm_yday) fmt = "%b%d"; /* Jun06 Aug27 */ if(tm_yday != proc_time->tm_yday) fmt = "%b%d"; /* Jun06 Aug27 */
@ -1005,7 +1039,7 @@ static int pr_stime(char *restrict const outbuf, const proc_t *restrict const pp
static int pr_start(char *restrict const outbuf, const proc_t *restrict const pp){ static int pr_start(char *restrict const outbuf, const proc_t *restrict const pp){
time_t t; time_t t;
char *str; char *str;
t = getbtime() + pp->start_time / Hertz; t = boot_time + pp->start_time / Hertz;
str = ctime(&t); str = ctime(&t);
if(str[8]==' ') str[8]='0'; if(str[8]==' ') str[8]='0';
if(str[11]==' ') str[11]='0'; if(str[11]==' ') str[11]='0';
@ -2096,40 +2130,40 @@ void show_one_proc(const proc_t *restrict const p, const format_node *restrict f
} }
void init_output(void){ void init_output(void)
int outbuf_pages; {
char *outbuf; int outbuf_pages;
char *outbuf;
switch(page_size){ switch(page_size) {
case 65536: page_shift = 16; break; case 65536: page_shift = 16; break;
case 32768: page_shift = 15; break; case 32768: page_shift = 15; break;
case 16384: page_shift = 14; break; case 16384: page_shift = 14; break;
case 8192: page_shift = 13; break; case 8192: page_shift = 13; break;
default: fprintf(stderr, _("unknown page size (assume 4096)\n")); default: /* Assume 4096 */
case 4096: page_shift = 12; break; case 4096: page_shift = 12; break;
case 2048: page_shift = 11; break; case 2048: page_shift = 11; break;
case 1024: page_shift = 10; break; case 1024: page_shift = 10; break;
} }
// add page_size-1 to round up // add page_size-1 to round up
outbuf_pages = (OUTBUF_SIZE+SPACE_AMOUNT+page_size-1)/page_size; outbuf_pages = (OUTBUF_SIZE+SPACE_AMOUNT+page_size-1)/page_size;
outbuf = mmap( outbuf = mmap(
0, 0,
page_size * (outbuf_pages+1), // 1 more, for guard page at high addresses page_size * (outbuf_pages+1), // 1 more, for guard page at high addresses
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, MAP_PRIVATE | MAP_ANONYMOUS,
-1, -1,
0 0);
); memset(outbuf, ' ', SPACE_AMOUNT);
memset(outbuf, ' ', SPACE_AMOUNT); if(SPACE_AMOUNT==page_size)
if(SPACE_AMOUNT==page_size) mprotect(outbuf, page_size, PROT_READ); mprotect(outbuf, page_size, PROT_READ);
mprotect(outbuf + page_size*outbuf_pages, page_size, PROT_NONE); // guard page mprotect(outbuf + page_size*outbuf_pages, page_size, PROT_NONE); // guard page
saved_outbuf = outbuf + SPACE_AMOUNT; saved_outbuf = outbuf + SPACE_AMOUNT;
// available space: page_size*outbuf_pages-SPACE_AMOUNT // available space: page_size*outbuf_pages-SPACE_AMOUNT
seconds_since_1970 = time(NULL);
seconds_since_1970 = time(NULL); get_boot_time();
get_memory_total();
meminfo(); check_header_width();
check_header_width();
} }

View File

@ -64,6 +64,7 @@
/*###### Miscellaneous global stuff ####################################*/ /*###### Miscellaneous global stuff ####################################*/
static long Hertz;
/* The original and new terminal definitions /* The original and new terminal definitions
(only set when not in 'Batch' mode) */ (only set when not in 'Batch' mode) */
static struct termios Tty_original, // our inherited terminal definition static struct termios Tty_original, // our inherited terminal definition
@ -3274,6 +3275,7 @@ static void before (char *me) {
// accommodate nls/gettext potential translations // accommodate nls/gettext potential translations
initialize_nls(); initialize_nls();
Hertz = procps_hertz_get();
// establish cpu particulars // establish cpu particulars
#ifdef PRETEND8CPUS #ifdef PRETEND8CPUS
smp_num_cpus = 8; smp_num_cpus = 8;

View File

@ -105,8 +105,8 @@ int main(int argc, char **argv)
} }
if (p) if (p)
printf("%s\n", sprint_uptime());
else
printf("%s\n", sprint_uptime_short()); printf("%s\n", sprint_uptime_short());
else
printf("%s\n", sprint_uptime());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -285,7 +285,7 @@ static void new_format(void)
unsigned int tog = 0; /* toggle switch for cleaner code */ unsigned int tog = 0; /* toggle switch for cleaner code */
unsigned int i; unsigned int i;
unsigned int hz = Hertz; long hz;
jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2], jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2],
cpu_xxx[2], cpu_yyy[2], cpu_sto[2]; cpu_xxx[2], cpu_yyy[2], cpu_sto[2];
jiff duse, dsys, didl, diow, dstl, Div, divo2; jiff duse, dsys, didl, diow, dstl, Div, divo2;
@ -302,6 +302,7 @@ static void new_format(void)
struct procps_meminfo *mem_info; struct procps_meminfo *mem_info;
sleep_half = (sleep_time / 2); sleep_half = (sleep_time / 2);
hz = procps_hertz_get();
new_header(); new_header();
if (procps_vmstat_new(&vm_info) < 0) if (procps_vmstat_new(&vm_info) < 0)

8
w.c
View File

@ -376,7 +376,9 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
unsigned i; unsigned i;
char uname[UT_NAMESIZE + 1] = "", tty[5 + UT_LINESIZE + 1] = "/dev/"; char uname[UT_NAMESIZE + 1] = "", tty[5 + UT_LINESIZE + 1] = "/dev/";
const proc_t *best; const proc_t *best;
long hertz;
hertz = procps_hertz_get();
for (i = 0; i < UT_LINESIZE; i++) for (i = 0; i < UT_LINESIZE; i++)
/* clean up tty if garbled */ /* clean up tty if garbled */
if (isalnum(u->ut_line[i]) || (u->ut_line[i] == '/')) if (isalnum(u->ut_line[i]) || (u->ut_line[i] == '/'))
@ -408,12 +410,12 @@ static void showinfo(utmp_t * u, int formtype, int maxcmd, int from,
printf(" ?xdm? "); printf(" ?xdm? ");
else else
print_time_ival7(idletime(tty), 0, stdout); print_time_ival7(idletime(tty), 0, stdout);
print_time_ival7(jcpu / Hertz, (jcpu % Hertz) * (100. / Hertz), print_time_ival7(jcpu / hertz, (jcpu % hertz) * (100. / hertz),
stdout); stdout);
if (best) { if (best) {
unsigned long long pcpu = best->utime + best->stime; unsigned long long pcpu = best->utime + best->stime;
print_time_ival7(pcpu / Hertz, print_time_ival7(pcpu / hertz,
(pcpu % Hertz) * (100. / Hertz), (pcpu % hertz) * (100. / hertz),
stdout); stdout);
} else } else
printf(" ? "); printf(" ? ");