From fb61a2ebb0492367b4de2eab13715d9a6928d1e8 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 22 Nov 2005 21:45:45 +0000 Subject: [PATCH] add proc_state.threads metric --- bindings/java/src/jni/generate.pl | 5 ++++ .../net/hyperic/sigar/test/TestProcState.java | 3 ++- include/sigar.h | 1 + src/os/linux/linux_sigar.c | 25 +++++++++++++++++++ src/os/solaris/solaris_sigar.c | 1 + 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/bindings/java/src/jni/generate.pl b/bindings/java/src/jni/generate.pl index 6e40d4f8..85afbf8d 100644 --- a/bindings/java/src/jni/generate.pl +++ b/bindings/java/src/jni/generate.pl @@ -348,6 +348,11 @@ my %classes = ( desc => 'Kernel scheduling priority of process', plat => 'DFHLSW' }, + { + name => 'threads', type => 'Long', + desc => 'Number of active threads', + plat => '' + }, ], ProcFd => [ { diff --git a/bindings/java/src/net/hyperic/sigar/test/TestProcState.java b/bindings/java/src/net/hyperic/sigar/test/TestProcState.java index 8c8231ac..c20d6565 100644 --- a/bindings/java/src/net/hyperic/sigar/test/TestProcState.java +++ b/bindings/java/src/net/hyperic/sigar/test/TestProcState.java @@ -21,7 +21,8 @@ public class TestProcState extends SigarTestCase { ProcState procState = sigar.getProcState(sigar.getPid()); char state = procState.getState(); traceln(procState.getName() + "=" + state); - + traceln("threads=" + procState.getThreads()); + assertTrue((state == 'R') || (state == 'S')); assertTrue(procState.getName().indexOf("java") != -1); diff --git a/include/sigar.h b/include/sigar.h index 8a08a578..34ec951d 100644 --- a/include/sigar.h +++ b/include/sigar.h @@ -255,6 +255,7 @@ typedef struct { int tty; int priority; int nice; + int threads; } sigar_proc_state_t; SIGAR_DECLARE(int) sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid, diff --git a/src/os/linux/linux_sigar.c b/src/os/linux/linux_sigar.c index 7f3ff415..84504d3c 100644 --- a/src/os/linux/linux_sigar.c +++ b/src/os/linux/linux_sigar.c @@ -696,6 +696,29 @@ int sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid, return SIGAR_OK; } +static int proc_status_get(sigar_t *sigar, sigar_pid_t pid, + sigar_proc_state_t *procstate) +{ + char buffer[BUFSIZ], *ptr; + int status = SIGAR_PROC_FILE2STR(buffer, pid, PROC_PSTATUS); + + if (status != SIGAR_OK) { + return status; + } + + ptr = strstr(buffer, "\nThreads:"); + if (ptr) { + /* 2.6+ kernel only */ + ptr = sigar_skip_token(ptr); + procstate->threads = sigar_strtoul(ptr); + } + else { + procstate->threads = SIGAR_FIELD_NOTIMPL; + } + + return SIGAR_OK; +} + int sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid, sigar_proc_state_t *procstate) { @@ -714,6 +737,8 @@ int sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid, procstate->priority = pstat->priority; procstate->nice = pstat->nice; + proc_status_get(sigar, pid, procstate); + return SIGAR_OK; } diff --git a/src/os/solaris/solaris_sigar.c b/src/os/solaris/solaris_sigar.c index 4a02ad86..a727a4cd 100644 --- a/src/os/solaris/solaris_sigar.c +++ b/src/os/solaris/solaris_sigar.c @@ -660,6 +660,7 @@ int sigar_proc_state_get(sigar_t *sigar, sigar_pid_t pid, procstate->tty = pinfo->pr_ttydev; procstate->priority = pinfo->pr_lwp.pr_pri; procstate->nice = pinfo->pr_lwp.pr_nice - NZERO; + procstate->threads = pinfo->pr_nlwp; switch (pinfo->pr_lwp.pr_state) { case SONPROC: