Example #1
0
static double get_cpu_load(void)
{
   double cpu_load;
   long cpu_new[CPUSTATES];
   static long cpu_old[CPUSTATES], cpu_diff[CPUSTATES];
   static double cpu_states[CPUSTATES];
   char buffer[1024];
   FILE *fp;

   fp = fopen(LINUX_KERNEL_STAT, "r");
   if (fp == NULL)
      return -1;

   if (fgets(buffer, sizeof(buffer), fp) == NULL)
      return -1;

   fclose(fp);

   if (sscanf(buffer, "%*s %ld %ld %ld %ld", &cpu_new[0], &cpu_new[1], &cpu_new[2], &cpu_new[3]) != CPUSTATES)
      return -1;

   percentages(CPUSTATES, cpu_states, cpu_new, cpu_old, cpu_diff);

   cpu_load = cpu_states[0] + cpu_states[1] + cpu_states[2];

   if (cpu_load < 0.0)
      cpu_load = -1.0;

   return cpu_load;
}
Example #2
0
int
updateCpuInfo(void)
{
        kvm_read(kd, cp_time_offset, (char *)cp_time, sizeof(cp_time));
        percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
	return (0);
}
Example #3
0
double get_cpu_load() 
{
   static long cpu_new[CPUSTATES];
   static long cpu_old[CPUSTATES];
   static long cpu_diff[CPUSTATES]; 
   double cpu_states[CPUSTATES];
   double cpu_load;
   struct sysinfo sys_info;
   int i;

   if (sysmp(MP_SAGET, MPSA_SINFO, &sys_info, sizeof(struct sysinfo)) == -1) {
      return -1.0;
   }

   for (i = 0; i < CPUSTATES; i++) {
      cpu_new[i] = sys_info.cpu[i];
   }

   percentages(CPUSTATES, cpu_states, cpu_new, cpu_old, cpu_diff);

   cpu_load = cpu_states[1] + cpu_states[2] + cpu_states[3] 
      + cpu_states[4] + cpu_states[5];
   if (cpu_load < 0.0) {
      cpu_load = -1.0;
   }
   return cpu_load;
}
Example #4
0
int updateCpuInfo(void)
{
    static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
    size_t size;
    size = sizeof(cp_time);
    sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0);
    percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
    return (0);
}
Example #5
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;
}
Example #6
0
double get_cpu_load(void) {
   int cpus_found, i, j;
   double cpu_load = -1.0;
   static long cpu_time[CPUSTATES] = { 0L, 0L, 0L, 0L, 0L};
   static long cpu_old[CPUSTATES]  = { 0L, 0L, 0L, 0L, 0L};
   static long cpu_diff[CPUSTATES] = { 0L, 0L, 0L, 0L, 0L}; 
   double cpu_states[CPUSTATES];
   int avenrun[3];

   DENTER(CULL_LAYER, "get_cpu_load.solaris");

   /* use kstat to update all processor information */
   if ((cpus_found = kupdate(avenrun)) < 0 ) {
      DEXIT;
      return cpu_load;
   }
   for (i=0; i<CPUSTATES; i++)
      cpu_time[i] = 0;

   for (i = 0; i < cpus_found; i++) {
      /* sum counters up to, but not including, wait state counter */
      for (j = 0; j < CPU_WAIT; j++) {
         cpu_time[j] += (long) cpu_stat[i].cpu_sysinfo.cpu[j];
         DPRINTF(("cpu_time[%d] = %ld (+ %ld)\n", j,  cpu_time[j],
               (long) cpu_stat[i].cpu_sysinfo.cpu[j]));
      }
      /* add in wait state breakdown counters */
      cpu_time[CPUSTATE_IOWAIT] += (long) cpu_stat[i].cpu_sysinfo.wait[W_IO] +
                                   (long) cpu_stat[i].cpu_sysinfo.wait[W_PIO];
      DPRINTF(("cpu_time[%d] = %ld (+ %ld)\n", CPUSTATE_IOWAIT,  cpu_time[CPUSTATE_IOWAIT],
                                (long) cpu_stat[i].cpu_sysinfo.wait[W_IO] +
                                (long) cpu_stat[i].cpu_sysinfo.wait[W_PIO]));


      cpu_time[CPUSTATE_SWAP] += (long) cpu_stat[i].cpu_sysinfo.wait[W_SWAP];
      DPRINTF(("cpu_time[%d] = %ld (+ %ld)\n", CPUSTATE_SWAP,  cpu_time[CPUSTATE_SWAP],
         (long) cpu_stat[i].cpu_sysinfo.wait[W_SWAP]));
   }
   percentages(CPUSTATES, cpu_states, cpu_time, cpu_old, cpu_diff);
   cpu_load = cpu_states[1] + cpu_states[2] + cpu_states[3] + cpu_states[4];
   DPRINTF(("cpu_load %f ( %f %f %f %f )\n", cpu_load,
         cpu_states[1], cpu_states[2], cpu_states[3], cpu_states[4]));
#if 0
#if defined(SOLARIS) && !defined(SOLARIS64)
   DPRINTF(("avenrun(%d %d %d) -> (%f %f %f)\n", avenrun[0], avenrun[1], avenrun[2],
      KERNEL_TO_USER_AVG(avenrun[0]), KERNEL_TO_USER_AVG(avenrun[1]), KERNEL_TO_USER_AVG(avenrun[2])));
#endif
#endif

   DEXIT;
   return cpu_load;
}
Example #7
0
void
get_system_info(struct system_info * si)

