Exemple #1
0
char *
format_next_process(caddr_t handle, char *(*get_userid) ())

{
	struct prpsinfo *pp;
	struct handle *hp;
	long		cputime;

	/* find and remember the next proc structure */
	hp = (struct handle *) handle;
	pp = *(hp->next_proc++);
	hp->remaining--;

	/* get the process cpu usage since startup */
	cputime = pp->pr_time.tv_sec;

	/* format this entry */
	sprintf(fmt,
			Proc_format,
			pp->pr_pid,
			pp->pr_pgrp,
			(*get_userid) (pp->pr_uid),
			format_prio(pp),
			format_k(pagetok(pp->pr_size)),
			format_k(pagetok(pp->pr_rssize)),
			format_state(pp),
			format_time(cputime),
			clip_percent(weighted_cpu(pp)),
			clip_percent(percent_cpu(pp)),
			printable(pp->pr_fname));

	/* return the result */
	return (fmt);
}
int
machine_get_meminfo(meminfo_type * result)
{
	size_t size;
	int mib[2];
	struct uvmexp suvm;

	mib[0] = CTL_VM;
	mib[1] = VM_UVMEXP;
	size = sizeof(suvm);

	if (sysctl(mib, 2, &suvm, &size, NULL, 0) < 0) {
		perror("sysctl vm.uvmexp failed");
		return (FALSE);
	}

	/* memory */
	result[0].total = pagetok(suvm.npages);
	result[0].free = pagetok(suvm.free);

	/* not used anyway */
	result[0].shared = 0;
	result[0].buffers = 0;
	result[0].cache = 0;

	/* swap */
	result[1].total = suvm.pagesize * suvm.swpages;
	result[1].free = suvm.pagesize * suvm.swpginuse;
	result[1].free = result[1].total - result[1].free;

	return (TRUE);
}
int
machine_get_meminfo(meminfo_type * result)
{
	int total_pages, free_pages;

	vm_statistics_data_t vm_info;
	mach_msg_type_number_t info_count;

	info_count = HOST_VM_INFO_COUNT;
	if (host_statistics(lcdproc_port, HOST_VM_INFO, (host_info_t) & vm_info, &info_count)) {
		perror("host_statistics");
		return (FALSE);
	}

	result[0].total = pagetok(vm_info.active_count + vm_info.inactive_count +
				  vm_info.free_count + vm_info.wire_count);
	result[0].free = pagetok(vm_info.free_count);
	result[0].buffers = 0;
	result[0].cache = 0;
	result[0].shared = 0;

	/* swap */
	result[1].total = 0;
	result[1].free = 0;

	if (swapmode(&total_pages, &free_pages) != -1) {
		result[1].total = total_pages;
		result[1].free = free_pages;
	}

	return (TRUE);
}
Exemple #4
0
void
get_system_info(struct system_info * si)

{
	int			i;
	int			avenrun[3];
	struct rminfo realmem;
	struct sysinfo sysinfo;
	static time_t cp_old[CPU_STATES];
	static time_t cp_diff[CPU_STATES];	/* for cpu state percentages */
	off_t		fswap;			/* current free swap in blocks */
	off_t		tswap;			/* total swap in blocks */

	(void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun");

	for (i = 0; i < 3; i++)
	{
		si->load_avg[i] = loaddouble(avenrun[i]);
		si->load_avg[i] /= 1024.0;
	}

	if ((numcpus = sysmp(MP_NPROCS)) == -1)
	{
		perror("sysmp(MP_NPROCS)");
		return;
	}

	if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == -1)
	{
		perror("sysmp(MP_SAGET,MPSA_RMINFO, ...)");
		return;
	}

	swapctl(SC_GETFREESWAP, &fswap);
	swapctl(SC_GETSWAPTOT, &tswap);

	memory_stats[0] = pagetok(realmem.physmem);
	memory_stats[1] = pagetok(realmem.availrmem);
	memory_stats[2] = pagetok(realmem.freemem);
	memory_stats[3] = tswap / 2;
	memory_stats[4] = fswap / 2;

	if (sysmp(MP_SAGET, MPSA_SINFO, &sysinfo, sizeof(struct sysinfo)) == -1)
	{
		perror("sysmp(MP_SAGET,MPSA_SINFO)");
		return;
	}
	(void) percentages(CPU_STATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

	si->cpustates = cpu_states;
	si->memory = memory_stats;
	si->last_pid = -1;

	return;
}
int
machine_get_procs(LinkedList * procs)
{
	struct kinfo_proc *kprocs;
	int nproc, i;
	procinfo_type *p;
	kvm_t *kvmd;

	if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
		perror("kvm_open");
		return (FALSE);
	}

#if OpenBSD >= 201111
	kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &nproc);
#else
	kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc);
