예제 #1
0
int	SYSTEM_CPU_INTR(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	kstat_ctl_t	*kc;
	kstat_t		*k;
	cpu_stat_t	*cpu;

	int	cpu_count = 0;
	double	intr_count = 0.0;

	kc = kstat_open();

	if(kc != NULL)
	{
		k = kc->kc_chain;
		while (k != NULL)
		{
			if( (strncmp(k->ks_name, "cpu_stat", 8) == 0) &&
					(kstat_read(kc, k, NULL) != -1) )
			{
				cpu = (cpu_stat_t*) k->ks_data;
				intr_count += (double) cpu->cpu_sysinfo.intr;
				cpu_count += 1;
			}
			k = k->ks_next;
		}
		kstat_close(kc);
	}

	if(cpu_count == 0)
		return SYSINFO_RET_FAIL;

	SET_UI64_RESULT(result, intr_count);

	return SYSINFO_RET_OK;
}
예제 #2
0
void
get_load_average(double *one, double *five, double *fifteen)
{
   kstat_ctl_t        *kc;
   kstat_t            *ks;
   kstat_named_t      *d1, *d5, *d15;

   if (!(kc = kstat_open()))
     {
	SET_AND_RETURN(0, 0, 0);
     }
   if (!(ks = kstat_lookup(kc, "unix", 0, "system_misc")))
     {
	SET_AND_RETURN(0, 0, 0);
     }
   if ((kstat_read(kc, ks, NULL)) < 0)
     {
	SET_AND_RETURN(0, 0, 0);
     }

   d1 = kstat_data_lookup(ks, "avenrun_1min");
   d5 = kstat_data_lookup(ks, "avenrun_5min");
   d15 = kstat_data_lookup(ks, "avenrun_15min");
   kstat_close(kc);

   SET_AND_RETURN(d1->value.ul / FSCALE, d5->value.ul / FSCALE,
		  d15->value.ul / FSCALE);
}
예제 #3
0
int	SYSTEM_BOOTTIME(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result)
{
	kstat_ctl_t	*kc;
	kstat_t		*kp;
	kstat_named_t	*kn;
	int		ret = SYSINFO_RET_FAIL;

	if (NULL == (kc = kstat_open()))
		return ret;

	if (NULL != (kp = kstat_lookup(kc, "unix", 0, "system_misc")))
	{
		if (-1 != kstat_read(kc, kp, 0))
		{
			if (NULL != (kn = (kstat_named_t*)kstat_data_lookup(kp, "boot_time")))
			{
				SET_UI64_RESULT(result, (zbx_uint64_t)kn->value.ul);
				ret = SYSINFO_RET_OK;
			}
		}
	}
	kstat_close(kc);

	return ret;
}
예제 #4
0
static int	get_kstat_system_misc(char *s, int *value)
{
	kstat_ctl_t	*kc;
	kstat_t		*kp;
	kstat_named_t	*kn = NULL;
	int		n, i;

	if (NULL == (kc = kstat_open()))
		return FAIL;

	if (NULL == (kp = kstat_lookup(kc, "unix", 0, "system_misc")))
	{
		kstat_close(kc);
		return FAIL;
	}

	if (-1 == kstat_read(kc, kp, NULL))
	{
		kstat_close(kc);
		return FAIL;
	}

	if (NULL == (kn = (kstat_named_t*)kstat_data_lookup(kp, s)))
	{
		kstat_close(kc);
		return FAIL;
	}

	kstat_close(kc);

	*value = kn->value.ul;

	return SUCCEED;
}
예제 #5
0
void
DisKstat(void)
{
    int i;
    kstat_ctl_t	*kc;
    kstat_t		*ksp;

    kc = kstat_open();
    if (kc == NULL) {
        Mvprintw(ln++, 0, catgets(catfd, SET, 7383,
                                  "kernel statistics not available"));
        return;
    }
    for (ksp = kc->kc_chain, i = 0; ksp != NULL; ksp = ksp->ks_next) {
        if (strcmp(ksp->ks_module, "sam-qfs") == 0) {
            if (i == KsIndex || ScreenMode == FALSE) {
                kstat_read(kc, ksp, NULL);
                DisKstatInfo(ksp);
                if (ksp->ks_next && ScreenMode == TRUE) {
                    Mvprintw(ln, 0, catgets(catfd, SET,
                                            7003, "     more (ctrl-f)"));
                }
            }
            i++;
        }
    }
    kstat_close(kc);
}
예제 #6
0
JNIEXPORT jlong JNICALL Java_com_jezhumble_javasysmon_SolarisMonitor_uptimeInSeconds (JNIEnv *env, jobject obj)
{
  struct timeval secs;
  kstat_ctl_t   *kc; 
  kstat_t       *ksp; 
  kstat_named_t *knp; 
  unsigned long long uptime;

  if (gettimeofday(&secs, NULL) != 0) {
    return (jlong) 0;
  }
  uptime = (unsigned long long) secs.tv_sec;

  kc = kstat_open();
  if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL) {
    fprintf(stderr, "%s\n", "ERROR: Can't read boot time.");
    return 0;
  }
  if ((kstat_read(kc, ksp, NULL) != -1) &&
  /* lookup the boot time record */
    ((knp = kstat_data_lookup(ksp, "boot_time")) != NULL)) {
      return (jlong) (uptime - knp->value.ui32);
  } else {
    return 0;
  }
}
예제 #7
0
JNIEXPORT jobject JNICALL Java_com_jezhumble_javasysmon_SolarisMonitor_cpuTimes (JNIEnv *env, jobject obj)
{
  kstat_ctl_t   *kc;
  kstat_t       *ksp;
  struct cpu_stat cpu_stats;
  int i;
  unsigned long long userticks, systicks, idleticks;
  jclass		cpu_times_class;
  jmethodID	cpu_times_constructor;
  jobject		cpu_times;

  idleticks = systicks = userticks = 0; 
  kc = kstat_open();
  for (i = 0; i < num_cpus; i++) {
    // the next line is wrong: should replace "cpu_stat0" with "cpu_stati"
    // but my C-fu is insufficient to work out how to do this in <10 lines
    if ((ksp = kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0")) != NULL) {
      kstat_read(kc, ksp, &cpu_stats);
      idleticks += cpu_stats.cpu_sysinfo.cpu[CPU_IDLE];
      userticks += cpu_stats.cpu_sysinfo.cpu[CPU_USER];
      systicks += cpu_stats.cpu_sysinfo.cpu[CPU_KERNEL];
      systicks += cpu_stats.cpu_sysinfo.cpu[CPU_WAIT];
    }
  }

  cpu_times_class = (*env)->FindClass(env, "com/jezhumble/javasysmon/CpuTimes");
  cpu_times_constructor = (*env)->GetMethodID(env, cpu_times_class, "<init>", "(JJJ)V");
  cpu_times = (*env)->NewObject(env, cpu_times_class, cpu_times_constructor, (jlong) userticks, (jlong) systicks, (jlong) idleticks);
  (*env)->DeleteLocalRef(env, cpu_times_class);
  return cpu_times;
}
예제 #8
0
파일: ap_rcm.c 프로젝트: bahamas10/openzfs
static int
getsyscpuids(int *ncpuids, cpuid_t **cpuids)
{
	int		ncpu;
	int		maxncpu;
	kstat_t		*ksp;
	kstat_ctl_t	*kc = NULL;
	cpuid_t		*cp;

	DBG("getsyscpuids\n");

	if ((maxncpu = sysconf(_SC_NPROCESSORS_MAX)) == -1 ||
	    (kc = kstat_open()) == NULL ||
	    (cp = (cpuid_t *)calloc(maxncpu, sizeof (cpuid_t))) == NULL) {
		/* if calloc failed, clean up kstats */
		if (kc != NULL) {
			(void) kstat_close(kc);
		}
		return (-1);
	}

	DBG("syscpuids: ");
	for (ncpu = 0, ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
			cp[ncpu++] = ksp->ks_instance;
			DBG("%d ", ksp->ks_instance);
		}
	}
	DBG("\n");

	(void) kstat_close(kc);
	*cpuids = cp;
	*ncpuids = ncpu;
	return (0);
}
예제 #9
0
/*
 * Return the number of physical CPU cores on the system.
 */
