|
| 1 | +package test.threads; |
| 2 | + |
| 3 | +import com.sun.jna.*; |
| 4 | +import com.sun.jna.ptr.LongByReference; |
| 5 | + |
| 6 | +import java.text.ParseException; |
| 7 | +import java.util.StringTokenizer; |
| 8 | + |
| 9 | +/** |
| 10 | + * For attaching threads to cores |
| 11 | + * |
| 12 | + * @author cheremin |
| 13 | + * @since 25.10.11, 14:18 |
| 14 | + */ |
| 15 | +public class ThreadAffinity { |
| 16 | + |
| 17 | + private static final Core[] cores; |
| 18 | + |
| 19 | + static { |
| 20 | + final int coresCount = Runtime.getRuntime().availableProcessors(); |
| 21 | + cores = new Core[coresCount]; |
| 22 | + |
| 23 | + for (int i = 0; i < cores.length; i++) { |
| 24 | + cores[i] = new Core(i); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public static final class Core { |
| 30 | + private final int sequence; |
| 31 | + |
| 32 | + public Core(final int sequence) { |
| 33 | + this.sequence = sequence; |
| 34 | + if (sequence > Integer.SIZE) { |
| 35 | + throw new IllegalStateException("Too many cores (" + sequence + ") for integer mask"); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public int sequence() { |
| 40 | + return sequence; |
| 41 | + } |
| 42 | + |
| 43 | + public void attachTo() throws Exception { |
| 44 | + |
| 45 | + final long mask = mask(); |
| 46 | + |
| 47 | + setCurrentThreadAffinityMask(mask); |
| 48 | + } |
| 49 | + |
| 50 | + public void attach(final Thread thread) throws Exception { |
| 51 | + |
| 52 | + final long mask = mask(); |
| 53 | + |
| 54 | + setThreadAffinityMask(thread.getId(), mask); |
| 55 | + } |
| 56 | + |
| 57 | + private int mask() { |
| 58 | + return 1 << sequence; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + public String toString() { |
| 64 | + return String.format("Core[#%d]", sequence()); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + public static void setCurrentThreadAffinityMask(final long mask) throws Exception { |
| 70 | + final CLibrary lib = CLibrary.INSTANCE; |
| 71 | + final int cpuMaskSize = Long.SIZE / 8; |
| 72 | + try { |
| 73 | + final int ret = lib.sched_setaffinity(0, cpuMaskSize, new LongByReference(mask)); |
| 74 | + if (ret < 0) { |
| 75 | + final int errNo = getErrorNo(); |
| 76 | + throw new Exception("sched_setaffinity( 0, (" + cpuMaskSize + ") , &(" + mask + ") ) return " + ret + ", errno() = " + errNo); |
| 77 | + } |
| 78 | + } catch (Throwable e) { |
| 79 | + throw new Exception(e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void setThreadAffinityMask(final long threadID, |
| 84 | + final long mask) throws Exception { |
| 85 | + final CLibrary lib = CLibrary.INSTANCE; |
| 86 | + final int cpuMaskSize = Long.SIZE / 8; |
| 87 | + try { |
| 88 | + final int ret = lib.sched_setaffinity((int) threadID, cpuMaskSize, new LongByReference(mask)); |
| 89 | + if (ret < 0) { |
| 90 | + final int errNo = getErrorNo(); |
| 91 | + throw new Exception("sched_setaffinity( " + threadID + ", (" + cpuMaskSize + ") , &(" + mask + ") ) return " + ret + ", errno() = " + errNo); |
| 92 | + } |
| 93 | + } catch (Throwable e) { |
| 94 | + throw new Exception(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + private static int getErrorNo() { |
| 100 | + final NativeLibrary nativeLib = NativeLibrary.getInstance("c"); |
| 101 | + final Pointer pErrNo = nativeLib.getFunction("errno"); |
| 102 | + return pErrNo.getInt(0); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public static Core[] cores() { |
| 107 | + return cores.clone(); |
| 108 | + } |
| 109 | + |
| 110 | + public static Core currentCore() { |
| 111 | + final int cpuSequence = CLibrary.INSTANCE.sched_getcpu(); |
| 112 | + return cores[cpuSequence]; |
| 113 | + } |
| 114 | + |
| 115 | + public static void nice(final int increment) throws Exception { |
| 116 | + final CLibrary lib = CLibrary.INSTANCE; |
| 117 | + try { |
| 118 | + final int ret = lib.nice(increment); |
| 119 | + if (ret < 0) { |
| 120 | + final int errNo = getErrorNo(); |
| 121 | + throw new Exception("nice( " + increment + " ) return " + ret + ", errno() = " + errNo); |
| 122 | + } |
| 123 | + } catch (Throwable e) { |
| 124 | + throw new Exception(e); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private interface CLibrary extends Library { |
| 129 | + public static final CLibrary INSTANCE = (CLibrary) |
| 130 | + Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); |
| 131 | + |
| 132 | + public void printf(final String format, |
| 133 | + final Object... args); |
| 134 | + |
| 135 | + public int nice(final int increment); |
| 136 | + |
| 137 | + public int sched_setaffinity(final int pid, |
| 138 | + final int cpusetsize, |
| 139 | + final PointerType cpuset); |
| 140 | + |
| 141 | + public int sched_getcpu(); |
| 142 | + } |
| 143 | + |
| 144 | + public static void main(final String[] args) throws Exception { |
| 145 | + |
| 146 | + final Core currentCore = currentCore(); |
| 147 | +// System.out.printf( "currentCore() -> %s\n", currentCore ); |
| 148 | + |
| 149 | +// final int niceRet = lib.nice( -20 ); |
| 150 | +// System.out.printf( "nice -> %d\n", niceRet ); |
| 151 | + |
| 152 | + |
| 153 | + for (final Core core : cores()) { |
| 154 | + new Thread() { |
| 155 | + @Override |
| 156 | + public void run() { |
| 157 | + try { |
| 158 | + core.attachTo(); |
| 159 | + System.out.printf("currentCore() -> %s\n", currentCore()); |
| 160 | + for (int i = 0; i < Integer.MAX_VALUE; i++) { |
| 161 | + i--; |
| 162 | + } |
| 163 | + } catch (Exception e) { |
| 164 | + e.printStackTrace(); |
| 165 | + } |
| 166 | + } |
| 167 | + }.start(); |
| 168 | + |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + public static int[] parseCoresIndexes(final String str, |
| 175 | + final int[] defaults) throws ParseException { |
| 176 | + final StringTokenizer stok = new StringTokenizer(str, ","); |
| 177 | + final int size = stok.countTokens(); |
| 178 | + if (size == 0) { |
| 179 | + return defaults; |
| 180 | + } |
| 181 | + |
| 182 | + final int maxIndex = Runtime.getRuntime().availableProcessors() - 1; |
| 183 | + final int[] indexes = new int[size]; |
| 184 | + for (int i = 0; stok.hasMoreTokens(); i++) { |
| 185 | + final String token = stok.nextToken(); |
| 186 | + final int index; |
| 187 | + try { |
| 188 | + index = Integer.parseInt(token); |
| 189 | + } catch (NumberFormatException e) { |
| 190 | + throw new ParseException("Can't parse [" + i + "]='" + token + "' as Integer", i); |
| 191 | + } |
| 192 | + if (index > maxIndex || index < 0) { |
| 193 | + throw new ParseException("Index[" + i + "]=" + index + " is out of bounds [0," + maxIndex + "]", i); |
| 194 | + } |
| 195 | + indexes[i] = index; |
| 196 | + } |
| 197 | + return indexes; |
| 198 | + } |
| 199 | + |
| 200 | + public static Core[] parseCores(final String str, |
| 201 | + final int[] defaults) throws ParseException { |
| 202 | + final int[] indexes = parseCoresIndexes(str, defaults); |
| 203 | + final Core[] cores = new Core[indexes.length]; |
| 204 | + for (int i = 0; i < cores.length; i++) { |
| 205 | + cores[i] = cores()[indexes[i]]; |
| 206 | + } |
| 207 | + return cores; |
| 208 | + } |
| 209 | +} |
0 commit comments