#endif
	if (kprocs == NULL) {
		perror("kvm_getprocs");
		kvm_close(kvmd);
		return (FALSE);
	}

	for (i = 0; i < nproc; i++) {
		p = malloc(sizeof(procinfo_type));
		if (!p) {
			perror("mem_top_malloc");
			kvm_close(kvmd);
			return (FALSE);
		}
#if OpenBSD >= 201111
		strncpy(p->name, kprocs->p_comm, 15);
		p->name[15] = '\0';
		p->totl = pagetok(PROCSIZE(kprocs));
		p->number = kprocs->p_pid;
#else
		strncpy(p->name, kprocs->kp_proc.p_comm, 15);
		p->name[15] = '\0';
		p->totl = pagetok(PROCSIZE(kprocs->kp_eproc.e_vm));
		p->number = kprocs->kp_proc.p_pid;
#endif
		LL_Push(procs, (void *)p);

		kprocs++;
	}
	kvm_close(kvmd);

	return (TRUE);
}
Exemple #6
0
void
get_system_info (struct system_info *si)
{
  long avenrun[3];
  struct sysinfo sysinfo;
  struct vmtotal total;
  struct anoninfo anoninfo;
  static time_t cp_old[CPUSTATES];
  static time_t cp_diff[CPUSTATES];	/* for cpu state percentages */
  register int i;

  getsysinfo(&sysinfo);

  /* convert cp_time counts to percentages */
  (void) percentages (CPUSTATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

  /* Find total CPU utilization, as a fraction of 1. */
  total_cpu = (cpu_states[CPU_USER] + cpu_states[CPU_KERNEL]) / 1000.0;

  /* get mpid -- process id of last process */
  (void) getkval (mpid_offset, &(si->last_pid), sizeof (si->last_pid),
		  "mpid");

  /* get load average array */
  (void) getkval (avenrun_offset, (int *) avenrun, sizeof (avenrun), "avenrun");

  /* convert load averages to doubles */
  for (i = 0; i < 3; i++)
    si->load_avg[i] = loaddouble (avenrun[i]);

  /* get total -- systemwide main memory usage structure */
  (void) getkval (total_offset, (int *) (&total), sizeof (total), "total");
  /* convert memory stats to Kbytes */
  memory_stats[0] = pagetok (total.t_rm);
  memory_stats[1] = pagetok (total.t_arm);
  memory_stats[2] = pagetok (total.t_free);
  (void) getkval (anoninfo_offset, (int *) (&anoninfo), sizeof (anoninfo),
		  "anoninfo");
  memory_stats[3] = pagetok (anoninfo.ani_max - anoninfo.ani_free);
  memory_stats[4] = pagetok (anoninfo.ani_max - anoninfo.ani_resv);

  /* set arrays and strings */
  si->cpustates = cpu_states;
  si->memory = memory_stats;
}
Exemple #7
0
char *
format_next_process(caddr_t handle, char *(*get_userid)(uid_t))
{
	char *p_wait, waddr[sizeof(void *) * 2 + 3];	/* Hexify void pointer */
	struct kinfo_proc2 *pp;
	struct handle *hp;
	int cputime;
	double pct;

	/* find and remember the next proc structure */
	hp = (struct handle *) handle;
	pp = *(hp->next_proc++);
	hp->remaining--;

	cputime = (pp->p_uticks + pp->p_sticks + pp->p_iticks) / stathz;

	/* calculate the base for cpu percentages */
	pct = pctdouble(pp->p_pctcpu);

	if (pp->p_wchan) {
		if (pp->p_wmesg)
			p_wait = pp->p_wmesg;
		else {
			snprintf(waddr, sizeof(waddr), "%llx",
			    (unsigned long long)(pp->p_wchan & ~KERNBASE));
			p_wait = waddr;
		}
	} else
		p_wait = "-";

	/* format this entry */
	snprintf(fmt, sizeof fmt, Proc_format,
	    pp->p_pid, (*get_userid)(pp->p_ruid),
	    pp->p_priority - PZERO, pp->p_nice - NZERO,
	    format_k(pagetok(PROCSIZE(pp))),
	    format_k(pagetok(pp->p_vm_rssize)),
	    (pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
	    "idle" : state_abbr(pp),
	    p_wait, format_time(cputime), 100.0 * pct,
	    printable(format_comm(pp)));

	/* return the result */
	return (fmt);
}
Exemple #8
0
int updateMemory( void ) {
#ifdef HAVE_KSTAT
	kstat_ctl_t		*kctl;
	kstat_t			*ksp;
	kstat_named_t		*kdata;

	/*
	 *  get a kstat handle and update the user's kstat chain
	 */
	if( (kctl = kstat_open()) == NULL )
		return( 0 );
	while( kstat_chain_update( kctl ) != 0 )
		;

	totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));

	/*
	 *  traverse the kstat chain to find the appropriate statistics
	 */
	if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL ) {
		goto done;
	}

	if( kstat_read( kctl, ksp, NULL ) == -1 ) {
		goto done;
	}

	/*
	 *  lookup the data
	 */
	 kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
	 if( kdata != NULL )
		freemem = pagetok( kdata->value.ui32 );

done:
	kstat_close( kctl );
#endif /* ! HAVE_KSTAT */

	return( 0 );
}
Exemple #9
0
int update_meminfo()
{
	static int mib[2] = { CTL_VM, VM_METER };
	struct vmtotal vmtotal;
	size_t size;
	int pagesize, pageshift, swap_avail, swap_used;

	pagesize = getpagesize();
	pageshift = 0;
	while (pagesize > 1) {
		pageshift++;
		pagesize >>= 1;
	}

	/* we only need the amount of log(2)1024 for our conversion */
	pageshift -= LOG1024;

	/* get total -- systemwide main memory usage structure */
	size = sizeof(vmtotal);
	if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
		warn("sysctl failed");
		bzero(&vmtotal, sizeof(vmtotal));
	}

	info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
	info.mem = pagetok(vmtotal.t_rm);
	info.memeasyfree = info.memfree = info.memmax - info.mem;

	if ((swapmode(&swap_used, &swap_avail)) >= 0) {
		info.swapmax = swap_avail;
		info.swap = swap_used;
		info.swapfree = swap_avail - swap_used;
	} else {
		info.swapmax = 0;
		info.swap = 0;
		info.swapfree = 0;
	}
	return 0;
}
Exemple #10
0
char *
format_next_process (
		      caddr_t handle,
		      char *(*get_userid) ())
{
  register struct prpsinfo *pp;
  struct handle *hp;
  register long cputime;
  double pctcpu;
  double pctwcpu;

  /* find and remember the next proc structure */
  hp = (struct handle *) handle;
  pp = *(hp->next_proc++);
  hp->remaining--;

  /* get the cpu usage and calculate the cpu percentages */
  cputime = pp->pr_time.tv_sec;
  get_cpu(pp, &pctcpu, &pctwcpu);

  /* format this entry */
  (void) sprintf (fmt,
		  Proc_format,
		  pp->pr_pid,
		  (*get_userid) (pp->pr_uid),
		  pp->pr_pri - PZERO,
		  pp->pr_nice - NZERO,
		  pagetok (pp->pr_size),
		  pagetok (pp->pr_rssize),
		  state_abbrev[pp->pr_state],
		  cputime / 60l,
		  cputime % 60l,
		  pctwcpu,
		  pctcpu,
		  pp->pr_fname);

  /* return the result */
  return (fmt);
}
Exemple #11
0
	const char *
	ram_used(void)
	{
		struct uvmexp uvmexp;

		if (load_uvmexp(&uvmexp)) {
			return fmt_human(pagetok(uvmexp.active,
			                         uvmexp.pageshift) * 1024,
			                 1024);
		}

		return NULL;
	}
int
machine_get_meminfo(meminfo_type * result)
{
	int total_pages, free_pages;
	size_t size;

	size = sizeof(int);

	if (sysctlbyname("vm.stats.vm.v_page_count", &total_pages, &size, NULL, 0) < 0) {
		perror("sysctl vm.stats.vm.v_page_count");
		return (FALSE);
	}

	if (sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &size, NULL, 0) < 0) {
		perror("sysctl vm.stats.vm.v_free_count");
		return (FALSE);
	}

	/* memory */
	result[0].total = pagetok(total_pages);
	result[0].free = pagetok(free_pages);

	/* unused anyway */
	result[0].shared = 0;
	result[0].buffers = 0;
	result[0].cache = 0;

	/* swap */
	result[1].total = 0;
	result[1].free = 0;

	if (swapmode(&total_pages, &free_pages) != -1) {
		result[1].total = total_pages;
		result[1].free = free_pages;
	}

	return (TRUE);
}
Exemple #13
0
	const char *
	ram_free(void)
	{
		struct uvmexp uvmexp;
		int free_pages;

		if (load_uvmexp(&uvmexp)) {
			free_pages = uvmexp.npages - uvmexp.active;
			return fmt_human(pagetok(free_pages, uvmexp.pageshift) *
			                 1024, 1024);
		}

		return NULL;
	}
/**
 * Reads info about swap space from system and returns it in parameters passed.
 * \param retavail  Total available swap space
 * \param retfree   Free swap space
 * \return  -1 on error, otherwise number of swap areas (typically 0 for total)
 */