static PyObject *
psutil_cpu_count_phys(PyObject *self, PyObject *args) {
    kstat_ctl_t *kc;
    kstat_t *ksp;
    int ncpus = 0;

    kc = kstat_open();
    if (kc == NULL)
        goto error;
    ksp = kstat_lookup(kc, "cpu_info", -1, NULL);
    if (ksp == NULL)
        goto error;

    for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
        if (strcmp(ksp->ks_module, "cpu_info") != 0)
            continue;
        if (kstat_read(kc, ksp, NULL) == -1)
            goto error;
        ncpus += 1;
    }

    kstat_close(kc);
    if (ncpus > 0)
        return Py_BuildValue("i", ncpus);
    else
        goto error;

error:
    // mimic os.cpu_count()
    if (kc != NULL)
        kstat_close(kc);
    Py_RETURN_NONE;
}
예제 #10
0
static void
find_cpu_model_freq(char **cpu_model, int *frequency) {

  kstat_t *ksp;
  kid_t nkcid;
  kstat_named_t *knp;
  int i,found_brand,found_freq;


  found_brand = 0;
  found_freq = 0;

  kc = kstat_open();

  if (NULL == kc) {
    *cpu_model = strdup("kstat_open");
    *frequency = -1;
    return;
  }

  ksp = kstat_lookup(kc, "cpu_info", 0, NULL);

  if ((NULL == ksp) ||
      ((ksp) && (KSTAT_TYPE_NAMED != ksp->ks_type))) {
    *cpu_model = strdup("kstat_lookup");
    *frequency = -1;
    kstat_close(kc);
    return;
  }

  nkcid = kstat_read(kc, ksp, NULL);

  if (-1 == nkcid) {
    *cpu_model = strdup("kstat_read");
    *frequency = -1;
    kstat_close(kc);
    return;
  }

  for (i = ksp->ks_ndata, knp = ksp->ks_data;
       i > 0;
       knp++, i--) {
    if (!strcmp("brand", knp->name)) {
      *cpu_model = strdup(KSTAT_NAMED_STR_PTR(knp));
      found_brand = 1;
    }
    else if (!strcmp("clock_MHz",knp->name)) {
      *frequency = (int)knp->value.ui32;
      found_freq = 1;
    }
  }
  if (!found_brand)
    *cpu_model = strdup("CPU Not Found");
  if (!found_freq)
    *frequency = -1;

  kstat_close(kc);
}
예제 #11
0
파일: guid.c 프로젝트: bongo-project/bongo
void GuidReset(void) {
	char name[256 + 1];
	char salt[32];
#ifdef HAVE_KSTAT_H
	kstat_t *ksp;
	kstat_ctl_t *kc;
#elifdef HAVE_SYS_SYSINFO_H
	struct sysinfo si;
#endif
	struct timeval tv;
	xpl_hash_context context;
	
	XplHashNew(&context, XPLHASH_SHA1);
	
	gettimeofday(&tv, NULL);
	sprintf(salt, "%010lu%010lu", tv.tv_sec, tv.tv_usec);
	XplHashWrite(&context, salt, strlen(salt));

	if (!gethostname(name, sizeof(name) - 1)) {
		XplHashWrite(&context, name, strlen(name));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}

#if HAVE_KSTAT_H
	if ((kc = kstat_open()) &&
		(ksp = kstat_lookup(kc, "unix", 0, "system_misc")) &&
		(kstat_read(kc, ksp, NULL) != -1)) {
		XplHashWrite(&context, ksp, sizeof(kstat_t));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}
	if (kc != NULL) {
		kstat_close(kc);
	}
#elif HAVE_SYS_SYSINFO_H
	if (!sysinfo(&si)) {
		XplHashWrite(&context, &si, sizeof(si));
	} else {
		XplRandomData(name, sizeof(name) - 1);
		XplHashWrite(&context, name, sizeof(name) - 1);
	}
#else 
	/* FIXME: should find a sysinfo equiv. for this */
	XplRandomData(name, sizeof(name) - 1);
	XplHashWrite(&context, name, sizeof(name) - 1);
#endif

	XplHashFinal(&context, XPLHASH_UPPERCASE, StoreAgent.guid.next,
		XPLHASH_SHA1_LENGTH);
	
	memset(StoreAgent.guid.next + NMAP_GUID_PREFIX_LENGTH, '0',
		NMAP_GUID_SUFFIX_LENGTH);

	return;
}
예제 #12
0
/* Feed kernel statistics structures and ks_data field to the RNG.
 * Returns status as well as the number of bytes successfully fed to the RNG.
 */
