Ejemplo n.º 1
0
int
initsyscall(void)
{
    static char name[] = "name";

    hertz = stathz ? stathz : hz;

    syscall_order(name);

    /* drvinit gets number of cpus! */
    drvinit(1);

    counts_mib_len = __arraycount(counts_mib);
    if (sysctlnametomib("kern.syscalls.counts", counts_mib,
                        &counts_mib_len))
        counts_mib_len = 0;

    times_mib_len = __arraycount(times_mib);
    if (sysctlnametomib("kern.syscalls.times", times_mib, &times_mib_len))
        times_mib_len = 0;

    getinfo(&s2, SHOW_COUNTS | SHOW_TIMES);
    s1 = s2;

    return(1);
}
Ejemplo n.º 2
0
/*
 * Try to use ACPI to find the AC line status.  If this fails, fall back
 * to APM.  If nothing succeeds, we'll just run in default mode.
 */
static void
acline_init(void)
{
	acline_mib_len = 4;
	acline_status = SRC_UNKNOWN;

	if (sysctlnametomib(ACPIAC, acline_mib, &acline_mib_len) == 0) {
		acline_mode = ac_sysctl;
		if (vflag)
			warnx("using sysctl for AC line status");
#if __powerpc__
	} else if (sysctlnametomib(PMUAC, acline_mib, &acline_mib_len) == 0) {
		acline_mode = ac_sysctl;
		if (vflag)
			warnx("using sysctl for AC line status");
#endif
#ifdef USE_APM
	} else if ((apm_fd = open(APMDEV, O_RDONLY)) >= 0) {
		if (vflag)
			warnx("using APM for AC line status");
		acline_mode = ac_apm;
#endif
	} else {
		warnx("unable to determine AC line status");
		acline_mode = ac_none;
	}
}
Ejemplo n.º 3
0
static int tish_uname(char * argv[])
{
    char * arg = argv[1];
    int mib[2];
    int len;
    size_t str_len;
    char type[20];
    char rele[40] = "";
    char vers[40] = "";

    len = sysctlnametomib("kern.ostype", mib, num_elem(mib));
    str_len = sizeof(type);
    sysctl(mib, len, &type, &str_len, 0, 0);

    if (arg && !strcmp(arg, "-a")) {
        len = sysctlnametomib("kern.osrelease", mib, num_elem(mib));
        str_len = sizeof(rele);
        sysctl(mib, len, &rele, &str_len, 0, 0);

        len = sysctlnametomib("kern.version", mib, num_elem(mib));
        str_len = sizeof(vers);
        sysctl(mib, len, &vers, &str_len, 0, 0);
    }

    printf("%s %s %s\n", type, rele, vers);

    return 0;
}
Ejemplo n.º 4
0
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
   FreeBSDProcessList* fpl = calloc(1, sizeof(FreeBSDProcessList));
   ProcessList* pl = (ProcessList*) fpl;
   ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId);

   int cpus = 1;
   size_t sizeof_cpus = sizeof(cpus);
   int err = sysctlbyname("hw.ncpu", &cpus, &sizeof_cpus, NULL, 0);
   if (err) cpus = 1;
   pl->cpuCount = MAX(cpus, 1);
   CPUData* tmp_cpus = (CPUData*) realloc(fpl->cpus, cpus * sizeof(CPUData));
   assert(tmp_cpus != NULL);
   fpl->cpus = tmp_cpus;

   for (int i = 0; i < cpus; i++) {
      fpl->cpus[i].totalTime = 1;
      fpl->cpus[i].totalPeriod = 1;
   }
   
   size_t len;
   len = 4; sysctlnametomib("vm.stats.vm.v_wire_count",  MIB_vm_stats_vm_v_wire_count, &len);
   len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len);
   len = 2; sysctlnametomib("hw.physmem",                MIB_hw_physmem, &len);
   pageSizeKb = PAGE_SIZE_KB;   
   
   fpl->kd = kvm_open(NULL, "/dev/null", NULL, 0, NULL);
   assert(fpl->kd);

   return pl;
}
Ejemplo n.º 5
0
static sg_error
sg_swap_init_comp(unsigned id) {
	ssize_t pagesize;
#if defined(HAVE_STRUCT_XSWDEV)
	size_t len = lengthof(swapinfo_mib);
#endif
	GLOBAL_SET_ID(swap,id);

	if((pagesize = sg_get_sys_page_size()) == -1) {
		RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCONF, "_SC_PAGESIZE");
	}