{
	long		total;

	/* get process id of the last process */
	(void) getkval(nlst[X_MPID].n_value, &(si->last_pid),
				   sizeof(si->last_pid),
				   nlst[X_MPID].n_name);
	/* get the cp_time array */
	(void) getkval(nlst[X_SYSINFO].n_value, (int *) cp_time, sizeof(cp_time),
				   nlst[X_SYSINFO].n_name);

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

	/* sum memory statistics */
	(void) getkval(nlst[X_PHYSMEM].n_value, &memory_stats[0],
				   sizeof(memory_stats[0]), nlst[X_PHYSMEM].n_name);
	(void) getkval(nlst[X_MAXMEM].n_value, &memory_stats[1],
				   sizeof(memory_stats[1]), nlst[X_MAXMEM].n_name);
	(void) getkval(nlst[X_FREEMEM].n_value, &memory_stats[2],
				   sizeof(memory_stats[2]), nlst[X_FREEMEM].n_name);
	(void) getkval(nlst[X_AVAILRMEM].n_value, &memory_stats[3],
				   sizeof(memory_stats[3]), nlst[X_AVAILRMEM].n_name);
	(void) getkval(nlst[X_AVAILSMEM].n_value, &memory_stats[4],
				   sizeof(memory_stats[4]), nlst[X_AVAILSMEM].n_name);
	(void) getkval(nlst[X_NSWAP].n_value, &memory_stats[5],
				   sizeof(memory_stats[5]), nlst[X_NSWAP].n_name);
	memory_stats[0] = bytetok(ctob(memory_stats[0]));	/* clicks -> bytes */
	memory_stats[1] = bytetok(ctob(memory_stats[1]));	/* clicks -> bytes */
	memory_stats[2] = bytetok(ctob(memory_stats[2]));	/* clicks -> bytes */
	memory_stats[3] = bytetok(memory_stats[3] * NBPP);	/* # bytes per page */
	memory_stats[4] = bytetok(memory_stats[4] * NBPP);	/* # bytes per page */
	memory_stats[5] = bytetok(memory_stats[5] * NBPSCTR);		/* # bytes per sector */

	/* set arrays and strings */

	/*
	 * Note: we keep memory_stats as an unsigned long to avoid sign extension
	 * problems when shifting in bytetok. But the module interface requires an
	 * array of signed longs. So we just cast the pointer here and hope for
	 * the best.   --wnl
	 */
	si->cpustates = cpu_states;
	si->memory = (long *) memory_stats;

	tab_avenrun(si->load_avg);	/* philiph */
}
Example #8
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;
}
Example #9
0
int
updateCpuInfo(void)
{
    int sid, id, tot_freq = 0, tot_temp = 0, freq_count = 0, temp_count = 0;
    char name[SYSCTL_ID_LEN];

    if (cp_time == NULL || freq == NULL || temp == NULL)
        return (0);

        size_t len = sizeof(long) * CPUSTATES * cores;
        sysctlbyname("kern.cp_times", cp_time, &len, NULL, 0);
    for (sid = 0; sid < CPUSTATES; ++sid)
        cpu_states[cores][sid] = 0;
    for (id = 0; id < cores; ++id) {
        percentages(CPUSTATES, cpu_states[id], cp_time[id], cp_old[id], cp_diff[id]);
        for (sid = 0; sid < CPUSTATES; ++sid)
            cpu_states[cores][sid] += cpu_states[id][sid];
    }
    for (id = 0; id < cores; ++id) {
        if (freq[id][0] != -1) {
            len = sizeof(int);
            snprintf(name, SYSCTL_ID_LEN, "dev.cpu.%d.freq", id);
            freq[id][0] = 0;
            if (!sysctlbyname(name, &freq[id][0], &len, NULL, 0)) {
                freq_count += 1;
                tot_freq += freq[id][0];
            }
        }
        if (temp[id] != -1) {
            len = sizeof(int);
            snprintf(name, SYSCTL_ID_LEN, "dev.cpu.%d.temperature", id);
            temp[id] = 0.0;
            if (!sysctlbyname(name, &temp[id], &len, NULL, 0)) {
                temp_count += 1;
                tot_temp += temp[id];
            }
        }
    }
    freq[cores][0] = freq_count == 0 ? 0 : tot_freq * 100 / freq_count;
    temp[cores] = temp_count == 0 ? 0.0 : tot_temp * 100 / temp_count;

    return (0);
}
Example #10
0
/*******************************************************************************
 * GET CPU ROAD VLAUE FROM /pro/stat FILE
*******************************************************************************/
int Read_CpuLoad(unsigned long long *system, unsigned long long *user, unsigned long long *nice, unsigned long long *idle, unsigned long long *total)
{
    char path[20] = "/proc/stat" ;
    char buff[1024];
	long long cpustate[4];
	static long long oldstate[4];
	static long long newstate[4];
	static long long diffstate[4];

    FILE *fp;

    fp = fopen( path, "r" );

    if( fp == NULL )
    {
        log_print(LOGN_DEBUG,"FILE OPEN ERROR [%s]\n", path);
        return -1;
    }

    memset( buff, 0x00, sizeof(char)*1024);

    fgets( buff, 1024, fp );

    fclose(fp);

    sscanf( buff, "%s %llu %llu %llu %llu", path, &newstate[0], &newstate[1], &newstate[2], &newstate[3] );


	/***************************************************************************
	 * CONVERT CPU STATUS VALUE
	***************************************************************************/
	percentages( 4, cpustate, newstate, oldstate, diffstate );

	*user = cpustate[0];
	*nice = cpustate[1];
	*system = cpustate[2];
	*idle = cpustate[3];

    *total = *user + *nice + *system + *idle ;

    return 1;
}
Example #11
0
SEXP top_cumprop_internal(Rcpp::RObject incoming, Rcpp::IntegerVector topset) {
    auto mat=beachmat::create_matrix<M>(incoming);
    const size_t ncells=mat->get_ncol();
    const size_t ngenes=mat->get_nrow();

    check_topset(topset);
    Rcpp::NumericMatrix percentages(topset.size(), ncells);
    typename M::vector holder(ngenes); 

    for (size_t c=0; c<ncells; ++c) {
        mat->get_col(c, holder.begin()); // need to copy as cumsum will change ordering.
        double totals=std::accumulate(holder.begin(), holder.end(), static_cast<typename M::type>(0));

        auto cur_col=percentages.column(c);
        compute_cumsum<typename M::type, typename M::vector>(holder.begin(), ngenes, topset, cur_col.begin());
        for (auto& p : cur_col) { p/=totals; }
    }

    return percentages;
}
Example #12
0
static double get_cpu_load()
{
   kernel_fd_type kernel_fd;
   long address = 0;
   static long cpu_time[CPUSTATES];
   static long cpu_old[CPUSTATES];
   static long cpu_diff[CPUSTATES];
   double cpu_states[CPUSTATES];
   double cpu_load;

   if (sge_get_kernel_fd(&kernel_fd)
       && sge_get_kernel_address("cp_time", &address)) {
      getkval(address, (void*)&cpu_time, sizeof(cpu_time), "cp_time"); 
      percentages(CPUSTATES, cpu_states, cpu_time, cpu_old, cpu_diff);
      cpu_load = cpu_states[0] + cpu_states[1] + cpu_states[2];
      if (cpu_load < 0.0) {
         cpu_load = -1.0;
      }
   } else {
      cpu_load = -1.0;
   }
   return cpu_load;
}    
Example #13
0
void
get_system_info(struct system_info * info)