static SECStatus RNG_kstat(PRUint32* fed)
{
    kstat_ctl_t*    kc = NULL;
    kstat_t*        ksp = NULL;
    PRUint32        entropy_buffered = 0;
    char*           entropy_buf = NULL;
    SECStatus       rv = SECSuccess;

    PORT_Assert(fed);
    if (!fed) {
        return SECFailure;
    }
    *fed = 0;

    kc = kstat_open();
    PORT_Assert(kc);
    if (!kc) {
        return SECFailure;
    }
    entropy_buf = (char*) PORT_Alloc(entropy_buf_len);
    PORT_Assert(entropy_buf);
    if (entropy_buf) {
        for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
            if (-1 == kstat_read(kc, ksp, NULL)) {
                /* missing data from a single kstat shouldn't be fatal */
                continue;
            }
            rv = BufferEntropy((char*)ksp, sizeof(kstat_t),
                                    entropy_buf, &entropy_buffered,
                                    fed);
            if (SECSuccess != rv) {
                break;
            }

            if (ksp->ks_data && ksp->ks_data_size>0 && ksp->ks_ndata>0) {
                rv = BufferEntropy((char*)ksp->ks_data, ksp->ks_data_size,
                                        entropy_buf, &entropy_buffered,
                                        fed);
                if (SECSuccess != rv) {
                    break;
                }
            }
        }
        if (SECSuccess == rv && entropy_buffered) {
            /* Buffer is not empty, time to feed it to the RNG */
            rv = RNG_RandomUpdate(entropy_buf, entropy_buffered);
        }
        PORT_Free(entropy_buf);
    } else {
        rv = SECFailure;
    }
    if (kstat_close(kc)) {
        PORT_Assert(0);
        rv = SECFailure;
    }
    return rv;
}
예제 #13
0
파일: dladm.c 프로젝트: andreiw/polaris
/*
 * In the following routines, we do the first kstat_lookup() assuming that
 * the device is gldv3-based and that the kstat name is the one passed in
 * as the "name" argument. If the lookup fails, we redo the kstat_lookup()
 * omitting the kstat name. This second lookup is needed for getting kstats
 * from legacy devices. This can fail too if the device is not attached or
 * the device is legacy and doesn't export the kstats we need.
 */