#if defined(HAVE_STRUCT_XSWDEV)
	if( sysctlnametomib("vm.swap_info", swapinfo_mib, &len) < 0 ) {
		if( sysctlnametomib("vm.swap_info_array", swapinfo_mib, &len) < 0 ) {
			RETURN_WITH_SET_ERROR_WITH_ERRNO("mem", SG_ERROR_SYSCTLNAMETOMIB, "vm.swap_info + vm.swap_info_array");
		}
		else {
			swapinfo_array = true;
			swapinfo_sysctl_name = "vm.swap_info_array";
		}
	}
	else {
		swapinfo_array = false;
		swapinfo_sysctl_name = "vm.swap_info";
	}
#endif

	return SG_ERROR_NONE;
}
Ejemplo n.º 6
0
/** Initialize hardware counters, setup the function vector table
 * and get hardware information, this routine is called when the
 * PAPI process is initialized (IE PAPI_library_init)
 */
int coretemp_init_substrate ()
{
	int ret;
	int i;
	int mib[4];
	size_t len;
	char tmp[128];

	SUBDBG("coretemp_init_substrate...\n");

	/* Count the number of cores (counters) that have sensors allocated */
	i = 0;
	CORETEMP_NUM_EVENTS = 0;
	sprintf (tmp, "dev.coretemp.%d.%%driver", i);
	len = 4;
	ret = sysctlnametomib (tmp, mib, &len);
	while (ret != -1)
	{
		CORETEMP_NUM_EVENTS++;
		i++;
		sprintf (tmp, "dev.coretemp.%d.%%driver", i);
		len = 4;
		ret = sysctlnametomib (tmp, mib, &len);
	}

	/* Allocate memory for the our event table */
	coretemp_native_table = (coretemp_native_event_entry_t *)
		papi_malloc (sizeof (coretemp_native_event_entry_t) * CORETEMP_NUM_EVENTS);
	if (coretemp_native_table == NULL)
	{
		perror( "malloc():Could not get memory for coretemp events table" );
		return EXIT_FAILURE;
	}

	/* Allocate native events internal structures */
	for (i = 0; i < CORETEMP_NUM_EVENTS; i++)
	{
		/* Event name */
		sprintf (coretemp_native_table[i].name, "CORETEMP_CPU_%d", i);

		/* Event description */
		sprintf (coretemp_native_table[i].description, "CPU On-Die Thermal Sensor #%d", i);

		/* Event extra bits -> save MIB to faster access later */
		sprintf (tmp, "dev.cpu.%d.temperature", i);
		len = 4;
		if (sysctlnametomib (tmp, coretemp_native_table[i].resources.mib, &len) == -1)
			return PAPI_ESBSTR;

		coretemp_native_table[i].resources.selector = i+1;
	}

	return PAPI_OK;
}
/* could have used kvm_getswapinfo() here for deeper backward-compatibility, but
 * the use of the kvm library seems to be deprecated in favour of sysctl, so that
 * is preferred here.  Thanks to Hubert Chu for contributing this.
 */
