这篇教程C++ sysctl函数代码示例写得很实用,希望能帮到您。
本文整理汇总了C++中sysctl函数的典型用法代码示例。如果您正苦于以下问题:C++ sysctl函数的具体用法?C++ sysctl怎么用?C++ sysctl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。 在下文中一共展示了sysctl函数的28个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。 示例1: SYSTEM_SWAP_SIZEint SYSTEM_SWAP_SIZE(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result){/* * FreeBSD 7.0 i386 */#ifdef XSWDEV_VERSION /* defined in <vm/vm_param.h> */ char swapdev[64], mode[64]; int mib[16], *mib_dev; size_t sz, mib_sz; struct xswdev xsw; zbx_uint64_t total = 0, used = 0; assert(result); init_result(result); if (num_param(param) > 2) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, swapdev, sizeof(swapdev))) return SYSINFO_RET_FAIL; if (0 != get_param(param, 2, mode, sizeof(mode))) *mode = '/0'; sz = sizeof(mib) / sizeof(mib[0]); 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 ('/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 ('/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}
开发者ID:phedders,项目名称:zabbix,代码行数:74,
示例2: vma_iterate_bsdstatic intvma_iterate_bsd (vma_iterate_callback_fn callback, void *data){ /* Documentation: http://man.netbsd.org/man/sysctl+7 */ unsigned int entry_size = /* If we wanted to have the path of each entry, we would need sizeof (struct kinfo_vmentry). But we need only the non-string parts of each entry. */ offsetof (struct kinfo_vmentry, kve_path); int info_path[] = { CTL_VM, VM_PROC, VM_PROC_MAP, getpid (), entry_size }; size_t len; size_t pagesize; size_t memneed; void *auxmap; unsigned long auxmap_start; unsigned long auxmap_end; char *mem; char *p; char *p_end; len = 0; if (sysctl (info_path, 5, NULL, &len, NULL, 0) < 0) return -1; /* Allow for small variations over time. In a multithreaded program new VMAs can be allocated at any moment. */ len = 2 * len + 10 * entry_size; /* But the system call rejects lengths > 1 MB. */ if (len > 0x100000) len = 0x100000; /* And the system call causes a kernel panic if the length is not a multiple of entry_size. */ len = (len / entry_size) * entry_size; /* Allocate memneed bytes of memory. We cannot use alloca here, because not much stack space is guaranteed. We also cannot use malloc here, because a malloc() call may call mmap() and thus pre-allocate available memory. So use mmap(), and ignore the resulting VMA. */ pagesize = getpagesize (); memneed = len; memneed = ((memneed - 1) / pagesize + 1) * pagesize; auxmap = (void *) mmap ((void *) 0, memneed, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (auxmap == (void *) -1) return -1; auxmap_start = (unsigned long) auxmap; auxmap_end = auxmap_start + memneed; mem = (char *) auxmap; if (sysctl (info_path, 5, mem, &len, NULL, 0) < 0 || len > 0x100000 - entry_size) { /* sysctl failed, or the list of VMAs is possibly truncated. */ munmap (auxmap, memneed); return -1; } p = mem; p_end = mem + len; while (p < p_end) { struct kinfo_vmentry *kve = (struct kinfo_vmentry *) p; unsigned long start = kve->kve_start; unsigned long end = kve->kve_end; unsigned int flags = 0; if (kve->kve_protection & KVME_PROT_READ) flags |= VMA_PROT_READ; if (kve->kve_protection & KVME_PROT_WRITE) flags |= VMA_PROT_WRITE; if (kve->kve_protection & KVME_PROT_EXEC) flags |= VMA_PROT_EXECUTE; if (start <= auxmap_start && auxmap_end - 1 <= end - 1) { /* Consider [start,end-1] / [auxmap_start,auxmap_end-1] = [start,auxmap_start-1] u [auxmap_end,end-1]. */ if (start < auxmap_start) if (callback (data, start, auxmap_start, flags)) break; if (auxmap_end - 1 < end - 1) if (callback (data, auxmap_end, end, flags)) break; } else { if (callback (data, start, end, flags)) break; } p += entry_size; } munmap (auxmap, memneed); return 0;}
开发者ID:cooljeanius,项目名称:emacs,代码行数:89,
示例3: if//.........这里部分代码省略......... // assume that you wish to create par2 files for it. u64 filesize = 0; if (DiskFile::FileExists(parfilename) && (filesize = DiskFile::GetFileSize(parfilename)) > 0) { extrafiles.push_back(ExtraFile(parfilename, filesize)); // track the total size of the source files and how // big the largest one is. totalsourcesize += filesize; if (largestsourcesize < filesize) largestsourcesize = filesize; } else { // The file does not exist or it is empty. cerr << "You must specify a list of files when creating." << endl; return false; } } } // Strip the ".par2" from the end of the filename of the main PAR2 file. if (parfilename.length() > 5 && 0 == stricmp(parfilename.substr(parfilename.length()-5, 5).c_str(), ".par2")) { parfilename = parfilename.substr(0, parfilename.length()-5); } // Assume a redundancy of 5% if neither redundancy or recoveryblockcount were set. if (!redundancyset && !recoveryblockcountset) { redundancy = 5; } } // Assume a memory limit of 16MB if not specified. if (memorylimit == 0) {#if defined(WIN32) || defined(WIN64) u64 TotalPhysicalMemory = 0; HMODULE hLib = ::LoadLibraryA("kernel32.dll"); if (NULL != hLib) { BOOL (WINAPI *pfn)(LPMEMORYSTATUSEX) = (BOOL (WINAPI*)(LPMEMORYSTATUSEX))::GetProcAddress(hLib, "GlobalMemoryStatusEx"); if (NULL != pfn) { MEMORYSTATUSEX mse; mse.dwLength = sizeof(mse); if (pfn(&mse)) { TotalPhysicalMemory = mse.ullTotalPhys; } } ::FreeLibrary(hLib); } if (TotalPhysicalMemory == 0) { MEMORYSTATUS ms; ::ZeroMemory(&ms, sizeof(ms)); ::GlobalMemoryStatus(&ms); TotalPhysicalMemory = ms.dwTotalPhys; } if (TotalPhysicalMemory == 0) { // Assume 128MB TotalPhysicalMemory = 128 * 1048576; } // Half of total physical memory memorylimit = (size_t)(TotalPhysicalMemory / 1048576 / 2);#elif __APPLE__ int name[2] = {CTL_HW, HW_USERMEM}; int usermem_bytes; size_t size = sizeof(usermem_bytes); sysctl( name, 2, &usermem_bytes, &size, NULL, 0 ); memorylimit = usermem_bytes / (2048 * 1024);#else #if WANT_CONCURRENT // Assume 128MB (otherwise processing is slower) memorylimit = 64; #else memorylimit = 16; #endif#endif } memorylimit *= 1048576; return true;}
开发者ID:t-b,项目名称:par2tbb,代码行数:101,
示例4: update_cpu_usagevoid update_cpu_usage(){#ifdef OLDCPU int mib[2] = { CTL_KERN, KERN_CPTIME }; long used, total; long cp_time[CPUSTATES]; size_t len = sizeof(cp_time);#else size_t size; unsigned int i;#endif /* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */ if ((cpu_setup == 0) || (!info.cpu_usage)) { get_cpu_count(); cpu_setup = 1; }#ifdef OLDCPU if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) { NORM_ERR("Cannot get kern.cp_time"); } fresh.load[0] = cp_time[CP_USER]; fresh.load[1] = cp_time[CP_NICE]; fresh.load[2] = cp_time[CP_SYS]; fresh.load[3] = cp_time[CP_IDLE]; fresh.load[4] = cp_time[CP_IDLE]; used = fresh.load[0] + fresh.load[1] + fresh.load[2]; total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3]; if ((total - oldtotal) != 0) { info.cpu_usage[0] = ((double) (used - oldused)) / (double) (total - oldtotal); } else { info.cpu_usage[0] = 0; } oldused = used; oldtotal = total;#else if (info.cpu_count > 1) { size = CPUSTATES * sizeof(int64_t); for (i = 0; i < info.cpu_count; i++) { int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i }; if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0) < 0) { NORM_ERR("sysctl kern.cp_time2 failed"); } } } 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) { NORM_ERR("sysctl kern.cp_time failed"); } for (i = 0; i < CPUSTATES; i++) { fresh[i] = (int64_t) cp_time_tmp[i]; } } /* XXX Do sg with this int64_t => long => double ? float hell. */ for (i = 0; i < info.cpu_count; i++) { int64_t used, total; int at = i * CPUSTATES; used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS]; total = used + fresh[at + CP_IDLE]; if ((total - oldtotal[i]) != 0) { info.cpu_usage[i] = ((double) (used - oldused[i])) / (double) (total - oldtotal[i]); } else { info.cpu_usage[i] = 0; } oldused[i] = used; oldtotal[i] = total; }#endif}
开发者ID:dilawar,项目名称:suckless,代码行数:85,
示例5: proc_find_topinline void proc_find_top(struct process **cpu, struct process **mem){ struct kinfo_proc2 *p; int n_processes; int i, j = 0; struct process *processes; int mib[2]; u_int total_pages; int64_t usermem; int pagesize = getpagesize(); /* we get total pages count again to be sure it is up to date */ mib[0] = CTL_HW; mib[1] = HW_USERMEM64; size_t size = sizeof(usermem); if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) { NORM_ERR("error reading usermem"); } /* translate bytes into page count */ total_pages = usermem / pagesize; int max_size = sizeof(struct kinfo_proc2); p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes); processes = malloc(n_processes * sizeof(struct process)); for (i = 0; i < n_processes; i++) { if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) { processes[j].pid = p[i].p_pid; processes[j].name = strndup(p[i].p_comm, text_buffer_size); processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE; j++; } } qsort(processes, j - 1, sizeof(struct process), comparemem); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = mem[i]; mem[i] = tmp; if (ttmp != NULL) { free(ttmp->name); free(ttmp); } } qsort(processes, j - 1, sizeof(struct process), comparecpu); for (i = 0; i < 10; i++) { struct process *tmp, *ttmp; tmp = malloc(sizeof(struct process)); tmp->pid = processes[i].pid; tmp->amount = processes[i].amount; tmp->name = strndup(processes[i].name, text_buffer_size); ttmp = cpu[i]; cpu[i] = tmp; if (ttmp != NULL) { free(ttmp->name); free(ttmp); } } for (i = 0; i < j; i++) { free(processes[i].name); } free(processes);}
开发者ID:dilawar,项目名称:suckless,代码行数:77,
示例6: PROC_NUMint PROC_NUM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result){ char procname[MAX_STRING_LEN], buffer[MAX_STRING_LEN], proccomm[MAX_STRING_LEN], *args; int zbx_proc_stat, count, i, proc_ok, stat_ok, comm_ok, mib[4], mibs; int proccount = 0; size_t sz; struct kinfo_proc *proc = NULL; struct passwd *usrinfo; if (num_param(param) > 4) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, procname, sizeof(procname))) *procname = '/0'; else if (strlen(procname) > ZBX_COMMLEN) procname[ZBX_COMMLEN] = '/0'; if (0 != get_param(param, 2, buffer, sizeof(buffer))) *buffer = '/0'; if (*buffer != '/0') { usrinfo = getpwnam(buffer); if (usrinfo == NULL) /* incorrect user name */ return SYSINFO_RET_FAIL; } else usrinfo = NULL; if (0 != get_param(param, 3, buffer, sizeof(buffer))) *buffer = '/0'; if (*buffer != '/0') { if (0 == strcmp(buffer, "run")) zbx_proc_stat = ZBX_PROC_STAT_RUN; else if (0 == strcmp(buffer, "sleep")) zbx_proc_stat = ZBX_PROC_STAT_SLEEP; else if (0 == strcmp(buffer, "zomb")) zbx_proc_stat = ZBX_PROC_STAT_ZOMB; else if (0 == strcmp(buffer, "all")) zbx_proc_stat = ZBX_PROC_STAT_ALL; else return SYSINFO_RET_FAIL; } else zbx_proc_stat = ZBX_PROC_STAT_ALL; if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) *proccomm = '/0'; mib[0] = CTL_KERN; mib[1] = KERN_PROC; if (NULL != usrinfo) { mib[2] = KERN_PROC_UID; mib[3] = usrinfo->pw_uid; mibs = 4; } else { mib[2] = KERN_PROC_ALL; mib[3] = 0; mibs = 3; } sz = 0; if (0 != sysctl(mib, mibs, NULL, &sz, NULL, 0)) return SYSINFO_RET_FAIL; proc = (struct kinfo_proc *)zbx_malloc(proc, sz); if (0 != sysctl(mib, mibs, proc, &sz, NULL, 0)) { zbx_free(proc); return SYSINFO_RET_FAIL; } count = sz / sizeof(struct kinfo_proc); for (i = 0; i < count; i++) {#if(__FreeBSD_version > 500000) if (proc[i].ki_flag & P_KTHREAD) /* skip a system thread */ continue;#endif proc_ok = 0; stat_ok = 0; comm_ok = 0; if (*procname == '/0' || 0 == strcmp(procname, proc[i].ZBX_PROC_COMM)) proc_ok = 1;//.........这里部分代码省略.........
开发者ID:gheja,项目名称:zabbix-ext,代码行数:101,
示例7: getSysctlstatic T getSysctl(int name, const T def) { T ret; int names[] = {CTL_HW, name}; size_t len = sizeof(def); return (sysctl(names, 2u, &ret, &len, NULL, 0) < 0) ? def : ret;}
开发者ID:akhilo,项目名称:cmplayer,代码行数:4,
示例8: get_network_io_counters/* * Return a Python list of named tuples with overall network I/O information */static PyObject*get_network_io_counters(PyObject* self, PyObject* args){ PyObject* py_retdict = PyDict_New(); PyObject* py_ifc_info; char *buf = NULL, *lim, *next; struct if_msghdr *ifm; int mib[6]; size_t len; mib[0] = CTL_NET; // networking subsystem mib[1] = PF_ROUTE; // type of information mib[2] = 0; // protocol (IPPROTO_xxx) mib[3] = 0; // address family mib[4] = NET_RT_IFLIST; // operation mib[5] = 0; if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { Py_DECREF(py_retdict); PyErr_SetFromErrno(0); return NULL; } buf = malloc(len); if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { if (buf) { free(buf); } Py_DECREF(py_retdict); PyErr_SetFromErrno(0); return NULL; } lim = buf + len; for (next = buf; next < lim; ) { ifm = (struct if_msghdr *)next; next += ifm->ifm_msglen; if (ifm->ifm_type == RTM_IFINFO) { struct if_msghdr *if2m = (struct if_msghdr *)ifm; struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1); char ifc_name[32]; strncpy(ifc_name, sdl->sdl_data, sdl->sdl_nlen); ifc_name[sdl->sdl_nlen] = 0; py_ifc_info = Py_BuildValue("(KKKK)", if2m->ifm_data.ifi_obytes, if2m->ifm_data.ifi_ibytes, if2m->ifm_data.ifi_opackets, if2m->ifm_data.ifi_ipackets); PyDict_SetItemString(py_retdict, ifc_name, py_ifc_info); Py_XDECREF(py_ifc_info); } else { continue; } } free(buf); return py_retdict;}
开发者ID:tamentis,项目名称:psutil,代码行数:69,
示例9: cpu_read//.........这里部分代码省略......... int cpu; counter_t user, syst, idle, wait; static cpu_stat_t cs; if (kc == NULL) return (-1); for (cpu = 0; cpu < numcpu; cpu++) { if (kstat_read (kc, ksp[cpu], &cs) == -1) continue; /* error message? */ idle = (counter_t) cs.cpu_sysinfo.cpu[CPU_IDLE]; user = (counter_t) cs.cpu_sysinfo.cpu[CPU_USER]; syst = (counter_t) cs.cpu_sysinfo.cpu[CPU_KERNEL]; wait = (counter_t) cs.cpu_sysinfo.cpu[CPU_WAIT]; submit (ksp[cpu]->ks_instance, "user", user); submit (ksp[cpu]->ks_instance, "system", syst); submit (ksp[cpu]->ks_instance, "idle", idle); submit (ksp[cpu]->ks_instance, "wait", wait); }/* #endif defined(HAVE_LIBKSTAT) */#elif CAN_USE_SYSCTL uint64_t cpuinfo[numcpu][CPUSTATES]; size_t cpuinfo_size; int status; int i; if (numcpu < 1) { ERROR ("cpu plugin: Could not determine number of " "installed CPUs using sysctl(3)."); return (-1); } memset (cpuinfo, 0, sizeof (cpuinfo));#if defined(KERN_CPTIME2) if (numcpu > 1) { for (i = 0; i < numcpu; i++) { int mib[] = {CTL_KERN, KERN_CPTIME2, i}; cpuinfo_size = sizeof (cpuinfo[0]); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), cpuinfo[i], &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } } else#endif /* defined(KERN_CPTIME2) */ { int mib[] = {CTL_KERN, KERN_CPTIME}; long cpuinfo_tmp[CPUSTATES]; cpuinfo_size = sizeof(cpuinfo_tmp); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
开发者ID:absperf,项目名称:collectd,代码行数:67,
示例10: mono_w32process_get_namegchar*mono_w32process_get_name (pid_t pid){ gint mib [6]; gsize size; struct kinfo_proc *pi; gchar *ret = NULL;#if defined(__FreeBSD__) mib [0] = CTL_KERN; mib [1] = KERN_PROC; mib [2] = KERN_PROC_PID; mib [3] = pid; if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0) { mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: sysctl() failed: %d", __func__, errno); return NULL; } if ((pi = g_malloc (size)) == NULL) return NULL; if (sysctl (mib, 4, pi, &size, NULL, 0) < 0) { if (errno == ENOMEM) { g_free (pi); mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: Didn't allocate enough memory for kproc info", __func__); } return NULL; } ret = strlen (pi->ki_comm) > 0 ? g_strdup (pi->ki_comm) : NULL; g_free (pi);#elif defined(__OpenBSD__) mib [0] = CTL_KERN; mib [1] = KERN_PROC; mib [2] = KERN_PROC_PID; mib [3] = pid; mib [4] = sizeof(struct kinfo_proc); mib [5] = 0;retry: if (sysctl(mib, 6, NULL, &size, NULL, 0) < 0) { mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER_PROCESS, "%s: sysctl() failed: %d", __func__, errno); return NULL; } if ((pi = g_malloc (size)) == NULL) return NULL; mib[5] = (int)(size / sizeof(struct kinfo_proc)); if ((sysctl (mib, 6, pi, &size, NULL, 0) < 0) || (size != sizeof (struct kinfo_proc))) { if (errno == ENOMEM) { g_free (pi); goto retry; } return NULL; } ret = strlen (pi->p_comm) > 0 ? g_strdup (pi->p_comm) : NULL; g_free (pi);#endif return ret;}
开发者ID:markusbeth,项目名称:mono,代码行数:67,
示例11: get_process_threads/* * Retrieves all threads used by process returning a list of tuples * including thread id, user time and system time. * Thanks to Robert N. M. Watson: * http://fxr.googlebit.com/source/usr.bin/procstat/procstat_threads.c?v=8-CURRENT */static PyObject*get_process_threads(PyObject* self, PyObject* args){ long pid; int mib[4]; struct kinfo_proc *kip; struct kinfo_proc *kipp; int error; unsigned int i; size_t size; PyObject* retList = PyList_New(0); PyObject* pyTuple = NULL; if (! PyArg_ParseTuple(args, "l", &pid)) { return NULL; } /* * We need to re-query for thread information, so don't use *kipp. */ mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; mib[3] = pid; size = 0; error = sysctl(mib, 4, NULL, &size, NULL, 0); if (error == -1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (size == 0) { return NoSuchProcess(); } kip = malloc(size); if (kip == NULL) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } error = sysctl(mib, 4, kip, &size, NULL, 0); if (error == -1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (size == 0) { return NoSuchProcess(); } for (i = 0; i < size / sizeof(*kipp); i++) { kipp = &kip[i]; pyTuple = Py_BuildValue("Idd", kipp->ki_tid, TV2DOUBLE(kipp->ki_rusage.ru_utime), TV2DOUBLE(kipp->ki_rusage.ru_stime) ); PyList_Append(retList, pyTuple); Py_XDECREF(pyTuple); } free(kip); return retList;}
开发者ID:tamentis,项目名称:psutil,代码行数:68,
示例12: physmem_total/* Return the total amount of physical memory. */doublephysmem_total (void){#if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE { /* This works on linux-gnu, solaris2 and cygwin. */ double pages = sysconf (_SC_PHYS_PAGES); double pagesize = sysconf (_SC_PAGESIZE); if (0 <= pages && 0 <= pagesize) return pages * pagesize; }#endif#if HAVE_PSTAT_GETSTATIC { /* This works on hpux11. */ struct pst_static pss; if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0)) { double pages = pss.physical_memory; double pagesize = pss.page_size; if (0 <= pages && 0 <= pagesize) return pages * pagesize; } }#endif#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE { /* This works on irix6. */ struct rminfo realmem; if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) { double pagesize = sysconf (_SC_PAGESIZE); double pages = realmem.physmem; if (0 <= pages && 0 <= pagesize) return pages * pagesize; } }#endif#if HAVE_GETSYSINFO && defined GSI_PHYSMEM { /* This works on Tru64 UNIX V4/5. */ int physmem; if (getsysinfo (GSI_PHYSMEM, (caddr_t) &physmem, sizeof (physmem), NULL, NULL, NULL) == 1) { double kbytes = physmem; if (0 <= kbytes) return kbytes * 1024.0; } }#endif#if HAVE_SYSCTL && defined HW_PHYSMEM { /* This works on *bsd and darwin. */ unsigned int physmem; size_t len = sizeof physmem; static int mib[2] = { CTL_HW, HW_PHYSMEM }; if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 && len == sizeof (physmem)) return (double) physmem; }#endif#if HAVE__SYSTEM_CONFIGURATION /* This works on AIX. */ return _system_configuration.physmem;#endif#if defined _WIN32 { /* this works on windows */ PFN_MS_EX pfnex; HMODULE h = GetModuleHandle ("kernel32.dll"); if (!h) return 0.0; /* Use GlobalMemoryStatusEx if available. */ if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) { lMEMORYSTATUSEX lms_ex; lms_ex.dwLength = sizeof lms_ex; if (!pfnex (&lms_ex)) return 0.0; return (double) lms_ex.ullTotalPhys; } /* Fall back to GlobalMemoryStatus which is always available. but returns wrong results for physical memory > 4GB. */ else { MEMORYSTATUS ms; GlobalMemoryStatus (&ms); return (double) ms.dwTotalPhys; } }#endif//.........这里部分代码省略.........
开发者ID:4solo,项目名称:cs35,代码行数:101,
示例13: physmem_available/* Return the amount of physical memory available. */doublephysmem_available (void){#if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE { /* This works on linux-gnu, solaris2 and cygwin. */ double pages = sysconf (_SC_AVPHYS_PAGES); double pagesize = sysconf (_SC_PAGESIZE); if (0 <= pages && 0 <= pagesize) return pages * pagesize; }#endif#if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC { /* This works on hpux11. */ struct pst_static pss; struct pst_dynamic psd; if (0 <= pstat_getstatic (&pss, sizeof pss, 1, 0) && 0 <= pstat_getdynamic (&psd, sizeof psd, 1, 0)) { double pages = psd.psd_free; double pagesize = pss.page_size; if (0 <= pages && 0 <= pagesize) return pages * pagesize; } }#endif#if HAVE_SYSMP && defined MP_SAGET && defined MPSA_RMINFO && defined _SC_PAGESIZE { /* This works on irix6. */ struct rminfo realmem; if (sysmp (MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0) { double pagesize = sysconf (_SC_PAGESIZE); double pages = realmem.availrmem; if (0 <= pages && 0 <= pagesize) return pages * pagesize; } }#endif#if HAVE_TABLE && defined TBL_VMSTATS { /* This works on Tru64 UNIX V4/5. */ struct tbl_vmstats vmstats; if (table (TBL_VMSTATS, 0, &vmstats, 1, sizeof (vmstats)) == 1) { double pages = vmstats.free_count; double pagesize = vmstats.pagesize; if (0 <= pages && 0 <= pagesize) return pages * pagesize; } }#endif#if HAVE_SYSCTL && defined HW_USERMEM { /* This works on *bsd and darwin. */ unsigned int usermem; size_t len = sizeof usermem; static int mib[2] = { CTL_HW, HW_USERMEM }; if (sysctl (mib, ARRAY_SIZE (mib), &usermem, &len, NULL, 0) == 0 && len == sizeof (usermem)) return (double) usermem; }#endif#if defined _WIN32 { /* this works on windows */ PFN_MS_EX pfnex; HMODULE h = GetModuleHandle ("kernel32.dll"); if (!h) return 0.0; /* Use GlobalMemoryStatusEx if available. */ if ((pfnex = (PFN_MS_EX) GetProcAddress (h, "GlobalMemoryStatusEx"))) { lMEMORYSTATUSEX lms_ex; lms_ex.dwLength = sizeof lms_ex; if (!pfnex (&lms_ex)) return 0.0; return (double) lms_ex.ullAvailPhys; } /* Fall back to GlobalMemoryStatus which is always available. but returns wrong results for physical memory > 4GB */ else { MEMORYSTATUS ms; GlobalMemoryStatus (&ms); return (double) ms.dwAvailPhys; } }#endif /* Guess 25% of physical memory. */ return physmem_total () / 4;}
开发者ID:4solo,项目名称:cs35,代码行数:100,
示例14: QStringvoid FeedbackDialog::GenerateSpecs(){ // Gather some information about the system and embed it into the report QDesktopWidget* screen = QApplication::desktop(); QString os_version = "Operating system: "; QString qt_version = QString("Qt version: ") + QT_VERSION_STR + QString("/n"); QString total_ram = "Total RAM: "; QString number_of_cores = "Number of cores: "; QString compiler_bits = "Compiler architecture: "; QString compiler_version = "Compiler version: "; QString kernel_line = "Kernel: "; QString screen_size = "Size of the screen(s): " + QString::number(screen->width()) + "x" + QString::number(screen->height()) + "/n"; QString number_of_screens = "Number of screens: " + QString::number(screen->screenCount()) + "/n"; QString processor_name = "Processor: "; // platform specific code#ifdef Q_OS_MACX number_of_cores += QString::number(sysconf(_SC_NPROCESSORS_ONLN)) + "/n"; uint64_t memsize; size_t len = sizeof(memsize); static int mib_s[2] = { CTL_HW, HW_MEMSIZE }; if (sysctl (mib_s, 2, &memsize, &len, NULL, 0) == 0) total_ram += QString::number(memsize/1024/1024) + " MB/n"; else total_ram += "Error getting total RAM information/n"; int mib[] = {CTL_KERN, KERN_OSRELEASE}; sysctl(mib, sizeof mib / sizeof(int), NULL, &len, NULL, 0); char *kernelVersion = (char *)malloc(sizeof(char)*len); sysctl(mib, sizeof mib / sizeof(int), kernelVersion, &len, NULL, 0); QString kernelVersionStr = QString(kernelVersion); free(kernelVersion); int major_version = kernelVersionStr.split(".").first().toUInt() - 4; int minor_version = kernelVersionStr.split(".").at(1).toUInt(); os_version += QString("Mac OS X 10.%1.%2").arg(major_version).arg(minor_version) + " "; switch(major_version) { case 4: os_version += "/"Tiger/"/n"; break; case 5: os_version += "/"Leopard/"/n"; break; case 6: os_version += "/"Snow Leopard/"/n"; break; case 7: os_version += "/"Lion/"/n"; break; case 8: os_version += "/"Mountain Lion/"/n"; break; default: os_version += "/"Unknown version/"/n"; break; }#endif#ifdef Q_OS_WIN SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); number_of_cores += QString::number(sysinfo.dwNumberOfProcessors) + "/n"; MEMORYSTATUSEX status; status.dwLength = sizeof(status); GlobalMemoryStatusEx(&status); total_ram += QString::number(status.ullTotalPhys/1024/1024) + " MB/n"; switch(QSysInfo::windowsVersion()) { case QSysInfo::WV_NT: os_version += "Windows NT/n"; break; case QSysInfo::WV_2000: os_version += "Windows 2000/n"; break; case QSysInfo::WV_XP: os_version += "Windows XP/n"; break; case QSysInfo::WV_2003: os_version += "Windows Server 2003/n"; break; case QSysInfo::WV_VISTA: os_version += "Windows Vista/n"; break; case QSysInfo::WV_WINDOWS7: os_version += "Windows 7/n"; break;#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) case QSysInfo::WV_WINDOWS8: os_version += "Windows 8/n"; break;#endif default: os_version += "Windows (Unknown version)/n"; break; } kernel_line += "Windows kernel/n";#endif#ifdef Q_OS_LINUX number_of_cores += QString::number(sysconf(_SC_NPROCESSORS_ONLN)) + "/n"; quint32 pages = sysconf(_SC_PHYS_PAGES); quint32 page_size = sysconf(_SC_PAGE_SIZE); quint64 total = (quint64)pages * page_size / 1024 / 1024; total_ram += QString::number(total) + " MB/n"; os_version += "GNU/Linux or BSD/n";#endif // uname -a#if defined(Q_OS_LINUX) || defined(Q_OS_MAC) QProcess *process = new QProcess(); QStringList arguments = QStringList("-a"); process->start("uname", arguments); if (process->waitForFinished()) kernel_line += QString(process->readAll()); delete process;#endif#if (defined(Q_OS_WIN) && defined(__i386__)) || defined(__x86_64__) // cpu info quint32 registers[4]; quint32 i; i = 0x80000002; asm volatile//.........这里部分代码省略.........
开发者ID:EchoLiao,项目名称:hedgewars,代码行数:101,
示例15: initstatic int init (void){#if PROCESSOR_CPU_LOAD_INFO || PROCESSOR_TEMPERATURE kern_return_t status; port_host = mach_host_self (); /* FIXME: Free `cpu_list' if it's not NULL */ if ((status = host_processors (port_host, &cpu_list, &cpu_list_len)) != KERN_SUCCESS) { ERROR ("cpu plugin: host_processors returned %i", (int) status); cpu_list_len = 0; return (-1); } DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors"); INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s"); cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (plugin_get_interval ());/* #endif PROCESSOR_CPU_LOAD_INFO */#elif defined(HAVE_LIBKSTAT) kstat_t *ksp_chain; numcpu = 0; if (kc == NULL) return (-1); /* Solaris doesn't count linear.. *sigh* */ for (numcpu = 0, ksp_chain = kc->kc_chain; (numcpu < MAX_NUMCPU) && (ksp_chain != NULL); ksp_chain = ksp_chain->ks_next) if (strncmp (ksp_chain->ks_module, "cpu_stat", 8) == 0) ksp[numcpu++] = ksp_chain;/* #endif HAVE_LIBKSTAT */#elif CAN_USE_SYSCTL size_t numcpu_size; int mib[2] = {CTL_HW, HW_NCPU}; int status; numcpu = 0; numcpu_size = sizeof (numcpu); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &numcpu, &numcpu_size, NULL, 0); if (status == -1) { char errbuf[1024]; WARNING ("cpu plugin: sysctl: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); }/* #endif CAN_USE_SYSCTL */#elif defined (HAVE_SYSCTLBYNAME) size_t numcpu_size; numcpu_size = sizeof (numcpu); if (sysctlbyname ("hw.ncpu", &numcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(hw.ncpu): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); }#ifdef HAVE_SYSCTL_KERN_CP_TIMES numcpu_size = sizeof (maxcpu); if (sysctlbyname("kern.smp.maxcpus", &maxcpu, &numcpu_size, NULL, 0) < 0) { char errbuf[1024]; WARNING ("cpu plugin: sysctlbyname(kern.smp.maxcpus): %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); }#else if (numcpu != 1) NOTICE ("cpu: Only one processor supported when using `sysctlbyname' (found %i)", numcpu);#endif/* #endif HAVE_SYSCTLBYNAME */#elif defined(HAVE_LIBSTATGRAB) /* nothing to initialize *//* #endif HAVE_LIBSTATGRAB */#elif defined(HAVE_PERFSTAT) /* nothing to initialize */#endif /* HAVE_PERFSTAT */ return (0);} /* int init */
开发者ID:adanin,项目名称:collectd,代码行数:95,
示例16: get_boot_timestatic time_tget_boot_time (void){#if defined (BOOT_TIME) int counter;#endif if (boot_time_initialized) return boot_time; boot_time_initialized = 1;#if defined (CTL_KERN) && defined (KERN_BOOTTIME) { int mib[2]; size_t size; struct timeval boottime_val; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; size = sizeof (boottime_val); if (sysctl (mib, 2, &boottime_val, &size, NULL, 0) >= 0) { boot_time = boottime_val.tv_sec; return boot_time; } }#endif /* defined (CTL_KERN) && defined (KERN_BOOTTIME) */ if (BOOT_TIME_FILE) { struct stat st; if (stat (BOOT_TIME_FILE, &st) == 0) { boot_time = st.st_mtime; return boot_time; } }#if defined (BOOT_TIME)#ifndef CANNOT_DUMP /* The utmp routines maintain static state. Don't touch that state unless we are initialized, since it might not survive dumping. */ if (! initialized) return boot_time;#endif /* not CANNOT_DUMP */ /* Try to get boot time from utmp before wtmp, since utmp is typically much smaller than wtmp. Passing a null pointer causes get_boot_time_1 to inspect the default file, namely utmp. */ get_boot_time_1 (0, 0); if (boot_time) return boot_time; /* Try to get boot time from the current wtmp file. */ get_boot_time_1 (WTMP_FILE, 1); /* If we did not find a boot time in wtmp, look at wtmp, and so on. */ for (counter = 0; counter < 20 && ! boot_time; counter++) { char cmd_string[sizeof WTMP_FILE ".19.gz"]; Lisp_Object tempname, filename; bool delete_flag = 0; filename = Qnil; tempname = make_formatted_string (cmd_string, "%s.%d", WTMP_FILE, counter); if (! NILP (Ffile_exists_p (tempname))) filename = tempname; else { tempname = make_formatted_string (cmd_string, "%s.%d.gz", WTMP_FILE, counter); if (! NILP (Ffile_exists_p (tempname))) { /* The utmp functions on mescaline.gnu.org accept only file names up to 8 characters long. Choose a 2 character long prefix, and call make_temp_file with second arg non-zero, so that it will add not more than 6 characters to the prefix. */ filename = Fexpand_file_name (build_string ("wt"), Vtemporary_file_directory); filename = make_temp_name (filename, 1); CALLN (Fcall_process, build_string ("gzip"), Qnil, list2 (QCfile, filename), Qnil, build_string ("-cd"), tempname); delete_flag = 1; } } if (! NILP (filename)) { get_boot_time_1 (SSDATA (filename), 1); if (delete_flag) unlink (SSDATA (filename)); } }//.........这里部分代码省略.........
开发者ID:AdrieanKhisbe,项目名称:emacs,代码行数:101,
示例17: cpu_read//.........这里部分代码省略......... static cpu_stat_t cs; if (kc == NULL) return (-1); for (cpu = 0; cpu < numcpu; cpu++) { derive_t derives[CPU_SUBMIT_MAX] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; if (kstat_read (kc, ksp[cpu], &cs) == -1) continue; /* error message? */ memset(derives, -1, sizeof(derives)); derives[CPU_SUBMIT_IDLE] = cs.cpu_sysinfo.cpu[CPU_IDLE]; derives[CPU_SUBMIT_USER] = cs.cpu_sysinfo.cpu[CPU_USER]; derives[CPU_SUBMIT_SYSTEM] = cs.cpu_sysinfo.cpu[CPU_KERNEL]; derives[CPU_SUBMIT_WAIT] = cs.cpu_sysinfo.cpu[CPU_WAIT]; submit (ksp[cpu]->ks_instance, derives); } submit_flush ();/* #endif defined(HAVE_LIBKSTAT) */#elif CAN_USE_SYSCTL uint64_t cpuinfo[numcpu][CPUSTATES]; size_t cpuinfo_size; int status; int i; if (numcpu < 1) { ERROR ("cpu plugin: Could not determine number of " "installed CPUs using sysctl(3)."); return (-1); } memset (cpuinfo, 0, sizeof (cpuinfo));#if defined(KERN_CPTIME2) if (numcpu > 1) { for (i = 0; i < numcpu; i++) { int mib[] = {CTL_KERN, KERN_CPTIME2, i}; cpuinfo_size = sizeof (cpuinfo[0]); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), cpuinfo[i], &cpuinfo_size, NULL, 0); if (status == -1) { char errbuf[1024]; ERROR ("cpu plugin: sysctl failed: %s.", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } } } else#endif /* defined(KERN_CPTIME2) */ { int mib[] = {CTL_KERN, KERN_CPTIME}; long cpuinfo_tmp[CPUSTATES]; cpuinfo_size = sizeof(cpuinfo_tmp); status = sysctl (mib, STATIC_ARRAY_SIZE (mib), &cpuinfo_tmp, &cpuinfo_size, NULL, 0);
开发者ID:adanin,项目名称:collectd,代码行数:67,
示例18: PROC_MEMint PROC_MEM(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result){ char procname[MAX_STRING_LEN], buffer[MAX_STRING_LEN], proccomm[MAX_STRING_LEN], *args; int do_task, pagesize, count, i, proc_ok, comm_ok, mib[4], mibs; double value = 0.0, memsize = 0; int proccount = 0; size_t sz; struct kinfo_proc *proc = NULL; struct passwd *usrinfo; if (num_param(param) > 4) return SYSINFO_RET_FAIL; if (0 != get_param(param, 1, procname, sizeof(procname))) *procname = '/0'; else if (strlen(procname) > ZBX_COMMLEN) procname[ZBX_COMMLEN] = '/0'; if (0 != get_param(param, 2, buffer, sizeof(buffer))) *buffer = '/0'; if (*buffer != '/0') { usrinfo = getpwnam(buffer); if (usrinfo == NULL) /* incorrect user name */ return SYSINFO_RET_FAIL; } else usrinfo = NULL; if (0 != get_param(param, 3, buffer, sizeof(buffer))) *buffer = '/0'; if (*buffer != '/0') { if (0 == strcmp(buffer, "avg")) do_task = DO_AVG; else if (0 == strcmp(buffer, "max")) do_task = DO_MAX; else if (0 == strcmp(buffer, "min")) do_task = DO_MIN; else if (0 == strcmp(buffer, "sum")) do_task = DO_SUM; else return SYSINFO_RET_FAIL; } else do_task = DO_SUM; if (0 != get_param(param, 4, proccomm, sizeof(proccomm))) *proccomm = '/0'; pagesize = getpagesize(); mib[0] = CTL_KERN; mib[1] = KERN_PROC; if (NULL != usrinfo) { mib[2] = KERN_PROC_UID; mib[3] = usrinfo->pw_uid; mibs = 4; } else { mib[2] = KERN_PROC_ALL; mib[3] = 0; mibs = 3; } sz = 0; if (0 != sysctl(mib, mibs, NULL, &sz, NULL, 0)) return SYSINFO_RET_FAIL; proc = (struct kinfo_proc *)zbx_malloc(proc, sz); if (0 != sysctl(mib, mibs, proc, &sz, NULL, 0)) { zbx_free(proc); return SYSINFO_RET_FAIL; } count = sz / sizeof(struct kinfo_proc); for (i = 0; i < count; i++) {#if(__FreeBSD_version > 500000) if (proc[i].ki_flag & P_KTHREAD) /* skip a system thread */ continue;#endif proc_ok = 0; comm_ok = 0; if (*procname == '/0' || 0 == strcmp(procname, proc[i].ZBX_PROC_COMM))//.........这里部分代码省略.........
开发者ID:gheja,项目名称:zabbix-ext,代码行数:101,
示例19: bsd_acpi_checkstatic voidbsd_acpi_check(void){ int bat_val = 0; int mib_state[4]; int mib_life[4]; int mib_time[4]; int mib_units[4]; size_t len; int state = 0; int level = 0; int time_min = 0; int life = 0; int batteries = 0; time_left = -1; battery_full = -1; have_battery = 0; have_power = 0; /* Read some information on first run. */ len = 4; sysctlnametomib("hw.acpi.battery.state", mib_state, &len); len = sizeof(state); if (sysctl(mib_state, 4, &state, &len, NULL, 0) == -1) /* ERROR */ state = -1; len = 4; sysctlnametomib("hw.acpi.battery.life", mib_life, &len); len = sizeof(life); if (sysctl(mib_life, 4, &life, &len, NULL, 0) == -1) /* ERROR */ level = -1; bat_val = life; len = 4; sysctlnametomib("hw.acpi.battery.time", mib_time, &len); len = sizeof(time); if (sysctl(mib_time, 4, &time_min, &len, NULL, 0) == -1) /* ERROR */ time_min = -1; len = 4; sysctlnametomib("hw.acpi.battery.units", mib_units, &len); len = sizeof(batteries); if (sysctl(mib_time, 4, &batteries, &len, NULL, 0) == -1) /* ERROR */ batteries = 1; if (time_min >= 0) time_left = time_min * 60; if (batteries == 1) /* hw.acpi.battery.units = 1 means NO BATTS */ time_left = -1; else if ((state == BATTERY_STATE_CHARGING) || (state == BATTERY_STATE_DISCHARGING)) { have_battery = 1; if (state == BATTERY_STATE_CHARGING) have_power = 1; else if (state == BATTERY_STATE_DISCHARGING) have_power = 0; if (level == -1) time_left = -1; else if (time_min == -1) { time_left = -1; battery_full = bat_val; } else battery_full = bat_val; } else { have_battery = 1; battery_full = 100; time_left = -1; have_power = 1; }}
开发者ID:tpham3783,项目名称:enlightenment,代码行数:77,
示例20: isc_interfaceiter_createisc_result_tisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) { isc_interfaceiter_t *iter; isc_result_t result; size_t bufsize; size_t bufused; char strbuf[ISC_STRERRORSIZE]; REQUIRE(mctx != NULL); REQUIRE(iterp != NULL); REQUIRE(*iterp == NULL); iter = isc_mem_get(mctx, sizeof(*iter)); if (iter == NULL) return (ISC_R_NOMEMORY); iter->mctx = mctx; iter->buf = 0; /* * Determine the amount of memory needed. */ bufsize = 0; if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) { isc__strerror(errno, strbuf, sizeof(strbuf)); UNEXPECTED_ERROR(__FILE__, __LINE__, isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL, ISC_MSG_GETIFLISTSIZE, "getting interface " "list size: sysctl: %s"), strbuf); result = ISC_R_UNEXPECTED; goto failure; } iter->bufsize = bufsize; iter->buf = isc_mem_get(iter->mctx, iter->bufsize); if (iter->buf == NULL) { result = ISC_R_NOMEMORY; goto failure; } bufused = bufsize; if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) { isc__strerror(errno, strbuf, sizeof(strbuf)); UNEXPECTED_ERROR(__FILE__, __LINE__, isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL, ISC_MSG_GETIFLIST, "getting interface list: " "sysctl: %s"), strbuf); result = ISC_R_UNEXPECTED; goto failure; } iter->bufused = bufused; INSIST(iter->bufused <= iter->bufsize); /* * A newly created iterator has an undefined position * until isc_interfaceiter_first() is called. */ iter->pos = (unsigned int) -1; iter->result = ISC_R_FAILURE; iter->magic = IFITER_MAGIC; *iterp = iter; return (ISC_R_SUCCESS); failure: if (iter->buf != NULL) isc_mem_put(mctx, iter->buf, iter->bufsize); isc_mem_put(mctx, iter, sizeof(*iter)); return (result);}
开发者ID:jhbsz,项目名称:netbsd,代码行数:76,
示例21: _getMemorySize/** * Returns the size of physical memory (RAM) in bytes. */size_t _getMemorySize(){#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__)) /* Cygwin under Windows. ------------------------------------ */ /* New 64-bit MEMORYSTATUSEX isn't available. Use old 32.bit */ MEMORYSTATUS status; status.dwLength = sizeof(status); GlobalMemoryStatus( &status ); return (size_t)status.dwTotalPhys;#elif defined(_WIN32) /* Windows. ------------------------------------------------- */ /* Use new 64-bit MEMORYSTATUSEX, not old 32-bit MEMORYSTATUS */ MEMORYSTATUSEX status; status.dwLength = sizeof(status); GlobalMemoryStatusEx( &status ); return (size_t)status.ullTotalPhys;#elif defined(__unix__) || defined(__unix) || defined(unix) || / (defined(__APPLE__) && defined(__MACH__)) /* UNIX variants. ------------------------------------------- */ /* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)) int mib[2]; mib[0] = CTL_HW;#if defined(HW_MEMSIZE) mib[1] = HW_MEMSIZE; /* OSX. --------------------- */#elif defined(HW_PHYSMEM64) mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */#endif int64_t size = 0; /* 64-bit */ size_t len = sizeof( size ); if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) return (size_t)size; return 0L; /* Failed? */#elif defined(_SC_AIX_REALMEM) /* AIX. ----------------------------------------------------- */ return (size_t)sysconf( _SC_AIX_REALMEM ) * (size_t)1024L;#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE) /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */ return (size_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGESIZE );#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE) /* Legacy. -------------------------------------------------- */ return (size_t)sysconf( _SC_PHYS_PAGES ) * (size_t)sysconf( _SC_PAGE_SIZE );#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM)) /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */ int mib[2]; mib[0] = CTL_HW;#if defined(HW_REALMEM) mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */#elif defined(HW_PYSMEM) mib[1] = HW_PHYSMEM; /* Others. ------------------ */#endif unsigned int size = 0; /* 32-bit */ size_t len = sizeof( size ); if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 ) return (size_t)size; return 0L; /* Failed? */#endif /* sysctl and sysconf variants */#else return 0L; /* Unknown OS. */#endif}
开发者ID:ambarrio,项目名称:mccortex,代码行数:74,
示例22: update_obsd_sensors/* read sensors from sysctl */void update_obsd_sensors(){ int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 }; struct sensor sensor; struct sensordev sensordev; size_t slen, sdlen; enum sensor_type type; slen = sizeof(sensor); sdlen = sizeof(sensordev); sensor_cnt = 0; dev = obsd_sensors.device; // FIXME: read more than one device /* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */ mib[2] = dev; if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) { if (errno != ENOENT) { warn("sysctl"); } return; // continue; } for (type = 0; type < SENSOR_MAX_TYPES; type++) { mib[3] = type; for (numt = 0; numt < sensordev.maxnumt[type]; numt++) { mib[4] = numt; if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) { if (errno != ENOENT) { warn("sysctl"); } continue; } if (sensor.flags & SENSOR_FINVALID) { continue; } switch (type) { case SENSOR_TEMP: obsd_sensors.temp[dev][sensor.numt] = (sensor.value - 273150000) / 1000000.0; break; case SENSOR_FANRPM: obsd_sensors.fan[dev][sensor.numt] = sensor.value; break; case SENSOR_VOLTS_DC: obsd_sensors.volt[dev][sensor.numt] = sensor.value / 1000000.0; break; default: break; } sensor_cnt++; } } /* } */ init_sensors = 1;}
开发者ID:dilawar,项目名称:suckless,代码行数:62,
示例23: GetBSDProcessListint GetBSDProcessList(kinfo_proc **procList, size_t *procCount)// Returns a list of all BSD processes on the system. This routine// allocates the list and puts it in *procList and a count of the// number of entries in *procCount. You are responsible for freeing// this list (use "free" from System framework).// On success, the function returns 0.// On error, the function returns a BSD errno value.{ int err; kinfo_proc * result; bool done; static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; // Declaring name as const requires us to cast it when passing it to // sysctl because the prototype doesn't include the const modifier. size_t length; assert( procList != NULL); assert(*procList == NULL); assert(procCount != NULL); *procCount = 0; // We start by calling sysctl with result == NULL and length == 0. // That will succeed, and set length to the appropriate length. // We then allocate a buffer of that size and call sysctl again // with that buffer. If that succeeds, we're done. If that fails // with ENOMEM, we have to throw away our buffer and loop. Note // that the loop causes use to call sysctl with NULL again; this // is necessary because the ENOMEM failure case sets length to // the amount of data returned, not the amount of data that // could have been returned. result = NULL; done = false; do { assert(result == NULL); // Call sysctl with a NULL buffer. length = 0; err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, NULL, &length, NULL, 0); if (err == -1) { err = errno; } // Allocate an appropriately sized buffer based on the results // from the previous call. if (err == 0) { result = malloc(length); if (result == NULL) { err = ENOMEM; } } // Call sysctl again with the new buffer. If we get an ENOMEM // error, toss away our buffer and start again. if (err == 0) { err = sysctl( (int *) name, (sizeof(name) / sizeof(*name)) - 1, result, &length, NULL, 0); if (err == -1) { err = errno; } if (err == 0) { done = true; } else if (err == ENOMEM) { assert(result != NULL); free(result); result = NULL; err = 0; } } } while (err == 0 && ! done); // Clean up and establish post conditions. if (err != 0 && result != NULL) { free(result); result = NULL; } *procList = result; if (err == 0) { *procCount = length / sizeof(kinfo_proc); } assert( (err == 0) == (*procList != NULL) ); return err;}
开发者ID:Comdex,项目名称:KeyCast,代码行数:93,
示例24: mainint main(int ac, char **av){ int exp_eno; int lc; char *msg; char osname[OSNAMESZ]; int osnamelth, status; int name[] = { CTL_KERN, KERN_OSTYPE }; pid_t pid; struct passwd *ltpuser; /* parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); } setup(); if ((tst_kvercmp(2, 6, 32)) <= 0) { exp_eno = EPERM; } else { /* ^^ Look above this warning. ^^ */ tst_resm(TWARN, "this test's results are based on potentially undocumented behavior in the kernel. read the NOTE in the source file for more details"); exp_eno = EACCES; exp_enos[0] = EACCES; } TEST_EXP_ENOS(exp_enos); for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; strcpy(osname, "Linux"); osnamelth = SIZE(osname); TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "sysctl(2) succeeded unexpectedly"); } else { TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == exp_eno) { tst_resm(TPASS|TTERRNO, "Got expected error"); } else if (errno == ENOSYS) { tst_resm(TCONF, "You may need to make CONFIG_SYSCTL_SYSCALL=y" " to your kernel config."); } else { tst_resm(TFAIL|TTERRNO, "Got unexpected error"); } } osnamelth = SIZE(osname); if ((ltpuser = getpwnam("nobody")) == NULL) { tst_brkm(TBROK, cleanup, "getpwnam() failed"); } /* set process ID to "ltpuser1" */ if (seteuid(ltpuser->pw_uid) == -1) { tst_brkm(TBROK, cleanup, "seteuid() failed, errno %d", errno); } if ((pid = FORK_OR_VFORK()) == -1) { tst_brkm(TBROK, cleanup, "fork() failed"); } if (pid == 0) { TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "call succeeded unexpectedly"); } else { TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == exp_eno) { tst_resm(TPASS|TTERRNO, "Got expected error"); } else if (TEST_ERRNO == ENOSYS) { tst_resm(TCONF, "You may need to make CONFIG_SYSCTL_SYSCALL=y" " to your kernel config."); } else { tst_resm(TFAIL|TTERRNO, "Got unexpected error"); } } cleanup(); } else { /* wait for the child to finish */ wait(&status); } /* set process ID back to root */ if (seteuid(0) == -1) {//.........这里部分代码省略.........
开发者ID:piet-delaney,项目名称:android-ltp-ndk,代码行数:101,
示例25: read_ip_stat//.........这里部分代码省略......... (!atime_ready (ip_stats_cache_marker, IP_STATS_CACHE_TIMEOUT * 1000)))#if !(defined(linux) || defined(solaris2)) return ((magic == IPFORWARDING ? forward : (magic == IPDEFAULTTTL ? ttl : 0)));#else return 0;#endif if (ip_stats_cache_marker) atime_setMarker(ip_stats_cache_marker); else ip_stats_cache_marker = atime_newMarker();#ifdef linux ret_value = linux_read_ip_stat(ipstat);#endif#ifdef solaris2 ret_value = getMibstat(MIB_IP, ipstat, sizeof(mib2_ip_t), GET_FIRST, &Get_everything, NULL);#endif#ifdef WIN32 ret_value = GetIpStatistics(ipstat);#endif#if !(defined(linux) || defined(solaris2) || defined(WIN32)) if (magic == IPFORWARDING) {#if defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS) len = sizeof i; sname[3] = IPCTL_FORWARDING; if (sysctl(sname, 4, &i, &len, 0, 0) < 0) forward = -1; else forward = (i ? 1 /* GATEWAY */ : 2 /* HOST */ );#else if (!auto_nlist (IP_FORWARDING_SYMBOL, (char *) &ret_value, sizeof(ret_value))) forward = -1; else forward = (ret_value ? 1 /* GATEWAY */ : 2 /* HOST */ );#endif if (forward == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return forward; } if (magic == IPDEFAULTTTL) {#if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) len = sizeof i; sname[3] = IPCTL_DEFTTL; if (sysctl(sname, 4, &i, &len, 0, 0) < 0) ttl = -1; else ttl = i;#else if (!auto_nlist (TCP_TTL_SYMBOL, (char *) &ret_value, sizeof(ret_value))) ttl = -1; else ttl = ret_value;#endif if (ttl == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return ttl; }#ifdef HAVE_SYS_TCPIPSTATS_H ret_value = sysmp(MP_SAGET, MPSA_TCPIPSTATS, ipstat, sizeof *ipstat);#endif#if (defined(CAN_USE_SYSCTL) && defined(IPCTL_STATS)) len = sizeof *ipstat; sname[3] = IPCTL_STATS; ret_value = sysctl(sname, 4, ipstat, &len, 0, 0);#endif#ifdef IPSTAT_SYMBOL if (auto_nlist(IPSTAT_SYMBOL, (char *) ipstat, sizeof(*ipstat))) ret_value = 0;#endif#endif /* !(defined(linux) || defined(solaris2)) */ if (ret_value == -1) { free(ip_stats_cache_marker); ip_stats_cache_marker = NULL; } return ret_value;#endif /* hpux11 */}
开发者ID:AllardJ,项目名称:Tomato,代码行数:101,
示例26: __xunameint__xuname(int namesize, void *namebuf){ int mib[2], rval; size_t len; char *p, *q; int oerrno; rval = 0; q = (char *)namebuf; mib[0] = CTL_KERN; if ((p = getenv("UNAME_s"))) strlcpy(q, p, namesize); else { mib[1] = KERN_OSTYPE; len = namesize; oerrno = errno; if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { if (errno == ENOMEM) errno = oerrno; else rval = -1; } q[namesize - 1] = '/0'; } q += namesize; mib[1] = KERN_HOSTNAME; len = namesize; oerrno = errno; if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { if (errno == ENOMEM) errno = oerrno; else rval = -1; } q[namesize - 1] = '/0'; q += namesize; if ((p = getenv("UNAME_r"))) strlcpy(q, p, namesize); else { mib[1] = KERN_OSRELEASE; len = namesize; oerrno = errno; if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { if (errno == ENOMEM) errno = oerrno; else rval = -1; } q[namesize - 1] = '/0'; } q += namesize; if ((p = getenv("UNAME_v"))) strlcpy(q, p, namesize); else { /* * The version may have newlines in it, turn them into * spaces. */ mib[1] = KERN_VERSION; len = namesize; oerrno = errno; if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { if (errno == ENOMEM) errno = oerrno; else rval = -1; } q[namesize - 1] = '/0'; for (p = q; len--; ++p) { if (*p == '/n' || *p == '/t') { if (len > 1) *p = ' '; else *p = '/0'; } } } q += namesize; if ((p = getenv("UNAME_m"))) strlcpy(q, p, namesize); else { mib[0] = CTL_HW; mib[1] = HW_MACHINE; len = namesize; oerrno = errno; if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { if (errno == ENOMEM) errno = oerrno; else rval = -1; } q[namesize - 1] = '/0';//.........这里部分代码省略.........
开发者ID:seco,项目名称:freebsd-pi,代码行数:101,
示例27: w_start_listenerbool w_start_listener(const char *path){ pthread_mutexattr_t mattr;#ifndef _WIN32 struct sigaction sa; sigset_t sigset;#endif void *ignored;#ifdef HAVE_LIBGIMLI_H volatile struct gimli_heartbeat *hb = NULL;#endif struct timeval tv; int n_clients = 0; listener_thread = pthread_self(); pthread_mutexattr_init(&mattr); pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&w_client_lock, &mattr); pthread_mutexattr_destroy(&mattr);#ifdef HAVE_LIBGIMLI_H hb = gimli_heartbeat_attach();#endif#if defined(HAVE_KQUEUE) || defined(HAVE_FSEVENTS) { struct rlimit limit;# ifndef __OpenBSD__ int mib[2] = { CTL_KERN,# ifdef KERN_MAXFILESPERPROC KERN_MAXFILESPERPROC# else KERN_MAXFILES# endif };# endif int maxperproc; getrlimit(RLIMIT_NOFILE, &limit);# ifndef __OpenBSD__ size_t len; len = sizeof(maxperproc); sysctl(mib, 2, &maxperproc, &len, NULL, 0); w_log(W_LOG_ERR, "file limit is %" PRIu64 " kern.maxfilesperproc=%i/n", limit.rlim_cur, maxperproc);# else maxperproc = limit.rlim_max; w_log(W_LOG_ERR, "openfiles-cur is %" PRIu64 " openfiles-max=%i/n", limit.rlim_cur, maxperproc);# endif if (limit.rlim_cur != RLIM_INFINITY && maxperproc > 0 && limit.rlim_cur < (rlim_t)maxperproc) { limit.rlim_cur = maxperproc; if (setrlimit(RLIMIT_NOFILE, &limit)) { w_log(W_LOG_ERR, "failed to raise limit to %" PRIu64 " (%s)./n", limit.rlim_cur, strerror(errno)); } else { w_log(W_LOG_ERR, "raised file limit to %" PRIu64 "/n", limit.rlim_cur); } } getrlimit(RLIMIT_NOFILE, &limit);#ifndef HAVE_FSEVENTS if (limit.rlim_cur < 10240) { w_log(W_LOG_ERR, "Your file descriptor limit is very low (%" PRIu64 "), " "please consult the watchman docs on raising the limits/n", limit.rlim_cur); }#endif }#endif proc_pid = (int)getpid(); if (gettimeofday(&tv, NULL) == -1) { w_log(W_LOG_ERR, "gettimeofday failed: %s/n", strerror(errno)); return false; } proc_start_time = (uint64_t)tv.tv_sec;#ifndef _WIN32 signal(SIGPIPE, SIG_IGN); /* allow SIGUSR1 and SIGCHLD to wake up a blocked thread, without restarting * syscalls */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = wakeme; sa.sa_flags = 0;//.........这里部分代码省略.........
开发者ID:kevinsawicki,项目名称:watchman,代码行数:101,
示例28: ip_rt_devintip_rt_dev(u_int32_t addr,u_char *name){ size_t needed; int mib[6], rlen, seqno; char *buf, *next, *lim,i; register struct rt_msghdr *rtm; struct sockaddr *sa ; struct sockaddr_in *sin; u_int32_t devip = 0,dest,mask,gate,local; char *cp; local = htonl(0x7f000001); mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = 0; mib[4] = NET_RT_DUMP; mib[5] = 0; if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0){ croak("route-sysctl-estimate"); } if ((buf = malloc(needed)) == NULL){ croak("malloc"); } if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0){ croak("route-sysctl-get"); } lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; sa = (struct sockaddr *)(rtm + 1); cp = (char*)sa; if (sa->sa_family != AF_INET) continue; dest = mask = gate = 0; for (i = 1; i; i <<= 1) if (i & rtm->rtm_addrs) { sa = (struct sockaddr *)cp; switch (i) { case RTA_DST: sin = (struct sockaddr_in*)sa; dest = sin->sin_addr.s_addr; break; case RTA_GATEWAY: if(rtm->rtm_flags & RTF_GATEWAY){ sin = (struct sockaddr_in*)sa; gate = sin->sin_addr.s_addr; } break; case RTA_NETMASK: sin = (struct sockaddr_in*)sa; mask = sin->sin_addr.s_addr; break; } ADVANCE(cp, sa); } if(!(rtm->rtm_flags & RTF_LLINFO) && (rtm->rtm_flags & RTF_HOST)) mask = 0xffffffff; if(!mask && dest && (dest != local)) continue; if(!dest) mask = 0; if(dest == local) { dest = htonl(0x7f000000); mask = htonl(0xff000000); } if(!((mask & addr) ^ dest)){ switch (gate) { case 0: devip = addr; break; default: devip = gate; } } } free(buf); return dev_name(devip,name);}
开发者ID:AsherBond,项目名称:fwknop,代码行数:77,
注:本文中的sysctl函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 C++ sysctl_ctx_init函数代码示例 C++ syscore_resume函数代码示例 |