From 7905904cb6961afd69d54ea7675e51bc413e96dc Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 31 Mar 2010 13:17:02 -0700 Subject: [PATCH] (SIGAR-207) Add Pdh.getDescription method --- bindings/java/src/jni/win32/pdh.c | 32 +++++++++++++++++++ .../java/src/org/hyperic/sigar/win32/Pdh.java | 13 ++++++++ .../org/hyperic/sigar/win32/test/TestPdh.java | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/bindings/java/src/jni/win32/pdh.c b/bindings/java/src/jni/win32/pdh.c index b2f16ae7..4b3a9045 100644 --- a/bindings/java/src/jni/win32/pdh.c +++ b/bindings/java/src/jni/win32/pdh.c @@ -222,6 +222,38 @@ JNIEXPORT jdouble SIGAR_JNI(win32_Pdh_pdhGetValue) } } +JNIEXPORT jstring SIGAR_JNI(win32_Pdh_pdhGetDescription) +(JNIEnv *env, jclass cur, jlong counter) +{ + HCOUNTER h_counter = (HCOUNTER)counter; + PDH_COUNTER_INFO *info = NULL; + jstring retval = NULL; + DWORD size = 0; + PDH_STATUS status; + + status = PdhGetCounterInfo(h_counter, TRUE, &size, NULL); + if (status != PDH_MORE_DATA) { + win32_throw_exception(env, get_error_message(status)); + return NULL; + } + + info = malloc(size); + + status = PdhGetCounterInfo(h_counter, 1, &size, info); + if (status == ERROR_SUCCESS) { + if (info->szExplainText) { + retval = JENV->NewString(env, info->szExplainText, + lstrlen(info->szExplainText)); + } + } + else { + win32_throw_exception(env, get_error_message(status)); + } + + free(info); + return retval; +} + JNIEXPORT jobjectArray SIGAR_JNI(win32_Pdh_pdhGetInstances) (JNIEnv *env, jclass cur, jstring cp) { diff --git a/bindings/java/src/org/hyperic/sigar/win32/Pdh.java b/bindings/java/src/org/hyperic/sigar/win32/Pdh.java index 110d9189..cc877742 100644 --- a/bindings/java/src/org/hyperic/sigar/win32/Pdh.java +++ b/bindings/java/src/org/hyperic/sigar/win32/Pdh.java @@ -292,6 +292,17 @@ public class Pdh extends Win32 { } } + /* PdhCounterInfo.ExplainText */ + public String getDescription(String path) throws Win32Exception { + long counter = + pdhAddCounter(this.query, translate(path)); + try { + return pdhGetDescription(counter); + } finally { + pdhRemoveCounter(counter); + } + } + public static String[] getInstances(String path) throws Win32Exception { return pdhGetInstances(getCounterName(path)); } @@ -319,6 +330,8 @@ public class Pdh extends Win32 { long counter, boolean fmt) throws Win32Exception; + private static final native String pdhGetDescription(long counter) + throws Win32Exception; private static final native String[] pdhGetInstances(String path) throws Win32Exception; private static final native String[] pdhGetKeys(String path) diff --git a/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java b/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java index a6f23c1e..a00643cd 100755 --- a/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java +++ b/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java @@ -33,7 +33,7 @@ public class TestPdh extends SigarTestCase { private void getValue(String key) throws Exception { Pdh pdh = new Pdh(); - traceln(key); + traceln(key + ": " + pdh.getDescription(key)); assertGtEqZeroTrace("raw", (long)pdh.getRawValue(key)); assertGtEqZeroTrace("fmt",