int getSysvmswapfree(struct xswdev *xswtotal)
{
    int mib[CTL_MAXNAME];
    int n;
    size_t size;
    struct xswdev xsw;

    xswtotal->xsw_nblks = 0;
    xswtotal->xsw_used = 0;
    size_t mibsize = sizeof(mib) / sizeof(mib[0]);

    if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) {
        return NO;
    }
    for (n=0; ; ++n) {
        mib[mibsize] = n;
        size = sizeof(xsw);
        if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) {
            break;
        }
        xswtotal->xsw_nblks += xsw.xsw_nblks;
        xswtotal->xsw_used += xsw.xsw_used;
    }
    return YES;
}
Ejemplo n.º 8
0
static sg_error
sg_mem_init_comp(unsigned id) {
	ssize_t pagesize;
#if defined(HAVE_STRUCT_VMTOTAL) && \
    !(defined(HAVE_STRUCT_UVMEXP_SYSCTL) && defined(VM_UVMEXP2)) && \
    !(defined(HAVE_STRUCT_UVMEXP) && defined(VM_UVMEXP)) && \
    !defined(DARWIN)
	size_t len = lengthof(vmtotal_mib);
#endif
	GLOBAL_SET_ID(mem,id);

#if defined(HAVE_STRUCT_VMTOTAL) && \
    !(defined(HAVE_STRUCT_UVMEXP_SYSCTL) && defined(VM_UVMEXP2)) && \
    !(defined(HAVE_STRUCT_UVMEXP) && defined(VM_UVMEXP)) && \
    !defined(DARWIN)
	if( -1 == sysctlnametomib( "vm.vmtotal", vmtotal_mib, &len ) ) {
		RETURN_WITH_SET_ERROR_WITH_ERRNO("mem", SG_ERROR_SYSCTLNAMETOMIB, "vm.vmtotal");
	}
#endif

	if((pagesize = sg_get_sys_page_size()) == -1) {
		RETURN_WITH_SET_ERROR_WITH_ERRNO("mem", SG_ERROR_SYSCONF, "_SC_PAGESIZE");
	}

#if defined(HAVE_HOST_STATISTICS) || defined(HAVE_HOST_STATISTICS64)
	self_host_port = mach_host_self();
#endif

	return SG_ERROR_NONE;
}
Ejemplo n.º 9
0
/*
 * Return the CPU type of the process.
 */
static int cpu_type_for_pid(pid_t pid, cpu_type_t *cputype) 
{
     int res = -1;
     static int mib[CTL_MAXNAME];
     static size_t miblen = 0;
     
     *cputype = 0;
     
     if (miblen == 0)
     {
          miblen = CTL_MAXNAME;
          res = sysctlnametomib("sysctl.proc_cputype", mib, &miblen);

          if (res != 0)
          {
               miblen = 0;
          }
     }

     if (miblen > 0)
     {
          mib[miblen] = pid;
          size_t len = sizeof(*cputype);
          res = sysctl(mib, miblen + 1, cputype, &len, NULL, 0);
     }

     return res;
}
Ejemplo n.º 10
0
int NumSystemCores() {
    if (PbrtOptions.nCores > 0) return PbrtOptions.nCores;
#ifdef WIN32
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    return sysinfo.dwNumberOfProcessors;
#elif defined(__linux__)
    return sysconf(_SC_NPROCESSORS_ONLN);
#else
    // mac/bsds
#ifdef __OpenBSD__
    int mib[2] = { CTL_HW, HW_NCPU };
#else
    int mib[2];
    mib[0] = CTL_HW;
    size_t length = 2;
    if (sysctlnametomib("hw.logicalcpu", mib, &length) == -1) {
        Error("sysctlnametomib() filed.  Guessing 2 CPU cores.");
        return 2;
    }
    Assert(length == 2);
#endif
    int nCores = 0;
    size_t size = sizeof(nCores);

    /* get the number of CPUs from the system */
    if (sysctl(mib, 2, &nCores, &size, NULL, 0) == -1) {
        Error("sysctl() to find number of cores present failed");
        return 2;
    }
    return nCores;
#endif
}
Ejemplo n.º 11
0
/** Figure out how many CPU cores there are in the system
 */
static int
lNumCPUCores() {
#if defined(__linux__)
    return sysconf(_SC_NPROCESSORS_ONLN);
#else
    // Mac
    int mib[2];
    mib[0] = CTL_HW;
    size_t length = 2;
    if (sysctlnametomib("hw.logicalcpu", mib, &length) == -1) {
        fprintf(stderr, "sysctlnametomib() filed.  Guessing 2 cores.");
        return 2;
    }
    assert(length == 2);

    int nCores = 0;
    size_t size = sizeof(nCores);

    if (sysctl(mib, 2, &nCores, &size, NULL, 0) == -1) {
        fprintf(stderr, "sysctl() to find number of cores present failed.  Guessing 2.");
        return 2;
    }
    return nCores;
#endif
}
/**
 * This routine returns system/user CPU time in use.
 * @return: true if successful, false if failed
 */
