From a4f77700676722c8eb2c1977f24e02eaced5b062 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sun, 4 Mar 2007 18:12:59 +0000 Subject: [PATCH] [SIGAR-29] Add pdh language translation support --- .../java/src/org/hyperic/sigar/win32/Pdh.java | 84 ++++++++++++++++++- .../org/hyperic/sigar/win32/test/TestPdh.java | 44 +++------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/bindings/java/src/org/hyperic/sigar/win32/Pdh.java b/bindings/java/src/org/hyperic/sigar/win32/Pdh.java index 1a319daa..bd1174b7 100644 --- a/bindings/java/src/org/hyperic/sigar/win32/Pdh.java +++ b/bindings/java/src/org/hyperic/sigar/win32/Pdh.java @@ -22,6 +22,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.StringTokenizer; + +import org.hyperic.sigar.SigarLoader; public class Pdh extends Win32 { @@ -30,6 +33,21 @@ public class Pdh extends Win32 { private long query = -1l; // Handle to the query private String hostname = null; + private static Map counters = null; + + static { + final String prop = "sigar.pdh.enableTranslation"; + if (SigarLoader.IS_WIN32 && + "true".equals(System.getProperty(prop))) + { + try { + enableTranslation(); + } catch (Exception e) { + System.err.println(prop + ": " + + e.getMessage()); + } + } + } public Pdh() throws Win32Exception { this.query = pdhOpenQuery(); @@ -55,6 +73,18 @@ public class Pdh extends Win32 { } } + public static void enableTranslation() throws Win32Exception { + if (counters != null) { + return; + } + + if (LocaleInfo.isEnglish()) { + return; + } + + counters = getEnglishPerflibCounterMap(); + } + private static class PerflibCounterMap extends ArrayList { private Map map = new HashMap(); private String index = null; @@ -123,6 +153,57 @@ public class Pdh extends Win32 { return getValue(path, true); } + private static final String DELIM = "\\"; + + private static String getCounterName(String englishName) + throws Win32Exception { + + String index = (String)counters.get(englishName); + if (index == null) { + return englishName; + } + int ix = Integer.parseInt(index); + String name = getCounterName(ix); + return name; + } + + public static String translate(String path) + throws Win32Exception { + + if (counters == null) { + return path; + } + + StringBuffer trans = new StringBuffer(); + StringTokenizer tok = + new StringTokenizer(path, DELIM); + + int num = tok.countTokens(); + + if (num == 3) { + String hostname = tok.nextToken(); + trans.append(DELIM).append(DELIM).append(hostname); + } + + String object = tok.nextToken(); + String instance = null; + int ix = object.indexOf('('); + if (ix != -1) { + instance = object.substring(ix); + object = object.substring(0, ix); + } + + trans.append(DELIM).append(getCounterName(object)); + if (instance != null) { + trans.append(instance); + } + + String counter = tok.nextToken(); + trans.append(DELIM).append(getCounterName(counter)); + + return trans.toString(); + } + private double getValue(String path, boolean format) throws Win32Exception { @@ -130,7 +211,8 @@ public class Pdh extends Win32 { pdhConnectMachine(this.hostname); } - long counter = pdhAddCounter(this.query, path); + long counter = + pdhAddCounter(this.query, translate(path)); try { return pdhGetValue(this.query, counter, format); } finally { 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 d2ce8af9..3c8b4d78 100644 --- a/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java +++ b/bindings/java/src/org/hyperic/sigar/win32/test/TestPdh.java @@ -39,44 +39,26 @@ public class TestPdh extends SigarTestCase { (long)pdh.getFormattedValue(key)); } - private String getCounterName(String index) - throws Exception { - - String name = - Pdh.getCounterName(Integer.parseInt(index)); - - return name; - } - - private void getValue(String object, String counter) - throws Exception { - - object = getCounterName(object); - counter = getCounterName(counter); - getValue("\\" + object + "\\" + counter); - } - - //XXX restore original test below when this is handled internally public void testGetValue() throws Exception { - Map counters = Pdh.getEnglishPerflibCounterMap(); + Pdh.enableTranslation(); + final String DL = "\\"; String[][] keys = { + { "System", "System Up Time" }, { "Memory", "Available Bytes" }, { "Memory", "Pages/sec" }, + { "Processor(_Total)", "% User Time" }, }; - for (int i=0; i" + trans); + } + + getValue(path); } /* XXX hangs for a while on my XP box String bogusKey = "\\Does Not\\Exist";