[SIGAR-125] bring back /private/var/vm swap impl for osx 10.3

This commit is contained in:
Doug MacEachern 2008-10-04 19:43:46 +00:00
parent dd471219c4
commit dd9dd214bd
1 changed files with 90 additions and 7 deletions

View File

@ -536,16 +536,81 @@ static int getswapinfo_sysctl(struct kvm_swap *swap_ary,
#define getswapinfo_sysctl(swap_ary, swap_max) SIGAR_ENOTIMPL
#endif
int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
#define SIGAR_FS_BLOCKS_TO_BYTES(val, bsize) ((val * bsize) >> 1)
#ifdef DARWIN
#define VM_DIR "/private/var/vm"
#define SWAPFILE "swapfile"
static int sigar_swap_fs_get(sigar_t *sigar, sigar_swap_t *swap) /* <= 10.3 */
{
int status;
#if defined(DARWIN)
DIR *dirp;
struct dirent *ent;
char swapfile[SSTRLEN(VM_DIR) + SSTRLEN("/") + SSTRLEN(SWAPFILE) + 12];
struct stat swapstat;
struct statfs vmfs;
sigar_uint64_t val, bsize;
swap->used = swap->total = swap->free = 0;
if (!(dirp = opendir(VM_DIR))) {
return errno;
}
/* looking for "swapfile0", "swapfile1", etc. */
while ((ent = readdir(dirp))) {
char *ptr = swapfile;
if ((ent->d_namlen < SSTRLEN(SWAPFILE)+1) || /* n/a, see comment above */
(ent->d_namlen > SSTRLEN(SWAPFILE)+11)) /* ensure no overflow */
{
continue;
}
if (!strnEQ(ent->d_name, SWAPFILE, SSTRLEN(SWAPFILE))) {
continue;
}
/* sprintf(swapfile, "%s/%s", VM_DIR, ent->d_name) */
memcpy(ptr, VM_DIR, SSTRLEN(VM_DIR));
ptr += SSTRLEN(VM_DIR);
*ptr++ = '/';
memcpy(ptr, ent->d_name, ent->d_namlen+1);
if (stat(swapfile, &swapstat) < 0) {
continue;
}
swap->used += swapstat.st_size;
}
closedir(dirp);
if (statfs(VM_DIR, &vmfs) < 0) {
return errno;
}
bsize = vmfs.f_bsize / 512;
val = vmfs.f_bfree;
swap->total = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize) + swap->used;
swap->free = swap->total - swap->used;
return SIGAR_OK;
}
static int sigar_swap_sysctl_get(sigar_t *sigar, sigar_swap_t *swap)
{
#ifdef VM_SWAPUSAGE /* => 10.4 */
struct xsw_usage sw_usage;
size_t size = sizeof(sw_usage);
int mib[] = { CTL_VM, VM_SWAPUSAGE };
vm_statistics_data_t vmstat;
if (sysctl(mib, NMIB(mib), &sw_usage, &size, NULL, 0) < 0) {
if (sysctl(mib, NMIB(mib), &sw_usage, &size, NULL, 0) != 0) {
return errno;
}
@ -553,6 +618,26 @@ int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
swap->used = sw_usage.xsu_used;
swap->free = sw_usage.xsu_avail;
return SIGAR_OK;
#else
return SIGAR_ENOTIMPL; /* <= 10.3 */
#endif
}
#endif /* DARWIN */
int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
{
int status;
#if defined(DARWIN)
vm_statistics_data_t vmstat;
if (sigar_swap_sysctl_get(sigar, swap) != SIGAR_OK) {
status = sigar_swap_fs_get(sigar, swap); /* <= 10.3 */
if (status != SIGAR_OK) {
return status;
}
}
if ((status = sigar_vmstat(sigar, &vmstat)) != SIGAR_OK) {
return status;
}
@ -1998,8 +2083,6 @@ int sigar_disk_usage_get(sigar_t *sigar, const char *name,
#endif
}
#define SIGAR_FS_BLOCKS_TO_BYTES(val, bsize) ((val * bsize) >> 1)
int sigar_file_system_usage_get(sigar_t *sigar,
const char *dirname,
sigar_file_system_usage_t *fsusage)