boolean_t used_system_cpu_sysdep(SystemInfo_T *si) {
        int    mib[2];
        long   cp_time[CPUSTATES];
        long   total_new = 0;
        long   total;
        size_t len;

        len = sizeof(mib);
        if (sysctlnametomib("kern.cp_time", mib, &len) == -1) {
                LogError("system statistic error -- cannot get cpu time handler: %s\n", STRERROR);
                return false;
        }

        len = sizeof(cp_time);
        if (sysctl(mib, 2, &cp_time, &len, NULL, 0) == -1) {
                LogError("system statistic error -- cannot get cpu time: %s\n", STRERROR);
                return false;
        }

        for (int i = 0; i < CPUSTATES; i++)
                total_new += cp_time[i];

        total     = total_new - total_old;
        total_old = total_new;

        si->total_cpu_user_percent = (total > 0) ? (100. * (double)(cp_time[CP_USER] - cpu_user_old) / total) : -1.;
        si->total_cpu_syst_percent = (total > 0) ? (100. * (double)(cp_time[CP_SYS] - cpu_syst_old) / total) : -1.;
        si->total_cpu_wait_percent = 0.; /* there is no wait statistic available */

        cpu_user_old = cp_time[CP_USER];
        cpu_syst_old = cp_time[CP_SYS];

        return true;
}
Ejemplo n.º 13
0
static int tish_ikut(char * argv[])
{
    int mib_test[5];
    int mib_cur[5];
    int mib_next[5];
    size_t len_cur, len_next;
    const int one = 1;

    const size_t len_test = sysctlnametomib("debug.test", mib_test,
            num_elem(mib_test));

    printf("     \n"); /* Hack */
    print_mib_name(mib_test, len_test);

    memcpy(mib_cur, mib_test, len_test * sizeof(int));
    len_cur = len_test;

    while ((len_next = sizeof(mib_next)),
           sysctlgetnext(mib_cur, len_cur, mib_next, &len_next) == 0) {
        if (!sysctltstmib(mib_next, mib_test, len_test)) {
            printf("End of tests\n");
            break; /* EOF debug.test */
        }
        memcpy(mib_cur, mib_next, len_next * sizeof(int));
        len_cur = len_next;

        print_mib_name(mib_cur, len_cur);
        sysctl(mib_cur, len_cur, 0, 0, (void *)(&one), sizeof(one));
    }

    return 0;
}
Ejemplo n.º 14
0
/*
 * This function is called only once by the gmond.  Use to 
 * initialize data structures, etc or just return SYNAPSE_SUCCESS;
 */
