sigar/src/os/darwin/darwin_sigar.c

2532 lines
62 KiB
C
Raw Normal View History

2006-07-16 01:46:36 +08:00
/*
* Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
* This file is part of SIGAR.
*
* SIGAR is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
2004-06-22 06:37:04 +08:00
#include "sigar.h"
#include "sigar_private.h"
#include "sigar_util.h"
#include "sigar_os.h"
#ifdef DARWIN
#include <mach/mach_init.h>
#include <mach/message.h>
#include <mach/kern_return.h>
2005-06-17 05:24:49 +08:00
#include <mach/mach_host.h>
#include <mach/mach_traps.h>
#include <mach/mach_port.h>
#include <mach/task.h>
2007-01-25 05:43:43 +08:00
#include <mach/thread_act.h>
#include <mach/thread_info.h>
2005-06-17 05:24:49 +08:00
#include <mach/vm_map.h>
#include <mach/shared_memory_server.h>
2006-09-26 21:32:45 +08:00
#include <Gestalt.h>
2004-06-22 06:37:04 +08:00
#else
2005-02-09 11:54:57 +08:00
#include <sys/dkstat.h>
2004-06-22 06:37:04 +08:00
#include <sys/types.h>
#include <sys/param.h>
#include <sys/user.h>
2005-02-09 12:19:55 +08:00
#include <sys/vmmeter.h>
2005-02-09 11:36:54 +08:00
#include <fcntl.h>
2006-10-01 01:13:10 +08:00
#include <stdio.h>
2004-06-22 06:37:04 +08:00
#endif
#include <sys/ioctl.h>
2004-06-22 06:37:04 +08:00
#include <sys/mount.h>
2005-03-22 04:54:23 +08:00
#include <sys/resource.h>
2004-06-22 06:37:04 +08:00
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <netinet/in.h>
#include <dirent.h>
#include <errno.h>
#define NMIB(mib) (sizeof(mib)/sizeof(mib[0]))
2006-01-06 03:33:12 +08:00
#ifdef __FreeBSD__
# if (__FreeBSD_version >= 500013)
# define SIGAR_FREEBSD5
# else
# define SIGAR_FREEBSD4
# endif
2005-02-20 08:45:28 +08:00
#endif
#ifdef SIGAR_FREEBSD5
2005-02-13 12:13:49 +08:00
2006-01-03 06:15:24 +08:00
#define KI_FD ki_fd
2005-02-13 12:13:49 +08:00
#define KI_PID ki_pid
#define KI_PPID ki_ppid
#define KI_PRI ki_pri.pri_user
#define KI_NICE ki_nice
#define KI_COMM ki_comm
#define KI_STAT ki_stat
#define KI_UID ki_ruid
#define KI_GID ki_rgid
#define KI_EUID ki_svuid
#define KI_EGID ki_svgid
2005-02-13 12:22:46 +08:00
#define KI_SIZE ki_size
#define KI_RSS ki_rssize
#define KI_TSZ ki_tsize
#define KI_DSZ ki_dsize
#define KI_SSZ ki_ssize
2005-02-13 13:13:30 +08:00
#define KI_FLAG ki_flag
2005-02-16 10:54:17 +08:00
#define KI_START ki_start
2006-01-03 06:15:24 +08:00
2005-02-13 12:13:49 +08:00
#else
2006-01-03 06:15:24 +08:00
#define KI_FD kp_proc.p_fd
2005-02-13 12:13:49 +08:00
#define KI_PID kp_proc.p_pid
#define KI_PPID kp_eproc.e_ppid
#define KI_PRI kp_proc.p_priority
#define KI_NICE kp_proc.p_nice
#define KI_COMM kp_proc.p_comm
#define KI_STAT kp_proc.p_stat
#define KI_UID kp_eproc.e_pcred.p_ruid
#define KI_GID kp_eproc.e_pcred.p_rgid
#define KI_EUID kp_eproc.e_pcred.p_svuid
#define KI_EGID kp_eproc.e_pcred.p_svgid
2005-02-13 12:22:46 +08:00
#define KI_SIZE XXX
#define KI_RSS kp_eproc.e_vm.vm_rssize
#define KI_TSZ kp_eproc.e_vm.vm_tsize
#define KI_DSZ kp_eproc.e_vm.vm_dsize
#define KI_SSZ kp_eproc.e_vm.vm_ssize
2005-02-13 13:13:30 +08:00
#define KI_FLAG kp_eproc.e_flag
2005-02-16 10:54:17 +08:00
#define KI_START kp_proc.p_starttime
2006-01-03 06:15:24 +08:00
2005-02-13 12:13:49 +08:00
#endif
2005-02-09 11:54:57 +08:00
#ifndef DARWIN
2005-02-13 12:13:49 +08:00
2005-02-13 13:55:27 +08:00
#define PROCFS_STATUS(status) \
((((status) != SIGAR_OK) && !sigar->proc_mounted) ? \
SIGAR_ENOTIMPL : status)
2005-02-13 13:55:27 +08:00
2005-02-09 11:54:57 +08:00
static int get_koffsets(sigar_t *sigar)
{
int i;
struct nlist klist[] = {
{ "_cp_time" },
2005-02-09 12:19:55 +08:00
{ "_cnt" },
2005-02-09 11:54:57 +08:00
{ NULL }
};
2005-02-12 09:50:56 +08:00
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
2005-02-09 11:56:27 +08:00
kvm_nlist(sigar->kmem, klist);
2005-02-09 11:54:57 +08:00
if (klist[0].n_type == 0) {
return errno;
}
for (i=0; i<KOFFSET_MAX; i++) {
sigar->koffsets[i] = klist[i].n_value;
}
return SIGAR_OK;
}
static int kread(sigar_t *sigar, void *data, int size, long offset)
{
2005-02-12 09:50:56 +08:00
if (!sigar->kmem) {
2005-02-09 11:54:57 +08:00
return SIGAR_EPERM_KMEM;
}
2005-02-09 11:56:27 +08:00
if (kvm_read(sigar->kmem, offset, data, size) != size) {
2005-02-09 11:54:57 +08:00
return errno;
}
return SIGAR_OK;
}
#endif
2004-06-22 06:37:04 +08:00
int sigar_os_open(sigar_t **sigar)
{
int mib[2];
int ncpu;
size_t len;
struct timeval boottime;
2005-02-13 13:46:10 +08:00
#ifndef DARWIN
struct stat sb;
#endif
2004-06-22 06:37:04 +08:00
len = sizeof(ncpu);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
if (sysctl(mib, NMIB(mib), &ncpu, &len, NULL, 0) < 0) {
return errno;
}
len = sizeof(boottime);
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
if (sysctl(mib, NMIB(mib), &boottime, &len, NULL, 0) < 0) {
return errno;
}
*sigar = malloc(sizeof(**sigar));
#ifdef DARWIN
(*sigar)->mach_port = mach_host_self();
2005-02-09 11:36:54 +08:00
#else
(*sigar)->kmem = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
2005-02-13 13:46:10 +08:00
if (stat("/proc/curproc", &sb) < 0) {
(*sigar)->proc_mounted = 0;
}
else {
(*sigar)->proc_mounted = 1;
}
2004-06-22 06:37:04 +08:00
#endif
2005-02-20 09:38:02 +08:00
#ifndef DARWIN
2005-02-09 11:54:57 +08:00
get_koffsets(*sigar);
2005-02-20 09:38:02 +08:00
#endif
2005-02-09 11:54:57 +08:00
2004-06-22 06:37:04 +08:00
(*sigar)->ncpu = ncpu;
(*sigar)->boot_time = boottime.tv_sec; /* XXX seems off a bit */
2005-02-09 12:19:55 +08:00
(*sigar)->pagesize = getpagesize();
2005-05-12 11:10:41 +08:00
#ifdef __FreeBSD__
(*sigar)->ticks = 100; /* sysconf(_SC_CLK_TCK) == 128 !? */
#else
(*sigar)->ticks = sysconf(_SC_CLK_TCK);
2005-05-12 11:10:41 +08:00
#endif
2004-06-22 06:37:04 +08:00
(*sigar)->last_pid = -1;
(*sigar)->pinfo = NULL;
return SIGAR_OK;
}
int sigar_os_close(sigar_t *sigar)
{
if (sigar->pinfo) {
free(sigar->pinfo);
}
2006-01-06 03:33:12 +08:00
#ifndef DARWIN
2006-01-05 02:04:51 +08:00
if (sigar->kmem) {
kvm_close(sigar->kmem);
}
2006-01-06 03:33:12 +08:00
#endif
2004-06-22 06:37:04 +08:00
free(sigar);
return SIGAR_OK;
}
2005-03-12 14:19:34 +08:00
char *sigar_os_error_string(sigar_t *sigar, int err)
2004-06-22 06:37:04 +08:00
{
2005-02-12 09:50:56 +08:00
switch (err) {
case SIGAR_EPERM_KMEM:
return "Failed to open /dev/kmem for reading";
2005-02-13 13:46:10 +08:00
case SIGAR_EPROC_NOENT:
return "/proc filesystem is not mounted";
2005-02-12 09:50:56 +08:00
default:
return NULL;
}
2004-06-22 06:37:04 +08:00
}
2007-08-05 02:39:49 +08:00
#if defined(DARWIN)
2007-08-05 02:03:28 +08:00
static int sigar_vmstat(sigar_t *sigar, vm_statistics_data_t *vmstat)
{
kern_return_t status;
mach_msg_type_number_t count = sizeof(*vmstat) / sizeof(integer_t);
status = host_statistics(sigar->mach_port, HOST_VM_INFO,
(host_info_t)vmstat, &count);
if (status == KERN_SUCCESS) {
return SIGAR_OK;
}
else {
return errno;
}
}
2007-08-05 02:39:49 +08:00
#elif defined(__FreeBSD__)
static int sigar_vmstat(sigar_t *sigar, struct vmmeter *vmstat)
{
int status;
size_t size = sizeof(unsigned int);
2007-08-05 02:39:49 +08:00
status = kread(sigar, vmstat, sizeof(*vmstat),
sigar->koffsets[KOFFSET_VMMETER]);
if (status == SIGAR_OK) {
return SIGAR_OK;
}
SIGAR_ZERO(vmstat);
/* derived from src/usr.bin/vmstat/vmstat.c */
/* only collect the ones we actually use */
#define GET_VM_STATS(cat, name, used) \
if (used) sysctlbyname("vm.stats." #cat "." #name, &vmstat->name, &size, NULL, 0)
/* sys */
GET_VM_STATS(sys, v_swtch, 0);
GET_VM_STATS(sys, v_trap, 0);
GET_VM_STATS(sys, v_syscall, 0);
GET_VM_STATS(sys, v_intr, 0);
GET_VM_STATS(sys, v_soft, 0);
/* vm */
GET_VM_STATS(vm, v_vm_faults, 0);
GET_VM_STATS(vm, v_cow_faults, 0);
GET_VM_STATS(vm, v_cow_optim, 0);
GET_VM_STATS(vm, v_zfod, 0);
GET_VM_STATS(vm, v_ozfod, 0);
GET_VM_STATS(vm, v_swapin, 1);
GET_VM_STATS(vm, v_swapout, 1);
GET_VM_STATS(vm, v_swappgsin, 0);
GET_VM_STATS(vm, v_swappgsout, 0);
GET_VM_STATS(vm, v_vnodein, 1);
GET_VM_STATS(vm, v_vnodeout, 1);
GET_VM_STATS(vm, v_vnodepgsin, 0);
GET_VM_STATS(vm, v_vnodepgsout, 0);
GET_VM_STATS(vm, v_intrans, 0);
GET_VM_STATS(vm, v_reactivated, 0);
GET_VM_STATS(vm, v_pdwakeups, 0);
GET_VM_STATS(vm, v_pdpages, 0);
GET_VM_STATS(vm, v_dfree, 0);
GET_VM_STATS(vm, v_pfree, 0);
GET_VM_STATS(vm, v_tfree, 0);
GET_VM_STATS(vm, v_page_size, 0);
GET_VM_STATS(vm, v_page_count, 0);
GET_VM_STATS(vm, v_free_reserved, 0);
GET_VM_STATS(vm, v_free_target, 0);
GET_VM_STATS(vm, v_free_min, 0);
GET_VM_STATS(vm, v_free_count, 1);
GET_VM_STATS(vm, v_wire_count, 0);
GET_VM_STATS(vm, v_active_count, 0);
GET_VM_STATS(vm, v_inactive_target, 0);
GET_VM_STATS(vm, v_inactive_count, 0);
GET_VM_STATS(vm, v_cache_count, 0);
GET_VM_STATS(vm, v_cache_min, 0);
GET_VM_STATS(vm, v_cache_max, 0);
GET_VM_STATS(vm, v_pageout_free_min, 0);
GET_VM_STATS(vm, v_interrupt_free_min, 0);
GET_VM_STATS(vm, v_forks, 0);
GET_VM_STATS(vm, v_vforks, 0);
GET_VM_STATS(vm, v_rforks, 0);
GET_VM_STATS(vm, v_kthreads, 0);
GET_VM_STATS(vm, v_forkpages, 0);
GET_VM_STATS(vm, v_vforkpages, 0);
GET_VM_STATS(vm, v_rforkpages, 0);
GET_VM_STATS(vm, v_kthreadpages, 0);
#undef GET_VM_STATS
return SIGAR_OK;
2007-08-05 02:39:49 +08:00
}
2007-08-05 02:03:28 +08:00
#endif
2004-06-22 06:37:04 +08:00
int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem)
{
#ifdef DARWIN
vm_statistics_data_t vmstat;
uint64_t mem_total;
2006-01-07 02:50:05 +08:00
#else
unsigned long mem_total;
2007-08-05 03:02:27 +08:00
struct vmmeter vmstat;
#endif
2004-06-22 06:37:04 +08:00
int mib[2];
2006-01-01 04:35:56 +08:00
size_t len;
2007-08-05 03:02:27 +08:00
int status;
2004-06-22 06:37:04 +08:00
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
2006-01-01 04:35:56 +08:00
len = sizeof(sigar->pagesize);
2004-06-22 06:37:04 +08:00
if (sysctl(mib, NMIB(mib), &sigar->pagesize, &len, NULL, 0) < 0) {
return errno;
}
2006-01-07 02:50:05 +08:00
#ifdef DARWIN
mib[1] = HW_MEMSIZE;
#else
2004-06-22 06:37:04 +08:00
mib[1] = HW_PHYSMEM;
2006-01-07 02:50:05 +08:00
#endif
len = sizeof(mem_total);
if (sysctl(mib, NMIB(mib), &mem_total, &len, NULL, 0) < 0) {
2004-06-22 06:37:04 +08:00
return errno;
}
mem->total = mem_total;
2006-01-03 01:18:55 +08:00
2007-06-04 08:37:08 +08:00
#if defined(DARWIN)
2007-08-05 02:03:28 +08:00
if ((status = sigar_vmstat(sigar, &vmstat)) != SIGAR_OK) {
return status;
2004-06-22 06:37:04 +08:00
}
mem->free = vmstat.free_count * sigar->pagesize;
2007-06-04 08:37:08 +08:00
#elif defined(__FreeBSD__)
2007-08-05 03:02:27 +08:00
if ((status = sigar_vmstat(sigar, &vmstat)) == SIGAR_OK) {
mem->free = vmstat.v_free_count;
mem->free *= sigar->pagesize;
}
2007-06-04 08:37:08 +08:00
#else
mem->free = -1; /*XXX OpenBSD*/
#endif
2004-06-22 06:37:04 +08:00
mem->used = mem->total - mem->free;
2005-02-09 12:19:55 +08:00
sigar_mem_calc_ram(sigar, mem);
mem->actual_free = mem->free;
mem->actual_used = mem->used;
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
#define SIGAR_FS_BLOCKS_TO_BYTES(buf, f) \
(((sigar_uint64_t)buf.f * (buf.f_bsize / 512)) >> 1)
2004-06-22 06:37:04 +08:00
#define VM_DIR "/private/var/vm"
#define SWAPFILE "swapfile"
#define NL_SWAPBLIST 0
#define NL_SWDEVT 1
#define NL_NSWDEV 2
#define NL_DMMAX 3
#define SWI_MAXMIB 3
#ifdef SIGAR_FREEBSD5
/* code in this function is based on FreeBSD 5.3 kvm_getswapinfo.c */
static int getswapinfo_sysctl(struct kvm_swap *swap_ary,
int swap_max)
{
int ti, ttl;
size_t mibi, len, size;
int soid[SWI_MAXMIB];
struct xswdev xsd;
struct kvm_swap tot;
int unswdev, dmmax;
/* XXX this can be optimized by using os_open */
size = sizeof(dmmax);
if (sysctlbyname("vm.dmmax", &dmmax, &size, NULL, 0) == -1) {
return errno;
}
mibi = SWI_MAXMIB - 1;
if (sysctlnametomib("vm.swap_info", soid, &mibi) == -1) {
return errno;
}
bzero(&tot, sizeof(tot));
for (unswdev = 0;; unswdev++) {
soid[mibi] = unswdev;
len = sizeof(xsd);
if (sysctl(soid, mibi + 1, &xsd, &len, NULL, 0) == -1) {
if (errno == ENOENT) {
break;
}
return errno;
}
#if 0
if (len != sizeof(xsd)) {
_kvm_err(kd, kd->program, "struct xswdev has unexpected "
"size; kernel and libkvm out of sync?");
return -1;
}
if (xsd.xsw_version != XSWDEV_VERSION) {
_kvm_err(kd, kd->program, "struct xswdev version "
"mismatch; kernel and libkvm out of sync?");
return -1;
}
#endif
ttl = xsd.xsw_nblks - dmmax;
if (unswdev < swap_max - 1) {
bzero(&swap_ary[unswdev], sizeof(swap_ary[unswdev]));
swap_ary[unswdev].ksw_total = ttl;
swap_ary[unswdev].ksw_used = xsd.xsw_used;
swap_ary[unswdev].ksw_flags = xsd.xsw_flags;
}
tot.ksw_total += ttl;
tot.ksw_used += xsd.xsw_used;
}
ti = unswdev;
if (ti >= swap_max) {
ti = swap_max - 1;
}
if (ti >= 0) {
swap_ary[ti] = tot;
}
return SIGAR_OK;
}
#else
#define getswapinfo_sysctl(swap_ary, swap_max) SIGAR_ENOTIMPL
2005-02-20 08:45:28 +08:00
#endif
2004-06-22 06:37:04 +08:00
int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap)
{
2007-08-05 02:39:49 +08:00
int status;
2007-06-04 08:37:08 +08:00
#if defined(DARWIN)
2004-06-22 06:37:04 +08:00
DIR *dirp;
struct dirent *ent;
char swapfile[SSTRLEN(VM_DIR) + SSTRLEN("/") + SSTRLEN(SWAPFILE) + 12];
struct stat swapstat;
struct statfs vmfs;
2007-08-05 02:06:16 +08:00
vm_statistics_data_t vmstat;
2004-06-22 06:37:04 +08:00
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;
}
swap->total = SIGAR_FS_BLOCKS_TO_BYTES(vmfs, f_bfree) + swap->used;
swap->free = swap->total - swap->used;
2007-08-05 02:06:16 +08:00
if ((status = sigar_vmstat(sigar, &vmstat)) != SIGAR_OK) {
return status;
}
swap->page_in = vmstat.pageins;
swap->page_out = vmstat.pageouts;
2007-06-04 08:37:08 +08:00
#elif defined(__FreeBSD__)
2005-02-09 11:36:54 +08:00
struct kvm_swap kswap[1];
2007-08-05 02:39:49 +08:00
struct vmmeter vmstat;
2005-02-09 11:36:54 +08:00
if (getswapinfo_sysctl(kswap, 1) != SIGAR_OK) {
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
2005-02-12 09:50:56 +08:00
if (kvm_getswapinfo(sigar->kmem, kswap, 1, 0) < 0) {
return errno;
}
2005-02-09 11:36:54 +08:00
}
if (kswap[0].ksw_total == 0) {
swap->total = 0;
swap->used = 0;
swap->free = 0;
return SIGAR_OK;
}
swap->total = kswap[0].ksw_total * sigar->pagesize;
swap->used = kswap[0].ksw_used * sigar->pagesize;
swap->free = swap->total - swap->used;
2007-07-21 02:02:21 +08:00
2007-08-05 02:39:49 +08:00
if ((status = sigar_vmstat(sigar, &vmstat)) == SIGAR_OK) {
swap->page_in = vmstat.v_swapin + vmstat.v_vnodein;
swap->page_out = vmstat.v_swapout + vmstat.v_vnodeout;
}
else {
swap->page_in = swap->page_out = -1;
}
2007-06-04 08:37:08 +08:00
#else
/*XXX OpenBSD*/
2004-06-22 06:37:04 +08:00
#endif
return SIGAR_OK;
}
int sigar_cpu_get(sigar_t *sigar, sigar_cpu_t *cpu)
{
2007-06-04 08:37:08 +08:00
#if defined(DARWIN)
2004-06-22 06:37:04 +08:00
kern_return_t status;
mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
host_cpu_load_info_data_t cpuload;
status = host_statistics(sigar->mach_port, HOST_CPU_LOAD_INFO,
(host_info_t)&cpuload, &count);
if (status != KERN_SUCCESS) {
return errno;
}
cpu->user = SIGAR_TICK2MSEC(cpuload.cpu_ticks[CPU_STATE_USER]);
cpu->sys = SIGAR_TICK2MSEC(cpuload.cpu_ticks[CPU_STATE_SYSTEM]);
cpu->idle = SIGAR_TICK2MSEC(cpuload.cpu_ticks[CPU_STATE_IDLE]);
cpu->nice = SIGAR_TICK2MSEC(cpuload.cpu_ticks[CPU_STATE_NICE]);
2004-11-22 09:51:34 +08:00
cpu->wait = 0; /*N/A*/
2004-06-22 06:37:04 +08:00
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle;
2007-06-04 08:37:08 +08:00
#elif defined(__FreeBSD__)
2005-02-09 11:54:57 +08:00
int status;
2007-02-06 04:21:53 +08:00
unsigned long cp_time[CPUSTATES];
2005-02-15 09:56:22 +08:00
size_t size = sizeof(cp_time);
2005-02-09 11:54:57 +08:00
2005-02-15 09:56:22 +08:00
/* try sysctl first, does not require /dev/kmem perms */
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) == -1) {
status = kread(sigar, &cp_time, sizeof(cp_time),
sigar->koffsets[KOFFSET_CPUINFO]);
}
else {
status = SIGAR_OK;
}
2005-02-09 11:54:57 +08:00
if (status != SIGAR_OK) {
return status;
}
cpu->user = SIGAR_TICK2MSEC(cp_time[CP_USER]);
cpu->nice = SIGAR_TICK2MSEC(cp_time[CP_NICE]);
cpu->sys = SIGAR_TICK2MSEC(cp_time[CP_SYS] + cp_time[CP_INTR]);
cpu->idle = SIGAR_TICK2MSEC(cp_time[CP_IDLE]);
2005-05-12 10:57:24 +08:00
cpu->wait = 0; /*N/A*/
2004-06-22 06:37:04 +08:00
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle;
2007-06-04 08:37:08 +08:00
#else
/*XXX OpenBSD*/
2004-06-22 06:37:04 +08:00
#endif
return SIGAR_OK;
}
int sigar_cpu_list_get(sigar_t *sigar, sigar_cpu_list_t *cpulist)
{
2006-01-01 03:51:04 +08:00
#ifdef DARWIN
kern_return_t status;
mach_msg_type_number_t count;
processor_cpu_load_info_data_t *cpuload;
natural_t i, ncpu;
2006-01-06 03:53:36 +08:00
status = host_processor_info(sigar->mach_port,
2006-01-01 03:51:04 +08:00
PROCESSOR_CPU_LOAD_INFO,
&ncpu,
(processor_info_array_t*)&cpuload,
&count);
if (status != KERN_SUCCESS) {
return errno;
}
sigar_cpu_list_create(cpulist);
for (i=0; i<ncpu; i++) {
sigar_cpu_t *cpu;
SIGAR_CPU_LIST_GROW(cpulist);
cpu = &cpulist->data[cpulist->number++];
cpu->user = SIGAR_TICK2MSEC(cpuload[i].cpu_ticks[CPU_STATE_USER]);
cpu->sys = SIGAR_TICK2MSEC(cpuload[i].cpu_ticks[CPU_STATE_SYSTEM]);
cpu->idle = SIGAR_TICK2MSEC(cpuload[i].cpu_ticks[CPU_STATE_IDLE]);
cpu->nice = SIGAR_TICK2MSEC(cpuload[i].cpu_ticks[CPU_STATE_NICE]);
2006-01-01 03:51:04 +08:00
cpu->wait = 0; /*N/A*/
cpu->total = cpu->user + cpu->nice + cpu->sys + cpu->idle;
}
vm_deallocate(mach_task_self(), (vm_address_t)cpuload, count);
return SIGAR_OK;
#else
int status, i;
sigar_cpu_t *cpu;
sigar_cpu_list_create(cpulist);
/* XXX howto multi cpu in freebsd?
* for now just report all metrics on the 1st cpu
* 0's for the rest
*/
cpu = &cpulist->data[cpulist->number++];
status = sigar_cpu_get(sigar, cpu);
if (status != SIGAR_OK) {
return status;
}
for (i=1; i<sigar->ncpu; i++) {
SIGAR_CPU_LIST_GROW(cpulist);
cpu = &cpulist->data[cpulist->number++];
SIGAR_ZERO(cpu);
}
return SIGAR_OK;
2006-01-01 03:51:04 +08:00
#endif
2004-06-22 06:37:04 +08:00
}
int sigar_uptime_get(sigar_t *sigar,
sigar_uptime_t *uptime)
{
uptime->uptime = time(NULL) - sigar->boot_time;
return SIGAR_OK;
}
int sigar_loadavg_get(sigar_t *sigar,
sigar_loadavg_t *loadavg)
{
getloadavg(loadavg->loadavg, 3);
return SIGAR_OK;
}
2005-02-13 13:13:30 +08:00
#ifndef KERN_PROC_PROC
/* freebsd 4.x */
#define KERN_PROC_PROC KERN_PROC_ALL
#endif
2007-04-22 11:54:04 +08:00
int sigar_os_proc_list_get(sigar_t *sigar,
sigar_proc_list_t *proclist)
2004-06-22 06:37:04 +08:00
{
2006-01-01 02:46:55 +08:00
#if defined(DARWIN) || defined(SIGAR_FREEBSD5)
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PROC, 0 };
2004-06-22 06:37:04 +08:00
int i, num;
size_t len;
struct kinfo_proc *proc;
if (sysctl(mib, NMIB(mib), NULL, &len, NULL, 0) < 0) {
return errno;
}
proc = malloc(len);
if (sysctl(mib, NMIB(mib), proc, &len, NULL, 0) < 0) {
free(proc);
return errno;
}
num = len/sizeof(*proc);
for (i=0; i<num; i++) {
2005-02-19 08:06:44 +08:00
if (proc[i].KI_FLAG & P_SYSTEM) {
continue;
}
SIGAR_PROC_LIST_GROW(proclist);
2005-02-13 12:13:49 +08:00
proclist->data[proclist->number++] = proc[i].KI_PID;
2004-06-22 06:37:04 +08:00
}
free(proc);
return SIGAR_OK;
#else
2005-02-09 12:31:08 +08:00
int i, num;
2005-02-12 09:50:56 +08:00
struct kinfo_proc *proc;
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
proc = kvm_getprocs(sigar->kmem, KERN_PROC_PROC, 0, &num);
2005-02-09 12:31:08 +08:00
for (i=0; i<num; i++) {
2005-02-13 13:13:30 +08:00
if (proc[i].KI_FLAG & P_SYSTEM) {
continue;
}
SIGAR_PROC_LIST_GROW(proclist);
2005-02-13 12:13:49 +08:00
proclist->data[proclist->number++] = proc[i].KI_PID;
2005-02-09 12:31:08 +08:00
}
2004-06-22 06:37:04 +08:00
#endif
2005-02-09 12:31:08 +08:00
return SIGAR_OK;
2004-06-22 06:37:04 +08:00
}
static int sigar_get_pinfo(sigar_t *sigar, sigar_pid_t pid)
{
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
size_t len = sizeof(*sigar->pinfo);
time_t timenow = time(NULL);
mib[3] = pid;
if (sigar->pinfo == NULL) {
sigar->pinfo = malloc(len);
}
if (sigar->last_pid == pid) {
if ((timenow - sigar->last_getprocs) < SIGAR_LAST_PROC_EXPIRE) {
return SIGAR_OK;
}
}
sigar->last_pid = pid;
sigar->last_getprocs = timenow;
if (sysctl(mib, NMIB(mib), sigar->pinfo, &len, NULL, 0) < 0) {
return errno;
}
return SIGAR_OK;
}
int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_mem_t *procmem)
{
2005-02-09 09:22:37 +08:00
#ifdef DARWIN
2004-06-22 06:37:04 +08:00
mach_port_t task, self = mach_task_self();
kern_return_t status;
task_basic_info_data_t info;
2005-11-29 01:40:51 +08:00
task_events_info_data_t events;
2005-06-17 05:24:49 +08:00
mach_msg_type_number_t count;
2004-06-22 06:37:04 +08:00
status = task_for_pid(self, pid, &task);
if (status != KERN_SUCCESS) {
return errno;
}
2005-06-17 06:21:24 +08:00
count = TASK_BASIC_INFO_COUNT;
status = task_info(task, TASK_BASIC_INFO, (task_info_t)&info, &count);
if (status != KERN_SUCCESS) {
return errno;
}
2004-06-22 06:37:04 +08:00
2005-11-29 01:40:51 +08:00
count = TASK_EVENTS_INFO_COUNT;
status = task_info(task, TASK_EVENTS_INFO, (task_info_t)&events, &count);
if (status == KERN_SUCCESS) {
procmem->page_faults = events.faults;
}
else {
procmem->page_faults = SIGAR_FIELD_NOTIMPL;
}
procmem->minor_faults = SIGAR_FIELD_NOTIMPL;
procmem->major_faults = SIGAR_FIELD_NOTIMPL;
2004-06-22 06:37:04 +08:00
if (task != self) {
mach_port_deallocate(self, task);
}
2006-03-04 10:48:58 +08:00
procmem->size = info.virtual_size;
procmem->resident = info.resident_size;
2006-03-17 02:36:07 +08:00
procmem->share = SIGAR_FIELD_NOTIMPL;
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
2005-02-09 09:22:37 +08:00
#else
2005-02-13 11:24:33 +08:00
int status = sigar_get_pinfo(sigar, pid);
struct kinfo_proc *pinfo = sigar->pinfo;
if (status != SIGAR_OK) {
return status;
}
2006-03-04 10:48:58 +08:00
procmem->size =
2005-02-13 12:22:46 +08:00
(pinfo->KI_TSZ + pinfo->KI_DSZ + pinfo->KI_SSZ) * sigar->pagesize;
2005-02-13 11:24:33 +08:00
2006-03-04 10:48:58 +08:00
procmem->resident = pinfo->KI_RSS * sigar->pagesize;
2005-02-13 11:24:33 +08:00
procmem->share = SIGAR_FIELD_NOTIMPL;
2005-11-24 02:00:02 +08:00
procmem->page_faults = SIGAR_FIELD_NOTIMPL;
procmem->minor_faults = SIGAR_FIELD_NOTIMPL;
procmem->major_faults = SIGAR_FIELD_NOTIMPL;
2005-02-13 11:24:33 +08:00
return SIGAR_OK;
2005-02-09 09:22:37 +08:00
#endif
2004-06-22 06:37:04 +08:00
}
int sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cred_t *proccred)
{
int status = sigar_get_pinfo(sigar, pid);
struct kinfo_proc *pinfo = sigar->pinfo;
if (status != SIGAR_OK) {
return status;
}
2005-02-13 12:13:49 +08:00
proccred->uid = pinfo->KI_UID;
proccred->gid = pinfo->KI_GID;
proccred->euid = pinfo->KI_EUID;
proccred->egid = pinfo->KI_EGID;
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
#define tv2msec(tv) \
(((sigar_uint64_t)tv.tv_sec * SIGAR_MSEC) + (((sigar_uint64_t)tv.tv_usec) / 1000))
2005-02-09 15:06:04 +08:00
#ifdef DARWIN
#define tval2msec(tval) \
((tval.seconds * SIGAR_MSEC) + (tval.microseconds / 1000))
2007-01-25 05:43:43 +08:00
#define tval2nsec(tval) \
(SIGAR_SEC2NANO((tval).seconds) + SIGAR_MICROSEC2NANO((tval).microseconds))
2004-06-22 06:37:04 +08:00
static int get_proc_times(sigar_pid_t pid, sigar_proc_time_t *time)
{
unsigned int count;
time_value_t utime = {0, 0}, stime = {0, 0};
task_basic_info_data_t ti;
task_thread_times_info_data_t tti;
task_port_t task, self = mach_task_self();
kern_return_t status;
status = task_for_pid(self, pid, &task);
if (status != KERN_SUCCESS) {
return errno;
}
count = TASK_BASIC_INFO_COUNT;
status = task_info(task, TASK_BASIC_INFO,
(task_info_t)&ti, &count);
if (status != KERN_SUCCESS) {
if (task != self) {
mach_port_deallocate(self, task);
}
return errno;
}
count = TASK_THREAD_TIMES_INFO_COUNT;
status = task_info(task, TASK_THREAD_TIMES_INFO,
(task_info_t)&tti, &count);
if (status != KERN_SUCCESS) {
if (task != self) {
mach_port_deallocate(self, task);
}
return errno;
}
time_value_add(&utime, &ti.user_time);
time_value_add(&stime, &ti.system_time);
time_value_add(&utime, &tti.user_time);
time_value_add(&stime, &tti.system_time);
time->user = tval2msec(utime);
time->sys = tval2msec(stime);
time->total = time->user + time->sys;
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
2005-02-09 15:06:04 +08:00
#endif
2004-06-22 06:37:04 +08:00
int sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_time_t *proctime)
{
2006-01-03 05:51:02 +08:00
#ifdef SIGAR_FREEBSD4
struct user user;
#endif
2004-06-22 06:37:04 +08:00
int status = sigar_get_pinfo(sigar, pid);
struct kinfo_proc *pinfo = sigar->pinfo;
2005-02-19 07:54:33 +08:00
2004-06-22 06:37:04 +08:00
if (status != SIGAR_OK) {
return status;
}
2005-02-20 08:45:28 +08:00
#if defined(DARWIN)
2004-06-22 06:37:04 +08:00
if ((status = get_proc_times(pid, proctime)) != SIGAR_OK) {
return status;
}
proctime->start_time = tv2msec(pinfo->KI_START);
2005-02-20 08:45:28 +08:00
#elif defined(SIGAR_FREEBSD5)
proctime->user = tv2msec(pinfo->ki_rusage.ru_utime);
proctime->sys = tv2msec(pinfo->ki_rusage.ru_stime);
2005-02-09 15:06:04 +08:00
proctime->total = proctime->user + proctime->sys;
proctime->start_time = tv2msec(pinfo->KI_START);
2007-06-04 08:37:08 +08:00
#elif defined(SIGAR_FREEBSD4)
2006-01-03 05:51:02 +08:00
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
2006-01-03 05:51:02 +08:00
}
status = kread(sigar, &user, sizeof(user),
(u_long)pinfo->kp_proc.p_addr);
if (status != SIGAR_OK) {
return status;
}
proctime->user = tv2msec(user.u_stats.p_ru.ru_utime);
proctime->sys = tv2msec(user.u_stats.p_ru.ru_stime);
2006-01-03 05:51:02 +08:00
proctime->total = proctime->user + proctime->sys;
proctime->start_time = tv2msec(user.u_stats.p_start);
2007-06-04 08:37:08 +08:00
#else
/*XXX OpenBSD*/
2005-02-20 08:45:28 +08:00
#endif
2006-01-03 05:51:02 +08:00
return SIGAR_OK;
2004-06-22 06:37:04 +08:00
}
2007-03-25 07:07:42 +08:00
#ifdef DARWIN
static int sigar_proc_threads_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_state_t *procstate)
{
mach_port_t task, self = mach_task_self();
kern_return_t status;
thread_array_t threads;
mach_msg_type_number_t count;
status = task_for_pid(self, pid, &task);
if (status != KERN_SUCCESS) {
return errno;
}
status = task_threads(task, &threads, &count);
if (status != KERN_SUCCESS) {
return errno;
}
procstate->threads = count;
vm_deallocate(self, (vm_address_t)threads, sizeof(thread_t) * count);
return SIGAR_OK;
}
#endif
2004-06-22 06:37:04 +08:00
int sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_state_t *procstate)
{
int status = sigar_get_pinfo(sigar, pid);
struct kinfo_proc *pinfo = sigar->pinfo;
if (status != SIGAR_OK) {
return status;
}
2005-02-13 12:13:49 +08:00
SIGAR_SSTRCPY(procstate->name, pinfo->KI_COMM);
procstate->ppid = pinfo->KI_PPID;
procstate->priority = pinfo->KI_PRI;
procstate->nice = pinfo->KI_NICE;
procstate->tty = SIGAR_FIELD_NOTIMPL; /*XXX*/
2005-11-23 08:39:37 +08:00
procstate->threads = SIGAR_FIELD_NOTIMPL;
2005-11-29 01:48:57 +08:00
procstate->processor = SIGAR_FIELD_NOTIMPL;
2004-06-22 06:37:04 +08:00
2007-03-25 07:07:42 +08:00
#ifdef DARWIN
sigar_proc_threads_get(sigar, pid, procstate);
#endif
2005-02-13 12:13:49 +08:00
switch (pinfo->KI_STAT) {
2004-06-22 06:37:04 +08:00
case SIDL:
procstate->state = 'D';
break;
case SRUN:
procstate->state = 'R';
break;
case SSLEEP:
procstate->state = 'S';
break;
case SSTOP:
procstate->state = 'T';
break;
case SZOMB:
procstate->state = 'Z';
break;
}
return SIGAR_OK;
}
2005-02-17 02:33:00 +08:00
#if defined(DARWIN)
typedef struct {
char buffer[8096], *ptr, *end;
int count;
} sigar_kern_proc_args_t;
/* re-usable hack for use by proc_args and proc_env */
static int sigar_kern_proc_args_get(sigar_pid_t pid,
char *exe,
sigar_kern_proc_args_t *kargs)
{
2005-05-26 04:05:46 +08:00
/*
* derived from:
* http://darwinsource.opendarwin.org/10.4.1/adv_cmds-79.1/ps.tproj/print.c
*/
int mib[3], len;
size_t size = sizeof(kargs->buffer);
char *args = kargs->buffer;
2005-05-26 04:05:46 +08:00
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
mib[2] = pid;
if (sysctl(mib, NMIB(mib), args, &size, NULL, 0) < 0) {
2005-05-26 04:05:46 +08:00
return errno;
}
kargs->end = &args[size];
2005-05-26 04:05:46 +08:00
memcpy(&kargs->count, args, sizeof(kargs->count));
kargs->ptr = args + sizeof(kargs->count);
2005-05-26 04:05:46 +08:00
len = strlen(kargs->ptr);
if (exe) {
memcpy(exe, kargs->ptr, len+1);
}
kargs->ptr += len+1;
2005-05-26 04:05:46 +08:00
if (kargs->ptr == kargs->end) {
return exe ? SIGAR_OK : ENOENT;
2005-05-26 04:05:46 +08:00
}
for (; kargs->ptr < kargs->end; kargs->ptr++) {
if (*kargs->ptr != '\0') {
2005-05-26 04:05:46 +08:00
break; /* start of argv[0] */
}
}
if (kargs->ptr == kargs->end) {
return exe ? SIGAR_OK : ENOENT;
2005-05-26 04:05:46 +08:00
}
return SIGAR_OK;
}
2006-08-29 09:36:42 +08:00
static int kern_proc_args_skip_argv(sigar_kern_proc_args_t *kargs)
{
char *ptr = kargs->ptr;
char *end = kargs->end;
int count = kargs->count;
/* skip over argv */
while ((ptr < end) && (count-- > 0)) {
int alen = strlen(ptr)+1;
ptr += alen;
}
kargs->ptr = ptr;
kargs->end = end;
kargs->count = 0;
if (ptr >= end) {
return ENOENT;
}
return SIGAR_OK;
}
#endif
2007-04-22 13:37:39 +08:00
int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_args_t *procargs)
{
#if defined(DARWIN)
int status, count;
sigar_kern_proc_args_t kargs;
char *ptr, *end;
status = sigar_kern_proc_args_get(pid, NULL, &kargs);
if (status != SIGAR_OK) {
return status;
}
count = kargs.count;
ptr = kargs.ptr;
end = kargs.end;
while ((ptr < end) && (count-- > 0)) {
2006-08-26 04:50:47 +08:00
int slen = strlen(ptr);
int alen = slen+1;
char *arg;
/*
* trim trailing whitespace.
* seen w/ postgresql, probably related
* to messing with argv[0]
*/
while (*(ptr + (slen-1)) == ' ') {
2006-08-26 05:06:18 +08:00
if (--slen <= 0) {
break;
}
2006-08-26 04:50:47 +08:00
}
arg = malloc(slen+1);
2005-05-26 04:05:46 +08:00
SIGAR_PROC_ARGS_GROW(procargs);
2006-08-26 04:50:47 +08:00
memcpy(arg, ptr, slen);
*(arg+slen) = '\0';
2005-05-26 04:05:46 +08:00
procargs->data[procargs->number++] = arg;
ptr += alen;
2005-05-26 04:05:46 +08:00
}
return SIGAR_OK;
2006-01-04 03:54:06 +08:00
#else
2006-04-04 01:27:06 +08:00
/* ARG_MAX in FreeBSD 6.0 == 262144, which blows up the stack */
#define SIGAR_ARG_MAX 65536
char buffer[SIGAR_ARG_MAX+1], *ptr=buffer;
2005-02-17 02:33:00 +08:00
size_t len = sizeof(buffer);
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ARGS, 0 };
mib[3] = pid;
if (sysctl(mib, NMIB(mib), buffer, &len, NULL, 0) < 0) {
return errno;
}
if (len == 0) {
procargs->number = 0;
return SIGAR_OK;
}
buffer[len] = '\0';
while (len > 0) {
2005-02-17 02:33:00 +08:00
int alen = strlen(ptr)+1;
char *arg = malloc(alen);
SIGAR_PROC_ARGS_GROW(procargs);
memcpy(arg, ptr, alen);
procargs->data[procargs->number++] = arg;
2006-01-04 04:22:12 +08:00
len -= alen;
if (len > 0) {
ptr += alen;
2006-01-04 04:22:12 +08:00
}
2005-02-17 02:33:00 +08:00
}
return SIGAR_OK;
2005-02-09 15:29:45 +08:00
#endif
2004-06-22 06:37:04 +08:00
}
int sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_env_t *procenv)
{
2005-02-09 15:53:28 +08:00
#ifdef DARWIN
2006-08-26 04:23:56 +08:00
int status, count;
sigar_kern_proc_args_t kargs;
char *ptr, *end;
status = sigar_kern_proc_args_get(pid, NULL, &kargs);
2006-08-26 04:23:56 +08:00
if (status != SIGAR_OK) {
return status;
}
2006-08-29 09:36:42 +08:00
status = kern_proc_args_skip_argv(&kargs);
if (status != SIGAR_OK) {
return status;
}
2006-08-26 04:23:56 +08:00
count = kargs.count;
ptr = kargs.ptr;
end = kargs.end;
/* into environ */
while (ptr < end) {
char *val = strchr(ptr, '=');
int klen, vlen, status;
char key[256]; /* XXX is there a max key size? */
if (val == NULL) {
/* not key=val format */
break;
}
klen = val - ptr;
SIGAR_SSTRCPY(key, ptr);
key[klen] = '\0';
++val;
vlen = strlen(val);
status = procenv->env_getter(procenv->data,
key, klen, val, vlen);
if (status != SIGAR_OK) {
/* not an error; just stop iterating */
break;
}
ptr += (klen + 1 + vlen + 1);
if (*ptr == '\0') {
break;
}
}
return SIGAR_OK;
2005-02-09 15:53:28 +08:00
#else
char **env;
struct kinfo_proc *pinfo;
int num;
2005-02-12 09:50:56 +08:00
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
2005-02-12 09:50:56 +08:00
}
2005-02-09 15:53:28 +08:00
pinfo = kvm_getprocs(sigar->kmem, KERN_PROC_PID, pid, &num);
if (!pinfo || (num < 1)) {
return errno;
}
if (!(env = kvm_getenvv(sigar->kmem, pinfo, 9086))) {
return errno;
}
while (*env) {
char *ptr = *env++;
char *val = strchr(ptr, '=');
int klen, vlen, status;
char key[128]; /* XXX is there a max key size? */
if (val == NULL) {
/* not key=val format */
procenv->env_getter(procenv->data, ptr, strlen(ptr), NULL, 0);
break;
}
klen = val - ptr;
SIGAR_SSTRCPY(key, ptr);
key[klen] = '\0';
++val;
vlen = strlen(val);
status = procenv->env_getter(procenv->data,
key, klen, val, vlen);
if (status != SIGAR_OK) {
/* not an error; just stop iterating */
break;
}
ptr += (klen + 1 + vlen + 1);
}
return SIGAR_OK;
#endif
2004-06-22 06:37:04 +08:00
}
int sigar_proc_fd_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_fd_t *procfd)
{
2007-06-04 08:37:08 +08:00
#ifdef __FreeBSD__
2005-02-19 11:10:56 +08:00
int status;
struct kinfo_proc *pinfo;
struct filedesc filed;
#if 0
struct file **ofiles;
int nfiles, i;
size_t size;
#endif
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
2005-02-19 11:10:56 +08:00
}
if ((status = sigar_get_pinfo(sigar, pid)) != SIGAR_OK) {
return status;
}
pinfo = sigar->pinfo;
2006-01-03 06:18:33 +08:00
status = kread(sigar, &filed, sizeof(filed), (u_long)pinfo->KI_FD);
2005-02-19 11:10:56 +08:00
if (status != SIGAR_OK) {
return status;
}
#if 0
nfiles = filed.fd_lastfile+1;
size = sizeof(*ofiles) * nfiles;
ofiles = malloc(size);
status = kread(sigar, ofiles, size, (u_long)filed.fd_ofiles);
if (status != SIGAR_OK) {
free(ofiles);
return status;
}
procfd->total = 0;
for (i=0; i<filed.fd_lastfile; i++) {
if (!ofiles[i]) {
continue;
}
procfd->total++;
}
free(ofiles);
#else
/* seems the same as the above */
procfd->total = filed.fd_lastfile;
#endif
return SIGAR_OK;
2005-02-20 08:45:28 +08:00
#else
return SIGAR_ENOTIMPL;
2005-02-19 11:10:56 +08:00
#endif
2004-06-22 06:37:04 +08:00
}
int sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_exe_t *procexe)
{
2005-02-09 16:07:32 +08:00
#ifdef DARWIN
2006-08-28 11:52:36 +08:00
int status;
sigar_kern_proc_args_t kargs;
status = sigar_kern_proc_args_get(pid, procexe->name, &kargs);
2006-08-28 11:52:36 +08:00
if (status != SIGAR_OK) {
return status;
}
procexe->cwd[0] = '\0';
procexe->root[0] = '\0';
2006-08-29 09:43:48 +08:00
/* attempt to determine cwd from $PWD */
status = kern_proc_args_skip_argv(&kargs);
if (status == SIGAR_OK) {
char *ptr = kargs.ptr;
char *end = kargs.end;
/* into environ */
while (ptr < end) {
int len = strlen(ptr);
if ((len > 4) &&
(ptr[0] == 'P') &&
(ptr[1] == 'W') &&
(ptr[2] == 'D') &&
(ptr[3] == '='))
{
memcpy(procexe->cwd, ptr+4, len-3);
break;
}
ptr += len+1;
}
}
2006-08-28 11:52:36 +08:00
return SIGAR_OK;
2005-02-09 16:07:32 +08:00
#else
int len;
char name[1024];
procexe->cwd[0] = '\0';
procexe->root[0] = '\0';
(void)SIGAR_PROC_FILENAME(name, pid, "/file");
if ((len = readlink(name, procexe->name,
sizeof(procexe->name)-1)) < 0)
{
2005-02-16 03:13:59 +08:00
return PROCFS_STATUS(errno);
2005-02-09 16:07:32 +08:00
}
procexe->name[len] = '\0';
return SIGAR_OK;
#endif
2004-06-22 06:37:04 +08:00
}
2004-06-23 03:40:37 +08:00
int sigar_proc_modules_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_modules_t *procmods)
{
return SIGAR_ENOTIMPL;
}
2005-03-21 08:07:20 +08:00
#define SIGAR_MICROSEC2NANO(s) \
((sigar_uint64_t)(s) * (sigar_uint64_t)1000)
#define TIME_NSEC(t) \
(SIGAR_SEC2NANO((t).tv_sec) + SIGAR_MICROSEC2NANO((t).tv_usec))
2004-11-17 13:35:17 +08:00
int sigar_thread_cpu_get(sigar_t *sigar,
sigar_uint64_t id,
sigar_thread_cpu_t *cpu)
{
2007-01-25 05:43:43 +08:00
#ifdef DARWIN
mach_port_t self = mach_thread_self();
thread_basic_info_data_t info;
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
kern_return_t status;
status = thread_info(self, THREAD_BASIC_INFO,
(thread_info_t)&info, &count);
if (status != KERN_SUCCESS) {
return errno;
}
mach_port_deallocate(mach_task_self(), self);
cpu->user = tval2nsec(info.user_time);
cpu->sys = tval2nsec(info.system_time);
cpu->total = cpu->user + cpu->sys;
#else
2005-03-21 08:07:20 +08:00
/* XXX this is not per-thread, it is for the whole-process.
* just want to use for the shell time command at the moment.
*/
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
cpu->user = TIME_NSEC(usage.ru_utime);
cpu->sys = TIME_NSEC(usage.ru_stime);
cpu->total = TIME_NSEC(usage.ru_utime) + TIME_NSEC(usage.ru_stime);
2007-01-25 05:43:43 +08:00
#endif
2005-03-21 08:07:20 +08:00
return SIGAR_OK;
2004-11-17 13:35:17 +08:00
}
2004-06-22 06:37:04 +08:00
int sigar_os_fs_type_get(sigar_file_system_t *fsp)
{
char *type = fsp->sys_type_name;
/* see sys/disklabel.h */
switch (*type) {
case 'h':
if (strEQ(type, "hfs")) {
fsp->type = SIGAR_FSTYPE_LOCAL_DISK;
}
break;
case 'u':
if (strEQ(type, "ufs")) {
fsp->type = SIGAR_FSTYPE_LOCAL_DISK;
}
break;
}
return fsp->type;
}
int sigar_file_system_list_get(sigar_t *sigar,
sigar_file_system_list_t *fslist)
{
struct statfs *fs;
int num, i;
long len;
int is_debug = SIGAR_LOG_IS_DEBUG(sigar);
2004-06-22 06:37:04 +08:00
if ((num = getfsstat(NULL, 0, MNT_NOWAIT)) < 0) {
return errno;
}
len = sizeof(*fs) * num;
fs = malloc(len);
if ((num = getfsstat(fs, len, MNT_NOWAIT)) < 0) {
return errno;
}
sigar_file_system_list_create(fslist);
for (i=0; i<num; i++) {
sigar_file_system_t *fsp;
2005-12-06 02:45:37 +08:00
#ifdef MNT_AUTOMOUNTED
2005-12-06 02:43:42 +08:00
if (fs[i].f_flags & MNT_AUTOMOUNTED) {
if (is_debug) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[file_system_list] skipping automounted %s: %s",
fs[i].f_fstypename, fs[i].f_mntonname);
}
continue;
}
#endif
#ifdef MNT_RDONLY
if (fs[i].f_flags & MNT_RDONLY) {
/* e.g. ftp mount or .dmg image */
if (is_debug) {
sigar_log_printf(sigar, SIGAR_LOG_DEBUG,
"[file_system_list] skipping readonly %s: %s",
fs[i].f_fstypename, fs[i].f_mntonname);
}
2005-12-06 02:43:42 +08:00
continue;
}
2005-12-06 02:45:37 +08:00
#endif
2005-12-06 02:43:42 +08:00
2004-06-22 06:37:04 +08:00
SIGAR_FILE_SYSTEM_LIST_GROW(fslist);
fsp = &fslist->data[fslist->number++];
SIGAR_SSTRCPY(fsp->dir_name, fs[i].f_mntonname);
SIGAR_SSTRCPY(fsp->dev_name, fs[i].f_mntfromname);
SIGAR_SSTRCPY(fsp->sys_type_name, fs[i].f_fstypename);
sigar_fs_type_init(fsp);
}
return SIGAR_OK;
}
int sigar_file_system_usage_get(sigar_t *sigar,
const char *dirname,
sigar_file_system_usage_t *fsusage)
{
struct statfs buf;
if (statfs(dirname, &buf) < 0) {
return errno;
}
fsusage->total = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_blocks);
fsusage->free = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_bfree);
fsusage->avail = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_bavail);
2005-04-27 03:55:21 +08:00
fsusage->used = fsusage->total - fsusage->free;
2004-06-22 06:37:04 +08:00
fsusage->files = buf.f_files;
2005-11-29 01:29:26 +08:00
fsusage->free_files = buf.f_ffree;
2004-06-22 06:37:04 +08:00
fsusage->use_percent = sigar_file_system_usage_calc_used(sigar, fsusage);
2007-06-04 08:37:08 +08:00
#ifdef __FreeBSD__
2005-02-17 03:27:29 +08:00
fsusage->disk_reads = buf.f_syncreads + buf.f_asyncreads;
fsusage->disk_writes = buf.f_syncwrites + buf.f_asyncwrites;
2005-04-07 09:28:07 +08:00
fsusage->disk_read_bytes = SIGAR_FIELD_NOTIMPL;
fsusage->disk_write_bytes = SIGAR_FIELD_NOTIMPL;
fsusage->disk_queue = SIGAR_FIELD_NOTIMPL;
2007-06-04 08:37:08 +08:00
#else
SIGAR_DISK_STATS_NOTIMPL(fsusage);
2005-02-17 03:27:29 +08:00
#endif
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
2005-02-12 08:18:00 +08:00
#ifdef DARWIN
#define CTL_HW_FREQ "hw.cpufrequency"
#else
/* XXX FreeBSD 5.x+ only? */
#define CTL_HW_FREQ "machdep.tsc_freq"
#endif
int sigar_cpu_info_list_get(sigar_t *sigar,
sigar_cpu_info_list_t *cpu_infos)
2004-06-22 06:37:04 +08:00
{
2005-02-17 09:42:22 +08:00
int i;
2005-12-02 10:30:53 +08:00
unsigned int mhz;
int cache_size=SIGAR_FIELD_NOTIMPL;
2005-02-12 08:11:43 +08:00
size_t size;
2005-02-12 08:40:52 +08:00
char model[128], vendor[128], *ptr;
2005-02-12 08:11:43 +08:00
2005-02-17 09:42:22 +08:00
size = sizeof(mhz);
2005-12-02 10:30:53 +08:00
2007-06-04 08:37:08 +08:00
#if defined(DARWIN)
2005-12-02 10:30:53 +08:00
{
2005-12-02 09:03:03 +08:00
int mib[] = { CTL_HW, HW_CPU_FREQ };
size = sizeof(mhz);
if (sysctl(mib, NMIB(mib), &mhz, &size, NULL, 0) < 0) {
mhz = SIGAR_FIELD_NOTIMPL;
}
2005-02-12 08:11:43 +08:00
}
2007-06-04 08:37:08 +08:00
#elif defined(__FreeBSD__)
2005-12-02 10:30:53 +08:00
if (sysctlbyname(CTL_HW_FREQ, &mhz, &size, NULL, 0) < 0) {
mhz = SIGAR_FIELD_NOTIMPL;
}
2007-06-04 08:37:08 +08:00
#else
/*XXX OpenBSD*/
mhz = SIGAR_FIELD_NOTIMPL;
2005-12-02 10:30:53 +08:00
#endif
2005-12-02 09:03:03 +08:00
if (mhz != SIGAR_FIELD_NOTIMPL) {
mhz /= 1000000;
2005-02-12 08:11:43 +08:00
}
2004-06-22 06:37:04 +08:00
2007-06-04 08:37:08 +08:00
#ifdef __OpenBSD__
strcpy(model, "Unknown");
#else
2005-02-12 08:40:52 +08:00
size = sizeof(model);
2005-12-02 09:12:51 +08:00
if (sysctlbyname("hw.model", &model, &size, NULL, 0) < 0) {
int mib[] = { CTL_HW, HW_MODEL };
size = sizeof(model);
if (sysctl(mib, NMIB(mib), &model[0], &size, NULL, 0) < 0) {
#ifdef DARWIN
strcpy(model, "powerpc");
#else
strcpy(model, "Unknown");
#endif
}
2005-02-12 08:40:52 +08:00
}
2007-06-04 08:37:08 +08:00
#endif
2005-12-02 09:12:51 +08:00
2006-01-04 05:48:30 +08:00
if (mhz == SIGAR_FIELD_NOTIMPL) {
/* freebsd4 */
mhz = sigar_cpu_mhz_from_model(model);
}
2006-07-06 03:20:12 +08:00
#ifdef DARWIN
size = sizeof(vendor);
if (sysctlbyname("machdep.cpu.vendor", &vendor, &size, NULL, 0) < 0) {
SIGAR_SSTRCPY(vendor, "Apple");
}
else {
/* GenuineIntel -> Intel */
if (strstr(vendor, "Intel")) {
SIGAR_SSTRCPY(vendor, "Intel");
}
}
#endif
2005-12-02 09:12:51 +08:00
if ((ptr = strchr(model, ' '))) {
2005-02-12 08:40:52 +08:00
*ptr = '\0';
if (strstr(model, "Intel")) {
SIGAR_SSTRCPY(vendor, "Intel");
}
else if (strstr(model, "AMD")) {
SIGAR_SSTRCPY(vendor, "AMD");
}
else {
SIGAR_SSTRCPY(vendor, "Unknown");
}
SIGAR_SSTRCPY(model, ptr+1);
}
2005-12-02 10:30:53 +08:00
#ifdef DARWIN
2005-12-02 09:22:16 +08:00
{
2005-12-02 10:32:07 +08:00
int mib[] = { CTL_HW, HW_L2CACHESIZE }; /* in bytes */
2005-12-02 09:22:16 +08:00
size = sizeof(cache_size);
if (sysctl(mib, NMIB(mib), &cache_size, &size, NULL, 0) < 0) {
cache_size = SIGAR_FIELD_NOTIMPL;
}
2005-12-02 12:56:57 +08:00
else {
cache_size /= 1024; /* convert to KB */
}
2005-12-02 09:22:16 +08:00
}
2005-12-02 10:30:53 +08:00
#endif
2005-12-02 09:22:16 +08:00
sigar_cpu_info_list_create(cpu_infos);
2004-06-22 06:37:04 +08:00
for (i=0; i<sigar->ncpu; i++) {
sigar_cpu_info_t *info;
SIGAR_CPU_INFO_LIST_GROW(cpu_infos);
2004-06-22 06:37:04 +08:00
info = &cpu_infos->data[cpu_infos->number++];
2006-07-06 03:20:12 +08:00
2005-02-12 08:40:52 +08:00
SIGAR_SSTRCPY(info->vendor, vendor);
SIGAR_SSTRCPY(info->model, model);
sigar_cpu_model_adjust(sigar, info);
2005-12-02 09:12:51 +08:00
2005-02-12 08:11:43 +08:00
info->mhz = mhz;
2005-12-02 09:22:16 +08:00
info->cache_size = cache_size;
2004-06-22 06:37:04 +08:00
}
return SIGAR_OK;
}
2005-07-15 11:19:11 +08:00
#define rt_s_addr(sa) ((struct sockaddr_in *)(sa))->sin_addr.s_addr
2005-09-24 23:16:08 +08:00
#ifndef SA_SIZE
#define SA_SIZE(sa) \
( (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ? \
sizeof(long) : \
1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
#endif
2004-06-22 06:37:04 +08:00
int sigar_net_route_list_get(sigar_t *sigar,
sigar_net_route_list_t *routelist)
{
2005-07-13 12:08:36 +08:00
size_t needed;
2005-07-15 11:19:11 +08:00
int bit;
2005-07-13 12:08:36 +08:00
char *buf, *next, *lim;
struct rt_msghdr *rtm;
2005-07-15 11:19:11 +08:00
int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_DUMP, 0 };
2005-07-13 12:08:36 +08:00
2005-07-15 11:19:11 +08:00
if (sysctl(mib, NMIB(mib), NULL, &needed, NULL, 0) < 0) {
2005-07-13 12:08:36 +08:00
return errno;
}
buf = malloc(needed);
2005-07-15 11:19:11 +08:00
if (sysctl(mib, NMIB(mib), buf, &needed, NULL, 0) < 0) {
2005-07-13 12:08:36 +08:00
free(buf);
return errno;
}
2004-06-22 06:37:04 +08:00
sigar_net_route_list_create(routelist);
2005-07-13 12:08:36 +08:00
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
struct sockaddr *sa;
sigar_net_route_t *route;
rtm = (struct rt_msghdr *)next;
2005-07-15 11:19:11 +08:00
if (rtm->rtm_type != RTM_GET) {
continue;
}
2005-07-13 12:08:36 +08:00
sa = (struct sockaddr *)(rtm + 1);
if (sa->sa_family != AF_INET) {
continue;
}
SIGAR_NET_ROUTE_LIST_GROW(routelist);
route = &routelist->data[routelist->number++];
SIGAR_ZERO(route);
route->flags = rtm->rtm_flags;
2005-07-15 11:19:11 +08:00
for (bit=RTA_DST;
bit && ((char *)sa < lim);
bit <<= 1)
{
if ((rtm->rtm_addrs & bit) == 0) {
continue;
}
switch (bit) {
case RTA_DST:
2006-07-05 03:30:04 +08:00
sigar_net_address_set(route->destination,
rt_s_addr(sa));
2005-07-15 11:19:11 +08:00
break;
case RTA_GATEWAY:
if (sa->sa_family == AF_INET) {
2006-07-05 03:30:04 +08:00
sigar_net_address_set(route->gateway,
rt_s_addr(sa));
2005-07-15 11:19:11 +08:00
}
break;
case RTA_NETMASK:
2006-07-05 03:30:04 +08:00
sigar_net_address_set(route->mask,
rt_s_addr(sa));
2005-07-15 11:19:11 +08:00
break;
case RTA_IFA:
break;
}
sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
}
2005-07-13 12:08:36 +08:00
}
free(buf);
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
typedef enum {
IFMSG_ITER_LIST,
IFMSG_ITER_GET
} ifmsg_iter_e;
typedef struct {
const char *name;
ifmsg_iter_e type;
union {
sigar_net_interface_list_t *iflist;
struct if_msghdr *ifm;
} data;
} ifmsg_iter_t;
static int sigar_ifmsg_init(sigar_t *sigar)
{
int mib[] = { CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_IFLIST, 0 };
size_t len;
if (sysctl(mib, NMIB(mib), NULL, &len, NULL, 0) < 0) {
return errno;
}
if (sigar->ifconf_len < len) {
sigar->ifconf_buf = realloc(sigar->ifconf_buf, len);
sigar->ifconf_len = len;
}
if (sysctl(mib, NMIB(mib), sigar->ifconf_buf, &len, NULL, 0) < 0) {
return errno;
}
return SIGAR_OK;
}
static int sigar_ifmsg_iter(sigar_t *sigar, ifmsg_iter_t *iter)
{
char *end = sigar->ifconf_buf + sigar->ifconf_len;
char *ptr = sigar->ifconf_buf;
if (iter->type == IFMSG_ITER_LIST) {
sigar_net_interface_list_create(iter->data.iflist);
}
while (ptr < end) {
char *name;
struct sockaddr_dl *sdl;
struct if_msghdr *ifm = (struct if_msghdr *)ptr;
if (ifm->ifm_type != RTM_IFINFO) {
break;
}
ptr += ifm->ifm_msglen;
while (ptr < end) {
struct if_msghdr *next = (struct if_msghdr *)ptr;
if (next->ifm_type != RTM_NEWADDR) {
break;
}
ptr += next->ifm_msglen;
}
sdl = (struct sockaddr_dl *)(ifm + 1);
if (sdl->sdl_family != AF_LINK) {
continue;
}
if (!((sdl->sdl_type == IFT_ETHER) ||
(sdl->sdl_type == IFT_LOOP)))
{
continue; /* XXX deal w/ other weirdo interfaces */
}
2004-06-22 06:37:04 +08:00
switch (iter->type) {
case IFMSG_ITER_LIST:
SIGAR_NET_IFLIST_GROW(iter->data.iflist);
name = malloc(sdl->sdl_nlen+1);
memcpy(name, sdl->sdl_data, sdl->sdl_nlen+1);
iter->data.iflist->data[iter->data.iflist->number++] = name;
break;
case IFMSG_ITER_GET:
if (strEQ(iter->name, sdl->sdl_data)) {
iter->data.ifm = ifm;
return SIGAR_OK;
}
}
}
switch (iter->type) {
case IFMSG_ITER_LIST:
return SIGAR_OK;
case IFMSG_ITER_GET:
default:
return ENXIO;
}
}
int sigar_net_interface_list_get(sigar_t *sigar,
sigar_net_interface_list_t *iflist)
{
int status;
ifmsg_iter_t iter;
if ((status = sigar_ifmsg_init(sigar)) != SIGAR_OK) {
return status;
}
iter.type = IFMSG_ITER_LIST;
iter.data.iflist = iflist;
return sigar_ifmsg_iter(sigar, &iter);
}
int sigar_net_interface_config_get(sigar_t *sigar, const char *name,
sigar_net_interface_config_t *ifconfig)
{
int sock;
int status;
ifmsg_iter_t iter;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
struct ifreq ifr;
if (!name) {
return sigar_net_interface_config_primary_get(sigar, ifconfig);
}
2004-06-22 06:37:04 +08:00
if (sigar->ifconf_len == 0) {
if ((status = sigar_ifmsg_init(sigar)) != SIGAR_OK) {
return status;
}
}
SIGAR_ZERO(ifconfig);
2004-06-22 06:37:04 +08:00
iter.type = IFMSG_ITER_GET;
iter.name = name;
if ((status = sigar_ifmsg_iter(sigar, &iter)) != SIGAR_OK) {
return status;
}
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return errno;
}
ifm = iter.data.ifm;
SIGAR_SSTRCPY(ifconfig->name, name);
sdl = (struct sockaddr_dl *)(ifm + 1);
2006-07-06 05:12:10 +08:00
sigar_net_address_mac_set(ifconfig->hwaddr,
LLADDR(sdl),
sdl->sdl_alen);
2004-06-22 06:37:04 +08:00
ifconfig->flags = ifm->ifm_flags;
ifconfig->mtu = ifm->ifm_data.ifi_mtu;
ifconfig->metric = ifm->ifm_data.ifi_metric;
SIGAR_SSTRCPY(ifr.ifr_name, name);
#define ifr_s_addr(ifr) \
((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr
if (!ioctl(sock, SIOCGIFADDR, &ifr)) {
sigar_net_address_set(ifconfig->address,
ifr_s_addr(ifr));
2004-06-22 06:37:04 +08:00
}
if (!ioctl(sock, SIOCGIFNETMASK, &ifr)) {
sigar_net_address_set(ifconfig->netmask,
ifr_s_addr(ifr));
2004-06-22 06:37:04 +08:00
}
if (ifconfig->flags & IFF_LOOPBACK) {
sigar_net_address_set(ifconfig->destination,
ifconfig->address.addr.in);
sigar_net_address_set(ifconfig->broadcast, 0);
2005-11-19 04:03:44 +08:00
SIGAR_SSTRCPY(ifconfig->type,
SIGAR_NIC_LOOPBACK);
2004-06-22 06:37:04 +08:00
}
else {
if (!ioctl(sock, SIOCGIFDSTADDR, &ifr)) {
sigar_net_address_set(ifconfig->destination,
ifr_s_addr(ifr));
2004-06-22 06:37:04 +08:00
}
if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
sigar_net_address_set(ifconfig->broadcast,
ifr_s_addr(ifr));
2004-06-22 06:37:04 +08:00
}
2005-11-19 04:03:44 +08:00
SIGAR_SSTRCPY(ifconfig->type,
SIGAR_NIC_ETHERNET);
2004-06-22 06:37:04 +08:00
}
close(sock);
2006-03-05 06:28:09 +08:00
/* XXX can we get a better description like win32? */
SIGAR_SSTRCPY(ifconfig->description,
ifconfig->name);
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
int sigar_net_interface_stat_get(sigar_t *sigar, const char *name,
sigar_net_interface_stat_t *ifstat)
{
int status;
ifmsg_iter_t iter;
struct if_msghdr *ifm;
2005-12-13 02:01:28 +08:00
if ((status = sigar_ifmsg_init(sigar)) != SIGAR_OK) {
return status;
2004-06-22 06:37:04 +08:00
}
iter.type = IFMSG_ITER_GET;
iter.name = name;
if ((status = sigar_ifmsg_iter(sigar, &iter)) != SIGAR_OK) {
return status;
}
ifm = iter.data.ifm;
ifstat->rx_bytes = ifm->ifm_data.ifi_ibytes;
ifstat->rx_packets = ifm->ifm_data.ifi_ipackets;
ifstat->rx_errors = ifm->ifm_data.ifi_ierrors;
ifstat->rx_dropped = ifm->ifm_data.ifi_iqdrops;
ifstat->rx_overruns = SIGAR_FIELD_NOTIMPL;
ifstat->rx_frame = SIGAR_FIELD_NOTIMPL;
2004-06-22 06:37:04 +08:00
ifstat->tx_bytes = ifm->ifm_data.ifi_obytes;
ifstat->tx_packets = ifm->ifm_data.ifi_opackets;
ifstat->tx_errors = ifm->ifm_data.ifi_oerrors;
ifstat->tx_collisions = ifm->ifm_data.ifi_collisions;
ifstat->tx_dropped = SIGAR_FIELD_NOTIMPL;
ifstat->tx_overruns = SIGAR_FIELD_NOTIMPL;
ifstat->tx_carrier = SIGAR_FIELD_NOTIMPL;
2004-06-22 06:37:04 +08:00
2006-03-07 09:26:27 +08:00
ifstat->speed = ifm->ifm_data.ifi_baudrate;
2006-03-07 09:08:23 +08:00
2004-06-22 06:37:04 +08:00
return SIGAR_OK;
}
2007-06-04 08:37:08 +08:00
#ifndef __OpenBSD__
2005-03-15 04:48:55 +08:00
#include <sys/socketvar.h>
#include <netinet/tcp.h>
2005-03-15 04:48:55 +08:00
#include <netinet/in_pcb.h>
#include <netinet/tcp_var.h>
#include <netinet/tcp_fsm.h>
2006-07-02 00:41:14 +08:00
static int net_connection_get(sigar_net_connection_walker_t *walker, int proto)
2005-03-15 04:48:55 +08:00
{
2006-07-02 00:41:14 +08:00
int flags = walker->flags;
2005-03-15 04:48:55 +08:00
int type, istcp = 0;
char *buf;
const char *mibvar;
struct tcpcb *tp = NULL;
struct inpcb *inp;
struct xinpgen *xig, *oxig;
struct xsocket *so;
size_t len;
switch (proto) {
case IPPROTO_TCP:
mibvar = "net.inet.tcp.pcblist";
istcp = 1;
type = SIGAR_NETCONN_TCP;
break;
case IPPROTO_UDP:
mibvar = "net.inet.udp.pcblist";
type = SIGAR_NETCONN_UDP;
break;
default:
mibvar = "net.inet.raw.pcblist";
type = SIGAR_NETCONN_RAW;
break;
}
len = 0;
if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
return errno;
}
if ((buf = malloc(len)) == 0) {
return errno;
}
if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
free(buf);
return errno;
}
oxig = xig = (struct xinpgen *)buf;
for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
xig->xig_len > sizeof(struct xinpgen);
xig = (struct xinpgen *)((char *)xig + xig->xig_len))
{
if (istcp) {
struct xtcpcb *cb = (struct xtcpcb *)xig;
tp = &cb->xt_tp;
inp = &cb->xt_inp;
so = &cb->xt_socket;
}
else {
2006-07-06 08:32:45 +08:00
struct xinpcb *cb = (struct xinpcb *)xig;
2005-03-15 04:48:55 +08:00
inp = &cb->xi_inp;
so = &cb->xi_socket;
}
if (so->xso_protocol != proto) {
continue;
}
if (inp->inp_gencnt > oxig->xig_gen) {
continue;
}
if ((((flags & SIGAR_NETCONN_SERVER) && so->so_qlimit) ||
((flags & SIGAR_NETCONN_CLIENT) && !so->so_qlimit)))
{
2006-07-02 00:41:14 +08:00
sigar_net_connection_t conn;
2005-03-15 04:48:55 +08:00
2006-07-02 00:41:14 +08:00
SIGAR_ZERO(&conn);
2005-03-15 04:48:55 +08:00
if (inp->inp_vflag & INP_IPV6) {
sigar_net_address6_set(conn.local_address,
&inp->in6p_laddr.s6_addr);
sigar_net_address6_set(conn.remote_address,
&inp->in6p_faddr.s6_addr);
}
else {
sigar_net_address_set(conn.local_address,
inp->inp_laddr.s_addr);
sigar_net_address_set(conn.remote_address,
inp->inp_faddr.s_addr);
}
2006-07-02 00:41:14 +08:00
conn.local_port = ntohs(inp->inp_lport);
conn.remote_port = ntohs(inp->inp_fport);
conn.receive_queue = so->so_rcv.sb_cc;
conn.send_queue = so->so_snd.sb_cc;
conn.type = type;
2005-03-15 04:48:55 +08:00
if (!istcp) {
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_UNKNOWN;
if (walker->add_connection(walker, &conn) != SIGAR_OK) {
break;
}
2005-03-15 04:48:55 +08:00
continue;
}
switch (tp->t_state) {
case TCPS_CLOSED:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_CLOSE;
2005-03-15 04:48:55 +08:00
break;
case TCPS_LISTEN:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_LISTEN;
2005-03-15 04:48:55 +08:00
break;
case TCPS_SYN_SENT:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_SYN_SENT;
2005-03-15 04:48:55 +08:00
break;
case TCPS_SYN_RECEIVED:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_SYN_RECV;
2005-03-15 04:48:55 +08:00
break;
case TCPS_ESTABLISHED:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_ESTABLISHED;
2005-03-15 04:48:55 +08:00
break;
case TCPS_CLOSE_WAIT:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_CLOSE_WAIT;
2005-03-15 04:48:55 +08:00
break;
case TCPS_FIN_WAIT_1:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_FIN_WAIT1;
2005-03-15 04:48:55 +08:00
break;
case TCPS_CLOSING:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_CLOSING;
2005-03-15 04:48:55 +08:00
break;
case TCPS_LAST_ACK:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_LAST_ACK;
2005-03-15 04:48:55 +08:00
break;
case TCPS_FIN_WAIT_2:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_FIN_WAIT2;
2005-03-15 04:48:55 +08:00
break;
case TCPS_TIME_WAIT:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_TIME_WAIT;
2005-03-15 04:48:55 +08:00
break;
default:
2006-07-02 00:41:14 +08:00
conn.state = SIGAR_TCP_UNKNOWN;
break;
}
if (walker->add_connection(walker, &conn) != SIGAR_OK) {
2005-03-15 04:48:55 +08:00
break;
}
}
}
free(buf);
return SIGAR_OK;
}
2006-07-02 00:41:14 +08:00
int sigar_net_connection_walk(sigar_net_connection_walker_t *walker)
2005-03-15 04:48:55 +08:00
{
2006-07-02 00:41:14 +08:00
int flags = walker->flags;
2005-03-15 04:48:55 +08:00
int status;
if (flags & SIGAR_NETCONN_TCP) {
2006-07-02 00:41:14 +08:00
status = net_connection_get(walker, IPPROTO_TCP);
2005-03-15 04:48:55 +08:00
if (status != SIGAR_OK) {
return status;
}
}
if (flags & SIGAR_NETCONN_UDP) {
2006-07-02 00:41:14 +08:00
status = net_connection_get(walker, IPPROTO_UDP);
2005-03-15 04:48:55 +08:00
if (status != SIGAR_OK) {
return status;
}
}
return SIGAR_OK;
}
2007-06-04 08:37:08 +08:00
#else
int sigar_net_connection_walk(sigar_net_connection_walker_t *walker)
{
return SIGAR_ENOTIMPL;
}
#endif
2007-07-15 00:01:58 +08:00
SIGAR_DECLARE(int)
sigar_tcp_stat_get(sigar_t *sigar,
sigar_tcp_stat_t *tcpstat)
{
2007-07-15 06:17:22 +08:00
struct tcpstat mib;
int var[4] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_STATS };
size_t len = sizeof(mib);
if (sysctl(var, NMIB(var), &mib, &len, NULL, 0) < 0) {
return errno;
}
tcpstat->max_conn = -1;
tcpstat->active_opens = mib.tcps_connattempt;
tcpstat->passive_opens = mib.tcps_accepts;
tcpstat->attempt_fails = mib.tcps_conndrops;
tcpstat->estab_resets = mib.tcps_drops;
2007-07-15 06:52:12 +08:00
if (sigar_tcp_stat_curr_estab(sigar, tcpstat) != SIGAR_OK) {
tcpstat->curr_estab = -1;
}
2007-07-15 06:17:22 +08:00
tcpstat->in_segs = mib.tcps_rcvtotal;
tcpstat->out_segs = mib.tcps_sndtotal - mib.tcps_sndrexmitpack;
tcpstat->retrans_segs = mib.tcps_sndrexmitpack;
tcpstat->out_rsts = mib.tcps_sndctrl - mib.tcps_closed;
return SIGAR_OK;
2007-07-15 00:01:58 +08:00
}
2007-07-26 14:30:29 +08:00
int sigar_nfs_client_v2_get(sigar_t *sigar,
sigar_nfs_client_v2_t *nfsstat)
{
return SIGAR_ENOTIMPL;
}
int sigar_nfs_server_v2_get(sigar_t *sigar,
sigar_nfs_server_v2_t *nfsstat)
{
return SIGAR_ENOTIMPL;
}
int sigar_nfs_client_v3_get(sigar_t *sigar,
sigar_nfs_client_v3_t *nfsstat)
{
return SIGAR_ENOTIMPL;
}
int sigar_nfs_server_v3_get(sigar_t *sigar,
sigar_nfs_server_v3_t *nfsstat)
{
return SIGAR_ENOTIMPL;
}
2007-06-04 08:37:08 +08:00
#ifdef __FreeBSD__
2005-03-15 04:48:55 +08:00
2005-03-20 13:34:11 +08:00
#define _KERNEL
#include <sys/file.h>
#undef _KERNEL
/* derived from
* /usr/ports/security/pidentd/work/pidentd-3.0.16/src/k_freebsd2.c
*/
int sigar_proc_port_get(sigar_t *sigar, int protocol,
unsigned long port, sigar_pid_t *pid)
{
struct nlist nl[2];
struct inpcbhead tcb;
struct socket *sockp = NULL;
struct kinfo_proc *pinfo;
struct inpcb *head, pcbp;
int i, nentries, status;
if (protocol != SIGAR_NETCONN_TCP) {
return SIGAR_ENOTIMPL;
}
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
2005-03-20 13:34:11 +08:00
}
nl[0].n_name = "_tcb"; /* XXX cache */
2005-03-21 03:08:58 +08:00
nl[1].n_name = "";
2005-03-20 13:34:11 +08:00
if (kvm_nlist(sigar->kmem, nl) < 0) {
return errno;
}
status = kread(sigar, &tcb, sizeof(tcb), nl[0].n_value);
if (status != SIGAR_OK) {
return status;
}
for (head = tcb.lh_first; head != NULL;
head = pcbp.inp_list.le_next)
{
status = kread(sigar, &pcbp, sizeof(pcbp), (long)head);
if (status != SIGAR_OK) {
return status;
}
if (!(pcbp.inp_vflag & INP_IPV4)) {
continue;
}
if (pcbp.inp_fport != 0) {
continue;
}
if (ntohs(pcbp.inp_lport) == port) {
sockp = pcbp.inp_socket;
break;
}
}
if (!sockp) {
return ENOENT;
}
2005-03-21 03:10:14 +08:00
pinfo = kvm_getprocs(sigar->kmem, KERN_PROC_PROC, 0, &nentries);
2005-03-20 13:34:11 +08:00
if (!pinfo) {
return errno;
}
for (i=0; i<nentries; i++) {
2005-03-21 03:10:14 +08:00
if (pinfo[i].KI_FLAG & P_SYSTEM) {
continue;
}
2006-01-03 06:15:24 +08:00
if (pinfo[i].KI_FD) {
2005-03-20 13:34:11 +08:00
struct filedesc pfd;
struct file **ofiles, ofile;
int j, osize;
2006-01-03 06:15:24 +08:00
status = kread(sigar, &pfd, sizeof(pfd), (long)pinfo[i].KI_FD);
2005-03-20 13:34:11 +08:00
if (status != SIGAR_OK) {
return status;
}
osize = pfd.fd_nfiles * sizeof(struct file *);
ofiles = malloc(osize); /* XXX reuse */
if (!ofiles) {
return errno;
}
status = kread(sigar, ofiles, osize, (long)pfd.fd_ofiles);
if (status != SIGAR_OK) {
free(ofiles);
return status;
}
for (j=0; j<pfd.fd_nfiles; j++) {
if (!ofiles[j]) {
continue;
}
status = kread(sigar, &ofile, sizeof(ofile), (long)ofiles[j]);
if (status != SIGAR_OK) {
free(ofiles);
return status;
}
if (ofile.f_count == 0) {
continue;
}
if (ofile.f_type == DTYPE_SOCKET &&
(struct socket *)ofile.f_data == sockp)
{
2006-01-03 06:15:24 +08:00
*pid = pinfo[i].KI_PID;
2005-03-20 13:34:11 +08:00
free(ofiles);
return SIGAR_OK;
}
}
free(ofiles);
}
}
return ENOENT;
}
2005-03-15 04:48:55 +08:00
#else
2005-03-16 10:47:57 +08:00
int sigar_proc_port_get(sigar_t *sigar, int protocol,
unsigned long port, sigar_pid_t *pid)
{
return SIGAR_ENOTIMPL;
}
2005-03-20 13:34:11 +08:00
#endif
int sigar_os_sys_info_get(sigar_t *sigar,
sigar_sys_info_t *sysinfo)
{
#ifdef DARWIN
char *codename = NULL;
long version, version_major, version_minor, version_fix;
2006-09-26 21:32:45 +08:00
SIGAR_SSTRCPY(sysinfo->name, "MacOSX");
2006-09-26 21:32:45 +08:00
SIGAR_SSTRCPY(sysinfo->vendor_name, "Mac OS X");
SIGAR_SSTRCPY(sysinfo->vendor, "Apple");
if (Gestalt(gestaltSystemVersion, &version) == noErr) {
if (version >= 0x00001040) {
Gestalt('sys1' /*gestaltSystemVersionMajor*/, &version_major);
Gestalt('sys2' /*gestaltSystemVersionMinor*/, &version_minor);
Gestalt('sys3' /*gestaltSystemVersionBugFix*/, &version_fix);
}
else {
version_fix = version & 0xf;
version >>= 4;
version_minor = version & 0xf;
version >>= 4;
version_major = version - (version >> 4) * 6;
}
}
else {
return SIGAR_ENOTIMPL;
}
2006-09-26 21:32:45 +08:00
2006-10-01 01:50:23 +08:00
snprintf(sysinfo->vendor_version,
sizeof(sysinfo->vendor_version),
"%ld.%ld",
version_major, version_minor);
snprintf(sysinfo->version,
sizeof(sysinfo->version),
"%s.%ld",
sysinfo->vendor_version, version_fix);
2006-09-26 21:32:45 +08:00
if (version_major == 10) {
switch (version_minor) {
case 2:
codename = "Jaguar";
break;
case 3:
codename = "Panther";
break;
case 4:
codename = "Tiger";
break;
case 5:
codename = "Leopard";
break;
default:
codename = "Unknown";
break;
}
}
2006-09-26 21:32:45 +08:00
else {
return SIGAR_ENOTIMPL;
}
2006-09-26 21:32:45 +08:00
SIGAR_SSTRCPY(sysinfo->vendor_code_name, codename);
2006-10-01 01:14:39 +08:00
snprintf(sysinfo->description,
sizeof(sysinfo->description),
"%s %s",
sysinfo->vendor_name, sysinfo->vendor_code_name);
#else
char *ptr;
2007-06-04 08:37:08 +08:00
#if defined(__FreeBSD__)
2006-10-01 01:13:10 +08:00
SIGAR_SSTRCPY(sysinfo->name, "FreeBSD");
2007-06-04 08:37:08 +08:00
#elif defined(__OpenBSD__)
SIGAR_SSTRCPY(sysinfo->name, "OpenBSD");
#else
SIGAR_SSTRCPY(sysinfo->name, "Unknown");
#endif
2006-10-01 01:13:10 +08:00
SIGAR_SSTRCPY(sysinfo->vendor_name, sysinfo->name);
SIGAR_SSTRCPY(sysinfo->vendor, sysinfo->name);
SIGAR_SSTRCPY(sysinfo->vendor_version,
sysinfo->version);
if ((ptr = strstr(sysinfo->vendor_version, "-"))) {
/* STABLE, RELEASE, CURRENT */
*ptr++ = '\0';
SIGAR_SSTRCPY(sysinfo->vendor_code_name, ptr);
}
2006-10-01 01:13:10 +08:00
snprintf(sysinfo->description,
sizeof(sysinfo->description),
"%s %s",
sysinfo->name, sysinfo->version);
#endif
return SIGAR_OK;
}