static int swapmode(int *retavail, int *retfree) { int n; int pagesize = getpagesize(); struct kvm_swap swapary[1]; static int kd_init = TRUE; if(kd_init) { kd_init = FALSE; if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) { g_warning("Cannot read kvm."); return -1; } } if(kd == NULL) { return -1; } *retavail = 0; *retfree = 0; #define CONVERT(v) ((quad_t)(v) * pagesize / 1024) n = kvm_getswapinfo(kd, swapary, 1, 0); if (n < 0 || swapary[0].ksw_total == 0) return(0); *retavail = CONVERT(swapary[0].ksw_total); *retfree = CONVERT(swapary[0].ksw_total - swapary[0].ksw_used); n = (int)((double)swapary[0].ksw_used * 100.0 / (double)swapary[0].ksw_total); return(n); }
static int swap_read (void) /* {{{ */ { struct kvm_swap data_s; int status; derive_t used; derive_t free; derive_t total; if (kvm_obj == NULL) return (-1); /* only one structure => only get the grand total, no details */ status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0); if (status == -1) return (-1); total = (derive_t) data_s.ksw_total; used = (derive_t) data_s.ksw_used; total *= (derive_t) kvm_pagesize; used *= (derive_t) kvm_pagesize; free = total - used; swap_submit_gauge (NULL, "used", (gauge_t) used); swap_submit_gauge (NULL, "free", (gauge_t) free); return (0); } /* }}} int swap_read */
/* * swapmode is based on a program called swapinfo written * by Kevin Lahey <*****@*****.**>. */ void swapmode(void) { struct kvm_swap kswap[16]; int i; int n; const char *header; int hlen; pagesize = getpagesize(); n = kvm_getswapinfo( kd, kswap, sizeof(kswap)/sizeof(kswap[0]), ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX ); #define CONVERT(v) ((long)((quad_t)(v) * pagesize / blocksize)) #define CONVERTB(v) ((long)((quad_t)(v) * pagesize)) if (humanflag) { hlen = 9; header = " Blocks"; } else { header = getbsize(&hlen, &blocksize); } if (totalflag == 0) { printf("%-15s %*s %8s %8s %8s %s\n", "Device", hlen, header, "Used", "Avail", "Capacity", "Type"); for (i = 0; i < n; ++i) { Output(kswap[i].ksw_devname, hlen, &kswap[i], 1); } } if (totalflag) { if (humanflag) { char buf1[6]; char buf2[6]; humanize_number(buf1, sizeof(buf1), CONVERTB(kswap[n].ksw_used), "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); humanize_number(buf2, sizeof(buf2), CONVERTB(kswap[n].ksw_total), "", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); printf("%s/%s swap space\n", buf1, buf2); } else { blocksize = 1024 * 1024; printf("%ldM/%ldM swap space\n", CONVERT(kswap[n].ksw_used), CONVERT(kswap[n].ksw_total)); } } else if (n > 1) { Output("Total", hlen, &kswap[n], 0); } }
g_val_t swap_free_func ( void ) { g_val_t val; struct kvm_swap swap[1]; struct xswdev xsw; size_t size; int totswap, usedswap, freeswap, n; val.f = 0; totswap = 0; usedswap = 0; if (use_vm_swap_info) { for (n = 0; ; ++n) { mibswap[mibswap_size] = n; size = sizeof(xsw); if (sysctl(mibswap, mibswap_size + 1, &xsw, &size, NULL, 0) == -1) break; if (xsw.xsw_version != XSWDEV_VERSION) return val; totswap += xsw.xsw_nblks; usedswap += xsw.xsw_used; } } else if(kd != NULL) { n = kvm_getswapinfo(kd, swap, 1, 0); totswap = swap[0].ksw_total; usedswap = swap[0].ksw_used; } freeswap = totswap - usedswap; val.f = freeswap * (pagesize / 1024); return val; }
int machine_init(pf_meminfo_t *ret) { size_t t = sizeof(ret->mem_used); int pagesize; struct kvm_swap swapinfo; kvm_t *kd; pagesize = getpagesize(); sysctlbyname("vm.stats.vm.v_active_count", &ret->mem_used, &t, NULL, 0); ret->mem_used *= pagesize; ret->mem_used *= K2B; sysctlbyname("vm.stats.vm.v_free_count", &ret->mem_free, &t, NULL, 0); ret->mem_free *= pagesize; ret->mem_free *= K2B; ret->mem_total = ret->mem_free + ret->mem_used; sysctlbyname("vfs.bufspace", &ret->buffers, &t, NULL, 0); sysctlbyname("vm.stats.vm.v_cache_count", &ret->cached, &t, NULL, 0); ret->cached *= pagesize; ret->cached *= K2B; kd = kvm_open(NULL, DEVNULL_PATH, NULL, O_RDONLY, "kvm_open"); kvm_getswapinfo(kd, &swapinfo, 1, 0); ret->swap_total *= pagesize; ret->swap_total *= K2B; ret->swap_used *= pagesize; ret->swap_used *= K2B; ret->swap_free = swapinfo.ksw_total - swapinfo.ksw_used; return 0; }
int MemSensor::getSwapTotal() { #ifdef Q_OS_FREEBSD # if defined(__FreeBSD_version) && __FreeBSD_version >= 500018 int n = -1; int pagesize = getpagesize(); int retavail = 0; if (kd != NULL) n = kvm_getswapinfo(kd, &swapinfo, 1, 0); if (n < 0 || swapinfo.ksw_total == 0) return(0); retavail = swapinfo.ksw_total * pagesize / 1024; return(retavail); #else return(swapTotal); # endif #elif defined(Q_OS_NETBSD) struct uvmexp_sysctl uvmexp; int STotal = 0; int pagesize = 1; int mib[2]; size_t ssize; mib[0] = CTL_VM; mib[1] = VM_UVMEXP; ssize = sizeof(uvmexp); if (sysctl(mib,2,&uvmexp,&ssize,NULL,0) != -1) { pagesize = uvmexp.pagesize; STotal = (pagesize*uvmexp.swpages) >> 10; }
int _kvm_swap_used_pages(uint64_t *out) { const int total_only = 1; // from kvm_getswapinfo(3) kvm_t *kd; struct kvm_swap current; kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); if (kd == NULL) { return -1; } if (kvm_getswapinfo(kd, ¤t, total_only, 0) == -1) { goto error1; } if (kvm_close(kd) != 0) { return -1; } kd = NULL; *out = current.ksw_used; return 0; error1: if (kd != NULL) { kvm_close(kd); } return -1; }
void fetchswap(void) { okvnsw = kvnsw; if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { error("systat: kvm_getswapinfo failed"); return; } odlen = dlen; oulen = ulen; calclens(); }
void mem_getfree() { struct vmmeter vm; struct kvm_swap sw; int bufspace = 0; unsigned long cnt_offset; unsigned long bufspace_offset; static int firsttime = 1; static time_t lasttime = 0; time_t curtime; if ((kvm_read(kd, nlst[0].n_value, &vm, sizeof(vm)) != sizeof(vm)) || (kvm_read(kd, nlst[1].n_value, &bufspace, sizeof(bufspace)) != sizeof(bufspace))) { perror("kvm_read"); exit(1); } mem_total = vm.v_page_count; mem_free = vm.v_free_count; mem_used = mem_total - mem_free; mem_cached = vm.v_cache_count; mem_buffers = bufspace / vm.v_page_size; /* * Only calculate when first time or when changes took place. * Do not call it more than 1 time per 2 seconds; otherwise * it can eat up to 50% of CPU time on heavy swap activity. */ curtime = time(NULL); if (firsttime || curtime > lasttime + 5) { if (kvm_getswapinfo(kd, &sw, 1, 0) >= 0 && sw.ksw_total) { swp_total = sw.ksw_total; swp_used = sw.ksw_used; swp_free = swp_total - swp_used; } firsttime = 0; lasttime = curtime; } }
int swapmode(long pagesize) { int i, n; static kvm_t *kd = NULL; struct kvm_swap kswap[16]; netsnmp_memory_info *mem; char buf[1024]; if (kd == NULL) kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); n = kvm_getswapinfo(kd, kswap, sizeof(kswap) / sizeof(kswap[0]), 0); swapUsed = swapTotal = swapFree = 0; if ( n > 1 ) { /* * If there are multiple swap devices, then record * the statistics for each one separately... */ for (i = 0; i < n; ++i) { mem = netsnmp_memory_get_byIdx( NETSNMP_MEM_TYPE_SWAP+1+i, 1 ); if (!mem) continue; if (!mem->descr) { sprintf(buf, "swap %s", kswap[i].ksw_devname); mem->descr = strdup( buf ); } mem->units = pagesize; mem->size = kswap[i].ksw_total; mem->free = kswap[i].ksw_total - kswap[i].ksw_used; /* * ... and keep a running total for the overall swap stats */ swapTotal += kswap[i].ksw_total; swapUsed += kswap[i].ksw_used; } } else { /* * If there's only one swap device, then don't bother * with individual statistics. */ swapTotal += kswap[0].ksw_total; swapUsed += kswap[0].ksw_used; } swapFree = swapTotal - swapUsed; return n; }
static int swapmode(guint64 *retavail, guint64 *retfree) { guint64 used, avail; static int psize = -1; struct kvm_swap kvmswap; /* * Counter for error messages. If we reach the limit, * stop reading information from swap devices and * return zero. This prevent endless 'bad address' * messages. */ static int warning = 10; if (warning <= 0) { /* a single warning */ if (!warning) { warning--; fprintf(stderr, "Too much errors, stop reading swap devices ...\n"); } return(0); } warning--; /* decrease counter, see end of function */ if (kvmd == NULL) return(0); if (kvm_getswapinfo(kvmd, &kvmswap, 1, 0) < 0) { fprintf(stderr, "kvm_getswapinfo failed\n"); return(0); } if (psize < 0) psize = getpagesize(); *retavail = avail = (quad_t)kvmswap.ksw_total * psize; used = (quad_t)kvmswap.ksw_used * psize; *retfree = avail - used; /* increase counter, no errors occurs */ warning++; return (int)(((double)used / (double)avail * 100.0) + 0.5); }
PyObject * psutil_swap_mem(PyObject *self, PyObject *args) { // Return swap memory stats (see 'swapinfo' cmdline tool) kvm_t *kd; struct kvm_swap kvmsw[1]; unsigned int swapin, swapout, nodein, nodeout; size_t size = sizeof(unsigned int); kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open failed"); if (kd == NULL) { PyErr_SetString(PyExc_RuntimeError, "kvm_open failed"); return NULL; } if (kvm_getswapinfo(kd, kvmsw, 1, 0) < 0) { kvm_close(kd); PyErr_SetString(PyExc_RuntimeError, "kvm_getswapinfo failed"); return NULL; } kvm_close(kd); if (sysctlbyname("vm.stats.vm.v_swapin", &swapin, &size, NULL, 0) == -1) goto sbn_error; if (sysctlbyname("vm.stats.vm.v_swapout", &swapout, &size, NULL, 0) == -1) goto sbn_error; if (sysctlbyname("vm.stats.vm.v_vnodein", &nodein, &size, NULL, 0) == -1) goto sbn_error; if (sysctlbyname("vm.stats.vm.v_vnodeout", &nodeout, &size, NULL, 0) == -1) goto sbn_error; return Py_BuildValue("(iiiII)", kvmsw[0].ksw_total, // total kvmsw[0].ksw_used, // used kvmsw[0].ksw_total - kvmsw[0].ksw_used, // free swapin + swapout, // swap in nodein + nodeout); // swap out sbn_error: PyErr_SetFromErrno(PyExc_OSError); return NULL; }
void swapmode(void) { int pagesize; int i, n; static kvm_t *kd = NULL; struct kvm_swap kswap[16]; if (kd == NULL) kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); n = kvm_getswapinfo(kd, kswap, sizeof(kswap) / sizeof(kswap[0]), 0); swapUsed = swapTotal = swapFree = 0; /* * Count up free swap space. */ for (i = 0; i < n; ++i) swapFree += kswap[i].ksw_total - kswap[i].ksw_used; /* * Count up total swap space */ for (i = 0; i < n; i++) swapTotal += kswap[i].ksw_total; /* * Calculate used swap space */ swapUsed = swapTotal - swapFree; /* * Convert to kb */ pagesize = getpagesize() / 1024; swapTotal *= pagesize; swapUsed *= pagesize; swapFree *= pagesize; }
/** * Reads info about swap space from system and returns it in parameters passed. * \param retavail Total available swap space * \param retfree Free swap space * \return -1 on error, otherwise number of swap areas (typically 0 for total) */ static int swapmode(int *retavail, int *retfree) { int n; struct kvm_swap swapary[1]; *retavail = 0; *retfree = 0; if (kvmd == NULL) return -1; n = kvm_getswapinfo(kvmd, swapary, 1, 0); if (n < 0 || swapary[0].ksw_total == 0) { /* strange */ } else { *retavail = pagetok(swapary[0].ksw_total); *retfree = pagetok(swapary[0].ksw_total - swapary[0].ksw_used); } return (n); }
int initswap(void) { static int once = 0; if (once) return (1); header = getbsize(&hlen, &blocksize); pagesize = getpagesize(); if ((kvnsw = kvm_getswapinfo(kd, kvmsw, NSWAP, 0)) < 0) { error("systat: kvm_getswapinfo failed"); return (0); } okvnsw = kvnsw; calclens(); odlen = dlen; oulen = ulen; once = 1; return (1); }
/** * Get swap info */ static void storage_OS_get_swap(void) { int nswapdev = 0; size_t len = sizeof(nswapdev); struct storage_entry *entry; char swap_w_prefix[SE_DESC_MLEN]; if (sysctlbyname("vm.nswapdev", &nswapdev, &len, NULL,0 ) < 0) { syslog(LOG_ERR, "hrStorageTable: sysctlbyname(\"vm.nswapdev\") " "failed. %m"); assert(0); return; } if (nswapdev <= 0) { HRDBG("vm.nswapdev is %d", nswapdev); return; } if (nswapdev + 1 != (int)swap_devs_len || swap_devs == NULL) { swap_devs_len = nswapdev + 1; swap_devs = reallocf(swap_devs, swap_devs_len * sizeof(struct kvm_swap)); assert(swap_devs != NULL); if (swap_devs == NULL) { swap_devs_len = 0; return; } } nswapdev = kvm_getswapinfo(hr_kd, swap_devs, swap_devs_len, 0); if (nswapdev < 0) { syslog(LOG_ERR, "hrStorageTable: kvm_getswapinfo failed. %m\n"); assert(0); return; } for (len = 0; len < (size_t)nswapdev; len++) { memset(&swap_w_prefix[0], '\0', sizeof(swap_w_prefix)); snprintf(swap_w_prefix, sizeof(swap_w_prefix) - 1, "Swap:%s%s", _PATH_DEV, swap_devs[len].ksw_devname); entry = storage_find_by_name(swap_w_prefix); if (entry == NULL) entry = storage_entry_create(swap_w_prefix); assert (entry != NULL); if (entry == NULL) return; /* Out of luck */ entry->flags |= HR_STORAGE_FOUND; entry->type = &OIDX_hrStorageVirtualMemory_c; entry->allocationUnits = getpagesize(); entry->size = swap_devs[len].ksw_total; entry->used = swap_devs[len].ksw_used; entry->allocationFailures = 0; } }
u_char * var_hrstore(struct variable *vp, oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method) { int store_idx = 0; #if !defined(linux) #if defined(solaris2) int freemem; int swap_total, swap_used; #elif defined(hpux10) || defined(hpux11) struct pst_dynamic pst_buf; #elif defined(darwin8) vm_statistics_data_t vm_stat; int count = HOST_VM_INFO_COUNT; #elif defined(TOTAL_MEMORY_SYMBOL) || defined(USE_SYSCTL_VM) #ifdef VM_UVMEXP struct uvmexp uvmexp_totals; #endif struct vmtotal memory_totals; #endif #if HAVE_KVM_GETSWAPINFO struct kvm_swap swapinfo; static kvm_t *kd = NULL; #endif #if HAVE_SYS_POOL_H struct pool mbpool, mclpool; int i; #endif #ifdef MBSTAT_SYMBOL struct mbstat mbstat; #endif #endif /* !linux */ static char string[1024]; struct HRFS_statfs stat_buf; if (vp->magic == HRSTORE_MEMSIZE) { if (header_hrstore(vp, name, length, exact, var_len, write_method) == MATCH_FAILED) return NULL; } else { really_try_next: store_idx = header_hrstoreEntry(vp, name, length, exact, var_len, write_method); if (store_idx == MATCH_FAILED) return NULL; if (store_idx > HRS_TYPE_FIXED_MAX) { if (HRFS_statfs(HRFS_entry->HRFS_mount, &stat_buf) < 0) { snmp_log_perror(HRFS_entry->HRFS_mount); goto try_next; } } #if !defined(linux) && !defined(solaris2) else switch (store_idx) { case HRS_TYPE_MEM: case HRS_TYPE_SWAP: #ifdef USE_SYSCTL_VM { int mib[2]; size_t len = sizeof(memory_totals); mib[0] = CTL_VM; mib[1] = VM_METER; sysctl(mib, 2, &memory_totals, &len, NULL, 0); #ifdef VM_UVMEXP mib[1] = VM_UVMEXP; len = sizeof(uvmexp_totals); sysctl(mib, 2, &uvmexp_totals, &len, NULL, 0); #endif } #elif defined(darwin8) host_statistics(myHost,HOST_VM_INFO,&vm_stat,&count); #elif defined(hpux10) || defined(hpux11) pstat_getdynamic(&pst_buf, sizeof(struct pst_dynamic), 1, 0); #elif defined(TOTAL_MEMORY_SYMBOL) auto_nlist(TOTAL_MEMORY_SYMBOL, (char *) &memory_totals, sizeof(struct vmtotal)); #endif #if HAVE_KVM_GETSWAPINFO if (kd == NULL) kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); if (!kd) { snmp_log_perror("kvm_openfiles"); goto try_next; } if (kvm_getswapinfo(kd, &swapinfo, 1, 0) < 0) { snmp_log_perror("kvm_getswapinfo"); goto try_next; } #endif break; #if !defined(hpux10) && !defined(hpux11) case HRS_TYPE_MBUF: #if HAVE_SYS_POOL_H auto_nlist(MBPOOL_SYMBOL, (char *) &mbpool, sizeof(mbpool)); auto_nlist(MCLPOOL_SYMBOL, (char *) &mclpool, sizeof(mclpool)); #endif #ifdef MBSTAT_SYMBOL auto_nlist(MBSTAT_SYMBOL, (char *) &mbstat, sizeof(mbstat)); #endif break; #endif /* !hpux10 && !hpux11 */ default: break; } #endif /* !linux && !solaris2 */ } switch (vp->magic) { case HRSTORE_MEMSIZE: long_return = physmem * (pagesize / 1024); return (u_char *) & long_return; case HRSTORE_INDEX: long_return = store_idx; return (u_char *) & long_return; case HRSTORE_TYPE: if (store_idx > HRS_TYPE_FIXED_MAX) if (storageUseNFS && Check_HR_FileSys_NFS()) storage_type_id[storage_type_len - 1] = 10; /* Network Disk */ else storage_type_id[storage_type_len - 1] = 4; /* Assume fixed */ else switch (store_idx) { case HRS_TYPE_MEM: storage_type_id[storage_type_len - 1] = 2; /* RAM */ break; case HRS_TYPE_SWAP: storage_type_id[storage_type_len - 1] = 3; /* Virtual Mem */ break; case HRS_TYPE_MBUF: storage_type_id[storage_type_len - 1] = 1; /* Other */ break; default: storage_type_id[storage_type_len - 1] = 1; /* Other */ break; } *var_len = sizeof(storage_type_id); return (u_char *) storage_type_id; case HRSTORE_DESCR: if (store_idx > HRS_TYPE_FIXED_MAX) { strncpy(string, HRFS_entry->HRFS_mount, sizeof(string)-1); string[ sizeof(string)-1 ] = 0; *var_len = strlen(string); return (u_char *) string; } else { /* store_idx = store_idx - 1; */ *var_len = strlen(hrs_descr[store_idx]); return (u_char *) hrs_descr[store_idx]; } case HRSTORE_UNITS: if (store_idx > HRS_TYPE_FIXED_MAX) #if HRFS_HAS_FRSIZE long_return = stat_buf.f_frsize; #else long_return = stat_buf.f_bsize; #endif else switch (store_idx) { case HRS_TYPE_MEM: case HRS_TYPE_SWAP: #if defined(USE_SYSCTL) || defined(solaris2) long_return = pagesize; #elif defined(NBPG) long_return = NBPG; #else long_return = 1024; /* Report in Kb */ #endif break; case HRS_TYPE_MBUF: #ifdef MSIZE long_return = MSIZE; #elif defined(linux) long_return = 1024; #else long_return = 256; #endif break; default: #if NO_DUMMY_VALUES goto try_next; #endif long_return = 1024; /* As likely as any! */ break; } return (u_char *) & long_return; case HRSTORE_SIZE: if (store_idx > HRS_TYPE_FIXED_MAX) long_return = stat_buf.f_blocks; else switch (store_idx) { #if defined(linux) case HRS_TYPE_MEM: case HRS_TYPE_SWAP: long_return = linux_mem(store_idx, HRSTORE_SIZE); break; #elif defined(solaris2) case HRS_TYPE_MEM: long_return = physmem; break; case HRS_TYPE_SWAP: sol_get_swapinfo(&swap_total, &swap_used); long_return = swap_total; break; #elif defined(hpux10) || defined(hpux11) case HRS_TYPE_MEM: long_return = pst_buf.psd_rm; break; case HRS_TYPE_SWAP: long_return = pst_buf.psd_vm; break; #elif defined(darwin8) case HRS_TYPE_MEM: long_return = physmem; break; case HRS_TYPE_SWAP: long_return = -1; break; #if defined(MBSTAT_SYMBOL) case HRS_TYPE_MBUF: long_return = mbstat.m_mbufs; break; #endif #elif defined(TOTAL_MEMORY_SYMBOL) || defined(USE_SYSCTL_VM) case HRS_TYPE_MEM: long_return = memory_totals.t_rm; break; case HRS_TYPE_SWAP: #if HAVE_KVM_GETSWAPINFO long_return = swapinfo.ksw_total; #elif defined(VM_UVMEXP) long_return = uvmexp_totals.swpages; #else long_return = memory_totals.t_vm; #endif break; #else /* !linux && !solaris2 && !hpux10 && !hpux11 && ... */ case HRS_TYPE_MEM: long_return = physmem; break; case HRS_TYPE_SWAP: #if NO_DUMMY_VALUES goto try_next; #endif long_return = 0; break; #endif /* !linux && !solaris2 && !hpux10 && !hpux11 && ... */ case HRS_TYPE_MBUF: #ifdef linux long_return = linux_mem(store_idx, HRSTORE_SIZE); #elif HAVE_SYS_POOL_H long_return = 0; for (i = 0; i < sizeof(mbstat.m_mtypes) / sizeof(mbstat.m_mtypes[0]); i++) long_return += mbstat.m_mtypes[i]; #elif defined(MBSTAT_SYMBOL) && defined(STRUCT_MBSTAT_HAS_M_MBUFS) long_return = mbstat.m_mbufs; #elif defined(NO_DUMMY_VALUES) goto try_next; #else long_return = 0; #endif break; default: #if NO_DUMMY_VALUES goto try_next; #endif long_return = 1024; break; } return (u_char *) & long_return; case HRSTORE_USED: if (store_idx > HRS_TYPE_FIXED_MAX) long_return = (stat_buf.f_blocks - stat_buf.f_bfree); else switch (store_idx) { #if defined(linux) case HRS_TYPE_MBUF: case HRS_TYPE_MEM: case HRS_TYPE_SWAP: long_return = linux_mem(store_idx, HRSTORE_USED); break; #elif defined(solaris2) case HRS_TYPE_MEM: getKstatInt("unix", "system_pages", "freemem", &freemem); long_return = physmem - freemem; break; case HRS_TYPE_SWAP: sol_get_swapinfo(&swap_total, &swap_used); long_return = swap_used; break; #elif defined(hpux10) || defined(hpux11) case HRS_TYPE_MEM: long_return = pst_buf.psd_arm; break; case HRS_TYPE_SWAP: long_return = pst_buf.psd_avm; break; #elif defined(darwin8) case HRS_TYPE_MEM: long_return = vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count; break; case HRS_TYPE_SWAP: long_return = -1; break; #if defined(MBSTAT_SYMBOL) case HRS_TYPE_MBUF: long_return = mbstat.m_mbufs; break; #endif #elif defined(TOTAL_MEMORY_SYMBOL) || defined(USE_SYSCTL_VM) case HRS_TYPE_MEM: long_return = memory_totals.t_arm; break; case HRS_TYPE_SWAP: #if HAVE_KVM_GETSWAPINFO long_return = swapinfo.ksw_used; #elif defined(VM_UVMEXP) long_return = uvmexp_totals.swpginuse; #else long_return = memory_totals.t_avm; #endif break; #endif /* linux || solaris2 || hpux10 || hpux11 || ... */ #if !defined(linux) && !defined(solaris2) && !defined(hpux10) && !defined(hpux11) case HRS_TYPE_MBUF: #if HAVE_SYS_POOL_H long_return = (mbpool.pr_nget - mbpool.pr_nput) * mbpool.pr_size + (mclpool.pr_nget - mclpool.pr_nput) * mclpool.pr_size; #ifdef MSIZE long_return /= MSIZE; #else long_return /= 256; #endif #elif defined(MBSTAT_SYMBOL) && defined(STRUCT_MBSTAT_HAS_M_CLUSTERS) long_return = mbstat.m_clusters - mbstat.m_clfree; /* unlikely, but... */ #elif defined(NO_DUMMY_VALUES) goto try_next; #else long_return = 0; #endif break; #endif /* !linux && !solaris2 && !hpux10 && !hpux11 && ... */ default: #if NO_DUMMY_VALUES goto try_next; #endif long_return = 1024; break; } return (u_char *) & long_return; case HRSTORE_FAILS: if (store_idx > HRS_TYPE_FIXED_MAX) #if NO_DUMMY_VALUES goto try_next; #else long_return = 0; #endif else switch (store_idx) {
int sysinfo(struct sysinfo *info){ kvm_t *kvmh; double load_avg[NLOADS]; int page_s = getpagesize(); if (info == NULL) R_ERROR(EFAULT); memset(info, 0, sizeof(struct sysinfo)); info -> mem_unit = UNIT_S; /*kvm init*/ if ((kvmh = kvm_open(NULL, "/dev/null", "/dev/null", O_RDONLY, "kvm_open")) == NULL) R_ERROR(0); /*load averages*/ if (kvm_getloadavg(kvmh, load_avg, NLOADS) == -1) R_ERROR(0); info -> loads[0] = (u_long)((float)load_avg[0] * USHRT_MAX); info -> loads[1] = (u_long)((float)load_avg[1] * USHRT_MAX); info -> loads[2] = (u_long)((float)load_avg[2] * USHRT_MAX); /*swap space*/ struct kvm_swap k_swap; if (kvm_getswapinfo(kvmh, &k_swap, 1, 0) == -1) R_ERROR(0); info -> totalswap = (u_long)PAGE_2_UNIT(k_swap.ksw_total); info -> freeswap = info -> totalswap - (u_long)PAGE_2_UNIT(k_swap.ksw_used); /*processes*/ int n_procs; if (kvm_getprocs(kvmh, KERN_PROC_ALL, 0, &n_procs) == NULL) R_ERROR(0); info -> procs = (u_short)n_procs; /*end of kvm session*/ if (kvm_close(kvmh) == -1) R_ERROR(0); /*uptime*/ struct timespec ts; if (clock_gettime(CLOCK_UPTIME, &ts) == -1) R_ERROR(R_IGNORE); info -> uptime = (long)ts.tv_sec; /*ram*/ int total_pages, free_pages, active_pages, inactive_pages; u_long shmmax; if (GETSYSCTL("vm.stats.vm.v_page_count", total_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_free_count", free_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_active_count", active_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("vm.stats.vm.v_inactive_count", inactive_pages) == -1) R_ERROR(R_IGNORE); if (GETSYSCTL("kern.ipc.shmmax", shmmax) == -1) R_ERROR(R_IGNORE); info -> totalram = (u_long)PAGE_2_UNIT(total_pages); info -> freeram = (u_long)PAGE_2_UNIT(free_pages); info -> bufferram = (u_long)PAGE_2_UNIT(active_pages); info -> sharedram = shmmax / UNIT_S; /*high mem (todo)*/ info -> totalhigh = 0; /*Does this supose to refer to HMA or reserved ram?*/ info -> freehigh = 0; return 0; }
int get_swap_stats(Digikam::KMemoryInfo::KMemoryInfoData* const data) { #ifdef Q_WS_MAC Q_UNUSED(data); #endif #ifdef Q_OS_HPUX struct pst_swapinfo pstat_swapinfo[SWAP_BATCH]; int swapidx = 0; int num, i; #endif // Q_OS_HPUX #ifdef Q_OS_SOLARIS struct anoninfo ai; int pagesize; #endif // Q_OS_SOLARIS #if defined(Q_OS_LINUX) //|| defined(Q_OS_CYGWIN) FILE* f = 0; char* line_ptr = 0; unsigned long long value; #endif // defined(Q_OS_LINUX) #if defined(Q_OS_FREEBSD) int pagesize; #ifdef Q_OS_FREEBSD5 struct xswdev xsw; int mib[16], n; size_t mibsize, size; #else struct kvm_swap swapinfo; kvm_t* kvmd = 0; #endif // Q_OS_FREEBSD5 #endif // defined(Q_OS_FREEBSD) #if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD) struct uvmexp* uvm = 0; #endif // defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD) #ifdef Q_OS_WIN MEMORYSTATUSEX memstats; #endif // Q_OS_WIN #ifdef Q_OS_HPUX data->totalSwap = 0; data->usedSwap = 0; data->freeSwap = 0; while (1) { num = pstat_getswap(pstat_swapinfo, sizeof pstat_swapinfo[0], SWAP_BATCH, swapidx); if (num == -1) { //sg_set_error_with_errno(SG_ERROR_PSTAT,"pstat_getswap"); return 0; } else if (num == 0) { break; } for (i = 0; i < num; i++) { struct pst_swapinfo* si = &pstat_swapinfo[i]; if ((si->pss_flags & SW_ENABLED) != SW_ENABLED) { continue; } if ((si->pss_flags & SW_BLOCK) == SW_BLOCK) { data->totalSwap += ((long long) si->pss_nblksavail) * 1024LL; data->usedSwap += ((long long) si->pss_nfpgs) * 1024LL; data->freeSwap = data->totalSwap - data->usedSwap; } if ((si->pss_flags & SW_FS) == SW_FS) { data->totalSwap += ((long long) si->pss_limit) * 1024LL; data->usedSwap += ((long long) si->pss_allocated) * 1024LL; data->freeSwap = data->totalSwap - data->usedSwap; } } swapidx = pstat_swapinfo[num - 1].pss_idx + 1; } return 1; #endif // Q_OS_HPUX #ifdef Q_OS_SOLARIS if((pagesize=sysconf(_SC_PAGESIZE)) == -1) { //sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); return 0; } if (swapctl(SC_AINFO, &ai) == -1) { //sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL); return 0; } data->totalSwap = (long long)ai.ani_max * (long long)pagesize; data->usedSwap = (long long)ai.ani_resv * (long long)pagesize; data->freeSwap = data->totalSwap - data->usedSwap; return 1; #endif // Q_OS_SOLARIS #if defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN) if ((f = fopen("/proc/meminfo", "r")) == NULL) { //sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo"); return 0; } while ((line_ptr = sg_f_read_line(f, "")) != NULL) { if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { continue; } value *= 1024; if (strncmp(line_ptr, "SwapTotal:", 10) == 0) { data->totalSwap = value; } else if (strncmp(line_ptr, "SwapFree:", 9) == 0) { data->freeSwap = value; } } fclose(f); data->usedSwap = data->totalSwap - data->freeSwap; return 1; #endif // defined(Q_OS_LINUX) || defined(Q_OS_CYGWIN) #if defined(Q_OS_FREEBSD) || defined(Q_OS_DFBSD) pagesize = getpagesize(); #ifdef Q_OS_FREEBSD5 data->totalSwap = 0; data->usedSwap = 0; mibsize = sizeof mib / sizeof mib[0]; if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) { //sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB, "vm.swap_info"); return 0; } for (n = 0; ; ++n) { mib[mibsize] = n; size = sizeof xsw; if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) < 0) { break; } if (xsw.xsw_version != XSWDEV_VERSION) { //sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL); return 0; } data->totalSwap += (long long) xsw.xsw_nblks; data->usedSwap += (long long) xsw.xsw_used; } if (errno != ENOENT) { //sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info"); return 0; } #else // Q_OS_FREEBSD5 if((kvmd = sg_get_kvm()) == NULL) { return 0; } if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1) { //sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL); return 0; } data->totalSwap = (long long)swapinfo.ksw_total; data->usedSwap = (long long)swapinfo.ksw_used; #endif // Q_OS_FREEBSD5 data->totalSwap *= pagesize; data->usedSwap *= pagesize; data->freeSwap = data->totalSwap - data->usedSwap; return 1; #endif // defined(Q_OS_FREEBSD) || defined(Q_OS_DFBSD) #if defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD) if ((uvm = sg_get_uvmexp()) == NULL) { return 0; } data->totalSwap = (long long)uvm->pagesize * (long long)uvm->swpages; data->usedSwap = (long long)uvm->pagesize * (long long)uvm->swpginuse; data->freeSwap = data->totalSwap - data->usedSwap; return 1; #endif // defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD) #ifdef Q_OS_WIN memstats.dwLength = sizeof(memstats); if (!GlobalMemoryStatusEx(&memstats)) { //sg_set_error_with_errno(SG_ERROR_MEMSTATUS, "GloblaMemoryStatusEx"); return 0; } /* the PageFile stats include Phys memory "minus an overhead". * Due to this unknown "overhead" there's no way to extract just page * file use from these numbers */ data->totalSwap = memstats.ullTotalPageFile; data->freeSwap = memstats.ullAvailPageFile; data->usedSwap = data->totalSwap - data->freeSwap; return 1; #endif return -1; }
static int swap_read (void) { #if KERNEL_LINUX FILE *fh; char buffer[1024]; char *fields[8]; int numfields; unsigned long long swap_used = 0LL; unsigned long long swap_cached = 0LL; unsigned long long swap_free = 0LL; unsigned long long swap_total = 0LL; if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { char errbuf[1024]; WARNING ("memory: fopen: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, 1024, fh) != NULL) { unsigned long long *val = NULL; if (strncasecmp (buffer, "SwapTotal:", 10) == 0) val = &swap_total; else if (strncasecmp (buffer, "SwapFree:", 9) == 0) val = &swap_free; else if (strncasecmp (buffer, "SwapCached:", 11) == 0) val = &swap_cached; else continue; numfields = strsplit (buffer, fields, 8); if (numfields < 2) continue; *val = atoll (fields[1]) * 1024LL; } if (fclose (fh)) { char errbuf[1024]; WARNING ("memory: fclose: %s", sstrerror (errno, errbuf, sizeof (errbuf))); } if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total)) return (-1); swap_used = swap_total - (swap_free + swap_cached); swap_submit ("used", swap_used); swap_submit ("free", swap_free); swap_submit ("cached", swap_cached); /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT unsigned long long swap_alloc; unsigned long long swap_resv; unsigned long long swap_avail; struct anoninfo ai; if (swapctl (SC_AINFO, &ai) == -1) { char errbuf[1024]; ERROR ("swap plugin: swapctl failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } /* * Calculations from: * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c * Also see: * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?) * /usr/include/vm/anon.h * * In short, swap -s shows: allocated + reserved = used, available * * However, Solaris does not allow to allocated/reserved more than the * available swap (physical memory + disk swap), so the pedant may * prefer: allocated + unallocated = reserved, available * * We map the above to: used + resv = n/a, free * * Does your brain hurt yet? - Christophe Kalt * * Oh, and in case you wonder, * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free ); * can suffer from a 32bit overflow. */ swap_alloc = ai.ani_max - ai.ani_free; swap_alloc *= pagesize; swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max; swap_resv *= pagesize; swap_avail = ai.ani_max - ai.ani_resv; swap_avail *= pagesize; swap_submit ("used", swap_alloc); swap_submit ("free", swap_avail); swap_submit ("reserved", swap_resv); /* #endif HAVE_LIBKSTAT */ #elif defined(VM_SWAPUSAGE) int mib[3]; size_t mib_len; struct xsw_usage sw_usage; size_t sw_usage_len; mib_len = 2; mib[0] = CTL_VM; mib[1] = VM_SWAPUSAGE; sw_usage_len = sizeof (struct xsw_usage); if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0) return (-1); /* The returned values are bytes. */ swap_submit ("used", sw_usage.xsu_used); swap_submit ("free", sw_usage.xsu_avail); /* #endif VM_SWAPUSAGE */ #elif HAVE_LIBKVM_GETSWAPINFO struct kvm_swap data_s; int status; unsigned long long used; unsigned long long free; unsigned long long total; if (kvm_obj == NULL) return (-1); /* only one structure => only get the grand total, no details */ status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0); if (status == -1) return (-1); total = (unsigned long long) data_s.ksw_total; used = (unsigned long long) data_s.ksw_used; total *= (unsigned long long) kvm_pagesize; used *= (unsigned long long) kvm_pagesize; free = total - used; swap_submit ("used", used); swap_submit ("free", free); /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB sg_swap_stats *swap; swap = sg_get_swap_stats (); if (swap == NULL) return (-1); swap_submit ("used", swap->used); swap_submit ("free", swap->free); #endif /* HAVE_LIBSTATGRAB */ return (0); } /* int swap_read */
static sg_error sg_get_swap_stats_int(sg_swap_stats *swap_stats_buf) { #ifdef HPUX #define SWAP_BATCH 5 struct pst_swapinfo pstat_swapinfo[SWAP_BATCH]; int swapidx = 0; int num, i; #elif defined(SOLARIS) # if defined(HAVE_STRUCT_SWAPTABLE) struct swaptable *swtbl; int nswap, i; # elif defined(HAVE_STRUCT_ANONINFO) struct anoninfo ai; # endif #elif defined(LINUX) || defined(CYGWIN) FILE *f; #define LINE_BUF_SIZE 256 char line_buf[LINE_BUF_SIZE]; unsigned long long value; unsigned matches = 0; #elif defined(HAVE_STRUCT_XSWDEV) struct xswdev xsw; struct xswdev *xswbuf = NULL, *xswptr = NULL; int n; int mib[16]; size_t mibsize, size; #elif defined(HAVE_STRUCT_XSW_USAGE) int mib[2] = {CTL_VM, VM_SWAPUSAGE}; struct xsw_usage xsw; size_t mibsize = 2, size = sizeof(xsw); #elif defined(HAVE_STRUCT_UVMEXP_SYSCTL) && defined(VM_UVMEXP2) int mib[2] = { CTL_VM, VM_UVMEXP2 }; struct uvmexp_sysctl uvm; size_t size = sizeof(uvm); #elif defined(HAVE_STRUCT_UVMEXP) && defined(VM_UVMEXP) int mib[2] = { CTL_VM, VM_UVMEXP }; struct uvmexp uvm; size_t size = sizeof(uvm); #elif defined(ALLBSD) /* fallback if no reasonable API is supported */ struct kvm_swap swapinfo; kvm_t *kvmd; #elif defined(AIX) perfstat_memory_total_t mem; #elif defined(WIN32) MEMORYSTATUSEX memstats; #endif swap_stats_buf->total = 0; swap_stats_buf->used = 0; swap_stats_buf->free = 0; #ifdef HPUX for(;;) { num = pstat_getswap(pstat_swapinfo, sizeof pstat_swapinfo[0], SWAP_BATCH, swapidx); if (num == -1) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_PSTAT, "pstat_getswap"); } else if (num == 0) { break; } for (i = 0; i < num; ++i) { struct pst_swapinfo *si = &pstat_swapinfo[i]; if ((si->pss_flags & SW_ENABLED) != SW_ENABLED) continue; if ((si->pss_flags & SW_BLOCK) == SW_BLOCK) { swap_stats_buf->total += ((long long) si->pss_nblksavail) * 1024LL; swap_stats_buf->used += ((long long) si->pss_nfpgs) * 1024LL; swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; } if ((si->pss_flags & SW_FS) == SW_FS) { swap_stats_buf->total += ((long long) si->pss_limit) * 1024LL; swap_stats_buf->used += ((long long) si->pss_allocated) * 1024LL; swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; } } swapidx = pstat_swapinfo[num - 1].pss_idx + 1; } #elif defined(AIX) /* return code is number of structures returned */ if(perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1) != 1) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTLBYNAME, "perfstat_memory_total"); } swap_stats_buf->total = ((long long)mem.pgsp_total) * sys_page_size; swap_stats_buf->free = ((long long)mem.pgsp_free) * sys_page_size; swap_stats_buf->used = swap_stats_buf->total - swap_stats_buf->free; #elif defined(SOLARIS) # if defined(HAVE_STRUCT_SWAPTABLE) again: if( ( nswap = swapctl(SC_GETNSWP, 0) ) == -1 ) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SWAPCTL, NULL); } if( nswap != 0 ) { char *buf = sg_malloc( nswap * sizeof(char) * (PATH_MAX+1) + 1 ); if( NULL == buf ) { RETURN_FROM_PREVIOUS_ERROR( "swap", sg_get_error() ); } swtbl = sg_malloc( sizeof(*swtbl) + ( nswap * sizeof(swtbl->swt_ent[0]) ) ); if( NULL == swtbl ) { free(buf); RETURN_FROM_PREVIOUS_ERROR( "swap", sg_get_error() ); } memset( buf, 0, nswap * sizeof(char) * (PATH_MAX+1) + 1 ); memset( swtbl, 0, sizeof(*swtbl) + ( nswap * sizeof(swtbl->swt_ent[0]) ) ); for( i = 0; i < nswap; ++i ) swtbl->swt_ent[i].ste_path = buf + (i * (PATH_MAX+1)); swtbl->swt_n = nswap; if ((i = swapctl(SC_LIST, swtbl)) < 0) { free( buf ); free( swtbl ); RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SWAPCTL, NULL); } if( i > nswap ) { free( swtbl ); free( buf ); goto again; } for( i = 0; i < nswap; ++i ) { swap_stats_buf->total = swtbl->swt_ent[i].ste_pages; swap_stats_buf->free = swtbl->swt_ent[i].ste_free; } free( swtbl ); free( buf ); swap_stats_buf->total *= sys_page_size; swap_stats_buf->free *= sys_page_size; swap_stats_buf->used = swap_stats_buf->total - swap_stats_buf->free; } # elif defined(HAVE_STRUCT_ANONINFO) if (swapctl(SC_AINFO, &ai) == -1) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SWAPCTL, NULL); } swap_stats_buf->total = ai.ani_max; swap_stats_buf->total *= sys_page_size; swap_stats_buf->used = ai.ani_resv; swap_stats_buf->used *= sys_page_size; swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; # else RETURN_WITH_SET_ERROR("swap", SG_ERROR_UNSUPPORTED, OS_TYPE); # endif #elif defined(LINUX) || defined(CYGWIN) if ((f = fopen("/proc/meminfo", "r")) == NULL) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_OPEN, "/proc/meminfo"); } while( (matches < 2) && (fgets(line_buf, sizeof(line_buf), f) != NULL) ) { if (sscanf(line_buf, "%*s %llu kB", &value) != 1) continue; value *= 1024; if (strncmp(line_buf, "SwapTotal:", 10) == 0) { swap_stats_buf->total = value; ++matches; } else if (strncmp(line_buf, "SwapFree:", 9) == 0) { swap_stats_buf->free = value; ++matches; } } fclose(f); if( matches < 2 ) { RETURN_WITH_SET_ERROR( "swap", SG_ERROR_PARSE, "/proc/meminfo" ); } swap_stats_buf->used = swap_stats_buf->total - swap_stats_buf->free; #elif defined(HAVE_STRUCT_XSWDEV) mibsize = 2; if( swapinfo_array ) { size = 0; if( sysctl( swapinfo_mib, 2, NULL, &size, NULL, 0 ) < 0 ) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, swapinfo_sysctl_name); } if( NULL == ( xswbuf = sg_malloc( size ) ) ) { RETURN_FROM_PREVIOUS_ERROR( "swap", sg_get_error() ); } if( sysctl( swapinfo_mib, 2, xswbuf, &size, NULL, 0 ) < 0 ) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, swapinfo_sysctl_name); } } else { mib[0] = swapinfo_mib[0]; mib[1] = swapinfo_mib[1]; } for (n = 0; ; ++n) { if( !swapinfo_array ) { mib[mibsize] = n; size = sizeof(xsw); if (sysctl(mib, (unsigned)(mibsize + 1), &xsw, &size, NULL, 0) < 0) { if (errno == ENOENT) break; free( xswbuf ); RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, swapinfo_sysctl_name); } xswptr = &xsw; } # if defined(HAVE_STRUCT_XSWDEV_SIZE) else { if( ((size_t)n) >= (size / xswbuf->xsw_size) ) break; xswptr = xswbuf + n; } if( xswptr == NULL ) { RETURN_WITH_SET_ERROR("swap", SG_ERROR_MEMSTATUS, "no swap status"); } # ifdef XSWDEV_VERSION if( xswptr->xsw_version != XSWDEV_VERSION ) { free( xswbuf ); RETURN_WITH_SET_ERROR("swap", SG_ERROR_XSW_VER_MISMATCH, NULL); } # endif # endif swap_stats_buf->total += (unsigned long long) xswptr->xsw_nblks; swap_stats_buf->used += (unsigned long long) xswptr->xsw_used; } free( xswbuf ); swap_stats_buf->total *= (size_t)sys_page_size; swap_stats_buf->used *= (size_t)sys_page_size; if( 0 == swap_stats_buf->free ) swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; else swap_stats_buf->free *= (size_t)sys_page_size; #elif defined(HAVE_STRUCT_XSW_USAGE) if (sysctl(mib, (unsigned)mibsize, &xsw, &size, NULL, 0) < 0) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, "CTL_VM.VM_SWAPUSAGE" ); } swap_stats_buf->total = (unsigned long long) xsw.xsu_total; swap_stats_buf->used = (unsigned long long) xsw.xsu_used; swap_stats_buf->free = (unsigned long long) xsw.xsu_avail; #elif defined(HAVE_STRUCT_UVMEXP_SYSCTL) && defined(VM_UVMEXP2) if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP2"); } swap_stats_buf->total = uvm.pagesize * uvm.swpages; swap_stats_buf->used = uvm.pagesize * uvm.swpginuse; /* XXX swpgonly ? */ swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; #elif defined(HAVE_STRUCT_UVMEXP) && defined(VM_UVMEXP) if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP"); } swap_stats_buf->total = (unsigned long long)uvm.pagesize * (unsigned long long)uvm.swpages; swap_stats_buf->used = (unsigned long long)uvm.pagesize * (unsigned long long)uvm.swpginuse; /* XXX swpgonly ? */ swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; #elif defined(ALLBSD) /* XXX probably not mt-safe! */ kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); if(kvmd == NULL) { RETURN_WITH_SET_ERROR("swap", SG_ERROR_KVM_OPENFILES, NULL); } if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1) { kvm_close( kvmd ); RETURN_WITH_SET_ERROR("swap", SG_ERROR_KVM_GETSWAPINFO, NULL); } swap_stats_buf->total = (long long)swapinfo.ksw_total; swap_stats_buf->used = (long long)swapinfo.ksw_used; kvm_close( kvmd ); swap_stats_buf->total *= sys_page_size; swap_stats_buf->used *= sys_page_size; swap_stats_buf->free = swap_stats_buf->total - swap_stats_buf->used; #elif defined(WIN32) memstats.dwLength = sizeof(memstats); if (!GlobalMemoryStatusEx(&memstats)) { RETURN_WITH_SET_ERROR_WITH_ERRNO("swap", SG_ERROR_MEMSTATUS, "GloblaMemoryStatusEx"); } /* the PageFile stats include Phys memory "minus an overhead". * Due to this unknown "overhead" there's no way to extract just page * file use from these numbers */ swap_stats_buf->total = memstats.ullTotalPageFile; swap_stats_buf->free = memstats.ullAvailPageFile; swap_stats_buf->used = swap_stats_buf->total - swap_stats_buf->free; #else RETURN_WITH_SET_ERROR("swap", SG_ERROR_UNSUPPORTED, OS_TYPE); #endif swap_stats_buf->systime = time(NULL); return SG_ERROR_NONE; }
static int swap_read (void) { #if KERNEL_LINUX FILE *fh; char buffer[1024]; char *fields[8]; int numfields; _Bool old_kernel=0; derive_t swap_used = 0; derive_t swap_cached = 0; derive_t swap_free = 0; derive_t swap_total = 0; derive_t swap_in = 0; derive_t swap_out = 0; if ((fh = fopen ("/proc/meminfo", "r")) == NULL) { char errbuf[1024]; WARNING ("memory: fopen: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (numfields < 2) continue; if (strcasecmp (fields[0], "SwapTotal:") == 0) strtoderive (fields[1], &swap_total); else if (strcasecmp (fields[0], "SwapFree:") == 0) strtoderive (fields[1], &swap_free); else if (strcasecmp (fields[0], "SwapCached:") == 0) strtoderive (fields[1], &swap_cached); } if (fclose (fh)) { char errbuf[1024]; WARNING ("memory: fclose: %s", sstrerror (errno, errbuf, sizeof (errbuf))); } if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total)) return (-1); swap_used = swap_total - (swap_free + swap_cached); if ((fh = fopen ("/proc/vmstat", "r")) == NULL) { // /proc/vmstat does not exist in kernels <2.6 if ((fh = fopen ("/proc/stat", "r")) == NULL ) { char errbuf[1024]; WARNING ("swap: fopen: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } else old_kernel = 1; } while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (!old_kernel) { if (numfields != 2) continue; if (strcasecmp ("pswpin", fields[0]) != 0) strtoderive (fields[1], &swap_in); else if (strcasecmp ("pswpout", fields[0]) == 0) strtoderive (fields[1], &swap_out); } else /* if (old_kernel) */ { if (numfields != 3) continue; if (strcasecmp ("page", fields[0]) == 0) { strtoderive (fields[1], &swap_in); strtoderive (fields[2], &swap_out); } } } /* while (fgets) */ if (fclose (fh)) { char errbuf[1024]; WARNING ("swap: fclose: %s", sstrerror (errno, errbuf, sizeof (errbuf))); } swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE); swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE); swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE); swap_submit ("in", swap_in, DS_TYPE_DERIVE); swap_submit ("out", swap_out, DS_TYPE_DERIVE); /* #endif KERNEL_LINUX */ #elif HAVE_LIBKSTAT derive_t swap_alloc; derive_t swap_resv; derive_t swap_avail; struct anoninfo ai; if (swapctl (SC_AINFO, &ai) == -1) { char errbuf[1024]; ERROR ("swap plugin: swapctl failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } /* * Calculations from: * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c * Also see: * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?) * /usr/include/vm/anon.h * * In short, swap -s shows: allocated + reserved = used, available * * However, Solaris does not allow to allocated/reserved more than the * available swap (physical memory + disk swap), so the pedant may * prefer: allocated + unallocated = reserved, available * * We map the above to: used + resv = n/a, free * * Does your brain hurt yet? - Christophe Kalt * * Oh, and in case you wonder, * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free ); * can suffer from a 32bit overflow. */ swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize); swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize); swap_submit ("used", swap_alloc, DS_TYPE_GAUGE); swap_submit ("free", swap_avail, DS_TYPE_GAUGE); swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE); /* #endif HAVE_LIBKSTAT */ #elif HAVE_SWAPCTL struct swapent *swap_entries; int swap_num; int status; int i; derive_t used = 0; derive_t total = 0; /* * XXX: This is the syntax for the *BSD `swapctl', which has the * following prototype: * swapctl (int cmd, void *arg, int misc); * * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl', * too, but with the following prototype: * swapctl (int cmd, void *arg); * * Solaris is usually handled in the KSTAT case above. For other UNIXes * a separate case for the other version of `swapctl' may be necessary. */ swap_num = swapctl (SWAP_NSWAP, NULL, 0); if (swap_num < 0) { ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.", swap_num); return (-1); } else if (swap_num == 0) return (0); swap_entries = calloc (swap_num, sizeof (*swap_entries)); if (swap_entries == NULL) { ERROR ("swap plugin: calloc failed."); return (-1); } status = swapctl (SWAP_STATS, swap_entries, swap_num); if (status != swap_num) { ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.", status); sfree (swap_entries); return (-1); } #if defined(DEV_BSIZE) && (DEV_BSIZE > 0) # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE) #else # define C_SWAP_BLOCK_SIZE ((derive_t) 512) #endif for (i = 0; i < swap_num; i++) { if ((swap_entries[i].se_flags & SWF_ENABLE) == 0) continue; used += ((derive_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE; total += ((derive_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE; } if (total < used) { ERROR ("swap plugin: Total swap space (%"PRIu64") " "is less than used swap space (%"PRIu64").", total, used); return (-1); } swap_submit ("used", used, DS_TYPE_GAUGE); swap_submit ("free", total - used, DS_TYPE_GAUGE); sfree (swap_entries); /* #endif HAVE_SWAPCTL */ #elif defined(VM_SWAPUSAGE) int mib[3]; size_t mib_len; struct xsw_usage sw_usage; size_t sw_usage_len; mib_len = 2; mib[0] = CTL_VM; mib[1] = VM_SWAPUSAGE; sw_usage_len = sizeof (struct xsw_usage); if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0) return (-1); /* The returned values are bytes. */ swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE); swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE); /* #endif VM_SWAPUSAGE */ #elif HAVE_LIBKVM_GETSWAPINFO struct kvm_swap data_s; int status; derive_t used; derive_t free; derive_t total; if (kvm_obj == NULL) return (-1); /* only one structure => only get the grand total, no details */ status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0); if (status == -1) return (-1); total = (derive_t) data_s.ksw_total; used = (derive_t) data_s.ksw_used; total *= (derive_t) kvm_pagesize; used *= (derive_t) kvm_pagesize; free = total - used; swap_submit ("used", used, DS_TYPE_GAUGE); swap_submit ("free", free, DS_TYPE_GAUGE); /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB sg_swap_stats *swap; swap = sg_get_swap_stats (); if (swap == NULL) return (-1); swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE); swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE); /* #endif HAVE_LIBSTATGRAB */ #elif HAVE_PERFSTAT if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0) { char errbuf[1024]; WARNING ("memory plugin: perfstat_memory_total failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE); swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE); #endif /* HAVE_PERFSTAT */ return (0); } /* int swap_read */