/* * If there are multiple swap devices, record * the statistics for each one individually. */ void swapinfo(long pagesize) { int i, n; struct swapent *s; netsnmp_memory_info *mem; char buf[1024]; /* * If there's only one swap device, don't bother */ n = swapctl( SWAP_NSWAP, NULL, 0 ); if ( n <= 1 ) return; s = (struct swapent*)calloc(n, sizeof(struct swapent)); swapctl( SWAP_STATS, s, n ); 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 #%d", s[i].se_dev); */ sprintf(buf, "swap %s", s[i].se_path); mem->descr = strdup( buf ); } mem->units = pagesize; mem->size = s[i].se_nblks; mem->free = s[i].se_nblks - s[i].se_inuse; mem->other = -1; } }
static void get_swapinfo(double *total, double *fr) { register int cnt, i, page_size; /* Support for >2Gb */ /* register int t, f;*/ double t, f; struct swaptable *swt; struct swapent *ste; static char path[256]; /* get total number of swap entries */ cnt = swapctl(SC_GETNSWP, 0); /* allocate enough space to hold count + n swapents */ swt = (struct swaptable *)malloc(sizeof(int) + cnt * sizeof(struct swapent)); if (swt == NULL) { *total = 0; *fr = 0; return; } swt->swt_n = cnt; /* fill in ste_path pointers: we don't care about the paths, so we point them all to the same buffer */ ste = &(swt->swt_ent[0]); i = cnt; while (--i >= 0) { ste++->ste_path = path; } /* grab all swap info */ swapctl(SC_LIST, swt); /* walk through the structs and sum up the fields */ t = f = 0; ste = &(swt->swt_ent[0]); i = cnt; while (--i >= 0) { /* don't count slots being deleted */ if (!(ste->ste_flags & ST_INDEL) && !(ste->ste_flags & ST_DOINGDEL)) { t += ste->ste_pages; f += ste->ste_free; } ste++; } page_size=getpagesize(); /* fill in the results */ *total = page_size*t; *fr = page_size*f; free(swt); }
/* * get swap info */ static int get_swapinfo( struct swaptable **swtpp, int *nswap, md_error_t *ep ) { int i; size_t swtsize; *swtpp = NULL; /* get number of entries */ if ((*nswap = swapctl(SC_GETNSWP, NULL)) < 0) { return (mdsyserror(ep, errno, "swapctl(SC_GETNSWP)")); } /* allocate structure */ swtsize = sizeof ((*swtpp)->swt_n) + ((*nswap) * sizeof ((*swtpp)->swt_ent[0])); *swtpp = (struct swaptable *)Zalloc(swtsize); (*swtpp)->swt_n = *nswap; for (i = 0; (i < (*nswap)); ++i) (*swtpp)->swt_ent[i].ste_path = Zalloc(MAXPATHLEN); /* get info */ if (((*nswap) = swapctl(SC_LIST, (*swtpp))) < 0) { (void) mdsyserror(ep, errno, "swapctl(SC_LIST)"); free_swapinfo(*swtpp); return (-1); } /* return success */ return (0); }
/* * returns -1 if malloc fails. */ static long getFreeSwap(void) { long free_mem = -1; size_t num; int i, n; swaptbl_t *s; char *strtab; num = swapctl(SC_GETNSWP, 0); s = malloc(num * sizeof(swapent_t) + sizeof(struct swaptable)); if (s) { strtab = (char *) malloc((num + 1) * MAXSTRSIZE); if (strtab) { free_mem = 0; for (i = 0; i < (num + 1); i++) { s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE); } s->swt_n = num + 1; n = swapctl(SC_LIST, s); for (i = 0; i < n; i++) free_mem += s->swt_ent[i].ste_free; free(strtab); } free(s); } return (free_mem); }
void system_memory(void) { #define pagetob(size) ((size) << (uvmexp.pageshift)) struct uvmexp uvmexp; int nswap, rnswap, i; int mib[] = { CTL_VM, VM_UVMEXP }; size_t size = sizeof (uvmexp); if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) return 0; bm.mem_used = pagetob(uvmexp.active); bm.mem_max = pagetob(uvmexp.npages); bm.swap_used = 0; bm.swap_max = 0; if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) != 0) { struct swapent *swdev = malloc(nswap * sizeof(*swdev)); if((rnswap = swapctl(SWAP_STATS, swdev, nswap)) != nswap) { for (i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { bm.swap_used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); bm.swap_max += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); } } } free(swdev); } }
/* * swapmode is rewritten by Tobias Weingartner <*****@*****.**> * to be based on the new swapctl(2) system call. */ static long swapmode(long *used, long *total) { struct swapent *swdev; int nswap, rnswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); if (nswap == 0) return 0; swdev = calloc(nswap, sizeof(*swdev)); if (swdev == NULL) return 0; rnswap = swapctl(SWAP_STATS, swdev, nswap); if (rnswap == -1) { free(swdev); return 0; } /* if rnswap != nswap, then what? */ /* Total things up */ *total = *used = 0; for (i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE)); *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE)); } } free(swdev); return 1; }
static int swapmode(int *retavail, int *retfree) { int n; struct swapent *sep; *retavail = 0; *retfree = 0; n = swapctl(SWAP_NSWAP, 0, 0); if (n < 1) { warn("could not get swap information"); return 0; } sep = (struct swapent *) malloc(n * (sizeof(*sep))); if (sep == NULL) { warn("memory allocation failed"); return 0; } if (swapctl(SWAP_STATS, (void *) sep, n) < n) { warn("could not get swap stats"); return 0; } for (; n > 0; n--) { *retavail += (int) dbtob(sep[n - 1].se_nblks); *retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse); } *retavail = (int) (*retavail / 1024); *retfree = (int) (*retfree / 1024); return 1; }
static int getstats(int *total, int *used) { struct swapent *sep, *fsep; int rnswap, nswap, i; if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) < 1) { warn("swaptctl 'SWAP_NSWAP':"); return 1; } if (!(fsep = sep = calloc(nswap, sizeof(*sep)))) { warn("calloc 'nswap':"); return 1; } if ((rnswap = swapctl(SWAP_STATS, (void *)sep, nswap)) < 0) { warn("swapctl 'SWAP_STATA':"); return 1; } if (nswap != rnswap) { warn("getstats: SWAP_STATS != SWAP_NSWAP"); return 1; } *total = 0; *used = 0; for (i = 0; i < rnswap; i++) { *total += sep->se_nblks >> 1; *used += sep->se_inuse >> 1; } free(fsep); return 0; }
void KMemoryWidget::update() { int pagesize = getpagesize(); struct rminfo rmi; if( sysmp(MP_SAGET, MPSA_RMINFO, &rmi, sizeof(rmi)) == -1 ) return; Memory_Info[TOTAL_MEM] = MEMORY(rmi.physmem) * pagesize; // total physical memory (without swaps) Memory_Info[FREE_MEM] = MEMORY(rmi.freemem) * pagesize; // total free physical memory (without swaps) Memory_Info[BUFFER_MEM] = MEMORY(rmi.bufmem) * pagesize; //FIXME: Memory_Info[CACHED_MEM]" Memory_Info[CACHED_MEM] = NO_MEMORY_INFO; // cached memory in ram long val; swapctl(SC_GETSWAPTOT, &val); Memory_Info[SWAP_MEM] = MEMORY(val) * UBSIZE; // total size of all swap-partitions swapctl(SC_GETFREESWAP, &val); Memory_Info[FREESWAP_MEM] = MEMORY(val) * UBSIZE; // free memory in swap-partitions #ifndef MPKA_SHMINFO /* Irix 6.5 (also 6.4?) */ Memory_Info[SHARED_MEM] = NO_MEMORY_INFO; #else FILE *kmem = fopen("/dev/kmem", "r"); if( kmem == 0 ) { Memory_Info[SHARED_MEM] = NO_MEMORY_INFO; return; } long shmip = sysmp(MP_KERNADDR, MPKA_SHMINFO); fseek( kmem, shmip, 0 ); struct shminfo shmi; fread( &shmi, sizeof(shmi), 1, kmem ); long shmem = sysmp(MP_KERNADDR, MPKA_SHM); val = 0; long pos; struct shmid_ds shmid; for( int i=0 ; i<shmi.shmmni ; i++ ) { fseek( kmem, shmem, 0 ); shmem += sizeof(shmem); fread( &pos, sizeof(shmem), 1, kmem ); if(pos != 0) { fseek( kmem, pos, 0 ); fread( &shmid, sizeof(shmid), 1, kmem ); val += shmid.shm_segsz; } } Memory_Info[SHARED_MEM] = MEMORY(val); fclose(kmem); #endif }
PyObject * psutil_swap_mem(PyObject *self, PyObject *args) { uint64_t swap_total, swap_free; struct swapent *swdev; int nswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); if (nswap == 0) { // This means there's no swap partition. return Py_BuildValue("(iiiii)", 0, 0, 0, 0, 0); } swdev = calloc(nswap, sizeof(*swdev)); if (swdev == NULL) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (swapctl(SWAP_STATS, swdev, nswap) == -1) { PyErr_SetFromErrno(PyExc_OSError); goto error; } // Total things up. swap_total = swap_free = 0; for (i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { swap_total += swdev[i].se_nblks * DEV_BSIZE; swap_free += (swdev[i].se_nblks - swdev[i].se_inuse) * DEV_BSIZE; } } free(swdev); // Get swap in/out unsigned int total; size_t size = sizeof(total); struct uvmexp_sysctl uv; int mib[] = {CTL_VM, VM_UVMEXP2}; long pagesize = getpagesize(); size = sizeof(uv); if (sysctl(mib, 2, &uv, &size, NULL, 0) < 0) { PyErr_SetFromErrno(PyExc_OSError); goto error; } return Py_BuildValue("(LLLll)", swap_total, (swap_total - swap_free), swap_free, (long) uv.pgswapin * pagesize, // swap in (long) uv.pgswapout * pagesize); // swap out error: free(swdev); return NULL; }
void get_system_info(struct system_info * si) { int i; int avenrun[3]; struct rminfo realmem; struct sysinfo sysinfo; static time_t cp_old[CPU_STATES]; static time_t cp_diff[CPU_STATES]; /* for cpu state percentages */ off_t fswap; /* current free swap in blocks */ off_t tswap; /* total swap in blocks */ (void) getkval(avenrun_offset, (int *) avenrun, sizeof(avenrun), "avenrun"); for (i = 0; i < 3; i++) { si->load_avg[i] = loaddouble(avenrun[i]); si->load_avg[i] /= 1024.0; } if ((numcpus = sysmp(MP_NPROCS)) == -1) { perror("sysmp(MP_NPROCS)"); return; } if (sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof(realmem)) == -1) { perror("sysmp(MP_SAGET,MPSA_RMINFO, ...)"); return; } swapctl(SC_GETFREESWAP, &fswap); swapctl(SC_GETSWAPTOT, &tswap); memory_stats[0] = pagetok(realmem.physmem); memory_stats[1] = pagetok(realmem.availrmem); memory_stats[2] = pagetok(realmem.freemem); memory_stats[3] = tswap / 2; memory_stats[4] = fswap / 2; if (sysmp(MP_SAGET, MPSA_SINFO, &sysinfo, sizeof(struct sysinfo)) == -1) { perror("sysmp(MP_SAGET,MPSA_SINFO)"); return; } (void) percentages(CPU_STATES, cpu_states, sysinfo.cpu, cp_old, cp_diff); si->cpustates = cpu_states; si->memory = memory_stats; si->last_pid = -1; return; }
gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64 *memory_cache, guint64 *memory_buffers, guint64 *swap_total, guint64 *swap_free) { kstat_t *ksp; kstat_named_t *knp; gint n; if (!kc) init_stats(); if (!(ksp = kstat_lookup (kc, "unix", 0, "system_pages"))) return FALSE; kstat_read (kc, ksp, NULL); knp = kstat_data_lookup (ksp, "physmem"); *memory_total = getpagesize () * knp->value.ui64; knp = kstat_data_lookup (ksp, "freemem"); *memory_free = getpagesize () * knp->value.ui64; *memory_cache = 0; *memory_buffers = 0; *swap_total = *swap_free = 0; if ((n = swapctl (SC_GETNSWP, NULL)) > 0) { struct swaptable *st; struct swapent *swapent; gchar path[MAXPATHLEN]; gint i; if ((st = malloc (sizeof (int) + n * sizeof (swapent_t))) == NULL) return FALSE; st->swt_n = n; swapent = st->swt_ent; for (i = 0; i < n; i++, swapent++) swapent->ste_path = path; if ((swapctl (SC_LIST, st)) == -1) { free (st); return FALSE; } swapent = st->swt_ent; for (i = 0; i < n; i++, swapent++) { *swap_total += swapent->ste_pages * getpagesize (); *swap_free += swapent->ste_free * getpagesize (); } free (st); } return TRUE; }
/* * Get the full list of swap entries. Returns -1 on error, or >= 0 to * indicate the number of entries in the list. Callers are responsible * for calling dm_free_swapentries() to deallocate memory. If this * returns 0, the swaptbl_t still needs to be freed. */ int dm_get_swapentries(swaptbl_t **stp, int *errp) { int count, i; swaptbl_t *tbl; char *ptr; *stp = NULL; /* get number of swap entries */ if ((count = swapctl(SC_GETNSWP, NULL)) < 0) { *errp = errno; return (-1); } if (count == 0) { return (0); } /* allocate space */ tbl = calloc(1, sizeof (int) + count * sizeof (swapent_t)); if (tbl == NULL) { *errp = ENOMEM; return (-1); } ptr = calloc(1, count * MAXPATHLEN); if (ptr == NULL) { *errp = ENOMEM; free(tbl); return (-1); } /* set up pointers to the pathnames */ tbl->swt_n = count; for (i = 0; i < count; i++) { tbl->swt_ent[i].ste_path = ptr; ptr += MAXPATHLEN; } /* get list of swap paths */ count = swapctl(SC_LIST, tbl); if (count < 0) { *errp = errno; free(ptr); free(tbl); return (-1); } *stp = tbl; return (count); }
int get_swap_stats(unsigned long long *total_swap, unsigned long long *free_swap) { swaptbl_t *swaptbl; swapent_t *swapent; int i, n, num_entries; char *strtab; n = num_entries = i = 0; again: if ((num_entries = swapctl(SC_GETNSWP, NULL)) == -1) { perror("swapctl: GETNSWP"); return 1; } if (num_entries == 0) { fprintf(stderr, "No swap devices configures\n"); return 2; } if ((swaptbl = (swaptbl_t *) malloc(num_entries * sizeof(swapent_t) + sizeof(struct swaptable))) == (void *) 0) { fprintf(stderr, "Malloc failed\n"); return 3; } /* allocate num+1 string holders */ if ((strtab = (char *) malloc((num_entries + 1) * MAXSTRSIZE)) == (void *) 0) { free(swaptbl); fprintf(stderr, "Malloc Failed\n"); return 4; } /* initialize string pointers */ for (i = 0; i < (num_entries + 1); i++) { swaptbl->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE); } swaptbl->swt_n = num_entries + 1; if ((n = swapctl(SC_LIST, swaptbl)) < 0) { perror("swapctl"); free(swaptbl); free(strtab); return 5; } if (n > num_entries) { free(swaptbl); free(strtab); goto again; } for (i = 0; i < n; i++) { *total_swap += swaptbl->swt_ent[i].ste_pages * pagesize; *free_swap += swaptbl->swt_ent[i].ste_free * pagesize; } free(swaptbl); free(strtab); }
gint read_memswap(gulong *mem, gulong *swap, gulong *MT, gulong *MU, gulong *ST, gulong *SU) { int pagesize; size_t len; #define ARRLEN(X) (sizeof(X)/sizeof(X[0])) { static int mib[2]; /* 64-bit datatype */ if(sizeof(size_t) == 8) { mib[0] = CTL_HW; mib[1] = HW_PHYSMEM64; } /* assume 32-bit datatype */ else { mib[0] = CTL_HW; mib[1] = HW_PHYSMEM; } len = sizeof(MTotal); sysctl(mib, ARRLEN(mib), &MTotal, &len, NULL, 0); MTotal >>= 10; } { static int mib[] = {CTL_HW, HW_PAGESIZE}; len = sizeof(pagesize); sysctl(mib, ARRLEN(mib), &pagesize, &len, NULL, 0); } #if __NetBSD_Version__ > 106210000 { struct swapent* swap; int nswap, n; STotal = SUsed = SFree = 0; if ((nswap = swapctl(SWAP_NSWAP, NULL, 0)) > 0) { swap = (struct swapent*)malloc(nswap * sizeof(*swap)); if (swapctl(SWAP_STATS, (void*)swap, nswap) == nswap) { for (n = 0; n < nswap; n++) { STotal += swap[n].se_nblks; SUsed += swap[n].se_inuse; } STotal = dbtob(STotal >> 10); SUsed = dbtob(SUsed >> 10); SFree = STotal - SUsed; } free(swap); } }
/* * change_priority: change the priority of a swap device. */ void change_priority(char *path) { if (swapctl(SWAP_CTL, path, pri) < 0) warn("%s", path); }
/* * del_swap: remove the pathname from the list of swap devices. */ void del_swap(char *path) { if (swapctl(SWAP_OFF, path, pri) < 0) err(1, "%s", path); }
int uadmin(int cmd, int fcn, uintptr_t mdep) { int error = 0, rv = 0; size_t nbytes = 0; cred_t *credp = CRED(); char *bootargs = NULL; int reset_status = 0; if (cmd == A_SHUTDOWN && fcn == AD_FASTREBOOT_DRYRUN) { ddi_walk_devs(ddi_root_node(), check_driver_quiesce, &reset_status); if (reset_status != 0) return (EIO); else return (0); } /* * The swapctl system call doesn't have its own entry point: it uses * uadmin as a wrapper so we just call it directly from here. */ if (cmd == A_SWAPCTL) { if (get_udatamodel() == DATAMODEL_NATIVE) error = swapctl(fcn, (void *)mdep, &rv); #if defined(_SYSCALL32_IMPL) else error = swapctl32(fcn, (void *)mdep, &rv); #endif /* _SYSCALL32_IMPL */ return (error ? set_errno(error) : rv); } /* * Certain subcommands intepret a non-NULL mdep value as a pointer to * a boot string. We pull that in as bootargs, if applicable. */ if (mdep != NULL && (cmd == A_SHUTDOWN || cmd == A_REBOOT || cmd == A_DUMP || cmd == A_FREEZE || cmd == A_CONFIG)) { bootargs = kmem_zalloc(BOOTARGS_MAX, KM_SLEEP); if ((error = copyinstr((const char *)mdep, bootargs, BOOTARGS_MAX, &nbytes)) != 0) { kmem_free(bootargs, BOOTARGS_MAX); return (set_errno(error)); } } /* * Invoke the appropriate kadmin() routine. */ if (getzoneid() != GLOBAL_ZONEID) error = zone_kadmin(cmd, fcn, bootargs, credp); else error = kadmin(cmd, fcn, bootargs, credp); if (bootargs != NULL) kmem_free(bootargs, BOOTARGS_MAX); return (error ? set_errno(error) : 0); }
/* * add_swap: add the pathname to the list of swap devices. */ void add_swap(char *path) { if (swapctl(SWAP_ON, path, pri) < 0) if (errno != EBUSY) err(1, "%s", path); }
PyObject * psutil_swap_mem(PyObject *self, PyObject *args) { uint64_t swap_total, swap_free; struct swapent *swdev; int nswap, i; if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) == 0) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL) { PyErr_NoMemory(); return NULL; } if (swapctl(SWAP_STATS, swdev, nswap) == -1) { PyErr_SetFromErrno(PyExc_OSError); goto error; } // Total things up. swap_total = swap_free = 0; for (i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { swap_free += (swdev[i].se_nblks - swdev[i].se_inuse); swap_total += swdev[i].se_nblks; } } free(swdev); return Py_BuildValue("(LLLII)", swap_total * DEV_BSIZE, (swap_total - swap_free) * DEV_BSIZE, swap_free * DEV_BSIZE, // swap in / swap out is not supported as the // swapent struct does not provide any info // about it. 0, 0); error: free(swdev); return NULL; }
PyObject * psutil_swap_mem(PyObject *self, PyObject *args) { uint64_t swap_total, swap_free; struct swapent *swdev; int nswap, i; if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) == 0) { warn("failed to get swap device count"); PyErr_SetFromErrno(PyExc_OSError); return NULL; } if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL) { warn("failed to allocate memory for swdev structures"); PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (swapctl(SWAP_STATS, swdev, nswap) == -1) { free(swdev); warn("failed to get swap stats"); PyErr_SetFromErrno(PyExc_OSError); return NULL; } /* Total things up */ swap_total = swap_free = 0; for (i = 0; i < nswap; i++) { if (swdev[i].se_flags & SWF_ENABLE) { swap_free += (swdev[i].se_nblks - swdev[i].se_inuse); swap_total += swdev[i].se_nblks; } } return Py_BuildValue("(LLLII)", swap_total * DEV_BSIZE, (swap_total - swap_free) * DEV_BSIZE, swap_free * DEV_BSIZE, 0 /* XXX swap in */, 0 /* XXX swap out */); }
/* return the available free swap space in unit of MB */ uint64_t get_free_swap(void) { struct anoninfo ai; unsigned freemem; if (swapctl(SC_AINFO, &ai) != -1) { /* in the unit of KB */ freemem = (int)(ctok(ai.ani_max) - ctok(ai.ani_resv)); } else freemem = 0; return (freemem/1024); }
/* kstat-based read function */ static int swap_read_kstat (void) /* {{{ */ { 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_gauge (NULL, "used", swap_alloc); swap_submit_gauge (NULL, "free", swap_avail); swap_submit_gauge (NULL, "reserved", swap_resv); return (0); } /* }}} int swap_read_kstat */
/****************************************************************************** * * * Function: get_swapinfo * * * * Purpose: get swap usage statistics * * * * Return value: SUCCEED if swap usage statistics retrieved successfully * * FAIL otherwise * * * * Author: Vladimir Levijev * * * * Comments: we make calculations the same way swap -s works: * * total = total swap memory * * used = allocated + reserved * * free = total - used * * * ******************************************************************************/ static int get_swapinfo(zbx_uint64_t *total, zbx_uint64_t *used) { static int pagesize = 0; struct anoninfo ai; if (-1 == swapctl(SC_AINFO, &ai)) return FAIL; if (0 == pagesize) pagesize = getpagesize(); *total = ai.ani_max * pagesize; *used = ai.ani_resv * pagesize; return SUCCEED; }
static long getTotalFree(void) { unsigned long free_mem, allocated, reserved, available, used_size; struct anoninfo ai; if (-1 == swapctl(SC_AINFO, &ai)) { snmp_log_perror("swapctl(SC_AINFO)"); return 0; } allocated = ai.ani_max - ai.ani_free; reserved = (ai.ani_resv - allocated); available = (ai.ani_max - ai.ani_resv); /* K-byte */ free_mem = used_size = reserved + allocated; free_mem = available; return (free_mem); }
static int swap_read (void) /* {{{ */ { struct swapent *swap_entries; int swap_num; int status; int i; derive_t used = 0; derive_t total = 0; 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_gauge (NULL, "used", (gauge_t) used); swap_submit_gauge (NULL, "free", (gauge_t) (total - used)); sfree (swap_entries); return (0); } /* }}} int swap_read */
/* swapctl-based read function */ static int swap_read (void) /* {{{ */ { swaptbl_t *s; char *s_paths; int swap_num; int status; int i; derive_t avail = 0; derive_t total = 0; swap_num = swapctl (SC_GETNSWP, NULL); if (swap_num < 0) { ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.", swap_num); return (-1); } else if (swap_num == 0) return (0); /* Allocate and initialize the swaptbl_t structure */ s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable)); if (s == NULL) { ERROR ("swap plugin: smalloc failed."); return (-1); } /* Memory to store the path names. We only use these paths when the * separate option has been configured, but it's easier to just * allocate enough memory in any case. */ s_paths = calloc (swap_num, PATH_MAX); if (s_paths == NULL) { ERROR ("swap plugin: malloc failed."); sfree (s); return (-1); } for (i = 0; i < swap_num; i++) s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX); s->swt_n = swap_num; status = swapctl (SC_LIST, s); if (status < 0) { char errbuf[1024]; ERROR ("swap plugin: swapctl (SC_LIST) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); sfree (s_paths); sfree (s); return (-1); } else if (swap_num < status) { /* more elements returned than requested */ ERROR ("swap plugin: I allocated memory for %i structure%s, " "but swapctl(2) claims to have returned %i. " "I'm confused and will give up.", swap_num, (swap_num == 1) ? "" : "s", status); sfree (s_paths); sfree (s); return (-1); } else if (swap_num > status) /* less elements returned than requested */ swap_num = status; for (i = 0; i < swap_num; i++) { char path[PATH_MAX]; derive_t this_total; derive_t this_avail; if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0) continue; this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize; this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize; /* Shortcut for the "combined" setting (default) */ if (!report_by_device) { avail += this_avail; total += this_total; continue; } sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path)); escape_slashes (path, sizeof (path)); swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail)); swap_submit_gauge (path, "free", (gauge_t) this_avail); } /* for (swap_num) */ if (total < avail) { ERROR ("swap plugin: Total swap space (%"PRIi64") " "is less than free swap space (%"PRIi64").", total, avail); sfree (s_paths); sfree (s); return (-1); } /* If the "separate" option was specified (report_by_device == 2), all * values have already been dispatched from within the loop. */ if (!report_by_device) { swap_submit_gauge (NULL, "used", (gauge_t) (total - avail)); swap_submit_gauge (NULL, "free", (gauge_t) avail); } sfree (s_paths); sfree (s); return (0); } /* }}} int swap_read */
/** * This routine returns kbyte of real memory in use. * @return: TRUE if successful, FALSE if failed (or not available) */ int used_system_memory_sysdep(SystemInfo_T *si) { int i, n, num; struct pst_static pst; struct pst_dynamic psd; struct swaptable *s; char *strtab; unsigned long long total = 0ULL; unsigned long long used = 0ULL; /* Memory */ if(pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) { LogError("system statistic error -- pstat_getstatic failed: %s\n", STRERROR); return FALSE; } if(pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1) { LogError("system statistic error -- pstat_getdynamic failed: %s\n", STRERROR); return FALSE; } si->total_mem_kbyte = (unsigned long)((pst.physical_memory - psd.psd_free) * (pst.page_size/1024)); /* Swap */ again: if ((num = swapctl(SC_GETNSWP, 0)) == -1) { LogError("system statistic error -- swap usage gathering failed: %s\n", STRERROR); return FALSE; } if (num == 0) { DEBUG("system statistic -- no swap configured\n"); si->swap_kbyte_max = 0; return TRUE; } s = (struct swaptable *)ALLOC(num * sizeof(struct swapent) + sizeof(struct swaptable)); strtab = (char *)ALLOC((num + 1) * MAXSTRSIZE); for (i = 0; i < (num + 1); i++) s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE); s->swt_n = num + 1; if ((n = swapctl(SC_LIST, s)) < 0) { LogError("system statistic error -- swap usage gathering failed: %s\n", STRERROR); si->swap_kbyte_max = 0; FREE(s); FREE(strtab); return FALSE; } if (n > num) { DEBUG("system statistic -- new swap added: deferring swap usage statistics to next cycle\n"); FREE(s); FREE(strtab); goto again; } for (i = 0; i < n; i++) { if (!(s->swt_ent[i].ste_flags & ST_INDEL) && !(s->swt_ent[i].ste_flags & ST_DOINGDEL)) { total += s->swt_ent[i].ste_pages; used += s->swt_ent[i].ste_pages - s->swt_ent[i].ste_free; } } FREE(s); FREE(strtab); si->swap_kbyte_max = (unsigned long)(double)(total * page_size) / 1024.; si->total_swap_kbyte = (unsigned long)(double)(used * page_size) / 1024.; return TRUE; }
TCN_IMPLEMENT_CALL(jint, OS, info)(TCN_STDARGS, jlongArray inf) { jint rv; int i; jsize ilen = (*e)->GetArrayLength(e, inf); jlong *pvals = (*e)->GetLongArrayElements(e, inf, NULL); UNREFERENCED(o); if (ilen < 16) { return APR_EINVAL; } for (i = 0; i < 16; i++) pvals[i] = 0; #if defined(__linux__) { struct sysinfo info; if (sysinfo(&info)) rv = apr_get_os_error(); else { pvals[0] = (jlong)(info.totalram * info.mem_unit); pvals[1] = (jlong)(info.freeram * info.mem_unit); pvals[2] = (jlong)(info.totalswap * info.mem_unit); pvals[3] = (jlong)(info.freeswap * info.mem_unit); pvals[4] = (jlong)(info.sharedram * info.mem_unit); pvals[5] = (jlong)(info.bufferram * info.mem_unit); pvals[6] = (jlong)(100 - (info.freeram * 100 / info.totalram)); rv = APR_SUCCESS; } } #elif defined(sun) { /* static variables with basic procfs info */ static long creation = 0; /* unix timestamp of process creation */ static int psinf_fd = 0; /* file descriptor for the psinfo procfs file */ static int prusg_fd = 0; /* file descriptor for the usage procfs file */ static size_t rss = 0; /* maximum of resident set size from previous calls */ /* static variables with basic kstat info */ static kstat_ctl_t *kstat_ctl = NULL; /* kstat control object, only initialized once */ static kstat_t *kstat_cpu[MAX_CPUS]; /* array of kstat objects for per cpu statistics */ static int cpu_count = 0; /* number of cpu structures found in kstat */ static kid_t kid = 0; /* kstat ID, for which the kstat_ctl holds the correct chain */ /* non-static variables - general use */ int res = 0; /* general result state */ /* non-static variables - sysinfo/swapctl use */ long ret_sysconf; /* value returned from sysconf call */ long tck_dividend; /* factor used by transforming tick numbers to milliseconds */ long tck_divisor; /* divisor used by transforming tick numbers to milliseconds */ long sys_pagesize = sysconf(_SC_PAGESIZE); /* size of a system memory page in bytes */ long sys_clk_tck = sysconf(_SC_CLK_TCK); /* number of system ticks per second */ struct anoninfo info; /* structure for information about sizes in anonymous memory system */ /* non-static variables - procfs use */ psinfo_t psinf; /* psinfo structure from procfs */ prusage_t prusg; /* usage structure from procfs */ size_t new_rss = 0; /* resident set size read from procfs */ time_t now; /* time needed for calculating process creation time */ /* non-static variables - kstat use */ kstat_t *kstat = NULL; /* kstat working pointer */ cpu_sysinfo_t cpu; /* cpu sysinfo working pointer */ kid_t new_kid = 0; /* kstat ID returned from chain update */ int new_kstat = 0; /* flag indicating, if kstat structure has changed since last call */ rv = APR_SUCCESS; if (sys_pagesize <= 0) { rv = apr_get_os_error(); } else { ret_sysconf = sysconf(_SC_PHYS_PAGES); if (ret_sysconf >= 0) { pvals[0] = (jlong)((jlong)sys_pagesize * ret_sysconf); } else { rv = apr_get_os_error(); } ret_sysconf = sysconf(_SC_AVPHYS_PAGES); if (ret_sysconf >= 0) { pvals[1] = (jlong)((jlong)sys_pagesize * ret_sysconf); } else { rv = apr_get_os_error(); } res=swapctl(SC_AINFO, &info); if (res >= 0) { pvals[2] = (jlong)((jlong)sys_pagesize * info.ani_max); pvals[3] = (jlong)((jlong)sys_pagesize * info.ani_free); pvals[6] = (jlong)(100 - (jlong)info.ani_free * 100 / info.ani_max); } else { rv = apr_get_os_error(); } } if (psinf_fd == 0) { psinf_fd = proc_open("psinfo"); } res = proc_read(&psinf, PSINFO_T_SZ, psinf_fd); if (res >= 0) { new_rss = psinf.pr_rssize*1024; pvals[13] = (jlong)(new_rss); if (new_rss > rss) { rss = new_rss; } pvals[14] = (jlong)(rss); } else { psinf_fd = 0; rv = apr_get_os_error(); } if (prusg_fd == 0) { prusg_fd = proc_open("usage"); } res = proc_read(&prusg, PRUSAGE_T_SZ, prusg_fd); if (res >= 0) { if (creation <= 0) { time(&now); creation = (long)(now - (prusg.pr_tstamp.tv_sec - prusg.pr_create.tv_sec)); } pvals[10] = (jlong)(creation); pvals[11] = (jlong)((jlong)prusg.pr_stime.tv_sec * 1000 + (prusg.pr_stime.tv_nsec / 1000000)); pvals[12] = (jlong)((jlong)prusg.pr_utime.tv_sec * 1000 + (prusg.pr_utime.tv_nsec / 1000000)); pvals[15] = (jlong)(prusg.pr_majf); } else { prusg_fd = 0; rv = apr_get_os_error(); } if (sys_clk_tck <= 0) { rv = apr_get_os_error(); } else { tck_dividend = 1000; tck_divisor = sys_clk_tck; for (i = 0; i < 3; i++) { if (tck_divisor % 2 == 0) { tck_divisor = tck_divisor / 2; tck_dividend = tck_dividend / 2; } if (tck_divisor % 5 == 0) { tck_divisor = tck_divisor / 5; tck_dividend = tck_dividend / 5; } } if (kstat_ctl == NULL) { kstat_ctl = kstat_open(); kid = kstat_ctl->kc_chain_id; new_kstat = 1; } else { new_kid = kstat_chain_update(kstat_ctl); if (new_kid < 0) { res=kstat_close(kstat_ctl); kstat_ctl = kstat_open(); kid = kstat_ctl->kc_chain_id; new_kstat = 1; } else if (new_kid > 0 && kid != new_kid) { kid = new_kid; new_kstat = 1; } } if (new_kstat) { cpu_count = 0; for (kstat = kstat_ctl->kc_chain; kstat; kstat = kstat->ks_next) { if (strncmp(kstat->ks_name, "cpu_stat", 8) == 0) { kstat_cpu[cpu_count++]=kstat; } } } for (i = 0; i < cpu_count; i++) { new_kid = kstat_read(kstat_ctl, kstat_cpu[i], NULL); if (new_kid >= 0) { cpu = ((cpu_stat_t *)kstat_cpu[i]->ks_data)->cpu_sysinfo; if ( tck_divisor == 1 ) { pvals[7] += (jlong)(((jlong)cpu.cpu[CPU_IDLE]) * tck_dividend); pvals[7] += (jlong)(((jlong)cpu.cpu[CPU_WAIT]) * tck_dividend); pvals[8] += (jlong)(((jlong)cpu.cpu[CPU_KERNEL]) * tck_dividend); pvals[9] += (jlong)(((jlong)cpu.cpu[CPU_USER]) * tck_dividend); } else { pvals[7] += (jlong)(((jlong)cpu.cpu[CPU_IDLE]) * tck_dividend / tck_divisor); pvals[7] += (jlong)(((jlong)cpu.cpu[CPU_WAIT]) * tck_dividend / tck_divisor); pvals[8] += (jlong)(((jlong)cpu.cpu[CPU_KERNEL]) * tck_dividend / tck_divisor); pvals[9] += (jlong)(((jlong)cpu.cpu[CPU_USER]) * tck_dividend / tck_divisor); } } } } /* * The next two are not implemented yet for Solaris * inf[4] - Amount of shared memory * inf[5] - Memory used by buffers * */ } #elif defined(DARWIN) uint64_t mem_total; size_t len = sizeof(mem_total); vm_statistics_data_t vm_info; mach_msg_type_number_t info_count = HOST_VM_INFO_COUNT; sysctlbyname("hw.memsize", &mem_total, &len, NULL, 0); pvals[0] = (jlong)mem_total; host_statistics(mach_host_self (), HOST_VM_INFO, (host_info_t)&vm_info, &info_count); pvals[1] = (jlong)(((double)vm_info.free_count)*vm_page_size); pvals[6] = (jlong)(100 - (pvals[1] * 100 / mem_total)); rv = APR_SUCCESS; /* DARWIN */ #else rv = APR_ENOTIMPL; #endif (*e)->ReleaseLongArrayElements(e, inf, pvals, 0); return rv; }
void list_swap(int pri, int kflag, int pflag, int dolong) { struct swapent *sep, *fsep; long blocksize; char *header; size_t l; int hlen, totalsize, size, totalinuse, inuse, ncounted, pathmax; int rnswap, nswap, i; nswap = swapctl(SWAP_NSWAP, 0, 0); if (nswap < 1) errx(1, "no swap devices configured"); fsep = sep = (struct swapent *)calloc(nswap, sizeof(*sep)); if (sep == NULL) err(1, "calloc"); rnswap = swapctl(SWAP_STATS, (void *)sep, nswap); if (rnswap < 0) err(1, "SWAP_STATS"); if (nswap != rnswap) warnx("SWAP_STATS different to SWAP_NSWAP (%d != %d)", rnswap, nswap); pathmax = 11; if (dolong) { if (kflag) { header = "1K-blocks"; blocksize = 1024; hlen = strlen(header); } else header = getbsize(&hlen, &blocksize); for (i = rnswap; i-- > 0; sep++) if (pathmax < (l = strlen(sep->se_path))) pathmax = l; sep = fsep; (void)printf("%-*s %*s %8s %8s %8s %s\n", pathmax, "Device", hlen, header, "Used", "Avail", "Capacity", "Priority"); } totalsize = totalinuse = ncounted = 0; for (; rnswap-- > 0; sep++) { if (pflag && sep->se_priority != pri) continue; ncounted++; size = sep->se_nblks; inuse = sep->se_inuse; totalsize += size; totalinuse += inuse; if (dolong) { (void)printf("%-*s %*ld ", pathmax, sep->se_path, hlen, (long)(dbtoqb(size) / blocksize)); (void)printf("%8ld %8ld %5.0f%% %d\n", (long)(dbtoqb(inuse) / blocksize), (long)(dbtoqb(size - inuse) / blocksize), (double)inuse / (double)size * 100.0, sep->se_priority); } } if (dolong == 0) printf("total: %ldk bytes allocated = %ldk used, " "%ldk available\n", (long)(dbtoqb(totalsize) / 1024), (long)(dbtoqb(totalinuse) / 1024), (long)(dbtoqb(totalsize - totalinuse) / 1024)); else if (ncounted > 1) (void)printf("%-*s %*ld %8ld %8ld %5.0f%%\n", pathmax, "Total", hlen, (long)(dbtoqb(totalsize) / blocksize), (long)(dbtoqb(totalinuse) / blocksize), (long)(dbtoqb(totalsize - totalinuse) / blocksize), (double)(totalinuse) / (double)totalsize * 100.0); if (fsep) free(fsep); }