g_val_t
metric_init(void)
{
   g_val_t val;

   /*
    * Try to use the vm.swap_info sysctl to gather swap data.  If it
    * isn't implemented, fall back to trying to old kvm based interface.
    */
   mibswap_size = MIB_SWAPINFO_SIZE;
   if (sysctlnametomib("vm.swap_info", mibswap, &mibswap_size) == -1) {
      kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "metric_init()");
   } else {
      /*
       * RELEASE versions of DragonFlyBSD with the swap mib have a version
       * of libkvm that doesn't need root for simple proc access so we
       * just open /dev/null to give us a working handle here.
       */
      kd = kvm_open(_PATH_DEVNULL, NULL, NULL, O_RDONLY, "metric_init()");
      use_vm_swap_info = 1;
   }
   pagesize = getpagesize();

   /* Initalize some counters */
   get_netbw(NULL, NULL, NULL, NULL);
   cpu_state(-1);

   val.int32 = SYNAPSE_SUCCESS;
   return val;
}
Ejemplo n.º 15
0
/* Obtains the OID for a sysctl by name. */
static void
oid_get_by_name(struct oid *o, const char *name)
{

    o->len = nitems(o->id);
    if (sysctlnametomib(name, o->id, &o->len) != 0)
        err(1, "sysctl(%s)", name);
}
Ejemplo n.º 16
0
static int getset_parm(char * arg)
{
    char * name;
    char * value;
    char * rest;

    int mib[CTL_MAXNAME];
    char fmt[5];
    unsigned int kind;
    int ctltype;
    size_t dlen;

    name = strtok_r(arg, "=", &rest);
    value = strtok_r(0, "=", &rest);
    if (!name) {
        printf("Invalid argument\n");

        errno = EINVAL;
        return -1;
    }

    const int mib_len = sysctlnametomib(name, mib, num_elem(mib));
    if (mib_len < 0) {
        printf("Node not found\n");

        return -1;
    }

    printf("%s = ", name);

    if (sysctloidfmt(mib, mib_len, fmt, &kind)) {
        printf("Invalid node\n");

        return -1;
    }
    if (sysctl(mib, mib_len, 0, &dlen, 0, 0)) {
        printf("Invalid node\n");

        return -1;
    }

    ctltype = (kind & CTLTYPE);
    switch (ctltype) {
    case CTLTYPE_STRING:
        return getset_svalue(mib, mib_len, dlen, value, strnlen(value, 80));
    case CTLTYPE_INT:
    case CTLTYPE_UINT:
        return getset_ivalue(mib, mib_len, dlen, value, sizeof(int));
    case CTLTYPE_LONG:
    case CTLTYPE_ULONG:
    case CTLTYPE_S64:
    case CTLTYPE_U64:
        printf("Data type not supported yet\n");
        break;
    }

    return 0;
}
Ejemplo n.º 17
0
int	SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result)
{
/*
 *  FreeBSD 7.0 i386
 */
#ifdef XSWDEV_VERSION	/* defined in <vm/vm_param.h> */
	char		*swapdev, *mode;
	int		mib[16], *mib_dev;
	size_t		sz, mib_sz;
	struct xswdev	xsw;
	zbx_uint64_t	total = 0, used = 0;

	if (2 < request->nparam)
		return SYSINFO_RET_FAIL;

	swapdev = get_rparam(request, 0);
	mode = get_rparam(request, 1);

	sz = ARRSIZE(mib);
	if (-1 == sysctlnametomib("vm.swap_info", mib, &sz))
		return FAIL;

	mib_sz = sz + 1;
	mib_dev = &(mib[sz]);

	*mib_dev = 0;
	sz = sizeof(xsw);

	while (-1 != sysctl(mib, mib_sz, &xsw, &sz, NULL, 0))
	{
		if (NULL == swapdev || '\0' == *swapdev || 0 == strcmp(swapdev, "all")	/* default parameter */
				|| 0 == strcmp(swapdev, devname(xsw.xsw_dev, S_IFCHR)))
		{
			total += (zbx_uint64_t)xsw.xsw_nblks;
			used += (zbx_uint64_t)xsw.xsw_used;
		}
		(*mib_dev)++;
	}

	if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "free"))	/* default parameter */
		SET_UI64_RESULT(result, (total - used) * getpagesize());
	else if (0 == strcmp(mode, "total"))
		SET_UI64_RESULT(result, total * getpagesize());
	else if (0 == strcmp(mode, "used"))
		SET_UI64_RESULT(result, used * getpagesize());
	else if (0 == strcmp(mode, "pfree"))
		SET_DBL_RESULT(result, total ? ((double)(total - used) * 100.0 / (double)total) : 0.0);
	else if (0 == strcmp(mode, "pused"))
		SET_DBL_RESULT(result, total ? ((double)used * 100.0 / (double)total) : 0.0);
	else
		return SYSINFO_RET_FAIL;

	return SYSINFO_RET_OK;
#else
	return SYSINFO_RET_FAIL;
#endif
}
Ejemplo n.º 18
0
int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, const void *newp, size_t newlen) {
    int mib[CTL_MAXNAME + 2]; // +2 because most of the relevant code I've seen uses +2 as well
    size_t miblen = CTL_MAXNAME + 2;

    if(sysctlnametomib(name, mib, &miblen) == -1)
        return -1;

    return sysctl(mib, miblen, oldp, oldlenp, newp, newlen);
}
Ejemplo n.º 19
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                mib[16];
  size_t             len;
  struct vmtotal     vm;
#if (__FreeBSD_version > 500000)
  int                n = 0;
  int                pagesize = getpagesize();
  size_t             miblen;
  struct xswdev      xsw;
  unsigned long long total = 0ULL;
  unsigned long long used  = 0ULL;
#endif

  /* Memory */
  memset(mib, 0, sizeof(mib));
  mib[0] = CTL_VM;
  mib[1] = VM_METER;
  len    = sizeof(struct vmtotal);
  if (sysctl(mib, 2, &vm, &len, NULL, 0) == -1) {
    LogError("system statistic error -- cannot get real memory usage: %s\n", STRERROR);
    return FALSE;
  }
  si->total_mem_kbyte = (unsigned long)(vm.t_arm * pagesize_kbyte);

  /* Swap */
