|
1 | 1 | /* |
2 | | - * Copyright (C) 2015 higherfrequencytrading.com |
3 | 2 | * |
4 | | - * This program is free software: you can redistribute it and/or modify |
5 | | - * it under the terms of the GNU Lesser General Public License as published by |
6 | | - * the Free Software Foundation, either version 3 of the License. |
| 3 | + * * Copyright (C) ${YEAR} higherfrequencytrading.com |
| 4 | + * * |
| 5 | + * * This program is free software: you can redistribute it and/or modify |
| 6 | + * * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * * the Free Software Foundation, either version 3 of the License. |
| 8 | + * * |
| 9 | + * * This program is distributed in the hope that it will be useful, |
| 10 | + * * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * * GNU Lesser General Public License for more details. |
| 13 | + * * |
| 14 | + * * You should have received a copy of the GNU Lesser General Public License |
| 15 | + * * along with this program. If not, see <http://www.gnu.org/licenses/>. |
7 | 16 | * |
8 | | - * This program is distributed in the hope that it will be useful, |
9 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | | - * GNU Lesser General Public License for more details. |
12 | | - * |
13 | | - * You should have received a copy of the GNU Lesser General Public License |
14 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
15 | 17 | */ |
16 | 18 |
|
17 | 19 | package net.openhft.affinity.impl; |
18 | 20 |
|
| 21 | +import com.sun.jna.NativeLong; |
19 | 22 | import com.sun.jna.Platform; |
20 | 23 | import net.openhft.affinity.IAffinity; |
21 | 24 | import org.slf4j.Logger; |
22 | 25 | import org.slf4j.LoggerFactory; |
23 | 26 |
|
24 | | -import java.nio.ByteBuffer; |
25 | | -import java.util.ArrayList; |
26 | 27 | import java.util.BitSet; |
27 | 28 |
|
28 | 29 | public enum LinuxJNAAffinity implements IAffinity { |
@@ -55,51 +56,18 @@ public enum LinuxJNAAffinity implements IAffinity { |
55 | 56 |
|
56 | 57 | private final ThreadLocal<Integer> THREAD_ID = new ThreadLocal<>(); |
57 | 58 |
|
58 | | - // TODO: FIXME!!! CHANGE IAffinity TO SUPPORT PLATFORMS WITH 64+ CORES FIXME!!! |
| 59 | + |
59 | 60 | @Override |
60 | 61 | public BitSet getAffinity() { |
61 | 62 | final LinuxHelper.cpu_set_t cpuset = LinuxHelper.sched_getaffinity(); |
62 | 63 |
|
63 | | - boolean collect = false; |
64 | | - ArrayList<Byte> bytes = new ArrayList<Byte>(); |
65 | | - |
66 | | - ByteBuffer buff = null; |
67 | | - if (Platform.is64Bit()) { |
68 | | - buff = ByteBuffer.allocate(Long.SIZE / 8); |
69 | | - } else { |
70 | | - buff = ByteBuffer.allocate(Integer.SIZE / 8); |
71 | | - } |
72 | | - |
73 | | - for (int i = cpuset.__bits.length - 1; i >= 0; --i) { |
74 | | - if (!collect && cpuset.__bits[i].longValue() != 0) { |
75 | | - collect = true; |
76 | | - } |
77 | | - |
78 | | - if (collect) { |
79 | | - if (Platform.is64Bit()) { |
80 | | - buff.putLong(cpuset.__bits[i].longValue()); |
81 | | - } else { |
82 | | - buff.putInt((int) cpuset.__bits[i].longValue()); |
83 | | - } |
84 | | - |
85 | | - final byte[] arr = buff.array(); |
86 | | - //for (int j = arr.length - 1; j >= 0; --j) |
87 | | - for (int j = 0; j < arr.length; j++) { |
88 | | - bytes.add(arr[j]); |
89 | | - } |
90 | | - } |
91 | | - } |
92 | | - |
93 | | - if (!bytes.isEmpty()) { |
94 | | - byte[] data = new byte[bytes.size()]; |
95 | | - for (int i = 0; i < bytes.size(); i++) { |
96 | | - // don't forget to reverse the order of long values |
97 | | - data[data.length - i - 1] = bytes.get(i); |
98 | | - } |
99 | | - return BitSet.valueOf(data); |
100 | | - } else { |
101 | | - return new BitSet(); |
| 64 | + BitSet ret = new BitSet(LinuxHelper.cpu_set_t.__CPU_SETSIZE); |
| 65 | + int i = 0; |
| 66 | + for (NativeLong nl : cpuset.__bits) { |
| 67 | + for (int j = 0; j < Long.SIZE; j++) |
| 68 | + ret.set(i++, ((nl.longValue() >>> j) & 1) != 0); |
102 | 69 | } |
| 70 | + return ret; |
103 | 71 | } |
104 | 72 |
|
105 | 73 | // TODO: FIXME!!! CHANGE IAffinity TO SUPPORT PLATFORMS WITH 64+ CORES FIXME!!! |
|
0 commit comments