/
WindowsJNAAffinity.java
98 lines (81 loc) · 3.28 KB
/
WindowsJNAAffinity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright 2011 Peter Lawrey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.higherfrequencytrading.affinity.impl;
import com.higherfrequencytrading.affinity.IAffinity;
import com.sun.jna.LastErrorException;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.PointerType;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.ptr.LongByReference;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Implementation of {@link com.higherfrequencytrading.affinity.IAffinity} based on JNA call of
* sched_SetThreadAffinityMask/GetProcessAffinityMask from Windows 'kernel32' library. Applicable for
* most windows platforms
*
* @author andre.monteiro
*/
public enum WindowsJNAAffinity implements IAffinity {
INSTANCE;
private static final Logger LOGGER = Logger.getLogger(WindowsJNAAffinity.class.getName());
public static final boolean LOADED;
/**
* @author BegemoT
*/
private interface CLibrary extends Library {
public static final CLibrary INSTANCE = (CLibrary) Native.loadLibrary("kernel32", CLibrary.class);
public int GetProcessAffinityMask(final int pid, final PointerType lpProcessAffinityMask, final PointerType lpSystemAffinityMask) throws LastErrorException;
public void SetThreadAffinityMask(final int pid, final WinDef.DWORD lpProcessAffinityMask) throws LastErrorException;
public int GetCurrentThread() throws LastErrorException;
}
static {
boolean loaded = false;
try {
INSTANCE.getAffinity();
loaded = true;
} catch (UnsatisfiedLinkError e) {
LOGGER.log(Level.WARNING, "Unable to load jna library " + e);
}
LOADED = loaded;
}
@Override
public long getAffinity() {
final CLibrary lib = CLibrary.INSTANCE;
final LongByReference cpuset1 = new LongByReference(0);
final LongByReference cpuset2 = new LongByReference(0);
try {
final int ret = lib.GetProcessAffinityMask(-1, cpuset1, cpuset2);
if (ret < 0)
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + cpuset1 + ") ) return " + ret);
return cpuset1.getValue();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public void setAffinity(final long affinity) {
final CLibrary lib = CLibrary.INSTANCE;
WinDef.DWORD aff = new WinDef.DWORD(affinity);
try {
lib.SetThreadAffinityMask(lib.GetCurrentThread(), aff);
} catch (LastErrorException e) {
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
}
}
}