static int
swapmode(int *retavail, int *retfree)
{
	int n;
	struct kvm_swap swapary[1];

	*retavail = 0;
	*retfree = 0;

	if (kvmd == NULL)
		return -1;

	n = kvm_getswapinfo(kvmd, swapary, 1, 0);
	if (n < 0 || swapary[0].ksw_total == 0) {
		/* strange */
	}
	else {
		*retavail = pagetok(swapary[0].ksw_total);
		*retfree = pagetok(swapary[0].ksw_total - swapary[0].ksw_used);
	}

	return (n);
}
Exemple #15
0
int MemSensor::getMemFree()
{
#ifdef Q_OS_FREEBSD
    static int mem = 0;
    size_t size = sizeof(mem);

    sysctlbyname("vm.stats.vm.v_free_count", &mem, &size, NULL, 0);
    return (pagetok(mem));
#elif defined(Q_OS_NETBSD)
    struct uvmexp_sysctl uvmexp;
    int mib[2];
    size_t ssize;
    mib[0] = CTL_VM;
    mib[1] = VM_UVMEXP2;
    ssize = sizeof(uvmexp);
    sysctl(mib,2,&uvmexp,&ssize,NULL,0);
    return pagetok(uvmexp.free);
#else
    QRegExp rx( "MemFree:\\s*(\\d+)" );
    rx.search( meminfo );
    return ( rx.cap(1).toInt() );
#endif
}
Exemple #16
0
int MemSensor::getCached()
{
#ifdef Q_OS_FREEBSD
    static int mem = 0;
    size_t size = sizeof(mem);

    sysctlbyname("vm.stats.vm.v_cache_count", &mem, &size, NULL, 0);
    return (pagetok(mem));
#elif defined(Q_OS_NETBSD)
    return 0;
#else
    QRegExp rx1( "Cached:\\s*(\\d+)" );
    QRegExp rx2( "SwapCached:\\s*(\\d+)" );
    rx1.search( meminfo );
    rx2.search( meminfo );
    return ( rx1.cap(1).toInt() + rx2.cap(1).toInt() );
#endif
}
Exemple #17
0
int init_process_info_sysdep(void) {
  register int pagesize;

  systeminfo.cpus = sysconf( _SC_NPROCESSORS_ONLN);

  pagesize  = sysconf(_SC_PAGESIZE);
  pageshift = 0;
  while (pagesize > 1) {
    pageshift++;
    pagesize >>= 1;
  }

  /* we only need the amount of log(2)1024 for our conversion */
  pageshift -= LOG1024;

  systeminfo.mem_kbyte_max = pagetok(sysconf(_SC_PHYS_PAGES));
  page_size = getpagesize();

  return (TRUE);
}
Exemple #18
0
void
get_system_info(struct system_info * si)
{
	long		avenrun[3];
	struct sysinfo sysinfo;
	static struct sysinfo *mpinfo = NULL;		/* array, per-processor
												 * sysinfo structures. */
	struct vmtotal total;
	struct anoninfo anoninfo;
	static long cp_old[CPUSTATES];
	static long cp_diff[CPUSTATES];		/* for cpu state percentages */
	static int	num_cpus;
	static int	fd_cpu = 0;
	register int i;

	if (use_stats == 1)
	{
		if (fd_cpu == 0)
		{
			if ((fd_cpu = open("/stats/cpuinfo", O_RDONLY)) == -1)
			{
				(void) fprintf(stderr, "%s: Open of /stats/cpuinfo failed\n", myname);
				quit(2);
			}
			if (read(fd_cpu, &num_cpus, sizeof(int)) != sizeof(int))
			{
				(void) fprintf(stderr, "%s: Read of /stats/cpuinfo failed\n", myname);
				quit(2);
			}
			close(fd_cpu);
		}
		if (mpinfo == NULL)
		{
			mpinfo = (struct sysinfo *) calloc(num_cpus, sizeof(mpinfo[0]));
			if (mpinfo == NULL)
			{
				(void) fprintf(stderr, "%s: can't allocate space for per-processor sysinfos\n", myname);
				quit(12);
			}
		}
		/* Read the per cpu sysinfo structures into mpinfo struct. */
		read_sysinfos(num_cpus, mpinfo);
		/* Add up all of the percpu sysinfos to get global sysinfo */
		sysinfo_data(num_cpus, &sysinfo, mpinfo);
	}
	else
	{
		(void) getkval(sysinfo_offset, &sysinfo, sizeof(struct sysinfo), "sysinfo");
	}

	/* convert cp_time counts to percentages */
	(void) percentages(CPUSTATES, cpu_states, sysinfo.cpu, cp_old, cp_diff);

	/* get mpid -- process id of last process */
	(void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
				   "mpid");

	/* get load average array */
	(void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun");

	/* convert load averages to doubles */
	for (i = 0; i < 3; i++)
		si->load_avg[i] = loaddouble(avenrun[i]);

	/* get total -- systemwide main memory usage structure */
	(void) getkval(total_offset, (int *) (&total), sizeof(total), "total");
	/* convert memory stats to Kbytes */
	memory_stats[0] = pagetok(total.t_rm);
	memory_stats[1] = pagetok(total.t_arm);
	memory_stats[2] = pagetok(total.t_free);
	(void) getkval(anoninfo_offset, (int *) (&anoninfo), sizeof(anoninfo),
				   "anoninfo");
	memory_stats[3] = pagetok(anoninfo.ani_max - anoninfo.ani_free);
	memory_stats[4] = pagetok(anoninfo.ani_max - anoninfo.ani_resv);

	/* set arrays and strings */
	si->cpustates = cpu_states;
	si->memory = memory_stats;
}
Exemple #19
0
static void
read_one_proc_stat(pid_t pid, struct top_proc * proc, struct process_select * sel)
{
	char		buffer[4096],
			   *p,
			   *q;
	int			fd,
				len;
	int			fullcmd;
	char		value[BUFFERLEN + 1];

	/* if anything goes wrong, we return with proc->state == 0 */
	proc->state = 0;

	/* full cmd handling */
	fullcmd = sel->fullcmd;
	if (fullcmd == 1)
	{
		sprintf(buffer, "%d/cmdline", pid);
		if ((fd = open(buffer, O_RDONLY)) != -1)
		{
			/* read command line data */
			/* (theres no sense in reading more than we can fit) */
			if ((len = read(fd, buffer, MAX_COLS)) > 1)
			{
				buffer[len] = '\0';
				xfrm_cmdline(buffer, len);
				update_procname(proc, buffer);
			}
			else
			{
				fullcmd = 0;
			}
			close(fd);
		}
		else
		{
			fullcmd = 0;
		}
	}

	/* grab the proc stat info in one go */
	sprintf(buffer, "%d/stat", pid);

	fd = open(buffer, O_RDONLY);
	len = read(fd, buffer, sizeof(buffer) - 1);
	close(fd);

	buffer[len] = '\0';

	proc->uid = (uid_t) proc_owner((int) pid);

	/* parse out the status, described in 'man proc' */

	/* skip pid and locate command, which is in parentheses */
	if ((p = strchr(buffer, '(')) == NULL)
	{
		return;
	}
	if ((q = strrchr(++p, ')')) == NULL)
	{
		return;
	}

	/* set the procname */
	*q = '\0';
	if (!fullcmd)
	{
		update_procname(proc, p);
	}

	/* scan the rest of the line */
	p = q + 1;
	p = skip_ws(p);
	switch (*p++)				/* state */
	{
		case 'R':
			proc->state = 1;
			break;
		case 'S':
			proc->state = 2;
			break;
		case 'D':
			proc->state = 3;
			break;
		case 'Z':
			proc->state = 4;
			break;
		case 'T':
			proc->state = 5;
			break;
		case 'W':
			proc->state = 6;
			break;
		case '\0':
			return;
	}

	p = skip_token(p);			/* skip ppid */
	p = skip_token(p);			/* skip pgrp */
	p = skip_token(p);			/* skip session */
	p = skip_token(p);			/* skip tty nr */
	p = skip_token(p);			/* skip tty pgrp */
	p = skip_token(p);			/* skip flags */
	p = skip_token(p);			/* skip min flt */
	p = skip_token(p);			/* skip cmin flt */
	p = skip_token(p);			/* skip maj flt */
	p = skip_token(p);			/* skip cmaj flt */

	proc->time = strtoul(p, &p, 10);	/* utime */
	proc->time += strtoul(p, &p, 10);	/* stime */

	p = skip_token(p);			/* skip cutime */
	p = skip_token(p);			/* skip cstime */

	proc->pri = strtol(p, &p, 10);		/* priority */
	proc->nice = strtol(p, &p, 10);		/* nice */
	p = skip_token(p);			/* skip num_threads */
	p = skip_token(p);			/* skip itrealvalue, 0 */
	proc->start_time = strtoul(p, &p, 10);		/* start_time */
	proc->size = bytetok(strtoul(p, &p, 10));	/* vsize */
	proc->rss = pagetok(strtoul(p, &p, 10));	/* rss */


#if 0
	/* for the record, here are the rest of the fields */
	p = skip_token(p);			/* skip rlim */
	p = skip_token(p);			/* skip start_code */
	p = skip_token(p);			/* skip end_code */
	p = skip_token(p);			/* skip start_stack */
	p = skip_token(p);			/* skip esp */
	p = skip_token(p);			/* skip eip */
	p = skip_token(p);			/* skip signal */
	p = skip_token(p);			/* skip sigblocked */
	p = skip_token(p);			/* skip sigignore */
	p = skip_token(p);			/* skip sigcatch */
	p = skip_token(p);			/* skip wchan */
	p = skip_token(p);			/* skip nswap, not maintained */
	p = skip_token(p);			/* exit signal */
	p = skip_token(p);			/* processor */
	p = skip_token(p);			/* rt_priority */
	p = skip_token(p);			/* policy */
	p = skip_token(p);			/* delayacct_blkio_ticks */
#endif

	/* Get the io stats. */
	sprintf(buffer, "%d/io", pid);
	fd = open(buffer, O_RDONLY);
	if (fd == -1)
	{
		/*
		 * CONFIG_TASK_IO_ACCOUNTING is not enabled in the Linux kernel or
		 * this version of Linux may not support collecting i/o statistics
		 * per pid.  Report 0's.
		 */
		proc->rchar = 0;
		proc->wchar = 0;
		proc->syscr = 0;
		proc->syscw = 0;
		proc->read_bytes = 0;
		proc->write_bytes = 0;
		proc->cancelled_write_bytes = 0;
		return;
	}
	len = read(fd, buffer, sizeof(buffer) - 1);
	close(fd);

	buffer[len] = '\0';
	p = buffer;
	GET_VALUE(proc->rchar);
	GET_VALUE(proc->wchar);
	GET_VALUE(proc->syscr);
	GET_VALUE(proc->syscw);
	GET_VALUE(proc->read_bytes);
	GET_VALUE(proc->write_bytes);
	GET_VALUE(proc->cancelled_write_bytes);
}
Exemple #20
0
void
get_system_info(struct system_info * si)
{
	register long total;
	register int i;
	unsigned int count = HOST_CPU_LOAD_INFO_COUNT;

	if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO,
			(host_info_t) & cpuload, &count) == KERN_SUCCESS)
	{
		for (i = 0; i < CPU_STATE_MAX; i++)
		{
			cp_time[i] = cpuload.cpu_ticks[i];
		}
	}