static void
get_stats(char *module, int instance, char *name, pktsum_t *stats)
{
	kstat_ctl_t	*kcp;
	kstat_t		*ksp;

	if ((kcp = kstat_open()) == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: kstat open operation failed\n"),
		    progname);
		return;
	}

	if ((ksp = kstat_lookup(kcp, module, instance, name)) == NULL &&
	    (ksp = kstat_lookup(kcp, module, instance, NULL)) == NULL) {
		/*
		 * The kstat query could fail if the underlying MAC
		 * driver was already detached.
		 */
		(void) kstat_close(kcp);
		return;
	}

	if (kstat_read(kcp, ksp, NULL) == -1)
		goto bail;

	if (kstat_value(ksp, "ipackets64", KSTAT_DATA_UINT64,
	    &stats->ipackets) < 0)
		goto bail;

	if (kstat_value(ksp, "opackets64", KSTAT_DATA_UINT64,
	    &stats->opackets) < 0)
		goto bail;

	if (kstat_value(ksp, "rbytes64", KSTAT_DATA_UINT64,
	    &stats->rbytes) < 0)
		goto bail;

	if (kstat_value(ksp, "obytes64", KSTAT_DATA_UINT64,
	    &stats->obytes) < 0)
		goto bail;

	if (kstat_value(ksp, "ierrors", KSTAT_DATA_UINT32,
	    &stats->ierrors) < 0)
		goto bail;

	if (kstat_value(ksp, "oerrors", KSTAT_DATA_UINT32,
	    &stats->oerrors) < 0)
		goto bail;

	(void) kstat_close(kcp);
	return;

bail:
	(void) kstat_close(kcp);
}
예제 #14
0
/*
 * Return system-wide CPU times.
 */
static PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args)
{
    kstat_ctl_t *kc;
    kstat_t *ksp;
    cpu_stat_t cs;
    int numcpus;
    int i;
    PyObject *py_retlist = PyList_New(0);
    PyObject *py_cputime = NULL;

    if (py_retlist == NULL)
        return NULL;

    kc = kstat_open();
    if (kc == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    numcpus = sysconf(_SC_NPROCESSORS_ONLN) - 1;
    for (i = 0; i <= numcpus; i++) {
        ksp = kstat_lookup(kc, "cpu_stat", i, NULL);
        if (ksp == NULL) {
            PyErr_SetFromErrno(PyExc_OSError);
            goto error;
        }
        if (kstat_read(kc, ksp, &cs) == -1) {
            PyErr_SetFromErrno(PyExc_OSError);
            goto error;
        }

        py_cputime = Py_BuildValue("ffff",
                                   (float)cs.cpu_sysinfo.cpu[CPU_USER],
                                   (float)cs.cpu_sysinfo.cpu[CPU_KERNEL],
                                   (float)cs.cpu_sysinfo.cpu[CPU_IDLE],
                                   (float)cs.cpu_sysinfo.cpu[CPU_WAIT]);
        if (py_cputime == NULL)
            goto error;
        if (PyList_Append(py_retlist, py_cputime))
            goto error;
        Py_DECREF(py_cputime);

    }

    kstat_close(kc);
    return py_retlist;

error:
    Py_XDECREF(py_cputime);
    Py_DECREF(py_retlist);
    if (kc != NULL)
        kstat_close(kc);
    return NULL;
}
예제 #15
0
/*
 * Return disk IO statistics.
 */
static PyObject *
psutil_disk_io_counters(PyObject *self, PyObject *args)
{
    kstat_ctl_t *kc;
    kstat_t *ksp;
    kstat_io_t kio;
    PyObject *py_retdict = PyDict_New();
    PyObject *py_disk_info = NULL;

    if (py_retdict == NULL)
        return NULL;
    kc = kstat_open();
    if (kc == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);;
        goto error;
    }
    ksp = kc->kc_chain;
    while (ksp != NULL) {
        if (ksp->ks_type == KSTAT_TYPE_IO) {
            if (strcmp(ksp->ks_class, "disk") == 0) {
                if (kstat_read(kc, ksp, &kio) == -1) {
                    kstat_close(kc);
                    return PyErr_SetFromErrno(PyExc_OSError);;
                }
                py_disk_info = Py_BuildValue(
                                   "(IIKKLL)",
                                   kio.reads,
                                   kio.writes,
                                   kio.nread,
                                   kio.nwritten,
                                   kio.rtime / 1000 / 1000,  // from nano to milli secs
                                   kio.wtime / 1000 / 1000   // from nano to milli secs
                               );
                if (!py_disk_info)
                    goto error;
                if (PyDict_SetItemString(py_retdict, ksp->ks_name,
                                         py_disk_info))
                    goto error;
                Py_DECREF(py_disk_info);
            }
        }
        ksp = ksp->ks_next;
    }
    kstat_close(kc);

    return py_retdict;

error:
    Py_XDECREF(py_disk_info);
    Py_DECREF(py_retdict);
    if (kc != NULL)
        kstat_close(kc);
    return NULL;
}
예제 #16
0
static int kstat_open_driver(struct ifstat_driver *driver,
			     char *options) {
  kstat_ctl_t *kc;
  
  if ((kc = kstat_open()) == NULL) {
    ifstat_perror("kstat_open");
    return 0;
  }

  driver->data = (void *) kc;
  return 1;
}
예제 #17
0
static int	get_swap_io(double *swapin, double *pgswapin, double *swapout, double *pgswapout)
{
    kstat_ctl_t	    *kc;
    kstat_t	    *k;
    cpu_stat_t	    *cpu;

    int	    cpu_count = 0;

    kc = kstat_open();

    if(kc != NULL)
    {
	k = kc->kc_chain;
  	while (k != NULL)
	{
	    if( (strncmp(k->ks_name, "cpu_stat", 8) == 0) &&
		(kstat_read(kc, k, NULL) != -1) )
	    {
		cpu = (cpu_stat_t*) k->ks_data;
		if(swapin)
		{
		   /* uint_t   swapin;	    	*/ /* swapins */
		   (*swapin) += (double) cpu->cpu_vminfo.swapin;
		}
		if(pgswapin)
		{
		   /* uint_t   pgswapin;	*/ /* pages swapped in */
		  (*pgswapin) += (double) cpu->cpu_vminfo.pgswapin;
		}
		if(swapout)
		{
		   /* uint_t   swapout;	    	*/ /* swapout */
		   (*swapout) += (double) cpu->cpu_vminfo.swapout;
		}
		if(pgswapout)
		{
		   /* uint_t   pgswapout;	*/ /* pages swapped out */
		  (*pgswapout) += (double) cpu->cpu_vminfo.pgswapout;
		}
		cpu_count += 1;
  	    }
	    k = k->ks_next;
        }
	kstat_close(kc);
    }

    if(cpu_count == 0)
    {
	return SYSINFO_RET_FAIL;
    }

    return SYSINFO_RET_OK;
}
예제 #18
0
static int	get_kstat_io(const char *name, zbx_kstat_t *zk)
{
	int		result = SYSINFO_RET_FAIL;
	kstat_ctl_t	*kc;
	kstat_t		*kt;
	kstat_io_t	kio;

	if (0 == (kc = kstat_open()))
		return result;

	if ('\0' != *name)
	{
		if (0 == (kt = kstat_lookup(kc, NULL, -1, (char *)name)))
			goto clean;

		if (KSTAT_TYPE_IO != kt->ks_type)
			goto clean;

		if (-1 != kstat_read(kc, kt, &kio))
		{
			zk->nread = kio.nread;
			zk->nwritten = kio.nwritten;
			zk->reads = kio.reads;
			zk->writes = kio.writes;

			result = SYSINFO_RET_OK;
		}
	}
	else
	{
		memset(zk, 0, sizeof(*zk));

		for (kt = kc->kc_chain; NULL != kt; kt = kt->ks_next)
		{
			if (KSTAT_TYPE_IO == kt->ks_type && 0 == strcmp("disk", kt->ks_class))
			{
				kstat_read(kc, kt, &kio);

				zk->nread += kio.nread;
				zk->nwritten += kio.nwritten;
				zk->reads += kio.reads;
				zk->writes += kio.writes;
			}
		}

		result = SYSINFO_RET_OK;
	}
clean:
	kstat_close(kc);

	return result;
}
static int init_cpu_kstat_counters() {
    static int initialized = 0;

    // Concurrence in this method is prevented by the lock in
    // the calling method get_cpu_load();
    if(!initialized) {
        if ((kstat_ctrl = kstat_open()) != NULL) {
            map_cpu_kstat_counters();
            initialized = 1;
        }
    }
    return initialized ? 0 : -1;
}
예제 #20
0
파일: solaris.c 프로젝트: dilawar/suckless
static void update_kstat()
{
	if (kstat == NULL) {
		kstat = kstat_open();
		if (kstat == NULL) {
			NORM_ERR("can't open kstat: %s", strerror(errno));
		}
	}

	if (kstat_chain_update(kstat) == -1) {
		perror("kstat_chain_update");
		return;
	}
}
예제 #21
0
/*
 * Return system-wide CPU times.
 */
