(SIGAR-207) Add Pdh.getCounterType method

This commit is contained in:
Doug MacEachern 2010-03-31 14:54:25 -07:00
parent 7905904cb6
commit 2dc981dfa8
3 changed files with 43 additions and 0 deletions

View File

@ -254,6 +254,23 @@ JNIEXPORT jstring SIGAR_JNI(win32_Pdh_pdhGetDescription)
return retval; return retval;
} }
JNIEXPORT jlong SIGAR_JNI(win32_Pdh_pdhGetCounterType)
(JNIEnv *env, jclass cur, jlong counter)
{
HCOUNTER h_counter = (HCOUNTER)counter;
PDH_COUNTER_INFO info;
DWORD size = sizeof(info);
PDH_STATUS status;
status = PdhGetCounterInfo(h_counter, FALSE, &size, &info);
if (status != ERROR_SUCCESS) {
win32_throw_exception(env, get_error_message(status));
return -1;
}
return info.dwType;
}
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

@ -63,6 +63,15 @@ public class Pdh extends Win32 {
public static final String PERFLIB_KEY = public static final String PERFLIB_KEY =
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib"; "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib";
//see winperf.h
public static final long PERF_TYPE_NUMBER = 0x00000000; // a number (not a counter)
public static final long PERF_TYPE_COUNTER = 0x00000400; // an increasing numeric value
public static final long PERF_TYPE_TEXT = 0x00000800; // a text field
public static final long PERF_TYPE_ZERO = 0x00000C00; // displays a zero
private long query = -1l; // Handle to the query private long query = -1l; // Handle to the query
private String hostname = null; private String hostname = null;
private static Map counters = null; private static Map counters = null;
@ -303,6 +312,16 @@ public class Pdh extends Win32 {
} }
} }
public long getCounterType(String path) throws Win32Exception {
long counter =
pdhAddCounter(this.query, translate(path));
try {
return pdhGetCounterType(counter);
} finally {
pdhRemoveCounter(counter);
}
}
public static String[] getInstances(String path) throws Win32Exception { public static String[] getInstances(String path) throws Win32Exception {
return pdhGetInstances(getCounterName(path)); return pdhGetInstances(getCounterName(path));
} }
@ -332,6 +351,8 @@ public class Pdh extends Win32 {
throws Win32Exception; throws Win32Exception;
private static final native String pdhGetDescription(long counter) private static final native String pdhGetDescription(long counter)
throws Win32Exception; throws Win32Exception;
private static final native long pdhGetCounterType(long counter)
throws Win32Exception;
private static final native String[] pdhGetInstances(String path) private static final native String[] pdhGetInstances(String path)
throws Win32Exception; throws Win32Exception;
private static final native String[] pdhGetKeys(String path) private static final native String[] pdhGetKeys(String path)

View File

@ -30,10 +30,15 @@ public class TestPdh extends SigarTestCase {
super(name); super(name);
} }
private static boolean isCounter(long type) {
return (type & Pdh.PERF_TYPE_COUNTER) == Pdh.PERF_TYPE_COUNTER;
}
private void getValue(String key) throws Exception { private void getValue(String key) throws Exception {
Pdh pdh = new Pdh(); Pdh pdh = new Pdh();
traceln(key + ": " + pdh.getDescription(key)); traceln(key + ": " + pdh.getDescription(key));
traceln("counter=" + isCounter(pdh.getCounterType(key)));
assertGtEqZeroTrace("raw", assertGtEqZeroTrace("raw",
(long)pdh.getRawValue(key)); (long)pdh.getRawValue(key));
assertGtEqZeroTrace("fmt", assertGtEqZeroTrace("fmt",