#if (__FreeBSD_version > 500000)
  memset(mib, 0, sizeof(mib));
  miblen = sizeof(mib) / sizeof(mib[0]);
  if (sysctlnametomib("vm.swap_info", mib, &miblen) == -1) {
    LogError("system statistic error -- cannot get swap usage: %s\n", STRERROR);
    si->swap_kbyte_max = 0;
    return FALSE;
  }
  while (TRUE) {
    mib[miblen] = n;
    len = sizeof(struct xswdev);
    if (sysctl(mib, miblen + 1, &xsw, &len, NULL, 0) == -1)
      break;
    if (xsw.xsw_version != XSWDEV_VERSION) {
      LogError("system statistic error -- cannot get swap usage: xswdev version mismatch\n");
      si->swap_kbyte_max = 0;
      return FALSE;
    }
    total += xsw.xsw_nblks;
    used  += xsw.xsw_used;
    n++;
  }
  si->swap_kbyte_max   = (unsigned long)(double)total * (double)pagesize / 1024.;
  si->total_swap_kbyte = (unsigned long)(double)used  * (double)pagesize / 1024.;
#else
  /* Not implemented - FreeBSD <= 5.x doesn't have vm.swap_info MIB and uses kvm instead. As such FreeBSD version is obsolete, no need to implement unless somebody will ask for it. */
  DEBUG("system statistic -- swap usage monitoring not implemented in FreeBSD <= 5.x\n");
  si->swap_kbyte_max = 0;
#endif

  return TRUE;
}
Ejemplo n.º 20
0
static Mib* getSysctlMib(char* nameFmt, int n) {
	char name[128];
	sprintf(name, nameFmt, n);

	Mib* mib = calloc(1, sizeof(Mib));
	if (mib == NULL) error(__LINE__);
	mib->len = 16;
	if (sysctlnametomib(name, mib->mib, &mib->len)) errorM(__LINE__,name);
	return mib;
}
Ejemplo n.º 21
0
void genControlInfoFromName(const std::string& name, QueryData& results,
                    const std::map<std::string, std::string>& config) {
  int request[CTL_DEBUG_MAXID + 2] = {0};
  size_t oid_size = CTL_DEBUG_MAXID;
  if (sysctlnametomib(name.c_str(), request, &oid_size) != 0) {
    // MIB lookup failed.
    return;
  }

  genControlInfo((int*)request, oid_size, results, config);
}
Ejemplo n.º 22
0
static void
npe_setifname(struct npestatfoo *wf0, const char *ifname)
{
	struct npestatfoo_p *wf = (struct npestatfoo_p *) wf0;
	size_t len;

	snprintf(wf->oid, sizeof(wf->oid), "dev.npe.%s.stats", ifname+3);
	len = 4;
	if (sysctlnametomib(wf->oid, wf->mib, &len) < 0)
		err(1, "sysctlnametomib: %s", wf->oid);
}
Ejemplo n.º 23
0
	long SysInfo::GetCPUCount()
	{
		int mib[4];
		int ncpu;
		size_t len = 4;

		sysctlnametomib("hw.ncpu", mib, &len);
		size_t len2 = sizeof(ncpu);
		sysctl(mib, len, &ncpu, &len2, NULL, 0);

		return ncpu;
	}
Ejemplo n.º 24
0
/**
 * This routine returns kbyte of real memory in use.
 * @return: true if successful, false if failed (or not available)
 */