static PyObject *
psutil_per_cpu_times(PyObject *self, PyObject *args)
{
    kstat_ctl_t *kc;
    kstat_t *ksp;
    cpu_stat_t cs;
    int numcpus;
    int i;
    PyObject *py_retlist = PyList_New(0);
    PyObject *py_cputime = NULL;

    if (py_retlist == NULL)
        return NULL;

    kc = kstat_open();
    if (kc == NULL) {
        PyErr_SetFromErrno(PyExc_OSError);
        goto error;
    }

    for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
        if (strcmp(ksp->ks_module, "cpu_stat") == 0) {
            if (kstat_read(kc, ksp, &cs) == -1) {
                PyErr_SetFromErrno(PyExc_OSError);
                goto error;
            }
            py_cputime = Py_BuildValue("ffff",
                                       (float)cs.cpu_sysinfo.cpu[CPU_USER],
                                       (float)cs.cpu_sysinfo.cpu[CPU_KERNEL],
                                       (float)cs.cpu_sysinfo.cpu[CPU_IDLE],
                                       (float)cs.cpu_sysinfo.cpu[CPU_WAIT]);
            if (py_cputime == NULL)
                goto error;
            if (PyList_Append(py_retlist, py_cputime))
                goto error;
            Py_DECREF(py_cputime);
            py_cputime = NULL;
        }
    }

    kstat_close(kc);
    return py_retlist;

error:
    Py_XDECREF(py_cputime);
    Py_DECREF(py_retlist);
    if (kc != NULL)
        kstat_close(kc);
    return NULL;
}
예제 #22
0
/**
 * Run once function that initializes the kstats we need here.
 *
 * @returns IPRT status code.
 * @param   pvUser1     Unused.
 * @param   pvUser2     Unused.
 */
