Skip to content

Commit

Permalink
Split object and instance return methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jun 24, 2018
1 parent 9401f05 commit efcd4d3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 43 deletions.
112 changes: 76 additions & 36 deletions contrib/platform/src/com/sun/jna/platform/win32/PdhUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package com.sun.jna.platform.win32;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.sun.jna.Memory;
Expand Down Expand Up @@ -73,34 +72,32 @@ public static String PdhLookupPerfNameByIndex(String szMachineName, int dwNameIn
}

/**
* Utility method to call Pdh's PdhEnumObjectItems that allocates the
* required memory for the mszCounterList and mszInstanceList parameters
* based on the type mapping used, calls to PdhEnumObjectItems, and returns
* the received lists of strings.
*
* @param szDataSource
* String that specifies the name of the log file used to
* enumerate the counter and instance names. If NULL, the
* function uses the computer specified in the szMachineName
* parameter to enumerate the names.
* @param szMachineName
* String that specifies the name of the computer that contains
* the counter and instance names that you want to enumerate.
* Include the leading slashes in the computer name, for example,
* \\computername. If the szDataSource parameter is NULL, you can
* set szMachineName to NULL to specify the local computer.
* @param szObjectName
* String that specifies the name of the object whose counter and
* instance names you want to enumerate.
* @param dwDetailLevel
* Detail level of the performance items to return. All items
* that are of the specified detail level or less will be
* returned.
* @return Returns a List with two elements. Index 0 contains a List of
* Strings of the counters for the object. Index 1 contains a List
* of Strings of the instances of the object.
*/
public static List<List<String>> PdhEnumObjectItems(String szDataSource, String szMachineName, String szObjectName,
* Utility method to call Pdh's PdhEnumObjectItems that allocates the
* required memory for the mszCounterList parameter based on the type
* mapping used, calls to PdhEnumObjectItems, and returns the received lists
* of strings.
*
* @param szDataSource
* String that specifies the name of the log file used to
* enumerate the counter and instance names. If NULL, the
* function uses the computer specified in the szMachineName
* parameter to enumerate the names.
* @param szMachineName
* String that specifies the name of the computer that contains
* the counter and instance names that you want to enumerate.
* Include the leading slashes in the computer name, for example,
* \\computername. If the szDataSource parameter is NULL, you can
* set szMachineName to NULL to specify the local computer.
* @param szObjectName
* String that specifies the name of the object whose counter and
* instance names you want to enumerate.
* @param dwDetailLevel
* Detail level of the performance items to return. All items
* that are of the specified detail level or less will be
* returned.
* @return Returns a List of Strings of the counters for the object.
*/
public static List<String> PdhEnumObjectItemCounters(String szDataSource, String szMachineName, String szObjectName,
int dwDetailLevel) {
int charToBytes = Boolean.getBoolean("w32.ascii") ? 1 : Native.WCHAR_SIZE;

Expand All @@ -117,7 +114,7 @@ public static List<List<String>> PdhEnumObjectItems(String szDataSource, String
pcchCounterListLength, mszInstanceList, pcchInstanceListLength, dwDetailLevel, 0);

// Fetch counters
List<String> counters = new LinkedList<String>();
List<String> counters = new ArrayList<String>();
int offset = 0;
while (offset < mszCounterList.size()) {
String s = null;
Expand All @@ -131,11 +128,57 @@ public static List<List<String>> PdhEnumObjectItems(String szDataSource, String
break;
}
counters.add(s);
// Increment for string + null terminator
offset += (s.length() + 1) * charToBytes;
}

List<String> instances = new LinkedList<String>();
offset = 0;
return counters;
}

/**
* Utility method to call Pdh's PdhEnumObjectItems that allocates the
* required memory for the mszInstanceList parameters based on the type
* mapping used, calls to PdhEnumObjectItems, and returns the received lists
* of strings.
*
* @param szDataSource
* String that specifies the name of the log file used to
* enumerate the counter and instance names. If NULL, the
* function uses the computer specified in the szMachineName
* parameter to enumerate the names.
* @param szMachineName
* String that specifies the name of the computer that contains
* the counter and instance names that you want to enumerate.
* Include the leading slashes in the computer name, for example,
* \\computername. If the szDataSource parameter is NULL, you can
* set szMachineName to NULL to specify the local computer.
* @param szObjectName
* String that specifies the name of the object whose counter and
* instance names you want to enumerate.
* @param dwDetailLevel
* Detail level of the performance items to return. All items
* that are of the specified detail level or less will be
* returned.
* @return Returns a Lists of Strings of the instances of the object.
*/
public static List<String> PdhEnumObjectItemInstances(String szDataSource, String szMachineName,
String szObjectName, int dwDetailLevel) {
int charToBytes = Boolean.getBoolean("w32.ascii") ? 1 : Native.WCHAR_SIZE;

// Call once to get string lengths
DWORDByReference pcchCounterListLength = new DWORDByReference(new DWORD(0));
DWORDByReference pcchInstanceListLength = new DWORDByReference(new DWORD(0));
Pdh.INSTANCE.PdhEnumObjectItems(szDataSource, szMachineName, szObjectName, null, pcchCounterListLength, null,
pcchInstanceListLength, dwDetailLevel, 0);

// Allocate memory and call again to populate strings
Memory mszCounterList = new Memory(pcchCounterListLength.getValue().intValue() * charToBytes);
Memory mszInstanceList = new Memory(pcchInstanceListLength.getValue().intValue() * charToBytes);
Pdh.INSTANCE.PdhEnumObjectItems(szDataSource, szMachineName, szObjectName, mszCounterList,
pcchCounterListLength, mszInstanceList, pcchInstanceListLength, dwDetailLevel, 0);

List<String> instances = new ArrayList<String>();
int offset = 0;
while (offset < mszInstanceList.size()) {
String s = null;
if (charToBytes == 1) {
Expand All @@ -152,9 +195,6 @@ public static List<List<String>> PdhEnumObjectItems(String szDataSource, String
offset += (s.length() + 1) * charToBytes;
}

List<List<String>> objectItems = new ArrayList<List<String>>(2);
objectItems.add(counters);
objectItems.add(instances);
return objectItems;
return instances;
}
}
11 changes: 4 additions & 7 deletions contrib/platform/test/com/sun/jna/platform/win32/PdhTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
package com.sun.jna.platform.win32;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.PrintStream;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -182,18 +179,18 @@ public void testLookupPerfIndex() {
@Test
public void testEnumObjectItems() {
if (AbstractWin32TestSupport.isEnglishLocale) {
String processorStr = "Process";
String processorStr = "Processor";
String processorTimeStr = "% Processor Time";

// Fetch the counter and instance names
List<List<String>> objectItems = PdhUtil.PdhEnumObjectItems(null, null, processorStr, 100);
List<String> counters = objectItems.get(0);
List<String> instances = objectItems.get(1);
List<String> instances = PdhUtil.PdhEnumObjectItemInstances(null, null, processorStr, 100);

// Should have at least one processor and total instance
assertTrue(instances.contains("0"));
assertTrue(instances.contains("_Total"));

// Should have a "% Processor Time" counter
List<String> counters = PdhUtil.PdhEnumObjectItemCounters(null, null, processorStr, 100);
assertTrue(counters.contains(processorTimeStr));
} else {
System.err.println("testEnumObjectItems test can only be run with english locale.");
Expand Down

0 comments on commit efcd4d3

Please sign in to comment.