Beispiel #1
0
int 
num_cpus(void)
{
	int cpu;

	GETSYSCTL("kern.smp.cpus", cpu);
	return cpu;
}
gint read_memswap(gulong *mem, gulong *swap, gulong *MT, gulong *MU, gulong *ST, gulong *SU)
{
    int total_pages;
    int free_pages;
    int inactive_pages;
    int pagesize = getpagesize();
    int swap_avail;
    int swap_free;

    if(GETSYSCTL("vm.stats.vm.v_page_count", total_pages)) {
        g_warning("Cannot read sysctl \"vm.stats.vm.v_page_count\"");
        return -1;
    }
    if(GETSYSCTL("vm.stats.vm.v_free_count", free_pages)) {
        g_warning("Cannot read sysctl \"vm.stats.vm.v_free_count\"");
        return -1;
    }
    if(GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages)) {
        g_warning("Cannot read sysctl \"vm.stats.vm.v_inactive_count\"");
        return -1;
    }

    *MT = (total_pages*pagesize) >> 10;
    *MU = ((total_pages-free_pages-inactive_pages) * pagesize) >> 10;
    *mem = *MU * 100 / *MT;

    if((*swap = swapmode(&swap_avail, &swap_free)) >= 0) {
        *ST = swap_avail;
        *SU = (swap_avail - swap_free);
    }
    else {
        *swap = 0;
        *ST = 0;
        *SU = 0;
    }

    return 0;
}
Beispiel #3
0
void
display(void)
{
	uint64_t arc_stat;
	int i, j;

	/* Get the load average over the last minute. */
	(void) getloadavg(avenrun, nitems(avenrun));
	(*curcmd->c_fetch)();
	if (curcmd->c_flags & CF_LOADAV) {
		j = 5.0*avenrun[0] + 0.5;
		dellave -= avenrun[0];
		if (dellave >= 0.0)
			c = '<';
		else {
			c = '>';
			dellave = -dellave;
		}
		if (dellave < 0.1)
			c = '|';
		dellave = avenrun[0];
		wmove(wload, 0, 0); wclrtoeol(wload);
		for (i = MIN(j, 50); i > 0; i--)
			waddch(wload, c);
		if (j > 50)
			wprintw(wload, " %4.1f", avenrun[0]);
	}
	if (curcmd->c_flags & CF_ZFSARC) {
	    uint64_t arc[7] = {};
	    size_t size = sizeof(arc[0]);
	    if (sysctlbyname("kstat.zfs.misc.arcstats.size",
		&arc[0], &size, NULL, 0) == 0 ) {
		    GETSYSCTL("vfs.zfs.mfu_size", arc[1]);
		    GETSYSCTL("vfs.zfs.mru_size", arc[2]);
		    GETSYSCTL("vfs.zfs.anon_size", arc[3]);
		    GETSYSCTL("kstat.zfs.misc.arcstats.hdr_size", arc[4]);
		    GETSYSCTL("kstat.zfs.misc.arcstats.l2_hdr_size", arc[5]);
		    GETSYSCTL("kstat.zfs.misc.arcstats.bonus_size", arc[6]);
		    GETSYSCTL("kstat.zfs.misc.arcstats.dnode_size", arc_stat);
		    arc[6] += arc_stat;
		    GETSYSCTL("kstat.zfs.misc.arcstats.dbuf_size", arc_stat);
		    arc[6] += arc_stat;
		    wmove(wload, 0, 0); wclrtoeol(wload);
		    for (i = 0 ; i < nitems(arc); i++) {
			if (arc[i] > 10llu * 1024 * 1024 * 1024 ) {
				wprintw(wload, "%7lluG", arc[i] >> 30);
			}
			else if (arc[i] > 10 * 1024 * 1024 ) {
Beispiel #4
0
//==============================================================================
void CPUInformation::initialise() noexcept
{
    const String features (BSDStatsHelpers::getDmesgInfo ("  Features="));
    hasMMX = features.contains ("MMX");
    hasSSE = features.contains ("SSE");
    hasSSE2 = features.contains ("SSE2");
    const String features2 (BSDStatsHelpers::getDmesgInfo ("  Features2="));
    hasSSE3 = features2.contains ("SSE3");
    const String amdfeatures2 (BSDStatsHelpers::getDmesgInfo ("  AMD Features2="));
    has3DNow = amdfeatures2.contains ("3DNow!");

    GETSYSCTL("hw.ncpu", numCpus);
    if (numCpus == -1) numCpus = 1;
}
const char *get_load_average()
{
  struct loadavg sysload;
  int i;
  static char buf[16];
  char *p;

  p = buf;

  // Load average
  GETSYSCTL("vm.loadavg", sysload);

  for (i = 0; i < 3; i++) {
    p+=snprintf(p, sizeof p, "%.2f", (float)sysload.ldavg[i] / sysload.fscale);

    if (i != 2)
      p += sprintf(p, " ");
  }

  return buf;
}
const char *get_cpu_stats(struct sysinfo *sys)
{
  /* Calculate total cpu utilization in % user, %nice, %system, %interrupt, %idle */
  int state;
  long diff[CPUSTATES], cpu_cycles_now[CPUSTATES];
  double cpu_pct[CPUSTATES];
  long total_change = 0, half_total;
  static char buf[256];
  char *p;
  float pct; // 1-idle

  GETSYSCTL("kern.cp_time", cpu_cycles_now);

  p = buf;

  // state is the counter for cp_times, j for cp_time
  for(state = 0; state < CPUSTATES; state++) {
    diff[state] = cpu_cycles_now[state] - sys->cpu_cycles[state];
    sys->cpu_cycles[state] = cpu_cycles_now[state]; // copy new values to old ones
    total_change += diff[state];
  }

  // don't divide by zero
  if (total_change == 0)
    total_change = 1;

  half_total = total_change / 2l;

  pct = .0;
  for(state=0; state < CPUSTATES-1; state++) { // the last value is idle
    cpu_pct[state] = (double)(diff[state] * 1000 + half_total)/total_change/10.0L;
    pct += cpu_pct[state];
  }

  p += snprintf(p, sizeof buf, "%.1f%%", pct);

  return buf;
}
Beispiel #7
0
int
kvm_getswapinfo_sysctl(kvm_t *kd, struct kvm_swap *swap_ary, int swap_max,
    int flags)
{
	int ti, ttl;
	size_t mibi, len;
	int soid[SWI_MAXMIB];
	struct xswdev xsd;
	struct kvm_swap tot;

	if (!GETSYSCTL(kd, "vm.dmmax", dmmax))
		return -1;

	mibi = SWI_MAXMIB - 1;
	if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
		_kvm_err(kd, kd->program, "sysctlnametomib failed: %s",
		    strerror(errno));
		return -1;
	}
	bzero(&tot, sizeof(tot));
	for (unswdev = 0;; unswdev++) {
		soid[mibi] = unswdev;
		len = sizeof(xsd);
		if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
			if (errno == ENOENT)
				break;
			_kvm_err(kd, kd->program, "cannot read sysctl: %s.",
			    strerror(errno));
			return -1;
		}
		if (len != sizeof(xsd)) {
			_kvm_err(kd, kd->program, "struct xswdev has unexpected "
			    "size;  kernel and libkvm out of sync?");
			return -1;
		}
		if (xsd.xsw_version != XSWDEV_VERSION) {
			_kvm_err(kd, kd->program, "struct xswdev version "
			    "mismatch; kernel and libkvm out of sync?");
			return -1;
		}

		ttl = xsd.xsw_nblks - dmmax;
		if (unswdev < swap_max - 1) {
			bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
			swap_ary[unswdev].ksw_total = ttl;
			swap_ary[unswdev].ksw_used = xsd.xsw_used;
			swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
			GETSWDEVNAME(xsd.xsw_dev, swap_ary[unswdev].ksw_devname,
			     flags);
		}
		tot.ksw_total += ttl;
		tot.ksw_used += xsd.xsw_used;
	}

	ti = unswdev;
	if (ti >= swap_max)
		ti = swap_max - 1;
	if (ti >= 0)
		swap_ary[ti] = tot;

        return(ti);
}
Beispiel #8
0
int sysinfo(struct sysinfo *info){
        kvm_t *kvmh;
        double load_avg[NLOADS];
        int page_s = getpagesize();
       
        if (info == NULL)
                R_ERROR(EFAULT);

        memset(info, 0, sizeof(struct sysinfo));        
        info -> mem_unit = UNIT_S;
       
        /*kvm init*/
        if ((kvmh = kvm_open(NULL, "/dev/null", "/dev/null",
        O_RDONLY, "kvm_open")) == NULL)
                R_ERROR(0);
       
        /*load averages*/
        if (kvm_getloadavg(kvmh, load_avg, NLOADS) == -1)
                R_ERROR(0);
       
        info -> loads[0] = (u_long)((float)load_avg[0] * USHRT_MAX);
        info -> loads[1] = (u_long)((float)load_avg[1] * USHRT_MAX);
        info -> loads[2] = (u_long)((float)load_avg[2] * USHRT_MAX);
       
        /*swap space*/
        struct kvm_swap k_swap;

        if (kvm_getswapinfo(kvmh, &k_swap, 1, 0) == -1)
                R_ERROR(0);

        info -> totalswap =
        (u_long)PAGE_2_UNIT(k_swap.ksw_total);
        info -> freeswap = info -> totalswap -
        (u_long)PAGE_2_UNIT(k_swap.ksw_used);
       
        /*processes*/
        int n_procs;    
       
        if (kvm_getprocs(kvmh, KERN_PROC_ALL, 0, &n_procs) == NULL)
                R_ERROR(0);
               
        info -> procs = (u_short)n_procs;
       
        /*end of kvm session*/
        if (kvm_close(kvmh) == -1)
                R_ERROR(0);
       
        /*uptime*/
        struct timespec ts;
       
        if (clock_gettime(CLOCK_UPTIME, &ts) == -1)
                R_ERROR(R_IGNORE);
               
        info -> uptime = (long)ts.tv_sec;      
       
        /*ram*/
        int total_pages,
            free_pages,
            active_pages,
            inactive_pages;
        u_long shmmax;
       
        if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) == -1)
                R_ERROR(R_IGNORE);      
        if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages) == -1)
                R_ERROR(R_IGNORE);              
        if (GETSYSCTL("vm.stats.vm.v_active_count", active_pages) == -1)
                R_ERROR(R_IGNORE);              
        if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages) == -1)
                R_ERROR(R_IGNORE);
        if (GETSYSCTL("kern.ipc.shmmax", shmmax) == -1)
                R_ERROR(R_IGNORE);
       
        info -> totalram = (u_long)PAGE_2_UNIT(total_pages);
        info -> freeram = (u_long)PAGE_2_UNIT(free_pages);
        info -> bufferram = (u_long)PAGE_2_UNIT(active_pages);
        info -> sharedram = shmmax / UNIT_S;
       
        /*high mem (todo)*/
        info -> totalhigh = 0; /*Does this supose to refer to HMA or reserved ram?*/
        info -> freehigh = 0;
       
        return 0;
}
Beispiel #9
0
static void
getinfo(struct Info *ls)
{
	struct devinfo *tmp_dinfo;
	size_t size;
	int mib[2];

	GETSYSCTL("kern.cp_time", ls->time);
	GETSYSCTL("kern.cp_time", cur.cp_time);
	GETSYSCTL("vm.stats.sys.v_swtch", ls->v_swtch);
	GETSYSCTL("vm.stats.sys.v_trap", ls->v_trap);
	GETSYSCTL("vm.stats.sys.v_syscall", ls->v_syscall);
	GETSYSCTL("vm.stats.sys.v_intr", ls->v_intr);
	GETSYSCTL("vm.stats.sys.v_soft", ls->v_soft);
	GETSYSCTL("vm.stats.vm.v_vm_faults", ls->v_vm_faults);
	GETSYSCTL("vm.stats.vm.v_io_faults", ls->v_io_faults);
	GETSYSCTL("vm.stats.vm.v_cow_faults", ls->v_cow_faults);
	GETSYSCTL("vm.stats.vm.v_zfod", ls->v_zfod);
	GETSYSCTL("vm.stats.vm.v_ozfod", ls->v_ozfod);
	GETSYSCTL("vm.stats.vm.v_swapin", ls->v_swapin);
	GETSYSCTL("vm.stats.vm.v_swapout", ls->v_swapout);
	GETSYSCTL("vm.stats.vm.v_swappgsin", ls->v_swappgsin);
	GETSYSCTL("vm.stats.vm.v_swappgsout", ls->v_swappgsout);
	GETSYSCTL("vm.stats.vm.v_vnodein", ls->v_vnodein);
	GETSYSCTL("vm.stats.vm.v_vnodeout", ls->v_vnodeout);
	GETSYSCTL("vm.stats.vm.v_vnodepgsin", ls->v_vnodepgsin);
	GETSYSCTL("vm.stats.vm.v_vnodepgsout", ls->v_vnodepgsout);
	GETSYSCTL("vm.stats.vm.v_intrans", ls->v_intrans);
	GETSYSCTL("vm.stats.vm.v_reactivated", ls->v_reactivated);
	GETSYSCTL("vm.stats.vm.v_pdwakeups", ls->v_pdwakeups);
	GETSYSCTL("vm.stats.vm.v_pdpages", ls->v_pdpages);
	GETSYSCTL("vm.stats.vm.v_dfree", ls->v_dfree);
	GETSYSCTL("vm.stats.vm.v_pfree", ls->v_pfree);
	GETSYSCTL("vm.stats.vm.v_tfree", ls->v_tfree);
	GETSYSCTL("vm.stats.vm.v_page_size", ls->v_page_size);
	GETSYSCTL("vm.stats.vm.v_free_count", ls->v_free_count);
	GETSYSCTL("vm.stats.vm.v_wire_count", ls->v_wire_count);
	GETSYSCTL("vm.stats.vm.v_active_count", ls->v_active_count);
	GETSYSCTL("vm.stats.vm.v_inactive_count", ls->v_inactive_count);
	GETSYSCTL("vfs.bufspace", ls->bufspace);
	GETSYSCTL("kern.maxvnodes", ls->desiredvnodes);
	GETSYSCTL("vfs.numvnodes", ls->numvnodes);
	GETSYSCTL("vfs.freevnodes", ls->freevnodes);
	GETSYSCTL("vfs.cache.nchstats", ls->nchstats);
	GETSYSCTL("vfs.numdirtybuffers", ls->numdirtybuffers);
	GETSYSCTL("vm.kmem_map_size", ls->v_kmem_map_size);
	getsysctl("hw.intrcnt", ls->intrcnt, nintr * sizeof(u_long));

	size = sizeof(ls->Total);
	mib[0] = CTL_VM;
	mib[1] = VM_TOTAL;
	if (sysctl(mib, 2, &ls->Total, &size, NULL, 0) < 0) {
		error("Can't get kernel info: %s\n", strerror(errno));
		bzero(&ls->Total, sizeof(ls->Total));
	}
	size = sizeof(ncpu);
	if (sysctlbyname("hw.ncpu", &ncpu, &size, NULL, 0) < 0 ||
	    size != sizeof(ncpu))
		ncpu = 1;

	tmp_dinfo = last.dinfo;
	last.dinfo = cur.dinfo;
	cur.dinfo = tmp_dinfo;

	last.snap_time = cur.snap_time;
	switch (devstat_getdevs(NULL, &cur)) {
	case -1:
		errx(1, "%s", devstat_errbuf);
		break;
	case 1:
		num_devices = cur.dinfo->numdevs;
		generation = cur.dinfo->generation;
		cmdkre("refresh", NULL);
		break;
	default:
		break;
	}
}
Beispiel #10
0
int
initkre(void)
{
	char *cp, *cp1, *cp2, *intrnamebuf, *nextcp;
	int i;
	size_t sz;

	if ((num_devices = devstat_getnumdevs(NULL)) < 0) {
		warnx("%s", devstat_errbuf);
		return(0);
	}

	cur.dinfo = calloc(1, sizeof(struct devinfo));
	last.dinfo = calloc(1, sizeof(struct devinfo));
	run.dinfo = calloc(1, sizeof(struct devinfo));

	if (dsinit(MAXDRIVES, &cur, &last, &run) != 1)
		return(0);

	if (nintr == 0) {
		if (sysctlbyname("hw.intrcnt", NULL, &sz, NULL, 0) == -1) {
			error("sysctl(hw.intrcnt...) failed: %s",
			      strerror(errno));
			return (0);
		}
		nintr = sz / sizeof(u_long);
		intrloc = calloc(nintr, sizeof (long));
		intrname = calloc(nintr, sizeof (char *));
		intrnamebuf = sysctl_dynread("hw.intrnames", NULL);
		if (intrnamebuf == NULL || intrname == NULL ||
		    intrloc == NULL) {
			error("Out of memory");
			if (intrnamebuf)
				free(intrnamebuf);
			if (intrname)
				free(intrname);
			if (intrloc)
				free(intrloc);
			nintr = 0;
			return(0);
		}
		for (cp = intrnamebuf, i = 0; i < nintr; i++) {
			nextcp = cp + strlen(cp) + 1;

			/* Discard trailing spaces. */
			for (cp1 = nextcp - 1; cp1 > cp && *(cp1 - 1) == ' '; )
				*--cp1 = '\0';

			/* Convert "irqN: name" to "name irqN". */
			if (strncmp(cp, "irq", 3) == 0) {
				cp1 = cp + 3;
				while (isdigit((u_char)*cp1))
					cp1++;
				if (cp1 != cp && *cp1 == ':' &&
				    *(cp1 + 1) == ' ') {
					sz = strlen(cp);
					*cp1 = '\0';
					cp1 = cp1 + 2;
					cp2 = strdup(cp);
					bcopy(cp1, cp, sz - (cp1 - cp) + 1);
					if (sz <= 10 + 4) {
						strcat(cp, " ");
						strcat(cp, cp2 + 3);
					}
					free(cp2);
				}
			}

			/*
			 * Convert "name irqN" to "name N" if the former is
			 * longer than the field width.
			 */
			if ((cp1 = strstr(cp, "irq")) != NULL &&
			    strlen(cp) > 10)
				bcopy(cp1 + 3, cp1, strlen(cp1 + 3) + 1);

			intrname[i] = cp;
			cp = nextcp;
		}
		nextintsrow = INTSROW + 2;
		allocinfo(&s);
		allocinfo(&s1);
		allocinfo(&s2);
		allocinfo(&z);
	}
	GETSYSCTL("vm.kmem_size", kmem_size);
	GETSYSCTL("vm.stats.vm.v_page_count", v_page_count);
	getinfo(&s2);
	copyinfo(&s2, &s1);
	return(1);
}