@@ -79,16 +79,25 @@ public static void cpuLayout(CpuLayout cpuLayout) {
7979 CORES = new TreeMap <Integer , AffinityLock []>();
8080 for (int i = 0 ; i < cpuLayout .cpus (); i ++) {
8181 AffinityLock al = LOCKS [i ] = new AffinityLock (i , ((BASE_AFFINITY >> i ) & 1 ) != 0 , ((RESERVED_AFFINITY >> i ) & 1 ) != 0 );
82- final int id = al .id ;
83- int core = coreForId (id );
84- AffinityLock [] als = CORES .get (core );
82+ final int layoutId = al .cpuId ;
83+ int logicalCpuId = coreForId (layoutId );
84+ AffinityLock [] als = CORES .get (logicalCpuId );
8585 if (als == null )
86- CORES .put (core , als = new AffinityLock [threads ]);
87- als [cpuLayout .threadId (id )] = al ;
86+ CORES .put (logicalCpuId , als = new AffinityLock [threads ]);
87+ als [cpuLayout .threadId (layoutId )] = al ;
8888 }
8989 }
9090 }
9191
92+ /**
93+ * Translate a layout id into a logical cpu id.
94+ * <p/>
95+ * This translation is perform so that regardless of how
96+ *
97+ * @param id
98+ * @return
99+ */
100+
92101 private static int coreForId (int id ) {
93102 return cpuLayout .socketId (id ) * cpuLayout .coresPerSocket () + cpuLayout .coreId (id );
94103 }
@@ -175,11 +184,10 @@ private static AffinityLock acquireCore(boolean bind, int cpuId, AffinityStrateg
175184 for (AffinityStrategy strategy : strategies ) {
176185 LOOP :
177186 for (AffinityLock [] als : CORES .descendingMap ().values ()) {
178- for (AffinityLock al : als ) {
179- int core = coreForId (al .id );
180- if (!al .canReserve () || !strategy .matches (cpuId , core ))
187+ for (AffinityLock al : als )
188+ if (!al .canReserve () || !strategy .matches (cpuId , al .cpuId ))
181189 continue LOOP ;
182- }
190+
183191 final AffinityLock al = als [0 ];
184192 al .assignCurrentThread (bind , true );
185193 return al ;
@@ -218,14 +226,14 @@ else if (al.base)
218226 }
219227
220228 //// Non static fields and methods.
221- private final int id ;
229+ private final int cpuId ;
222230 private final boolean base ;
223231 private final boolean reserved ;
224232 boolean bound = false ;
225233 Thread assignedThread ;
226234
227- AffinityLock (int id , boolean base , boolean reserved ) {
228- this .id = id ;
235+ AffinityLock (int cpuId , boolean base , boolean reserved ) {
236+ this .cpuId = cpuId ;
229237 this .base = base ;
230238 this .reserved = reserved ;
231239 }
@@ -250,13 +258,13 @@ public void bind() {
250258 */
251259 public void bind (boolean wholeCore ) {
252260 if (bound && assignedThread != null && assignedThread .isAlive ())
253- throw new IllegalStateException ("cpu " + id + " already bound to " + assignedThread );
261+ throw new IllegalStateException ("cpu " + cpuId + " already bound to " + assignedThread );
254262
255263 if (wholeCore ) {
256- int core = coreForId (id );
264+ int core = coreForId (cpuId );
257265 for (AffinityLock al : CORES .get (core )) {
258266 if (bound && al .assignedThread != null && al .assignedThread .isAlive ()) {
259- LOGGER .severe ("cpu " + al .id + " already bound to " + al .assignedThread );
267+ LOGGER .severe ("cpu " + al .cpuId + " already bound to " + al .assignedThread );
260268 } else {
261269 al .bound = true ;
262270 al .assignedThread = Thread .currentThread ();
@@ -266,7 +274,7 @@ public void bind(boolean wholeCore) {
266274 StringBuilder sb = new StringBuilder ().append ("Assigning core " ).append (core );
267275 String sep = ": cpus " ;
268276 for (AffinityLock al : CORES .get (core )) {
269- sb .append (sep ).append (al .id );
277+ sb .append (sep ).append (al .cpuId );
270278 sep = ", " ;
271279 }
272280 sb .append (" to " ).append (assignedThread );
@@ -276,10 +284,10 @@ public void bind(boolean wholeCore) {
276284 bound = true ;
277285 assignedThread = Thread .currentThread ();
278286 if (LOGGER .isLoggable (Level .INFO ))
279- LOGGER .info ("Assigning cpu " + id + " to " + assignedThread );
287+ LOGGER .info ("Assigning cpu " + cpuId + " to " + assignedThread );
280288 }
281- if (id >= 0 )
282- AffinitySupport .setAffinity (1L << id );
289+ if (cpuId >= 0 )
290+ AffinitySupport .setAffinity (1L << cpuId );
283291 }
284292
285293 private boolean canReserve () {
@@ -301,7 +309,7 @@ private boolean canReserve() {
301309 * @return A matching AffinityLock.
302310 */
303311 public AffinityLock acquireLock (AffinityStrategy ... strategies ) {
304- return acquireLock (false , id , strategies );
312+ return acquireLock (false , cpuId , strategies );
305313 }
306314
307315 /**
@@ -314,11 +322,11 @@ public void release() {
314322 Thread at = al .assignedThread ;
315323 if (at == t ) {
316324 if (LOGGER .isLoggable (Level .INFO ))
317- LOGGER .info ("Releasing cpu " + al .id + " from " + t );
325+ LOGGER .info ("Releasing cpu " + al .cpuId + " from " + t );
318326 al .assignedThread = null ;
319327 al .bound = false ;
320328 } else if (at != null && !at .isAlive ()) {
321- LOGGER .warning ("Releasing cpu " + al .id + " from " + t + " as it is not alive." );
329+ LOGGER .warning ("Releasing cpu " + al .cpuId + " from " + t + " as it is not alive." );
322330 al .assignedThread = null ;
323331 al .bound = false ;
324332 }
0 commit comments