#ifdef MAX_VERBOSE

	/*
	 * print out the entries
	 */

	for (i = 0; i < CPU_STATE_MAX; i++)
		printf("cp_time[%d] = %d\n", i, cp_time[i]);
	fflush(stdout);
#endif /* MAX_VERBOSE */

	/*
	 * get the load averages
	 */

#ifdef MAX_VERBOSE
	printf("%-30s%03.2f, %03.2f, %03.2f\n",
			"load averages:",
			si->load_avg[0],
			si->load_avg[1],
			si->load_avg[2]);
#endif /* MAX_VERBOSE */

	total = percentages(CPU_STATE_MAX, cpu_states, cp_time, cp_old, cp_diff);

	/*
	 * get the memory statistics
	 */

	{
		kern_return_t status;

		count = HOST_VM_INFO_COUNT;
		status = host_statistics(mach_host_self(), HOST_VM_INFO,
				(host_info_t) & vm_stats, &count);

		if (status != KERN_SUCCESS)
		{
			puke("error: vm_statistics() failed (%s)", strerror(errno));
			return;
		}

		/*
		 * we already have the total memory, we just need to get it in the
		 * right format.
		 */

		pagesize = 1; /* temporary fix to div by 0 errors */
		memory_stats[0] = pagetok(maxmem / pagesize);
		memory_stats[1] = pagetok(vm_stats.free_count);
		memory_stats[2] = pagetok(vm_stats.active_count);
		memory_stats[3] = pagetok(vm_stats.inactive_count);
		memory_stats[4] = pagetok(vm_stats.wire_count);

		if (swappgsin < 0)
		{
			memory_stats[5] = 1;
			memory_stats[6] = 1;
		}
		else
		{
			memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin)));
			memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout)));
		}
		swappgsin = vm_stats.pageins;
		swappgsout = vm_stats.pageouts;
	}

	si->cpustates = cpu_states;
	si->memory = memory_stats;
	si->last_pid = -1;

	return;
}
Exemple #21
0
char *
format_next_process(caddr_t handle, char *(*getuserid) ())
{
	register struct macos_proc *pp;
	register long cputime;
	register double pct;
	struct handle *hp;
	char *command; /* text outputted to describe the command */
	int show_cmd_local = show_fullcmd;

	/*
	 * we need to keep track of the next proc structure.
	 */

	hp = (struct handle *) handle;
	pp = *(hp->next_proc++);
	hp->remaining--;

	/*
	 * get the process structure and take care of the cputime
	 */

	if ((MPP(pp, p_flag) & P_INMEM) == 0)
	{
		/* we want to print swapped processes as <pname> */
		char *comm = MPP(pp, p_comm);

#define COMSIZ	sizeof(MPP(pp, p_comm))
		char		buf[COMSIZ];

		strncpy(buf, comm, COMSIZ);
		comm[0] = '<';
		strncpy(&comm[1], buf, COMSIZ - 2);
		comm[COMSIZ - 2] = '\0';
		strncat(comm, ">", COMSIZ - 1);
		comm[COMSIZ - 1] = '\0';
		command = comm;
	}

	/*
	 * count the cpu time, but ignore the interrupts
	 *
	 * At the present time (DR2 8/1998), MacOS X doesn't correctly report this
	 * information through the kinfo_proc structure.  We need to get it from
	 * the task threads.
	 *
	 * cputime = PP(pp, p_rtime).tv_sec;
	 */

	cputime = RP(pp, user_time).seconds + RP(pp, system_time).seconds;

	/*
	 * calculate the base cpu percentages
	 *
	 * Again, at the present time, MacOS X doesn't report this information
	 * through the kinfo_proc.	We need to talk to the threads.
	 */

	pct = (double) (RP(pp, cpu_usage)) / TH_USAGE_SCALE;

	/* get the process's command name in to "cmd" */
	if (show_fullcmd)
 		if (get_fullcmd(MPP(pp, p_pid), pp->fullcmd) < 0)
			show_cmd_local = 0; /* Don't show if full command not found. */

	/*
	 * format the entry
	 */

	/*
	 * In the final version, I would expect this to work correctly, but it
	 * seems that not all of the fields in the proc structure are being used.
	 *
	 * For now, we'll attempt to get some of the things we need from the mach
	 * task info.
	 */

	sprintf(fmt,
			Proc_format,
			MPP(pp, p_pid),
			(*getuserid) (MEP(pp, e_pcred.p_ruid)),
			0,
			pp->thread_count,
			format_k(TASKSIZE(pp) / 1024),
			format_k(pagetok(RSSIZE(pp))),
			state_abbrev[(u_char) MPP(pp, p_stat)],
			format_time(cputime),
			100.0 * TP(pp, resident_size) / maxmem,
			100.0 * pct,
			(show_cmd_local == 0 ? command : pp->fullcmd));

	return (fmt);
}
Exemple #22
0
int updateMemory( void ) {

	long			swaptotal;
	long			swapfree;
	long			swapused;
#ifdef HAVE_KSTAT
	kstat_ctl_t		*kctl;
	kstat_t			*ksp;
	kstat_named_t		*kdata;
#endif /* HAVE_KSTAT */
	swaptotal = swapused = swapfree = 0L;

	/*
	 *  Retrieve overall swap information from anonymous memory structure -
	 *  which is the same way "swap -s" retrieves it's statistics.
	 *
	 *  swapctl(SC_LIST, void *arg) does not return what we are looking for.
	 */

	if (swapctl(SC_AINFO, &am_swap) == -1)
		return(0);

	swaptotal = am_swap.ani_max;
	swapused = am_swap.ani_resv;
	swapfree = swaptotal - swapused;

	totalswap = pagetok(swaptotal);
	freeswap = pagetok(swapfree);

#ifdef HAVE_KSTAT
	/*
	 *  get a kstat handle and update the user's kstat chain
	 */
	if( (kctl = kstat_open()) == NULL )
		return( 0 );
	while( kstat_chain_update( kctl ) != 0 )
		;

	totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));

	/*
	 *  traverse the kstat chain to find the appropriate statistics
	 */
	if( (ksp = kstat_lookup( kctl, "unix", 0, "system_pages" )) == NULL )
		return( 0 );
	if( kstat_read( kctl, ksp, NULL ) == -1 )
		return( 0 );

	/*
	 *  lookup the data
	 */
	 kdata = (kstat_named_t *) kstat_data_lookup( ksp, "freemem" );
	 if( kdata != NULL )
	 	freemem = pagetok( kdata->value.ui32 );

	kstat_close( kctl );
