mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
Revert back to ps callout.
Going back to ps callout on MacOSX to avoid cgo for pending changes to release process.
This commit is contained in:
@@ -1,93 +1,23 @@
|
||||
// Copyright 2015 Apcera Inc. All rights reserved.
|
||||
// Copyright 2015-2016 Apcera Inc. All rights reserved.
|
||||
|
||||
package server
|
||||
|
||||
/*
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <mach/task.h>
|
||||
#include <mach/mach_init.h>
|
||||
#include <mach/mach_port.h>
|
||||
#include <mach/thread_act.h>
|
||||
#include <mach/vm_map.h>
|
||||
#include <libproc.h>
|
||||
|
||||
#define TH_USAGE_SCALE 1000
|
||||
|
||||
int getusage(double *pcpu, unsigned int *rss, unsigned int *vss)
|
||||
{
|
||||
struct task_basic_info t_info;
|
||||
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
|
||||
task_t task = MACH_PORT_NULL;
|
||||
int ret;
|
||||
|
||||
*rss = *vss = *pcpu = 0;
|
||||
ret = task_for_pid(mach_task_self(), getpid(), &task);
|
||||
if (ret != KERN_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
|
||||
*rss = t_info.resident_size;
|
||||
*vss = t_info.virtual_size;
|
||||
|
||||
// Percent CPU Calculations
|
||||
|
||||
thread_port_array_t thread_list;
|
||||
unsigned int thread_count;
|
||||
|
||||
ret = task_threads(task, &thread_list, &thread_count);
|
||||
if (ret != KERN_SUCCESS) {
|
||||
mach_port_deallocate(mach_task_self(), task);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int j;
|
||||
int cpu_usage;
|
||||
unsigned int thread_info_count = THREAD_BASIC_INFO_COUNT;
|
||||
|
||||
cpu_usage = 0;
|
||||
|
||||
for (j = 0; j < thread_count; j++) {
|
||||
int tstate;
|
||||
int error;
|
||||
struct thread_basic_info tb;
|
||||
|
||||
thread_info_count = THREAD_BASIC_INFO_COUNT;
|
||||
error = thread_info(thread_list[j], THREAD_BASIC_INFO,
|
||||
(thread_info_t)&tb, &thread_info_count);
|
||||
if (error != KERN_SUCCESS) {
|
||||
ret=1;
|
||||
}
|
||||
cpu_usage += tb.cpu_usage;
|
||||
}
|
||||
vm_deallocate(mach_task_self(), (vm_address_t)(thread_list),
|
||||
sizeof(thread_list) * thread_count);
|
||||
mach_port_deallocate(mach_task_self(), task);
|
||||
|
||||
*pcpu = ((double)cpu_usage) * 100.0 / ((double)TH_USAGE_SCALE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func procUsage(pcpu *float64, rss, vss *int64) error {
|
||||
var r, v C.uint
|
||||
var c C.double
|
||||
|
||||
if ret := C.getusage(&c, &r, &v); ret != 0 {
|
||||
return syscall.Errno(ret)
|
||||
pidStr := fmt.Sprintf("%d", os.Getpid())
|
||||
out, err := exec.Command("ps", "o", "pcpu=,rss=,vsz=", "-p", pidStr).Output()
|
||||
if err != nil {
|
||||
*rss, *vss = -1, -1
|
||||
return errors.New(fmt.Sprintf("ps call failed:%v", err))
|
||||
}
|
||||
*rss = int64(r)
|
||||
*vss = int64(v)
|
||||
*pcpu = float64(c)
|
||||
fmt.Sscanf(string(out), "%f %d %d", pcpu, rss, vss)
|
||||
*rss *= 1024 // 1k blocks, want bytes.
|
||||
*vss *= 1024 // 1k blocks, want bytes.
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user