add Pdh.getFormattedValue method

This commit is contained in:
Doug MacEachern 2005-06-14 23:51:06 +00:00
parent 30a2812d1a
commit 241b1066e6
3 changed files with 56 additions and 14 deletions

View File

@ -127,12 +127,13 @@ JNIEXPORT void SIGAR_JNI(win32_Pdh_pdhRemoveCounter)
} }
JNIEXPORT jdouble SIGAR_JNI(win32_Pdh_pdhGetSingleValue) 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; HCOUNTER h_counter = (HCOUNTER)counter;
HQUERY h_query = (HQUERY)query; HQUERY h_query = (HQUERY)query;
PDH_STATUS status; PDH_STATUS status;
PDH_RAW_COUNTER raw_value; PDH_RAW_COUNTER raw_value;
PDH_FMT_COUNTERVALUE fmt_value;
DWORD type; DWORD type;
status = PdhCollectQueryData(h_query); status = PdhCollectQueryData(h_query);
@ -142,15 +143,28 @@ JNIEXPORT jdouble SIGAR_JNI(win32_Pdh_pdhGetSingleValue)
return 0; return 0;
} }
if (fmt) {
status = PdhGetFormattedCounterValue(h_counter,
PDH_FMT_DOUBLE,
(LPDWORD)NULL,
&fmt_value);
}
else {
status = PdhGetRawCounterValue(h_counter, &type, &raw_value); status = PdhGetRawCounterValue(h_counter, &type, &raw_value);
}
if (status != ERROR_SUCCESS) { if (status != ERROR_SUCCESS) {
win32_throw_exception(env, get_error_message(status)); win32_throw_exception(env, get_error_message(status));
return 0; return 0;
} }
if (fmt) {
return fmt_value.doubleValue;
}
else {
return (jdouble)raw_value.FirstValue; return (jdouble)raw_value.FirstValue;
} }
}
JNIEXPORT jobjectArray SIGAR_JNI(win32_Pdh_pdhGetInstances) JNIEXPORT jobjectArray SIGAR_JNI(win32_Pdh_pdhGetInstances)
(JNIEnv *env, jclass cur, jstring cp) (JNIEnv *env, jclass cur, jstring cp)

View File

@ -40,15 +40,32 @@ public class Pdh extends Win32Bindings {
} }
} }
public double getSingleValue() throws Win32Exception {
return getSingleValue(false);
}
public double getSingleValue(String cp) throws Win32Exception { 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) { if (this.hostname != null) {
pdhConnectMachine(this.hostname); pdhConnectMachine(this.hostname);
} }
setCounterPath(cp); setCounterPath(cp);
return getSingleValue(); return getSingleValue(format);
} }
public double getSingleValue() throws Win32Exception { private double getSingleValue(boolean format) throws Win32Exception {
if (h_counter != -1l) { if (h_counter != -1l) {
pdhRemoveCounter(h_counter); pdhRemoveCounter(h_counter);
h_counter = -1l; h_counter = -1l;
@ -56,7 +73,7 @@ public class Pdh extends Win32Bindings {
h_counter = pdhAddCounter(h_query, getCounterPath()); 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 { 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) private static final native void pdhRemoveCounter(long counter)
throws Win32Exception; throws Win32Exception;
private static final native double pdhGetSingleValue(long query, private static final native double pdhGetSingleValue(long query,
long counter) long counter,
boolean fmt)
throws Win32Exception; throws Win32Exception;
private static final native String[] pdhGetInstances(String cp) private static final native String[] pdhGetInstances(String cp)
throws Win32Exception; throws Win32Exception;

View File

@ -1,21 +1,31 @@
package net.hyperic.sigar.win32.test; package net.hyperic.sigar.win32.test;
import junit.framework.TestCase; import net.hyperic.sigar.test.SigarTestCase;
import net.hyperic.sigar.win32.Pdh; import net.hyperic.sigar.win32.Pdh;
public class TestPdh extends TestCase { public class TestPdh extends SigarTestCase {
public TestPdh(String name) { public TestPdh(String name) {
super(name); super(name);
} }
public void testPdhSingleValue() throws Exception { private void getValue(String key) throws Exception {
Pdh pdh = new Pdh(); 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<keys.length; i++) {
getValue(keys[i]);
}
} }
public void testPdh () throws Exception { public void testPdh () throws Exception {