#endif /* ! HAVE_KSTAT */

	Dirty = 0;

	return( 0 );
}
Exemple #23
0
get_system_info(struct system_info *si)
{
    long avenrun[3];
    long total;

    /* get the cp_time array */
    (void) getkval(cp_time_offset, (int *)cp_time, sizeof(cp_time),
                   "_cp_time");

    /* get load average array */
    (void) getkval(avenrun_offset, (int *)avenrun, sizeof(avenrun),
                   "_avenrun");

    /* get mpid -- process id of last process */
    (void) getkval(mpid_offset, &(si->last_pid), sizeof(si->last_pid),
                   "_mpid");

    /* convert load averages to doubles */
    {
        register int i;
        for(i=0; i<3; i++)
            si->load_avg[i] = ((double)avenrun[i])/LSCALE;
    }

    /* convert cp_time counts to percentages */
    total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);

    /* sum memory statistics */
    {
        /* get total -- systemwide main memory usage structure */
        /* Does not work on NeXT system.  Use vm_statistics() for paging info. */
        /* struct vmtotal total;
         * (void) getkval(total_offset, (int *)(&total), sizeof(total),
         *	       "_total");
         */
        /* convert memory stats to Kbytes */
        /* memory_stats[0] = -1;
         * memory_stats[1] = pagetok(total.t_arm);
         * memory_stats[2] = pagetok(total.t_rm);
         * memory_stats[3] = -1;
         * memory_stats[4] = pagetok(total.t_avm);
         * memory_stats[5] = pagetok(total.t_vm);
         * memory_stats[6] = -1;
         * memory_stats[7] = pagetok(total.t_free);
         */
        kern_return_t status;
        unsigned int count=HOST_BASIC_INFO_COUNT;
        status = vm_statistics(task_self(), &vm_stats);
#ifdef DEBUG
        if(status != KERN_SUCCESS)
            mach_error("An error calling vm_statistics()!", status);
#endif
        status = host_info(host_self(), HOST_BASIC_INFO, (host_info_t)&host_stats, &count);
#ifdef DEBUG
        if(status != KERN_SUCCESS)
            mach_error("An error calling host_info()!", status);
#endif
        /* convert memory stats to Kbytes */
        memory_stats[0] = pagetok(host_stats.memory_size / vm_stats.pagesize);
        memory_stats[1] = pagetok(vm_stats.active_count);
        memory_stats[2] = pagetok(vm_stats.inactive_count);
        memory_stats[3] = pagetok(vm_stats.wire_count);
        memory_stats[4] = pagetok(vm_stats.free_count);
        if (swappgsin < 0)
        {
            memory_stats[5] = 1;
            memory_stats[6] = 1;
        } else {
            memory_stats[5] = pagetok(((vm_stats.pageins - swappgsin)));
            memory_stats[6] = pagetok(((vm_stats.pageouts - swappgsout)));
        }
        swappgsin = vm_stats.pageins;
        swappgsout = vm_stats.pageouts;
    }

    /* set arrays and strings */
    si->cpustates = cpu_states;
    si->memory = memory_stats;
}
Exemple #24
0
/**
 * This routine returns kbyte of real memory in use.
 * @return: TRUE if successful, FALSE if failed (or not available)
 */
