fold java CurrentProcessSummary metric into sigar_proc_stat_t

This commit is contained in:
Doug MacEachern 2007-04-14 22:28:25 +00:00
parent 578b7607db
commit 9a04924df3
5 changed files with 79 additions and 98 deletions

View File

@ -381,6 +381,31 @@ my %classes = (
desc => 'Total number of processes',
plat => '*'
},
{
name => 'idle', type => 'Long',
desc => 'Total number of processes in idle state',
plat => '*'
},
{
name => 'running', type => 'Long',
desc => 'Total number of processes in run state',
plat => '*'
},
{
name => 'sleeping', type => 'Long',
desc => 'Total number of processes in sleep state',
plat => '*'
},
{
name => 'stopped', type => 'Long',
desc => 'Total number of processes in stop state',
plat => '*'
},
{
name => 'zombie', type => 'Long',
desc => 'Total number of processes in zombie state',
plat => '*'
},
],
ProcExe => [
{

View File

@ -1,95 +0,0 @@
/*
* 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.
*/
package org.hyperic.sigar;
/**
* This class provides a summary of current process states.
* @see org.hyperic.sigar.cmd.Top
*/
public class CurrentProcessSummary {
private int total=0, sleeping=0, running=0, zombie=0, stopped=0;
private CurrentProcessSummary() { }
public static CurrentProcessSummary get(SigarProxy sigar)
throws SigarException {
long[] pids = sigar.getProcList();
CurrentProcessSummary summary =
new CurrentProcessSummary();
for (int i=0; i<pids.length; i++) {
ProcState state;
try {
state = sigar.getProcState(pids[i]);
} catch (SigarException e) {
continue; //e.g. stale pid
}
summary.total++;
switch (state.getState()) {
case ProcState.RUN:
summary.running++;
break;
case ProcState.STOP:
summary.stopped++;
break;
case ProcState.SLEEP:
summary.sleeping++;
break;
case ProcState.ZOMBIE:
summary.zombie++;
break;
}
}
return summary;
}
public int getTotal() {
return this.total;
}
public int getSleeping() {
return this.sleeping;
}
public int getRunning() {
return this.running;
}
public int getZombie() {
return this.zombie;
}
public int getStopped() {
return this.stopped;
}
public String toString() {
return
this.total + " processes: " +
this.sleeping + " sleeping, " +
this.running + " running, " +
this.zombie + " zombie, " +
this.stopped + " stopped";
}
}

View File

@ -26,7 +26,7 @@ import org.hyperic.sigar.SigarProxy;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.SigarProxyCache;
import org.hyperic.sigar.ProcCpu;
import org.hyperic.sigar.CurrentProcessSummary;
import org.hyperic.sigar.ProcStat;
/**
* Display system resource utilization summaries and process information.
@ -45,6 +45,15 @@ public class Top {
private static final String HEADER =
"PID\tUSER\tSTIME\tSIZE\tRSS\tSHARE\tSTATE\tTIME\t%CPU\tCOMMAND";
private static String toString(ProcStat stat) {
return
stat.getTotal() + " processes: " +
stat.getSleeping() + " sleeping, " +
stat.getRunning() + " running, " +
stat.getZombie() + " zombie, " +
stat.getStopped() + " stopped";
}
public static void main(String[] args) throws Exception {
Sigar sigarImpl = new Sigar();
@ -56,7 +65,7 @@ public class Top {
System.out.println(Uptime.getInfo(sigar));
System.out.println(CurrentProcessSummary.get(sigar));
System.out.println(toString(sigar.getProcStat()));
System.out.println(sigar.getCpuPerc());

View File

@ -244,6 +244,11 @@ SIGAR_DECLARE(int) sigar_proc_list_destroy(sigar_t *sigar,
typedef struct {
sigar_uint64_t total;
sigar_uint64_t sleeping;
sigar_uint64_t running;
sigar_uint64_t zombie;
sigar_uint64_t stopped;
sigar_uint64_t idle;
} sigar_proc_stat_t;
SIGAR_DECLARE(int) sigar_proc_stat_get(sigar_t *sigar,
@ -308,6 +313,12 @@ typedef struct {
SIGAR_DECLARE(int) sigar_proc_cpu_get(sigar_t *sigar, sigar_pid_t pid,
sigar_proc_cpu_t *proccpu);
#define SIGAR_PROC_STATE_SLEEP 'S'
#define SIGAR_PROC_STATE_RUN 'R'
#define SIGAR_PROC_STATE_STOP 'T'
#define SIGAR_PROC_STATE_ZOMBIE 'Z'
#define SIGAR_PROC_STATE_IDLE 'D'
#define SIGAR_PROC_NAME_LEN 128
typedef struct {

View File

@ -137,15 +137,46 @@ SIGAR_DECLARE(int) sigar_proc_cpu_get(sigar_t *sigar, sigar_pid_t pid,
SIGAR_DECLARE(int) sigar_proc_stat_get(sigar_t *sigar,
sigar_proc_stat_t *procstat)
{
int status;
int status, i;
sigar_proc_list_t proclist;
SIGAR_ZERO(procstat);
if ((status = sigar_proc_list_get(sigar, &proclist)) != SIGAR_OK) {
return status;
}
procstat->total = proclist.number;
for (i=0; i<proclist.number; i++) {
sigar_proc_state_t state;
status = sigar_proc_state_get(sigar, proclist.data[i], &state);
if (status != SIGAR_OK) {
continue;
}
switch (state.state) {
case SIGAR_PROC_STATE_IDLE:
procstat->idle++;
break;
case SIGAR_PROC_STATE_RUN:
procstat->running++;
break;
case SIGAR_PROC_STATE_SLEEP:
procstat->sleeping++;
break;
case SIGAR_PROC_STATE_STOP:
procstat->stopped++;
break;
case SIGAR_PROC_STATE_ZOMBIE:
procstat->zombie++;
break;
default:
break;
}
}
sigar_proc_list_destroy(sigar, &proclist);
return SIGAR_OK;