static DECLCALLBACK(int) rtMpSolarisOnce(void *pvUser1, void *pvUser2)
{
    int rc = VINF_SUCCESS;
    NOREF(pvUser1); NOREF(pvUser2);

    /*
     * Open kstat and find the cpu_info entries for each of the CPUs.
     */
    g_pKsCtl = kstat_open();
    if (g_pKsCtl)
    {
        g_capCpuInfo = RTMpGetCount();
        g_papCpuInfo = (kstat_t **)RTMemAllocZ(g_capCpuInfo * sizeof(kstat_t *));
        if (g_papCpuInfo)
        {
            rc = RTCritSectInit(&g_MpSolarisCritSect);
            if (RT_SUCCESS(rc))
            {
                RTCPUID i = 0;
                for (kstat_t *pKsp = g_pKsCtl->kc_chain; pKsp != NULL; pKsp = pKsp->ks_next)
                {
                    if (!strcmp(pKsp->ks_module, "cpu_info"))
                    {
                        AssertBreak(i < g_capCpuInfo);
                        g_papCpuInfo[i++] = pKsp;
                        /** @todo ks_instance == cpu_id (/usr/src/uts/common/os/cpu.c)? Check this and fix it ASAP. */
                    }
                }

                return VINF_SUCCESS;
            }

            /* bail out, we failed. */
            RTMemFree(g_papCpuInfo);
        }
        else
            rc = VERR_NO_MEMORY;
        kstat_close(g_pKsCtl);
        g_pKsCtl = NULL;
    }
    else
    {
        rc = RTErrConvertFromErrno(errno);
        if (RT_SUCCESS(rc))
            rc = VERR_INTERNAL_ERROR;
        Log(("kstat_open() -> %d (%Rrc)\n", errno, rc));
    }

    return rc;
}
예제 #23
0
kstat_ctl_t *
open_kstat(void)
{
	kstat_ctl_t *kc;

	while ((kc = kstat_open()) == NULL) {
		if (errno == EAGAIN)
			(void) poll(NULL, 0, RETRY_DELAY);
		else
			fail(1, "kstat_open failed");
	}

	return (kc);
}
예제 #24
0
/*
 * Solaris specific routines
 */
void psolnames_collect(TABLE tab) {
     kstat_ctl_t *kc;
     kstat_t *ksp;

     /* process kstat data of type KSTAT_TYPE_RAW */
     kc = kstat_open();

     for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next)
	  if (ksp->ks_type == KSTAT_TYPE_NAMED) {
	       /* collect row stats */
	       psolnames_col_names(tab, kc, ksp);
	  }

     kstat_close(kc);
}
예제 #25
0
static int
kstat_probe(void)
{
	kstat_ctl_t *kc = kstat_open();

	if (kc) {
		kstat_t * kst = kstat_lookup(kc, NULL, -1, NULL);
		kstat_close(kc);

		if (kst)
			return 1;
	}

	return 0;
}
예제 #26
0
파일: collectd.c 프로젝트: octo/collectd
static void update_kstat(void) {
  if (kc == NULL) {
    if ((kc = kstat_open()) == NULL)
      ERROR("Unable to open kstat control structure");
  } else {
    kid_t kid = kstat_chain_update(kc);
    if (kid > 0) {
      INFO("kstat chain has been updated");
      plugin_init_all();
    } else if (kid < 0)
      ERROR("kstat chain update failed");
    /* else: everything works as expected */
  }

  return;
} /* static void update_kstat (void) */
예제 #27
0
    /*
     * Initialise the list of CPUs on the system
     *   (including descriptions)
     */
