add Pdh.getPerflibCounterMap()

This commit is contained in:
Doug MacEachern 2007-03-03 21:37:44 +00:00
parent 8f70da2dd3
commit fc5d1b1823
2 changed files with 70 additions and 0 deletions

View File

@ -19,11 +19,16 @@
package org.hyperic.sigar.win32;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Pdh extends Win32 {
public static final String PERFLIB_KEY =
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib";
private long query = -1l; // Handle to the query
private String hostname = null;
private Properties names;
@ -57,6 +62,52 @@ public class Pdh extends Win32 {
}
}
private static class PerflibCounterMap extends ArrayList {
private Map map = new HashMap();
private String index = null;
//called by RegistryKey.getMultiStringValue
//format description see: http://support.microsoft.com/kb/q287159/
public boolean add(Object o) {
if (index == null) {
index = (String)o;
return true;
}
//name -> index
this.map.put(o, index);
index = null; //reset
return true;
}
}
public static Map getEnglishPerflibCounterMap()
throws Win32Exception {
LocaleInfo locale =
new LocaleInfo(LocaleInfo.LANG_ENGLISH);
return getPerflibCounterMap(locale);
}
public static Map getPerflibCounterMap(LocaleInfo locale)
throws Win32Exception {
String path =
PERFLIB_KEY + "\\" + locale.getPerflibLangId();
RegistryKey key =
RegistryKey.LocalMachine.openSubKey(path);
PerflibCounterMap counters = new PerflibCounterMap();
try {
key.getMultiStringValue("Counter", counters);
} finally {
key.close();
}
return counters.map;
}
/**
* @deprecated
* @see #getRawValue(String path)

View File

@ -18,6 +18,8 @@
package org.hyperic.sigar.win32.test;
import java.util.Map;
import org.hyperic.sigar.test.SigarTestCase;
import org.hyperic.sigar.win32.Pdh;
@ -56,6 +58,23 @@ public class TestPdh extends SigarTestCase {
*/
}
public void testCounterMap() throws Exception {
Map counters = Pdh.getEnglishPerflibCounterMap();
assertGtZeroTrace("counters", counters.size());
String[] keys = {
"System", "System Up Time"
};
String last = null;
for (int i=0; i<keys.length; i++) {
String index = (String)counters.get(keys[i]);
assertFalse(index.equals(last));
traceln(keys[i] + "=" + index);
last = index;
}
}
public void testPdh() throws Exception {
String[] iface = Pdh.getKeys("Thread");