2121
2222import java .io .File ;
2323import java .io .IOException ;
24+ import java .util .NavigableMap ;
25+ import java .util .TreeMap ;
2426import java .util .logging .Level ;
2527import java .util .logging .Logger ;
2628
@@ -41,7 +43,7 @@ public class AffinityLock {
4143 private static final Logger LOGGER = Logger .getLogger (AffinityLock .class .getName ());
4244
4345 private static final AffinityLock [] LOCKS = new AffinityLock [PROCESSORS ];
44- private static AffinityLock [][] CORES ; // set by cpuLayout()
46+ private static NavigableMap < Integer , AffinityLock []> CORES ; // set by cpuLayout()
4547 private static final AffinityLock NONE = new AffinityLock (-1 , false , false );
4648 private static CpuLayout cpuLayout = new NoCpuLayout (PROCESSORS );
4749
@@ -61,11 +63,14 @@ public static void cpuLayout(CpuLayout cpuLayout) {
6163 AffinityLock .cpuLayout = cpuLayout ;
6264 int cores = cpuLayout .sockets () * cpuLayout .coresPerSocket ();
6365 int threads = cpuLayout .threadsPerCore ();
64- CORES = new AffinityLock [cores ][ threads ] ;
66+ CORES = new TreeMap < Integer , AffinityLock []>() ;
6567 for (AffinityLock al : LOCKS ) {
6668 final int id = al .id ;
6769 int core = coreForId (id );
68- CORES [core ][cpuLayout .threadId (id )] = al ;
70+ AffinityLock [] als = CORES .get (core );
71+ if (als == null )
72+ CORES .put (core , als = new AffinityLock [threads ]);
73+ als [cpuLayout .threadId (id )] = al ;
6974 }
7075 }
7176 }
@@ -122,10 +127,10 @@ private static AffinityLock acquireCore(boolean bind, int cpuId, AffinityStrateg
122127 synchronized (AffinityLock .class ) {
123128 for (AffinityStrategy strategy : strategies ) {
124129 LOOP :
125- for (int i = CORES .length - 1 ; i > 0 ; i --) {
126- AffinityLock [] als = CORES [i ];
130+ for (AffinityLock [] als : CORES .descendingMap ().values ()) {
127131 for (AffinityLock al : als ) {
128- if (!al .canReserve () || !strategy .matches (cpuId , i ))
132+ int core = coreForId (al .id );
133+ if (!al .canReserve () || !strategy .matches (cpuId , core ))
129134 continue LOOP ;
130135 }
131136 final AffinityLock al = als [0 ];
@@ -191,7 +196,7 @@ public void bind(boolean wholeCore) {
191196
192197 if (wholeCore ) {
193198 int core = coreForId (id );
194- for (AffinityLock al : CORES [ core ] ) {
199+ for (AffinityLock al : CORES . get ( core ) ) {
195200 if (bound && al .assignedThread != null && al .assignedThread .isAlive ()) {
196201 LOGGER .severe ("cpu " + al .id + " already bound to " + al .assignedThread );
197202 } else {
@@ -202,7 +207,7 @@ public void bind(boolean wholeCore) {
202207 if (LOGGER .isLoggable (Level .INFO )) {
203208 StringBuilder sb = new StringBuilder ().append ("Assigning core " ).append (core );
204209 String sep = ": cpus " ;
205- for (AffinityLock al : CORES [ core ] ) {
210+ for (AffinityLock al : CORES . get ( core ) ) {
206211 sb .append (sep ).append (al .id );
207212 sep = ", " ;
208213 }
0 commit comments