boolean_t used_system_memory_sysdep(SystemInfo_T *si) {
        /* Memory */
        size_t len = sizeof(unsigned int);
        unsigned int active;
        if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1) {
                LogError("system statistic error -- cannot get for active memory usage: %s\n", STRERROR);
                return false;
        }
        if (len != sizeof(unsigned int)) {
                LogError("system statistic error -- active memory usage statics error\n");
                return false;
        }
        unsigned int wired;
        if (sysctlbyname("vm.stats.vm.v_wire_count", &wired, &len, NULL, 0) == -1) {
                LogError("system statistic error -- cannot get for wired memory usage: %s\n", STRERROR);
                return false;
        }
        if (len != sizeof(unsigned int)) {
                LogError("system statistic error -- wired memory usage statics error\n");
                return false;
        }
        si->total_mem_kbyte = (active + wired) * pagesize_kbyte;

        /* Swap */
        int mib[16] = {};
        unsigned long long total = 0ULL;
        unsigned long long used  = 0ULL;
        size_t miblen = sizeof(mib) / sizeof(mib[0]);
        if (sysctlnametomib("vm.swap_info", mib, &miblen) == -1) {
                LogError("system statistic error -- cannot get swap usage: %s\n", STRERROR);
                si->swap_kbyte_max = 0;
                return false;
        }
        int n = 0;
        while (true) {
                struct xswdev xsw;
                mib[miblen] = n;
                len = sizeof(struct xswdev);
                if (sysctl(mib, miblen + 1, &xsw, &len, NULL, 0) == -1)
                        break;
                if (xsw.xsw_version != XSWDEV_VERSION) {
                        LogError("system statistic error -- cannot get swap usage: xswdev version mismatch\n");
                        si->swap_kbyte_max = 0;
                        return false;
                }
                total += xsw.xsw_nblks;
                used  += xsw.xsw_used;
                n++;
        }
        si->swap_kbyte_max = total * pagesize_kbyte;
        si->total_swap_kbyte = used * pagesize_kbyte;
        return true;
}
Ejemplo n.º 25
0
int
sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
    const void *newp, size_t newlen)
{
	int real_oid[CTL_MAXNAME+2];
	size_t oidlen;

	oidlen = sizeof(real_oid) / sizeof(int);
	if (sysctlnametomib(name, real_oid, &oidlen) < 0)
		return (-1);
	return (sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen));
}
Ejemplo n.º 26
0
/* Get the MIB entry for our sysctl */
static void
getmib(void)
{

	/* have we already fetched it? */
	if (miblen != 0)
		return;
	
	miblen = sizeof(mib) / sizeof(mib[0]);
	if (sysctlnametomib(pathctl, mib, &miblen) != 0)
		err(1, "sysctlnametomib(%s)", pathctl);
}
Ejemplo n.º 27
0
bool sysctlPresent(QString sysctlName)
{
    int mib[64];
    size_t len = sysctlName.split(".").size();
    if (len>=64)
        return false;

    if (sysctlnametomib(sysctlName.toLocal8Bit(), mib, &len) < 0)
    {
        return false;
    }
    return true;
}
Ejemplo n.º 28
0
Archivo: mac.c Proyecto: 2asoft/freebsd
/*
 * Simply test whether the TrustedBSD/MAC MIB tree is present; if so,
 * return 1 to indicate that the system has MAC enabled overall or for
 * a given policy.
 */
int
mac_is_present(const char *policyname)
{
	int mib[5];
	size_t siz;
	char *mibname;
	int error;

	if (policyname != NULL) {
		if (policyname[strcspn(policyname, ".=")] != '\0') {
			errno = EINVAL;
			return (-1);
		}
		mibname = malloc(sizeof("security.mac.") - 1 +
		    strlen(policyname) + sizeof(".enabled"));
		if (mibname == NULL)
			return (-1);
		strcpy(mibname, "security.mac.");
		strcat(mibname, policyname);
		strcat(mibname, ".enabled");
		siz = 5;
		error = sysctlnametomib(mibname, mib, &siz);
		free(mibname);
	} else {
		siz = 3;
		error = sysctlnametomib("security.mac", mib, &siz);
	}
	if (error == -1) {
		switch (errno) {
		case ENOTDIR:
		case ENOENT:
			return (0);
		default:
			return (error);
		}
	}
	return (1);
}
Ejemplo n.º 29
0
/** This is called whenever a thread is initialized */
int coretemp_init (hwd_context_t * ctx)
{
	int mib[4];
	size_t len;
	UNREFERENCED(ctx);

	SUBDBG("coretemp_init %p...\n", ctx);

	len = 4;
	if (sysctlnametomib ("dev.coretemp.0.%driver", mib, &len) == -1)
		return PAPI_ESBSTR;

	return PAPI_OK;
}
Ejemplo n.º 30
0
int
bsde_get_mib(const char *string, int *name, size_t *namelen)
{
	size_t len;
	int error;

	len = *namelen;
	error = sysctlnametomib(string, name, &len);
	if (error)
		return (error);

	*namelen = len;
	return (0);
}