{
	char		buffer[4096 + 1];
	int			fd,
				len;
	char	   *p;

	/* get load averages */

	if ((fd = open("loadavg", O_RDONLY)) != -1)
	{
		if ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0)
		{
			buffer[len] = '\0';
			info->load_avg[0] = strtod(buffer, &p);
			info->load_avg[1] = strtod(p, &p);
			info->load_avg[2] = strtod(p, &p);
			p = skip_token(p);	/* skip running/tasks */
			p = skip_ws(p);
			if (*p)
			{
				info->last_pid = atoi(p);
			}
			else
			{
				info->last_pid = -1;
			}
		}
		close(fd);
	}

	/* get the cpu time info */
	if ((fd = open("stat", O_RDONLY)) != -1)
	{
		if ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0)
		{
			buffer[len] = '\0';
			p = skip_token(buffer);		/* "cpu" */
			cp_time[0] = strtoul(p, &p, 0);
			cp_time[1] = strtoul(p, &p, 0);
			cp_time[2] = strtoul(p, &p, 0);
			cp_time[3] = strtoul(p, &p, 0);
			if (show_iowait)
			{
				cp_time[4] = strtoul(p, &p, 0);
			}

			/* convert cp_time counts to percentages */
			percentages(NCPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
		}
		close(fd);
	}

	/* get system wide memory usage */
	if ((fd = open("meminfo", O_RDONLY)) != -1)
	{
		char	   *p;
		int			mem = 0;
		int			swap = 0;
		unsigned long memtotal = 0;
		unsigned long memfree = 0;
		unsigned long swaptotal = 0;

		if ((len = read(fd, buffer, sizeof(buffer) - 1)) > 0)
		{
			buffer[len] = '\0';
			p = buffer - 1;

			/* iterate thru the lines */
			while (p != NULL)
			{
				p++;
				if (p[0] == ' ' || p[0] == '\t')
				{
					/* skip */
				}
				else if (strncmp(p, "Mem:", 4) == 0)
				{
					p = skip_token(p);	/* "Mem:" */
					p = skip_token(p);	/* total memory */
					memory_stats[MEMUSED] = strtoul(p, &p, 10);
					memory_stats[MEMFREE] = strtoul(p, &p, 10);
					memory_stats[MEMSHARED] = strtoul(p, &p, 10);
					memory_stats[MEMBUFFERS] = strtoul(p, &p, 10);
					memory_stats[MEMCACHED] = strtoul(p, &p, 10);
					memory_stats[MEMUSED] = bytetok(memory_stats[MEMUSED]);
					memory_stats[MEMFREE] = bytetok(memory_stats[MEMFREE]);
					memory_stats[MEMSHARED] = bytetok(memory_stats[MEMSHARED]);
					memory_stats[MEMBUFFERS] =
							bytetok(memory_stats[MEMBUFFERS]);
					memory_stats[MEMCACHED] = bytetok(memory_stats[MEMCACHED]);
					mem = 1;
				}
				else if (strncmp(p, "Swap:", 5) == 0)
				{
					p = skip_token(p);	/* "Swap:" */
					p = skip_token(p);	/* total swap */
					swap_stats[SWAPUSED] = strtoul(p, &p, 10);
					swap_stats[SWAPFREE] = strtoul(p, &p, 10);
					swap_stats[SWAPUSED] = bytetok(swap_stats[SWAPUSED]);
					swap_stats[SWAPFREE] = bytetok(swap_stats[SWAPFREE]);
					swap = 1;
				}
				else if (!mem && strncmp(p, "MemTotal:", 9) == 0)
				{
					p = skip_token(p);
					memtotal = strtoul(p, &p, 10);
				}
				else if (!mem && memtotal > 0 && strncmp(p, "MemFree:", 8) == 0)
				{
					p = skip_token(p);
					memfree = strtoul(p, &p, 10);
					memory_stats[MEMUSED] = memtotal - memfree;
					memory_stats[MEMFREE] = memfree;
				}
				else if (!mem && strncmp(p, "MemShared:", 10) == 0)
				{
					p = skip_token(p);
					memory_stats[MEMSHARED] = strtoul(p, &p, 10);
				}
				else if (!mem && strncmp(p, "Buffers:", 8) == 0)
				{
					p = skip_token(p);
					memory_stats[MEMBUFFERS] = strtoul(p, &p, 10);
				}
				else if (!mem && strncmp(p, "Cached:", 7) == 0)
				{
					p = skip_token(p);
					memory_stats[MEMCACHED] = strtoul(p, &p, 10);
				}
				else if (!swap && strncmp(p, "SwapTotal:", 10) == 0)
				{
					p = skip_token(p);
					swaptotal = strtoul(p, &p, 10);
				}
				else if (!swap && swaptotal > 0 && strncmp(p, "SwapFree:", 9) == 0)
				{
					p = skip_token(p);
					memfree = strtoul(p, &p, 10);
					swap_stats[SWAPUSED] = swaptotal - memfree;
					swap_stats[SWAPFREE] = memfree;
				}
				else if (!mem && strncmp(p, "SwapCached:", 11) == 0)
				{
					p = skip_token(p);
					swap_stats[SWAPCACHED] = strtoul(p, &p, 10);
				}

				/* move to the next line */
				p = strchr(p, '\n');
			}
		}
		close(fd);
	}

	/* set arrays and strings */
	info->cpustates = cpu_states;
	info->memory = memory_stats;
	info->swap = swap_stats;
}
Example #14
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;
}
Example #15
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;
}
static int
get_system_info (kstat_ctl_t *kc, sys_stat *sst)
{
  kstat_t *ksp;
  kstat_named_t *kn;
  kid_t kcid, nkcid;
  int totalswap, freeswap;
  int i, j;
  cpu_stat_t cpu_stat[MAX_CPU];
  static int pagesize = 0, maxmem = 0;
  static long cp_time[CPUSTATES];
  static long cp_old[CPUSTATES];
  static long cp_diff[CPUSTATES];
  static kstat_t *cpu_ks[MAX_CPU];
  static int ncpu = 0;
  static int freemem_check_time = 0;
  int changed = 0;

  kcid = kc->kc_chain_id;

kcid_changed:

  nkcid = kstat_chain_update (kc);
  if (nkcid)
    {
      kcid = nkcid;
      changed = 1;
    }
  CHECK_KCID (nkcid, 0);

  ksp = kstat_lookup (kc, "unix", -1, "system_misc");
  nkcid = kstat_read (kc, ksp, NULL);
  CHECK_KCID (nkcid, kcid);

  /*
   *  collect load average information
   */
  kn = kstat_data_lookup (ksp, "avenrun_1min");
  sst->load_avg[0] = kn->value.ui32;
  kn = kstat_data_lookup (ksp, "avenrun_5min");
  sst->load_avg[1] = kn->value.ui32;
  kn = kstat_data_lookup (ksp, "avenrun_15min");
  sst->load_avg[2] = kn->value.ui32;

  /*
   *  collect cpu information
   */
  if (changed == 1 || ncpu == 0)
    {
      ncpu = 0;
      for (ksp = kc->kc_chain; ksp && ncpu < MAX_CPU; ksp = ksp->ks_next)
        {
          if (strncmp (ksp->ks_name, "cpu_stat", 8) == 0)
            {
              nkcid = kstat_read (kc, ksp, NULL);
              CHECK_KCID (nkcid, kcid);
              cpu_ks[ncpu] = ksp;
              ncpu++;
            }
        }
    }

  for (i = 0; i < ncpu; i++)
    {
      nkcid = kstat_read (kc, cpu_ks[i], & (cpu_stat[i]));
      CHECK_KCID (nkcid, kcid);
    }

  for (j = 0; j < CPUSTATES; j++)
    {
      cp_time[j] = 0L;
    }

  for (i = 0; i < ncpu; i++)
    {
      for (j = 0; j < CPU_WAIT; j++)
        {
          cp_time[j] += (long) cpu_stat[i].cpu_sysinfo.cpu[j];
        }

      cp_time[CPUSTATE_IOWAIT] += (long) cpu_stat[i].cpu_sysinfo.wait[W_IO] +
                                  (long) cpu_stat[i].cpu_sysinfo.wait[W_PIO];
      cp_time[CPUSTATE_SWAP] = (long) cpu_stat[i].cpu_sysinfo.wait[W_SWAP];
    }

  percentages (CPUSTATES, sst->cpu_states, cp_time, cp_old, cp_diff);

  /*
   *  collect memory information
   */
  if (pagesize == 0)
    {
      pagesize = sysconf (_SC_PAGESIZE);
    }
  if (maxmem == 0)
    {
      maxmem = sysconf (_SC_PHYS_PAGES);
      sst->memory_stats[0] = PAGETOM (maxmem, pagesize);
    }

  if (time (NULL) - freemem_check_time > 30)
    {
      ksp = kstat_lookup (kc, "unix", 0, "system_pages");
      nkcid = kstat_read (kc, ksp, NULL);
      CHECK_KCID (nkcid, kcid);

      kn = kstat_data_lookup (ksp, "freemem");
      sst->memory_stats[2] = PAGETOM (kn->value.ui32, pagesize);

      if (sst->memory_stats[0] - sst->memory_stats[2] > 0)
        {
          sst->memory_stats[1] = sst->memory_stats[0] - sst->memory_stats[2];
        }
      freemem_check_time = time (NULL);
    }

#if 0
  kn = kstat_data_lookup (ksp, "availrmem");
#endif

  get_swapinfo (&totalswap, &freeswap);
  sst->memory_stats[3] = PAGETOM ((totalswap - freeswap), pagesize);
  sst->memory_stats[4] = PAGETOM (freeswap, pagesize);

  return 1;
}
Example #17
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;
}
Example #18
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;
}