Skip to content

Commit

Permalink
Add GetLogicalProcessorInformationEx function
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis authored and matthiasblaesing committed Feb 9, 2019
1 parent 07f3ce0 commit e0d5537
Show file tree
Hide file tree
Showing 5 changed files with 586 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ Features
* [#1058](https://github.com/java-native-access/jna/pull/1058): Add selectable timeout to stopService() and improve timeout handling - [@keithharp](https://github.com/keithharp).
* [#1050](https://github.com/java-native-access/jna/pull/1050): Add `c.s.j.p.win32.VersionHelpers` and supporting functions - [@dbwiddis](https://github.com/dbwiddis).
* [#1061](https://github.com/java-native-access/jna/pull/1061): replace toArray(new T[size]) with toArray(new T[0]) for better performance - [@hc-codersatlas](https://github.com/hc-codersatlas).
* [#1064](https://github.com/java-native-access/jna/pull/1064): Add `c.s.j.p.win32.Kernel32.GetLogicalProcessorInformationEx` function, convenience Util method and supporting structures - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
* [#1052](https://github.com/java-native-access/jna/issues/1052), [#1053](https://github.com/java-native-access/jna/issues/1053): WinXP compatibility for `c.s.j.p.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis).
* [#1052](https://github.com/java-native-access/jna/pull/1052), [#1053](https://github.com/java-native-access/jna/issues/1053): WinXP compatibility for `c.s.j.p.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis).
* [#1055](https://github.com/java-native-access/jna/pull/1055): Include `c.s.j.p.linux` in OSGi bundle. - [@dbwiddis](https://github.com/dbwiddis).

Release 5.2.0
=============
Expand Down
40 changes: 40 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.sun.jna.platform.win32;

import com.sun.jna.LastErrorException;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
Expand Down Expand Up @@ -1534,6 +1535,45 @@ boolean CreateProcessW(String lpApplicationName, char[] lpCommandLine,
boolean GetLogicalProcessorInformation(Pointer buffer,
DWORDByReference returnLength);

/**
* Retrieves information about the relationships of logical processors and
* related hardware.
*
* @param relationshipType
* The type of relationship to retrieve. This parameter can be
* one of the following values:
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationCache},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationGroup},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationNumaNode},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorCore},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorPackage},
* or {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationAll}
* @param buffer
* A pointer to a buffer that receives an array of
* {@link WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX}
* structures. If the function fails, the contents of this buffer
* are undefined.
* @param returnedLength
* On input, specifies the length of the buffer pointed to by
* Buffer, in bytes. If the buffer is large enough to contain all
* of the data, this function succeeds and ReturnedLength is set
* to the number of bytes returned. If the buffer is not large
* enough to contain all of the data, the function fails,
* GetLastError returns
* {@link WinError#ERROR_INSUFFICIENT_BUFFER}, and ReturnedLength
* is set to the buffer length required to contain all of the
* data. If the function fails with an error other than
* {@link WinError#ERROR_INSUFFICIENT_BUFFER}, the value of
* ReturnedLength is undefined.
* @return If the function succeeds, the return value is {@code TRUE} and at
* least one {@link WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX}
* structure is written to the output buffer.
* <p>
* If the function fails, the return value is {@code FALSE}. To get
* extended error information, call {@link #GetLastError()}.
*/
boolean GetLogicalProcessorInformationEx(int relationshipType, Pointer buffer, DWORDByReference returnedLength);

/**
* Retrieves information about the system's current usage of both physical
* and virtual memory.
Expand Down
46 changes: 44 additions & 2 deletions contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.platform.win32.WinNT.HRESULT;
import com.sun.jna.platform.win32.WinNT.LOGICAL_PROCESSOR_RELATIONSHIP;
import com.sun.jna.platform.win32.WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.W32APITypeMapper;
Expand Down Expand Up @@ -671,12 +673,52 @@ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProce
}
WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION firstInformation = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION(
memory);
int returnedStructCount = bufferSize.getValue().intValue()
/ sizePerStruct;
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0]);
}

/**
* Convenience method to get the processor information. Takes care of
* auto-growing the array and populating variable-length arrays in
* structures.
*
* @param relationshipType
* The type of relationship to retrieve. This parameter can be
* one of the following values:
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationCache},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationGroup},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationNumaNode},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorCore},
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorPackage},
* or {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationAll}
* @return the array of processor information.
*/
public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcessorInformationEx(
int relationshipType) {
WinDef.DWORDByReference bufferSize = new WinDef.DWORDByReference(new WinDef.DWORD(1));
Memory memory;
while (true) {
memory = new Memory(bufferSize.getValue().intValue());
if (!Kernel32.INSTANCE.GetLogicalProcessorInformationEx(relationshipType, memory, bufferSize)) {
int err = Kernel32.INSTANCE.GetLastError();
if (err != WinError.ERROR_INSUFFICIENT_BUFFER)
throw new Win32Exception(err);
} else {
break;
}
}
// Array elements have variable size; iterate to populate array
List<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX> procInfoList = new ArrayList<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>();
int offset = 0;
while (offset < bufferSize.getValue().intValue()) {
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX information = SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
.fromPointer(memory.share(offset));
procInfoList.add(information);
offset += information.size;
}
return procInfoList.toArray(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[0]);
}

/**
* Retrieves all the keys and values for the specified section of an initialization file.
*
Expand Down
Loading

0 comments on commit e0d5537

Please sign in to comment.