Skip to content

Commit b47521b

Browse files
committed
Add a test for LinuxJNAAffinity
1 parent 4529097 commit b47521b

3 files changed

Lines changed: 94 additions & 68 deletions

File tree

affinity/src/main/java/net/openhft/affinity/impl/LinuxJNAAffinity.java

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
/*
2-
* Copyright (C) 2015 higherfrequencytrading.com
32
*
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/>.
716
*
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/>.
1517
*/
1618

1719
package net.openhft.affinity.impl;
1820

21+
import com.sun.jna.NativeLong;
1922
import com.sun.jna.Platform;
2023
import net.openhft.affinity.IAffinity;
2124
import org.slf4j.Logger;
2225
import org.slf4j.LoggerFactory;
2326

24-
import java.nio.ByteBuffer;
25-
import java.util.ArrayList;
2627
import java.util.BitSet;
2728

2829
public enum LinuxJNAAffinity implements IAffinity {
@@ -55,51 +56,18 @@ public enum LinuxJNAAffinity implements IAffinity {
5556

5657
private final ThreadLocal<Integer> THREAD_ID = new ThreadLocal<>();
5758

58-
// TODO: FIXME!!! CHANGE IAffinity TO SUPPORT PLATFORMS WITH 64+ CORES FIXME!!!
59+
5960
@Override
6061
public BitSet getAffinity() {
6162
final LinuxHelper.cpu_set_t cpuset = LinuxHelper.sched_getaffinity();
6263

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);
10269
}
70+
return ret;
10371
}
10472

10573
// TODO: FIXME!!! CHANGE IAffinity TO SUPPORT PLATFORMS WITH 64+ CORES FIXME!!!
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
*
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/>.
16+
*
17+
*/
18+
19+
package net.openhft.affinity.impl;
20+
21+
import org.junit.Assume;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
24+
25+
import java.util.BitSet;
26+
27+
import static org.junit.Assert.assertEquals;
28+
29+
/**
30+
* Created by peter on 23/03/16.
31+
*/
32+
public class LinuxJNAAffinityTest {
33+
@BeforeClass
34+
public static void checkJniLibraryPresent() {
35+
Assume.assumeTrue(LinuxJNAAffinity.LOADED);
36+
}
37+
38+
@Test
39+
public void LinuxJNA() {
40+
int nbits = Runtime.getRuntime().availableProcessors();
41+
BitSet affinity0 = LinuxJNAAffinity.INSTANCE.getAffinity();
42+
System.out.println(affinity0);
43+
44+
BitSet affinity = new BitSet(nbits);
45+
46+
affinity.set(1);
47+
LinuxJNAAffinity.INSTANCE.setAffinity(affinity);
48+
BitSet affinity2 = LinuxJNAAffinity.INSTANCE.getAffinity();
49+
System.out.println(affinity2);
50+
assertEquals(1, LinuxJNAAffinity.INSTANCE.getCpu());
51+
assertEquals(affinity, affinity2);
52+
53+
affinity.set(0, nbits);
54+
LinuxJNAAffinity.INSTANCE.setAffinity(affinity);
55+
}
56+
57+
}

affinity/src/test/java/software/chronicle/enterprise/internals/NativeAffinityTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
/*
2-
* Copyright 2011 Peter Lawrey
32
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
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/>.
716
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
1517
*/
1618

1719
package software.chronicle.enterprise.internals;
@@ -34,8 +36,7 @@ public class NativeAffinityTest {
3436
protected static final int CORES = Runtime.getRuntime().availableProcessors();
3537
protected static final BitSet CORES_MASK = new BitSet(CORES);
3638

37-
static
38-
{
39+
static {
3940
CORES_MASK.set(0, CORES, true);
4041
}
4142

@@ -59,8 +60,8 @@ public void getAffinityReturnsValidValue() {
5960
final int allCoresMask = (1 << CORES) - 1;
6061
assertTrue(
6162
"Affinity mask " + Utilities.toBinaryString(affinity) + " must be <=(2^" + CORES + "-1 = " + allCoresMask + ")",
62-
affinity.length() <= CORES_MASK.length()
63-
);
63+
affinity.length() <= CORES_MASK.length()
64+
);
6465
}
6566

6667
@Test

0 commit comments

Comments
 (0)