int used_system_memory_sysdep(SystemInfo_T *si) {
  int                 i, n, num;
  kstat_ctl_t        *kctl;  
  kstat_named_t      *knamed;
  kstat_t            *kstat;
  swaptbl_t          *s;
  char               *strtab;
  unsigned long long  total = 0ULL;
  unsigned long long  used  = 0ULL;

  /* Memory */
  kctl  = kstat_open();
  kstat = kstat_lookup(kctl, "unix", 0, "system_pages");
  if (kstat_read(kctl, kstat, 0) == -1) {
    LogError("system statistic error -- memory usage gathering failed\n");
    kstat_close(kctl);
    return FALSE;
  }
  knamed = kstat_data_lookup(kstat, "freemem");
  if (knamed)
    si->total_mem_kbyte = systeminfo.mem_kbyte_max-pagetok(knamed->value.ul);
  kstat_close(kctl);

 /* Swap */
again:
 if ((num = swapctl(SC_GETNSWP, 0)) == -1) {
    LogError("system statistic error -- swap usage gathering failed: %s\n", STRERROR);
    return FALSE;
  }
  if (num == 0) {
    DEBUG("system statistic -- no swap configured\n");
    si->swap_kbyte_max = 0;
    return TRUE;
  }
  s = (swaptbl_t *)xmalloc(num * sizeof(swapent_t) + sizeof(struct swaptable));
  strtab = (char *)xmalloc((num + 1) * MAXSTRSIZE);
  for (i = 0; i < (num + 1); i++)
    s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
  s->swt_n = num + 1;
  if ((n = swapctl(SC_LIST, s)) < 0) {
    LogError("system statistic error -- swap usage gathering failed: %s\n", STRERROR);
    si->swap_kbyte_max = 0;
    FREE(s);
    FREE(strtab);
    return FALSE;
  }
  if (n > num) {
    DEBUG("system statistic -- new swap added: deferring swap usage statistics to next cycle\n");
    FREE(s);
    FREE(strtab);
    goto again;
  }
  for (i = 0; i < n; i++) {
    if (!(s->swt_ent[i].ste_flags & ST_INDEL) && !(s->swt_ent[i].ste_flags & ST_DOINGDEL)) {
      total += s->swt_ent[i].ste_pages;
      used  += s->swt_ent[i].ste_pages - s->swt_ent[i].ste_free;
    }
  }
  FREE(s);
  FREE(strtab);
  si->swap_kbyte_max   = (unsigned long)(double)(total * page_size) / 1024.;
  si->total_swap_kbyte = (unsigned long)(double)(used  * page_size) / 1024.;

  return TRUE;
}
Exemple #25
0
void
get_system_info(struct system_info *si)
{
	static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
	static int vmtotal_mib[] = {CTL_VM, VM_METER};
	struct loadavg sysload;
	struct vmtotal vmtotal;
	double *infoloadp;
	size_t size;
	int i;
	int64_t *tmpstate;

	if (ncpu > 1) {
		size = CPUSTATES * sizeof(int64_t);
		for (i = 0; i < ncpu; i++) {
			int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, i};
			tmpstate = cpu_states + (CPUSTATES * i);
			if (sysctl(cp_time_mib, 3, cp_time[i], &size, NULL, 0) < 0)
				warn("sysctl kern.cp_time2 failed");
			/* convert cp_time2 counts to percentages */
			(void) percentages(CPUSTATES, tmpstate, cp_time[i],
			    cp_old[i], cp_diff[i]);
		}
	} else {
		int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
		long cp_time_tmp[CPUSTATES];

		size = sizeof(cp_time_tmp);
		if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0)
			warn("sysctl kern.cp_time failed");
		for (i = 0; i < CPUSTATES; i++)
			cp_time[0][i] = cp_time_tmp[i];
		/* convert cp_time counts to percentages */
		(void) percentages(CPUSTATES, cpu_states, cp_time[0],
		    cp_old[0], cp_diff[0]);
	}

	size = sizeof(sysload);
	if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0)
		warn("sysctl failed");
	infoloadp = si->load_avg;
	for (i = 0; i < 3; i++)
		*infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;


	/* get total -- systemwide main memory usage structure */
	size = sizeof(vmtotal);
	if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
		warn("sysctl failed");
		bzero(&vmtotal, sizeof(vmtotal));
	}
	/* convert memory stats to Kbytes */
	memory_stats[0] = -1;
	memory_stats[1] = pagetok(vmtotal.t_arm);
	memory_stats[2] = pagetok(vmtotal.t_rm);
	memory_stats[3] = -1;
	memory_stats[4] = pagetok(vmtotal.t_free);
	memory_stats[5] = -1;

	if (!swapmode(&memory_stats[6], &memory_stats[7])) {
		memory_stats[6] = 0;
		memory_stats[7] = 0;
	}

	/* set arrays and strings */
	si->cpustates = cpu_states;
	si->memory = memory_stats;
	si->last_pid = -1;
}
Exemple #26
0
int updateSwap( int upd ) {
	long			swaptotal;
	long			swapfree;
	long			swapused;
#ifdef HAVE_KSTAT
	kstat_ctl_t		*kctl;
	kstat_t			*ksp;
	struct timeval 		sampling;
	vminfo_t		vmi;

	uint64_t _swap_resv = 0;
	uint64_t _swap_free = 0;
	uint64_t _swap_avail = 0;
	uint64_t _swap_alloc = 0;

#endif /* HAVE_KSTAT */
	swaptotal = swapused = swapfree = 0L;

#ifndef HAVE_KSTAT
	/*
	 *  Retrieve overall swap information from anonymous memory structure -
	 *  which is the same way "swap -s" retrieves it's statistics.
	 *
	 *  swapctl(SC_LIST, void *arg) does not return what we are looking for.
	 */

	if (swapctl(SC_AINFO, &am_swap) == -1)
		return(0);

	swaptotal = am_swap.ani_max;
	swapused = am_swap.ani_resv;
	swapfree = swaptotal - swapused;

	usedswap = pagetok(swapused);
	freeswap = pagetok(swapfree);

#else /* HAVE_KSTAT */
	/*
	 *  get a kstat handle and update the user's kstat chain
	 */
	if( (kctl = kstat_open()) == NULL )
		return( 0 );
	while( kstat_chain_update( kctl ) != 0 )
		;

	totalmem = pagetok( sysconf( _SC_PHYS_PAGES ));

	if( (ksp = kstat_lookup( kctl, "unix", -1, "vminfo" )) == NULL ) {
		goto done;
	}

	if( kstat_read( kctl, ksp, &vmi ) == -1 ) {
                goto done;
        }

	_swap_resv = vmi.swap_resv - swap_resv;
	if (_swap_resv)
		swap_resv = vmi.swap_resv;

	_swap_free = vmi.swap_free - swap_free;
	if (_swap_free)
		swap_free = vmi.swap_free;

	_swap_avail = vmi.swap_avail - swap_avail;
	if (_swap_avail)
		swap_avail = vmi.swap_avail;

	_swap_alloc = vmi.swap_alloc - swap_alloc;
	if (_swap_alloc)
		swap_alloc = vmi.swap_alloc;

	if (upd) {
		long timeInterval;

		gettimeofday(&sampling, 0);
		timeInterval = sampling.tv_sec - lastSampling.tv_sec +
		    ( sampling.tv_usec - lastSampling.tv_usec ) / 1000000.0;
		lastSampling = sampling;
		timeInterval = timeInterval > 0 ? timeInterval : 1;

		_swap_resv = _swap_resv / timeInterval;
		_swap_free = _swap_free / timeInterval;
		_swap_avail = _swap_avail / timeInterval;
		_swap_alloc = _swap_alloc / timeInterval;

		if (_swap_alloc)
			usedswap = pagetok((unsigned long)(_swap_alloc + _swap_free - _swap_avail));

		/*
		 * Assume minfree = totalmem / 8, i.e. it has not been tuned
		 */
		if (_swap_avail)
			freeswap = pagetok((unsigned long)(_swap_avail + totalmem / 8));
	}
done:
	kstat_close( kctl );
#endif /* ! HAVE_KSTAT */

	return( 0 );
}
Exemple #27
0
GetHwInfo()
{
	struct tbl_sysinfo		sibuf;
	vm_statistics_data_t	vmstats;
	int		r_tot;
	long	old_ticks[4],new_ticks[4],diff_ticks[4];
	long	delta_ticks=0;
	int		i;

	/* CPU의 사용량을 계산한다. */
	loc_sadb->cpuCount = 1; /* TRU64는 모든 CPU의 값을 합해서 사용한다 */

	if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) {
		Error("TBL_SYSINFO");
	}
	old_ticks[0] = sibuf.si_user ; old_ticks[1] = sibuf.si_nice;
	old_ticks[2] = sibuf.si_sys  ; old_ticks[3] = sibuf.si_idle;

	commlib_microSleep(100000);
	if (table(TBL_SYSINFO,0,&sibuf,1,sizeof(struct tbl_sysinfo))<0) {
		Error("TBL_SYSINFO");
	}
	new_ticks[0] = sibuf.si_user ; new_ticks[1] = sibuf.si_nice;
	new_ticks[2] = sibuf.si_sys  ; new_ticks[3] = sibuf.si_idle;

	for(i=0;i<4;i++) {
		diff_ticks[i] = new_ticks[i] - old_ticks[i];
		delta_ticks += diff_ticks[i];
	}

	if(delta_ticks) {
		loc_sadb->cpu_usage[0] = 1000 - (long)(diff_ticks[3]*1000/delta_ticks);
	}

	/* MEM의 사용량을 계산한다. */
#if 1
	(void) vm_statistics(task_self(),&vmstats);
	r_tot = pagetok((vmstats.free_count +
		vmstats.active_count + vmstats.inactive_count +
		vmstats.wire_count ))/1024;
	loc_sadb->mem_usage = (long) (1000*pagetok(vmstats.active_count + 
		vmstats.wire_count)/1024) /r_tot;
#endif

#if 0 /* shell command : "vmstat -r 1" */
	loc_sadb->mem_usage = getMemUsage_sh ();
#endif

	/* 통계 데이타를 위해 */
	statistic_cnt++;
#ifdef DEBUG
fprintf(stderr,"jean2222 statistic_cnt %d\n", statistic_cnt);
#endif
	for ( i=0 ; i < loc_sadb->cpuCount ; i++) {
		system_statistic.average_cpu[i] += loc_sadb->cpu_usage[i];
		if (system_statistic.max_cpu[i] < loc_sadb->cpu_usage[i] ) {
			system_statistic.max_cpu[i] = loc_sadb->cpu_usage[i];
		}
	}
	system_statistic.average_mem += loc_sadb->mem_usage;
	if (system_statistic.max_mem < loc_sadb->mem_usage)
		system_statistic.max_mem = loc_sadb->mem_usage;
}