Skip to content

Commit e0d5537

Browse files
dbwiddismatthiasblaesing
authored andcommitted
Add GetLogicalProcessorInformationEx function
1 parent 07f3ce0 commit e0d5537

5 files changed

Lines changed: 586 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ Features
1010
* [#1058](https://github.com/java-native-access/jna/pull/1058): Add selectable timeout to stopService() and improve timeout handling - [@keithharp](https://github.com/keithharp).
1111
* [#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).
1212
* [#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).
13+
* [#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).
1314

1415
Bug Fixes
1516
---------
16-
* [#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).
17+
* [#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).
18+
* [#1055](https://github.com/java-native-access/jna/pull/1055): Include `c.s.j.p.linux` in OSGi bundle. - [@dbwiddis](https://github.com/dbwiddis).
1719

1820
Release 5.2.0
1921
=============

contrib/platform/src/com/sun/jna/platform/win32/Kernel32.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.sun.jna.platform.win32;
2525

2626
import com.sun.jna.LastErrorException;
27+
import com.sun.jna.Memory;
2728
import com.sun.jna.Native;
2829
import com.sun.jna.Pointer;
2930
import com.sun.jna.ptr.IntByReference;
@@ -1534,6 +1535,45 @@ boolean CreateProcessW(String lpApplicationName, char[] lpCommandLine,
15341535
boolean GetLogicalProcessorInformation(Pointer buffer,
15351536
DWORDByReference returnLength);
15361537

1538+
/**
1539+
* Retrieves information about the relationships of logical processors and
1540+
* related hardware.
1541+
*
1542+
* @param relationshipType
1543+
* The type of relationship to retrieve. This parameter can be
1544+
* one of the following values:
1545+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationCache},
1546+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationGroup},
1547+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationNumaNode},
1548+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorCore},
1549+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorPackage},
1550+
* or {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationAll}
1551+
* @param buffer
1552+
* A pointer to a buffer that receives an array of
1553+
* {@link WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX}
1554+
* structures. If the function fails, the contents of this buffer
1555+
* are undefined.
1556+
* @param returnedLength
1557+
* On input, specifies the length of the buffer pointed to by
1558+
* Buffer, in bytes. If the buffer is large enough to contain all
1559+
* of the data, this function succeeds and ReturnedLength is set
1560+
* to the number of bytes returned. If the buffer is not large
1561+
* enough to contain all of the data, the function fails,
1562+
* GetLastError returns
1563+
* {@link WinError#ERROR_INSUFFICIENT_BUFFER}, and ReturnedLength
1564+
* is set to the buffer length required to contain all of the
1565+
* data. If the function fails with an error other than
1566+
* {@link WinError#ERROR_INSUFFICIENT_BUFFER}, the value of
1567+
* ReturnedLength is undefined.
1568+
* @return If the function succeeds, the return value is {@code TRUE} and at
1569+
* least one {@link WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX}
1570+
* structure is written to the output buffer.
1571+
* <p>
1572+
* If the function fails, the return value is {@code FALSE}. To get
1573+
* extended error information, call {@link #GetLastError()}.
1574+
*/
1575+
boolean GetLogicalProcessorInformationEx(int relationshipType, Pointer buffer, DWORDByReference returnedLength);
1576+
15371577
/**
15381578
* Retrieves information about the system's current usage of both physical
15391579
* and virtual memory.

contrib/platform/src/com/sun/jna/platform/win32/Kernel32Util.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import com.sun.jna.platform.win32.WinNT.HANDLE;
4040
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
4141
import com.sun.jna.platform.win32.WinNT.HRESULT;
42+
import com.sun.jna.platform.win32.WinNT.LOGICAL_PROCESSOR_RELATIONSHIP;
43+
import com.sun.jna.platform.win32.WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;
4244
import com.sun.jna.ptr.IntByReference;
4345
import com.sun.jna.ptr.PointerByReference;
4446
import com.sun.jna.win32.W32APITypeMapper;
@@ -671,12 +673,52 @@ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProce
671673
}
672674
WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION firstInformation = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION(
673675
memory);
674-
int returnedStructCount = bufferSize.getValue().intValue()
675-
/ sizePerStruct;
676676
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
677677
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0]);
678678
}
679679

680+
/**
681+
* Convenience method to get the processor information. Takes care of
682+
* auto-growing the array and populating variable-length arrays in
683+
* structures.
684+
*
685+
* @param relationshipType
686+
* The type of relationship to retrieve. This parameter can be
687+
* one of the following values:
688+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationCache},
689+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationGroup},
690+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationNumaNode},
691+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorCore},
692+
* {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationProcessorPackage},
693+
* or {@link LOGICAL_PROCESSOR_RELATIONSHIP#RelationAll}
694+
* @return the array of processor information.
695+
*/
696+
public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcessorInformationEx(
697+
int relationshipType) {
698+
WinDef.DWORDByReference bufferSize = new WinDef.DWORDByReference(new WinDef.DWORD(1));
699+
Memory memory;
700+
while (true) {
701+
memory = new Memory(bufferSize.getValue().intValue());
702+
if (!Kernel32.INSTANCE.GetLogicalProcessorInformationEx(relationshipType, memory, bufferSize)) {
703+
int err = Kernel32.INSTANCE.GetLastError();
704+
if (err != WinError.ERROR_INSUFFICIENT_BUFFER)
705+
throw new Win32Exception(err);
706+
} else {
707+
break;
708+
}
709+
}
710+
// Array elements have variable size; iterate to populate array
711+
List<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX> procInfoList = new ArrayList<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>();
712+
int offset = 0;
713+
while (offset < bufferSize.getValue().intValue()) {
714+
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX information = SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
715+
.fromPointer(memory.share(offset));
716+
procInfoList.add(information);
717+
offset += information.size;
718+
}
719+
return procInfoList.toArray(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[0]);
720+
}
721+
680722
/**
681723
* Retrieves all the keys and values for the specified section of an initialization file.
682724
*

0 commit comments

Comments
 (0)