void init_cpu_kstat( void ) {
    int               i = 0, n = 0, clock, state_begin;
    char              ctype[15], ftype[15], state[10];
    kstat_t          *ksp;
    kstat_named_t    *ks_data;
    netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
    strcpy(cpu->name, "Overall CPU statistics");

    if (kstat_fd == NULL)
        kstat_fd = kstat_open();
    kstat_chain_update( kstat_fd );

    DEBUGMSGTL(("cpu", "cpu_kstat init\n "));
    for (ksp = kstat_fd->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
        if (ksp->ks_flags & KSTAT_FLAG_INVALID)
            continue;
        if ((strcmp(ksp->ks_module, "cpu_info") == 0) &&
            (strcmp(ksp->ks_class,  "misc"    ) == 0)) {
            kstat_read(kstat_fd, ksp, NULL );
            n++;
            clock = 999999;
            memset(ctype, 0, sizeof(ctype));
            memset(ftype, 0, sizeof(ftype));
            memset(state, 0, sizeof(state));
            for (i=0, ks_data = ksp->ks_data; i < ksp->ks_ndata; i++, ks_data++) {
                if ( strcmp( ks_data->name, "state" ) == 0 ) {
                    strlcpy(state, ks_data->value.c, sizeof(state));
                } else if ( strcmp( ks_data->name, "state_begin" ) == 0 ) {
                    state_begin = ks_data->value.i32;
                } else if ( strcmp( ks_data->name, "cpu_type" ) == 0 ) {
                    strlcpy(ctype, ks_data->value.c, sizeof(ctype));
                } else if ( strcmp( ks_data->name, "fpu_type" ) == 0 ) {
                    strlcpy(ftype, ks_data->value.c, sizeof(ftype));
                } else if ( strcmp( ks_data->name, "clock_MHz" ) == 0 ) {
                    clock = ks_data->value.i32;
                }
            }
            i   = ksp->ks_instance;
            cpu = netsnmp_cpu_get_byIdx( i, 1 );
            sprintf( cpu->name,  "cpu%d", i );
            sprintf( cpu->descr, "CPU %d Sun %d MHz %s with %s FPU %s",
                                 i, clock, ctype, ftype, state  );
            cpu->status = _cpu_status(state); /* XXX - or in 'n_c_a_load' ? */
        }
    }
    cpu_num = i;
}
void
cpu_util_init(void)
{
  kstat_t   *ksp;
  int i;
  kc = kstat_open();

  if (kc == NULL) {
    fprintf(where,
	    "cpu_util_init: kstat_open: errno %d %s\n",
	    errno,
	    strerror(errno));
    fflush(where);
    exit(-1);
  }

  /* lets flesh-out a CPU instance number map since it seems that some
     systems, not even those which are partitioned, can have
     non-contiguous CPU numbers.  discovered "the hard way" on a
     T5220. raj 20080804 */
  i = 0;
  for (ksp = kc->kc_chain, i = 0;
       (ksp != NULL) && (i < MAXCPUS);
       ksp = ksp->ks_next) {
    if ((strcmp(ksp->ks_module,"cpu") == 0) &&
	(strcmp(ksp->ks_name,"sys") == 0)) {
      if (debug) {
	fprintf(where,"Mapping CPU instance %d to entry %d\n",
		ksp->ks_instance,i);
	fflush(where);
      }
      lib_cpu_map[i++] = ksp->ks_instance;
    }
  }

  if (MAXCPUS == i) {
    fprintf(where,
            "Sorry, this system has more CPUs (%d) than netperf can handle (%d).\n"
            "Please alter MAXCPUS in netlib.h and recompile.\n",
            i,
            MAXCPUS);
    fflush(where);
    exit(1);
  }

  return;
}
예제 #29
0
/* MT-NOTE kstat is not thread save */
int get_freemem(
long *freememp 
) {
   kstat_ctl_t   *kc;  
   kstat_t       *ksp;  
   kstat_named_t *knp;
   kc = kstat_open();  
   ksp = kstat_lookup(kc, "unix", 0, "system_pages");
   if (kstat_read(kc, ksp, NULL) == -1) {
      kstat_close(kc);
      return -1;
   } 
   knp = kstat_data_lookup(ksp, "freemem");
   *freememp = (long)knp -> value.ul;
   kstat_close(kc);
   return 0;
}
예제 #30
0
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
   SolarisProcessList* spl = xCalloc(1, sizeof(SolarisProcessList));
   ProcessList* pl = (ProcessList*) spl;
   ProcessList_init(pl, Class(SolarisProcess), usersTable, pidWhiteList, userId);

   spl->kd = kstat_open();

   pl->cpuCount = sysconf(_SC_NPROCESSORS_ONLN);

   if (pl->cpuCount == 1 ) {
      spl->cpus = xRealloc(spl->cpus, sizeof(CPUData));
   } else {
      spl->cpus = xRealloc(spl->cpus, (pl->cpuCount + 1) * sizeof(CPUData));
   }

   return pl;
}