Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setAffinity does not work as expected in Windows #117

Open
kopavel opened this issue Apr 5, 2024 · 8 comments
Open

setAffinity does not work as expected in Windows #117

kopavel opened this issue Apr 5, 2024 · 8 comments
Assignees

Comments

@kopavel
Copy link

kopavel commented Apr 5, 2024

On win 11 got warning:
"Tried to set affinity to {7} but was {0,1,2,3,4,5,6,7} you may have insufficient access rights"
and nohing heppend - thread remain affined to all cores.

Java version: jdk-21.0.2.13-hotspot (Eclipse Adoptium)
jna and jna-platform verstion: 5.14.0
affinity version: 3.23.3

Thread, for which i try to lock affiniti created by :

Thread.ofPlatform().name("AffinedThread").start(() -> {
try (AffinityLock al = AffinityLock.acquireLock()) {
.....
}
});

Starting console in which java started with Admin rights change nothing.

@tgd tgd self-assigned this Apr 8, 2024
@tgd
Copy link
Contributor

tgd commented Apr 8, 2024

Thanks @kopavel we will take a look soon.

For expedited support we do offer commercial options - https://chronicle.software/contact-us/

@tgd tgd assigned yevgenp and unassigned tgd Apr 8, 2024
@kopavel
Copy link
Author

kopavel commented Apr 8, 2024

Just re-check sittuation again using task manager - thead actually ARE binded as expected... so actually - it'a a false WARNing from code...

@tgd
Copy link
Contributor

tgd commented Apr 8, 2024

Thanks for letting us know - we will close for now and potentially re-open in future.

@tgd tgd closed this as completed Apr 8, 2024
@tgd tgd unassigned yevgenp Apr 8, 2024
@yevgenp yevgenp changed the title Access right problem setAffinity does not work as expected in Windows Apr 9, 2024
@yevgenp yevgenp self-assigned this Apr 9, 2024
@mr-infty
Copy link

mr-infty commented Jun 4, 2024

I am experiencing the same problem (affinity version 3.23.3), i.e. I get the same error message. I have also checked manually using the task manager, and it shows that the affinity was not modified.

According to https://learn.microsoft.com/en-en/windows/win32/api/winbase/nf-winbase-setthreadaffinitymask the return value of SetThreadAffinityMask already indicates whether the operation was succesful. Why don't you test for that?

P.S.: I have run java.exe from a PowerShell with administrative privileges.

@tgd
Copy link
Contributor

tgd commented Jun 4, 2024

Thanks for reporting it @mr-infty - we have a PR in flight for this and will work on getting it tested and released. We will update the ticket here when that's done.

@tgd tgd reopened this Jun 4, 2024
@tgd tgd assigned tgd and unassigned yevgenp Jun 4, 2024
@mr-infty
Copy link

mr-infty commented Jun 4, 2024

Sorry, my bad: I failed to distinguish between processes and threads.

Indeed, the affinity of my process is not changed by .acquireCore(); however, the thread's one is (probably), at least calling SetThreadAffinityMask manually from my own binding to kernel32.dll works. So I guess the problem really lies in the code that checks the result.

On that note, an alternative would be to call SetThreadAffinityMask twice, as that method returns (on a succesful call) the previous affinity mask. Also, checking the return value for null already allows you to determine whether the call was successful, at least if you believe the Windows API.

@yevgenp
Copy link
Contributor

yevgenp commented Jun 4, 2024

Thanks @mr-infty. We considered such option (call SetThreadAffinityMask twice) as Win API is not flexible enough.
But I'm afraid it's gonna be too expensive (latency wise).

@mr-infty
Copy link

mr-infty commented Jun 4, 2024

I see @yevgenp. However, you could still just check the return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants