package com.epickrram.workshop.perf.support;

//////////////////////////////////////////////////////////////////////////////////
//   Copyright 2015   Mark Price     mark at epickrram.com                      //
//                                                                              //
//   Licensed under the Apache License, Version 2.0 (the "License");            //
//   you may not use this file except in compliance with the License.           //
//   You may obtain a copy of the License at                                    //
//                                                                              //
//       http://www.apache.org/licenses/LICENSE-2.0                             //
//                                                                              //
//   Unless required by applicable law or agreed to in writing, software        //
//   distributed under the License is distributed on an "AS IS" BASIS,          //
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   //
//   See the License for the specific language governing permissions and        //
//   limitations under the License.                                             //
//////////////////////////////////////////////////////////////////////////////////


import net.openhft.affinity.Affinity;

import java.util.Arrays;
import java.util.BitSet;
import java.util.concurrent.TimeUnit;

public enum Threads
{
    THREADS;

    public Runnable runOnCpu(final Runnable task, final int... cpus)
    {
        return () -> {
            setCurrentThreadAffinity(cpus);
            task.run();
        };
    }

    public void setCurrentThreadAffinity(final int... cpus)
    {
        if(cpus != null && cpus.length != 0)
        {
            final BitSet requiredAffinity = cpuListToBitMask(cpus);
            Affinity.setAffinity(requiredAffinity);

            if(!requiredAffinity.equals(Affinity.getAffinity()))
            {
                throw new IllegalStateException("Unable to set CPU affinity");
            }

            System.out.println("Set affinity for thread " + Thread.currentThread().getName() + " to " + Arrays.toString(cpus));
        }
    }

    public int getCurrentThreadId()
    {
        return Affinity.getThreadId();
    }

    public void sleep(final long duration, final TimeUnit unit)
    {
        try
        {
            Thread.sleep(unit.toMillis(duration));
        }
        catch (InterruptedException e)
        {
            throw new RuntimeException("Interrupted during sleep!");
        }
    }

    private BitSet cpuListToBitMask(final int[] cpus)
    {
        final BitSet bitSet = new BitSet();

        for(int cpu : cpus)
        {
            bitSet.set(cpu);
        }

        return bitSet;
    }
}

@@ -1325,16 +1325,91 @@
 inity;%0A%0A
+import java.io.IOException;%0Aimport java.lang.management.ManagementFactory;%0A
 import j
@@ -1789,37 +1789,22 @@
   if
+ 
 (cpus
- != null && cpus
 .length 
 != 0
@@ -1803,21 +1803,13 @@
 gth 
-!
+=
 = 0)
-%0A       
  %7B%0A 
@@ -1823,62 +1823,14 @@
     
-final BitSet requiredAffinity = cpuListToBitMask(cpus)
+return
 ;%0A  
@@ -1839,117 +1839,96 @@
     
+%7D%0A
     
-Affinity.setAffinity(requiredAffinity);%0A%0A            if(!requiredAffinity.equals(Affinity.getAffinity()))
+    if (cpus.length != 1) %7B%0A            throw new UnsupportedOperationException();
 %0A   
@@ -1936,333 +1936,1019 @@
     
+%7D%0A%0A
     
-%7B%0A
     
-            throw new IllegalStateException(%22Unable to set CPU affinity%22);%0A            %7D%0A%0A            System.out.println(%22Set affinity for thread %22 + Thread.currentThread().getName() + %22 to %22 + Arrays.toString(cpus));%0A        %7D%0A    %7D%0A%0A    public int getCurrentThreadId()%0A    %7B%0A        return Affinity.getThreadId();
+ProcessBuilder builder = new ProcessBuilder(%22taskset%22, %22-cp%22,%0A          Integer.toString(cpus%5B0%5D), Integer.toString(getCurrentThreadId()));%0A%0A        try %7B%0A            builder.start().waitFor();%0A            System.out.println(%22Set affinity for thread %22 + Thread.currentThread().getName() + %22 to %22 + Arrays.toString(cpus));%0A        %7D catch (InterruptedException %7C IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    public int getCurrentThreadId()%0A    %7B%0A        try%0A        %7B%0A            // TODO: if Java 9, then use ProcessHandle.%0A            final String pidPorpertyValue = System.getProperty(%22sun.java.launcher.pid%22);%0A%0A            if (null != pidPorpertyValue)%0A            %7B%0A                return Integer.parseInt(pidPorpertyValue);%0A            %7D%0A%0A            final String jvmName = ManagementFactory.getRuntimeMXBean().getName();%0A%0A            return Integer.parseInt(jvmName.split(%22@%22)%5B0%5D);%0A        %7D%0A        catch (final Throwable ex)%0A        %7B%0A            return -1;%0A        %7D
 %0A   

Use standard linux tools for taskset