示例#1
0
func fdusage() (int, error) {
	pid := C.int(os.Getpid())
	// Query for a rough estimate on the amout of data that
	// proc_pidinfo will return.
	rlen, err := C.proc_pidinfo(pid, C.PROC_PIDLISTFDS, 0, nil, 0)
	if rlen <= 0 {
		return 0, err
	}
	// Load the list of file descriptors. We don't actually care about
	// the content, only about the size. Since the number of fds can
	// change while we're reading them, the loop enlarges the buffer
	// until proc_pidinfo says the result fitted.
	var buf unsafe.Pointer
	defer func() {
		if buf != nil {
			C.free(buf)
		}
	}()
	for buflen := rlen; ; buflen *= 2 {
		buf, err = C.reallocf(buf, C.size_t(buflen))
		if buf == nil {
			return 0, err
		}
		rlen, err = C.proc_pidinfo(pid, C.PROC_PIDLISTFDS, 0, buf, buflen)
		if rlen <= 0 {
			return 0, err
		} else if rlen == buflen {
			continue
		}
		return int(rlen / C.PROC_PIDLISTFD_SIZE), nil
	}
	panic("unreachable")
}
示例#2
0
func getProcCurrentDir(pid int) (string, int) {
	var pathInfo PathInfo
	pathInfoPtr := unsafe.Pointer(&pathInfo)
	result := int(C.proc_pidinfo(C.int(pid), PROC_PIDVNODEPATHINFO, 0, pathInfoPtr,
		C.int(unsafe.Sizeof(pathInfo))))
	return C.GoString(&pathInfo.cdir.vip_path[0]), result
}
示例#3
0
func task_info(pid int, info *C.struct_proc_taskallinfo) error {
	size := C.int(unsafe.Sizeof(*info))
	ptr := unsafe.Pointer(info)

	n := C.proc_pidinfo(C.int(pid), C.PROC_PIDTASKALLINFO, 0, ptr, size)
	if n != size {
		return syscall.ENOMEM
	}

	return nil
}
示例#4
0
func task_info(pid int, info *C.struct_proc_taskallinfo) error {
	size := C.int(unsafe.Sizeof(*info))
	ptr := unsafe.Pointer(info)

	n := C.proc_pidinfo(C.int(pid), C.PROC_PIDTASKALLINFO, 0, ptr, size)
	if n != size {
		return fmt.Errorf("Could not read process info for pid %d", pid)
	}

	return nil
}
示例#5
0
func threadInfoHandler(info *processInfo) *processInfo {
	taskInfo := C.malloc(C.size_t(C.PROC_PIDTHREADINFO_SIZE))
	defer C.free(taskInfo)
	size := C.proc_pidinfo(C.int(info.pid), C.PROC_PIDTHREADINFO, 0, taskInfo, C.int(C.PROC_PIDTHREADINFO_SIZE))

	if size < C.int(unsafe.Sizeof(taskInfo)) {
		return info
	}

	casted := (*C.struct_proc_threadinfo)(taskInfo)
	//info.threadUserTime = int64(casted.pth_user_time) // nanoseconds
	//info.threadSystemTime = int64(casted.pth_system_time) // nanoseconds
	info.cpuUsage = int32(casted.pth_cpu_usage) // percent

	return info
}
示例#6
0
func taskInfoHandler(info *processInfo) *processInfo {
	taskInfo := C.malloc(C.size_t(C.PROC_PIDTASKINFO_SIZE))
	defer C.free(taskInfo)
	size := C.proc_pidinfo(C.int(info.pid), C.PROC_PIDTASKINFO, 0, taskInfo, C.int(C.PROC_PIDTASKINFO_SIZE))

	// checking size as described in http://goo.gl/Lta0IO
	if size < C.int(unsafe.Sizeof(taskInfo)) {
		return info
	}

	casted := (*C.struct_proc_taskinfo)(taskInfo)
	info.virtualSize = int64(casted.pti_virtual_size) // bytes
	//info.taskUserTime = int64(casted.pti_total_user) // nanoseconds
	//info.taskSystemTime = int64(casted.pti_total_system) // nanoseconds

	return info
}