* 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.
*/
-unsigned long Hertz;
+unsigned long long Hertz;
static void init_Hertz_value(void) __attribute__((constructor));
static void init_Hertz_value(void){
- unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
+ unsigned long long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
double up_1, up_2, seconds;
- unsigned long jiffies, h;
+ unsigned long long jiffies, h;
char *savelocale;
smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j);
FILE_TO_BUF(UPTIME_FILE,uptime_fd); sscanf(buf, "%lf", &up_2);
/* uptime(&up_2, NULL); */
- } while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
+ } while((long long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
setlocale(LC_NUMERIC, savelocale);
jiffies = user_j + nice_j + sys_j + other_j;
seconds = (up_1 + up_2) / 2;
- h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus );
+ h = (unsigned long long)( (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 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
default:
#ifdef HZ
- Hertz = (unsigned long)HZ; /* <asm/param.h> */
+ Hertz = (unsigned long long)HZ; /* <asm/param.h> */
#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;
#ifndef NAN
#define NAN (-0.0)
#endif
-#define JT unsigned long
+#define JT unsigned long long
void four_cpu_numbers(double *uret, double *nret, double *sret, double *iret){
double tmp_u, tmp_n, tmp_s, tmp_i;
double scale; /* scale values to % */
/* elapsed wall clock time, [[dd-]hh:]mm:ss format (not same as "time") */
static int pr_etime(void){
- unsigned t, dd,hh,mm,ss;
+ unsigned long long t;
+ unsigned dd,hh,mm,ss;
char *cp = outbuf;
- t = (
- ((unsigned long)seconds_since_boot)
- - ((unsigned long)pp->start_time)
- / Hertz
- );
+ t = seconds_since_boot - pp->start_time / Hertz;
ss = t%60;
t /= 60;
mm = t%60;
/* "Processor utilisation for scheduling." --- we use %cpu w/o fraction */
static int pr_c(void){
- unsigned long total_time; /* jiffies used by this process */
- unsigned long pcpu = 0; /* scaled %cpu, 999 means 99.9% */
- unsigned long seconds; /* seconds of process life */
+ unsigned long long total_time; /* jiffies used by this process */
+ unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */
+ unsigned long long seconds; /* seconds of process life */
total_time = pp->utime + pp->stime;
if(include_dead_children) total_time += (pp->cutime + pp->cstime);
- seconds =
- seconds_since_boot - ((unsigned long)pp->start_time) / Hertz
- ;
- /* Use 100ULL (not 100) to avoid 32-bit overflow. */
+ seconds = seconds_since_boot - pp->start_time / Hertz;
if(seconds) pcpu = (total_time * 100ULL / Hertz) / seconds;
- if (pcpu > 99) pcpu = 99;
- return snprintf(outbuf, COLWID, "%2u", (unsigned)pcpu);
+ if (pcpu > 99U) pcpu = 99U;
+ return snprintf(outbuf, COLWID, "%2u", pcpu);
}
/* normal %CPU in ##.# format. */
static int pr_pcpu(void){
- unsigned long total_time; /* jiffies used by this process */
- unsigned long pcpu = 0; /* scaled %cpu, 999 means 99.9% */
- unsigned long seconds; /* seconds of process life */
+ unsigned long long total_time; /* jiffies used by this process */
+ unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */
+ unsigned long long seconds; /* seconds of process life */
total_time = pp->utime + pp->stime;
if(include_dead_children) total_time += (pp->cutime + pp->cstime);
- seconds =
- seconds_since_boot - ((unsigned long)pp->start_time) / Hertz
- ;
- /* Use 1000ULL (not 1000) to avoid 32-bit overflow. */
+ seconds = seconds_since_boot - pp->start_time / Hertz;
if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds;
- if (pcpu > 999) pcpu = 999;
- return snprintf(outbuf, COLWID, "%2u.%u", (unsigned)(pcpu/10), (unsigned)(pcpu%10));
+ if (pcpu > 999U) pcpu = 999U;
+ return snprintf(outbuf, COLWID, "%2u.%u", pcpu/10U, pcpu%10U);
}
/* this is a "per-mill" format, like %cpu with no decimal point */
static int pr_cp(void){
- unsigned long total_time; /* jiffies used by this process */
- unsigned long pcpu = 0; /* scaled %cpu, 999 means 99.9% */
- unsigned long seconds; /* seconds of process life */
+ unsigned long long total_time; /* jiffies used by this process */
+ unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */
+ unsigned long long seconds; /* seconds of process life */
total_time = pp->utime + pp->stime;
if(include_dead_children) total_time += (pp->cutime + pp->cstime);
- seconds =
- seconds_since_boot - ((unsigned long)pp->start_time) / Hertz
- ;
- /* Use 1000ULL (not 1000) to avoid 32-bit overflow. */
+ seconds = seconds_since_boot - pp->start_time / Hertz ;
if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds;
- if (pcpu > 999) pcpu = 999;
- return snprintf(outbuf, COLWID, "%3u", (unsigned)pcpu);
+ if (pcpu > 999U) pcpu = 999U;
+ return snprintf(outbuf, COLWID, "%3u", pcpu);
}
static int pr_pgid(void){
/* cumulative CPU time, [dd-]hh:mm:ss format (not same as "etime") */
static int pr_time(void){
- unsigned t, dd,hh,mm,ss;
+ unsigned long long t;
+ unsigned dd,hh,mm,ss;
int c;
- t = (unsigned)(
- (
- (unsigned long)(pp->utime)
- +
- (unsigned long)(pp->stime)
- )
- /
- (unsigned long)Hertz
- );
+ t = (pp->utime + pp->stime) / Hertz;
ss = t%60;
t /= 60;
mm = t%60;
*/
static int pr_ruser(void){
- if(user_is_number || (strlen(pp->ruser)>max_rightward))
- return snprintf(outbuf, COLWID, "%d", pp->ruid);
- return snprintf(outbuf, COLWID, "%s", pp->ruser);
+ int width = COLWID;
+
+ if(user_is_number)
+ return snprintf(outbuf, COLWID, "%d", pp->ruid);
+ if (strlen(pp->ruser)>max_rightward)
+ width = max_rightward;
+ return snprintf(outbuf, width, "%s", pp->ruser);
}
static int pr_egroup(void){
if(strlen(pp->egroup)>max_rightward) return snprintf(outbuf, COLWID, "%d", pp->egid);
return snprintf(outbuf, COLWID, "%s", pp->rgroup);
}
static int pr_euser(void){
- if(user_is_number || (strlen(pp->euser)>max_rightward)) return snprintf(outbuf, COLWID, "%d", pp->euid);
- return snprintf(outbuf, COLWID, "%s", pp->euser);
+ int width = COLWID;
+ if(user_is_number)
+ return snprintf(outbuf, COLWID, "%d", pp->euid);
+ if (strlen(pp->euser)>max_rightward)
+ width = max_rightward;
+ return snprintf(outbuf, width, "%s", pp->euser);
}
/********* maybe standard (Unix98 only defines the header) **********/
}
/* This function helps print old-style time formats */
-static int old_time_helper(char *dst, unsigned long t, unsigned long rel) {
+static int old_time_helper(char *dst, unsigned long long t, unsigned long long rel) {
if(!t) return snprintf(dst, COLWID, " -");
- if((long)t == -1) return snprintf(dst, COLWID, " xx");
- if((long)(t-=rel) < 0) t=0;
- if(t>9999) return snprintf(dst, COLWID, "%5lu", t/100);
- else return snprintf(dst, COLWID, "%2lu.%02lu", t/100, t%100);
+ if(t == ~0ULL) return snprintf(dst, COLWID, " xx");
+ if((long long)(t-=rel) < 0) t=0ULL;
+ if(t>9999ULL) return snprintf(dst, COLWID, "%5Lu", t/100ULL);
+ else return snprintf(dst, COLWID, "%2lu.%02lu", (unsigned)t/100U, (unsigned)t%100U);
}
static int pr_bsdtime(void){
- unsigned long t;
+ unsigned long long t;
+ unsigned u;
t = pp->utime + pp->stime;
if(include_dead_children) t += (pp->cutime + pp->cstime);
- t /= Hertz;
- return snprintf(outbuf, COLWID, "%3ld:%02d", t/60, (int)(t%60));
+ u = t / Hertz;
+ return snprintf(outbuf, COLWID, "%3u:%02u", u/60U, u%60U);
}
static int pr_bsdstart(void){
time_t start;
time_t seconds_ago;
- start = time_of_boot + pp->start_time/Hertz;
+ start = time_of_boot + pp->start_time / Hertz;
seconds_ago = seconds_since_1970 - start;
if(seconds_ago < 0) seconds_ago=0;
if(seconds_ago > 3600*24) strcpy(outbuf, ctime(&start)+4);
}
static int pr_alarm(void){
- return old_time_helper(outbuf, pp->it_real_value, 0);
+ return old_time_helper(outbuf, pp->it_real_value, 0ULL);
}
/* HP-UX puts this in pages and uses "vsz" for kB */
static int pr_lstart(void){
time_t t;
- t = (
- ((unsigned long)time_of_boot)
- + ((unsigned long)pp->start_time)
- / Hertz
- );
+ t = time_of_boot + pp->start_time / Hertz;
return snprintf(outbuf, COLWID, "%24.24s", ctime(&t));
}
our_time = localtime(&seconds_since_1970); /* not reentrant */
tm_year = our_time->tm_year;
tm_yday = our_time->tm_yday;
- t = (time_t)(
- ((unsigned long)time_of_boot)
- + ((unsigned long)pp->start_time)
- / Hertz
- );
+ t = time_of_boot + pp->start_time / Hertz;
proc_time = localtime(&t); /* not reentrant, this corrupts our_time */
fmt = "%H:%M"; /* 03:02 23:59 */
if(tm_yday != proc_time->tm_yday) fmt = "%b%d"; /* Jun06 Aug27 */
static int pr_start(void){
time_t t;
char *str;
- t = (
- ((unsigned long)time_of_boot)
- + ((unsigned long)pp->start_time)
- / Hertz
- );
+ t = time_of_boot + pp->start_time / Hertz;
str = ctime(&t);
if(str[8]==' ') str[8]='0';
if(str[11]==' ') str[11]='0';
return snprintf(outbuf, COLWID, "%s", pp->sgroup);
}
static int pr_fuser(void){
- if(user_is_number || (strlen(pp->fuser)>max_rightward)) return snprintf(outbuf, COLWID, "%d", pp->fuid);
- return snprintf(outbuf, COLWID, "%s", pp->fuser);
+ int width = COLWID;
+
+ if(user_is_number)
+ return snprintf(outbuf, COLWID, "%d", pp->fuid);
+ if (strlen(pp->fuser)>max_rightward)
+ width = max_rightward;
+ return snprintf(outbuf, width, "%s", pp->fuser);
}
static int pr_suser(void){
- if(user_is_number || (strlen(pp->suser)>max_rightward)) return snprintf(outbuf, COLWID, "%d", pp->suid);
- return snprintf(outbuf, COLWID, "%s", pp->suser);
+ int width = COLWID;
+
+ if(user_is_number)
+ return snprintf(outbuf, COLWID, "%d", pp->suid);
+ if (strlen(pp->suser)>max_rightward)
+ width = max_rightward;
+ return snprintf(outbuf, width, "%s", pp->suser);
}