From 241b1066e67e777605e9325850b42c18fe1a7581 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 14 Jun 2005 23:51:06 +0000 Subject: [PATCH] add Pdh.getFormattedValue method --- bindings/java/src/jni/win32/pdh.c | 20 +++++++++++--- .../java/src/net/hyperic/sigar/win32/Pdh.java | 26 ++++++++++++++++--- .../net/hyperic/sigar/win32/test/TestPdh.java | 24 ++++++++++++----- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/bindings/java/src/jni/win32/pdh.c b/bindings/java/src/jni/win32/pdh.c index 36daf05c..4c7ca6a6 100644 --- a/bindings/java/src/jni/win32/pdh.c +++ b/bindings/java/src/jni/win32/pdh.c @@ -127,12 +127,13 @@ JNIEXPORT void SIGAR_JNI(win32_Pdh_pdhRemoveCounter) } JNIEXPORT jdouble SIGAR_JNI(win32_Pdh_pdhGetSingleValue) -(JNIEnv *env, jclass cur, jlong query, jlong counter) +(JNIEnv *env, jclass cur, jlong query, jlong counter, jboolean fmt) { HCOUNTER h_counter = (HCOUNTER)counter; HQUERY h_query = (HQUERY)query; PDH_STATUS status; PDH_RAW_COUNTER raw_value; + PDH_FMT_COUNTERVALUE fmt_value; DWORD type; status = PdhCollectQueryData(h_query); @@ -142,14 +143,27 @@ JNIEXPORT jdouble SIGAR_JNI(win32_Pdh_pdhGetSingleValue) return 0; } - status = PdhGetRawCounterValue(h_counter, &type, &raw_value); + if (fmt) { + status = PdhGetFormattedCounterValue(h_counter, + PDH_FMT_DOUBLE, + (LPDWORD)NULL, + &fmt_value); + } + else { + status = PdhGetRawCounterValue(h_counter, &type, &raw_value); + } if (status != ERROR_SUCCESS) { win32_throw_exception(env, get_error_message(status)); return 0; } - return (jdouble)raw_value.FirstValue; + if (fmt) { + return fmt_value.doubleValue; + } + else { + return (jdouble)raw_value.FirstValue; + } } JNIEXPORT jobjectArray SIGAR_JNI(win32_Pdh_pdhGetInstances) diff --git a/bindings/java/src/net/hyperic/sigar/win32/Pdh.java b/bindings/java/src/net/hyperic/sigar/win32/Pdh.java index 985aca79..de763e34 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/Pdh.java +++ b/bindings/java/src/net/hyperic/sigar/win32/Pdh.java @@ -40,15 +40,32 @@ public class Pdh extends Win32Bindings { } } + public double getSingleValue() throws Win32Exception { + return getSingleValue(false); + } + public double getSingleValue(String cp) throws Win32Exception { + return getSingleValue(cp, false); + } + + public double getFormattedValue() throws Win32Exception { + return getSingleValue(true); + } + + public double getFormattedValue(String cp) throws Win32Exception { + return getSingleValue(cp, true); + } + + private double getSingleValue(String cp, boolean format) + throws Win32Exception { if (this.hostname != null) { pdhConnectMachine(this.hostname); } setCounterPath(cp); - return getSingleValue(); + return getSingleValue(format); } - public double getSingleValue() throws Win32Exception { + private double getSingleValue(boolean format) throws Win32Exception { if (h_counter != -1l) { pdhRemoveCounter(h_counter); h_counter = -1l; @@ -56,7 +73,7 @@ public class Pdh extends Win32Bindings { h_counter = pdhAddCounter(h_query, getCounterPath()); - return pdhGetSingleValue(h_query, h_counter); + return pdhGetSingleValue(h_query, h_counter, format); } public static String[] getInstances(String cp) throws Win32Exception { @@ -89,7 +106,8 @@ public class Pdh extends Win32Bindings { private static final native void pdhRemoveCounter(long counter) throws Win32Exception; private static final native double pdhGetSingleValue(long query, - long counter) + long counter, + boolean fmt) throws Win32Exception; private static final native String[] pdhGetInstances(String cp) throws Win32Exception; diff --git a/bindings/java/src/net/hyperic/sigar/win32/test/TestPdh.java b/bindings/java/src/net/hyperic/sigar/win32/test/TestPdh.java index f1b82d37..581cdeac 100644 --- a/bindings/java/src/net/hyperic/sigar/win32/test/TestPdh.java +++ b/bindings/java/src/net/hyperic/sigar/win32/test/TestPdh.java @@ -1,21 +1,31 @@ package net.hyperic.sigar.win32.test; -import junit.framework.TestCase; +import net.hyperic.sigar.test.SigarTestCase; import net.hyperic.sigar.win32.Pdh; -public class TestPdh extends TestCase { +public class TestPdh extends SigarTestCase { public TestPdh(String name) { super(name); } - public void testPdhSingleValue() throws Exception { - + private void getValue(String key) throws Exception { Pdh pdh = new Pdh(); - String key = "\\Memory\\Available Bytes"; - double val = pdh.getSingleValue(key); - assertTrue(val > 0); + assertGtZeroTrace("raw..." + key, + (long)pdh.getSingleValue(key)); + assertGtZeroTrace("fmt..." + key, + (long)pdh.getFormattedValue(key)); + } + + public void testGetValue() throws Exception { + String[] keys = { + "\\Memory\\Available Bytes", + "\\Memory\\Pages/sec", + }; + for (int i=0; i