static error_t rootdir_gc_uptime (void *hook, char **contents, ssize_t *contents_len) { struct timeval time, boottime, idletime; double up_secs, idle_secs; error_t err; err = gettimeofday (&time, NULL); if (err < 0) return errno; err = get_boottime (hook, &boottime); if (err) return err; err = get_idletime (hook, &idletime); if (err) return err; timersub (&time, &boottime, &time); up_secs = (time.tv_sec * 1000000. + time.tv_usec) / 1000000.; idle_secs = (idletime.tv_sec * 1000000. + idletime.tv_usec) / 1000000.; /* The second field is the total idle time. As far as I know we don't keep track of it. However, procps uses it to compute "USER_HZ", and proc(5) specifies that it should be equal to USER_HZ times the idle value in ticks from /proc/stat. So we assume a completely idle system both here and there to make that work. */ *contents_len = asprintf (contents, "%.2lf %.2lf\n", up_secs, idle_secs); return 0; }
static error_t rootdir_gc_stat (void *hook, char **contents, ssize_t *contents_len) { struct timeval boottime, time, idletime; struct vm_statistics vmstats; unsigned long up_ticks, idle_ticks; error_t err; err = gettimeofday (&time, NULL); if (err < 0) return errno; err = get_boottime (hook, &boottime); if (err) return err; err = get_idletime (hook, &idletime); if (err) return err; err = vm_statistics (mach_task_self (), &vmstats); if (err) return EIO; timersub (&time, &boottime, &time); up_ticks = opt_clk_tck * (time.tv_sec * 1000000. + time.tv_usec) / 1000000.; idle_ticks = opt_clk_tck * (idletime.tv_sec * 1000000. + idletime.tv_usec) / 1000000.; *contents_len = asprintf (contents, "cpu %lu 0 0 %lu 0 0 0 0 0\n" "cpu0 %lu 0 0 %lu 0 0 0 0 0\n" "intr 0\n" "page %d %d\n" "btime %lu\n", up_ticks - idle_ticks, idle_ticks, up_ticks - idle_ticks, idle_ticks, vmstats.pageins, vmstats.pageouts, boottime.tv_sec); return 0; }
/** * Returns the system uptime in centiseconds. * * @note The value returned by this function is not identical to sysUpTime * defined in RFC 1213. get_uptime() returns the system uptime while * sysUpTime represents the time that has elapsed since the most recent * restart of the network manager (snmpd). * * @see See also netsnmp_get_agent_uptime(). */ long get_uptime(void) { #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7) static char lbolt_name[] = "lbolt"; struct nlist nl; int kmem; time_t lbolt; nl.n_name = lbolt_name; if(knlist(&nl, 1, sizeof(struct nlist)) != 0) return(0); if(nl.n_type == 0 || nl.n_value == 0) return(0); if((kmem = open("/dev/mem", 0)) < 0) return 0; lseek(kmem, (long) nl.n_value, L_SET); read(kmem, &lbolt, sizeof(lbolt)); close(kmem); return(lbolt); #elif defined(solaris2) kstat_ctl_t *ksc = kstat_open(); kstat_t *ks; kid_t kid; kstat_named_t *named; u_long lbolt = 0; if (ksc) { ks = kstat_lookup(ksc, "unix", -1, "system_misc"); if (ks) { kid = kstat_read(ksc, ks, NULL); if (kid != -1) { named = kstat_data_lookup(ks, "lbolt"); if (named) { #ifdef KSTAT_DATA_UINT32 lbolt = named->value.ui32; #else lbolt = named->value.ul; #endif } } } kstat_close(ksc); } return lbolt; #elif defined(linux) || defined(cygwin) FILE *in = fopen("/proc/uptime", "r"); long uptim = 0, a, b; if (in) { if (2 == fscanf(in, "%ld.%ld", &a, &b)) uptim = a * 100 + b; fclose(in); } return uptim; #else struct timeval now; long boottime_csecs, nowtime_csecs; boottime_csecs = get_boottime(); if (boottime_csecs == 0) return 0; gettimeofday(&now, (struct timezone *) 0); nowtime_csecs = (now.tv_sec * 100) + (now.tv_usec / 10000); return (nowtime_csecs - boottime_csecs); #endif }