net.openhft.affinity.AffinityLock Java Examples

The following examples show how to use net.openhft.affinity.AffinityLock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AffinityThreadFactory.java    From exchange-core with Apache License 2.0 6 votes vote down vote up
private void executePinned(@NotNull Runnable runnable) {

        try (final AffinityLock lock = getAffinityLockSync()) {

            final int threadId = threadsCounter.incrementAndGet();
            Thread.currentThread().setName(String.format("Thread-AF-%d-cpu%d", threadId, lock.cpuId()));

            log.debug("{} will be running on thread={} pinned to cpu {}",
                    runnable, Thread.currentThread().getName(), lock.cpuId());

            runnable.run();

        } finally {
            log.debug("Removing cpu lock/reservation from {}", runnable);
            synchronized (this) {
                affinityReservations.remove(runnable);
            }
        }
    }
 
Example #2
Source File: AffinityTestMain.java    From Java-Thread-Affinity with Apache License 2.0 6 votes vote down vote up
private static void acquireAndDoWork() {

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try (AffinityLock al = Affinity.acquireLock()) {
                    String threadName = Thread.currentThread().getName();
                    System.out.println("Thread (" + threadName + ") locked onto cpu " + al.cpuId());

                    while (true) {
                        System.out.println(df.format(new Date()) + " - Thread (" + threadName + ") doing work on cpu " + al.cpuId() + ". IsAllocated = " + al.isAllocated() + ", isBound = " + al.isBound() + ". " + al.toString());

                        try {
                            Thread.sleep(10000L);
                        } catch (InterruptedException e) {
                            //nothing
                        }
                    }
                }
            }
        });
        t.start();
    }
 
Example #3
Source File: AffinityNamedThreadFactory.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private synchronized AffinityLock acquireLockBasedOnLast() {
    final AffinityLock al = this.lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock
        .acquireLock(this.strategies);
    if (al.cpuId() >= 0) {
        if (!al.isBound()) {
            al.bind();
        }
        this.lastAffinityLock = al;
    }
    return al;
}
 
Example #4
Source File: FetchDataProducer.java    From kiritoDB with MIT License 5 votes vote down vote up
public void startFetch() {
    for (int threadNo = 0; threadNo < windowsNum; threadNo++) {
        final int threadPartition = threadNo;
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // bound core
                try (final AffinityLock al2 = AffinityLock.acquireLock()) {
                    for (int i = 0; i < Constant.partitionNum / windowsNum; i++) {
                        int dbIndex = i * windowsNum + threadPartition;
                        CacheItem cacheItem;
                        while (true) {
                            cacheItem = getCacheItem(dbIndex);
                            if (cacheItem != null) {
                                break;
                            }
                        }
                        commitLogs[dbIndex].loadAll(cacheItem.buffer);
                        cacheItem.ready = true;
                        while (true) {
                            if (cacheItem.allReach) {
                                break;
                            }
                        }
                        release(dbIndex);
                    }
                } catch (IOException e) {
                    logger.error("threadNo{} load failed", threadPartition, e);
                }
            }
        });
        t.setDaemon(true);
        t.start();
    }
}
 
Example #5
Source File: AffinityNamedThreadFactory.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
private synchronized AffinityLock acquireLockBasedOnLast() {
    AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock.acquireLock(strategies);
    if (al.cpuId() >= 0) {
        if (!al.isBound()) {
            al.bind();
        }
        lastAffinityLock = al;
    }
    return al;
}
 
Example #6
Source File: AffinityTestMain.java    From Java-Thread-Affinity with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        int cpus = 1;
        if (args.length == 0) {
            cpus = AffinityLock.cpuLayout().cpus() / 12;
        } else {
            cpus = Integer.valueOf(args[0]);
        }

        for (int i = 0; i < cpus; i++) {
            acquireAndDoWork();
        }
    }
 
Example #7
Source File: AffinityThreadFactory.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
private synchronized AffinityLock getAffinityLockSync() {
    return threadAffinityMode == ThreadAffinityMode.THREAD_AFFINITY_ENABLE_PER_PHYSICAL_CORE
            ? AffinityLock.acquireCore()
            : AffinityLock.acquireLock();
}
 
Example #8
Source File: ITOrderBookBase.java    From exchange-core with Apache License 2.0 4 votes vote down vote up
private void performanceTest(int numOrders, int targetOrderBookOrders) {

        try (AffinityLock cpuLock = AffinityLock.acquireLock()) {

            TestOrdersGenerator.GenResult genResult = TestOrdersGenerator.generateCommands(
                    numOrders,
                    targetOrderBookOrders,
                    1000,
                    TestOrdersGenerator.UID_PLAIN_MAPPER,
                    0,
                    false,
                    false,
                    TestOrdersGenerator.createAsyncProgressLogger(numOrders),
                    101572685);
            Iterable<OrderCommand> orderCommands = genResult.getCommands();
            log.debug("orderCommands size: {}", genResult.size());

            List<Float> perfResults = new ArrayList<>();
            for (int j = 0; j < 32; j++) {
                orderBook = createNewOrderBook();

                long t = System.currentTimeMillis();
                OrderCommand workCmd = new OrderCommand();
                for (OrderCommand cmd : orderCommands) {
                    cmd.writeTo(workCmd);
                    workCmd.resultCode = CommandResultCode.VALID_FOR_MATCHING_ENGINE;
                    IOrderBook.processCommand(orderBook, workCmd);
                }
                t = System.currentTimeMillis() - t;

                // weak compare orderBook final state just to make sure all commands executed same way
                // TODO compare events
                orderBook.validateInternalState();
                assertThat(orderBook.stateHash(), is(genResult.getFinalOrderbookHash()));

                float perfMt = (float) genResult.size() / (float) t / 1000.0f;
                perfResults.add(perfMt);
                float averageMt = (float) perfResults.stream().mapToDouble(x -> x).average().orElse(0);
                log.info("{}. {} MT/s ({} ms) average: {} MT/s", j, perfMt, t, averageMt);
            }

            double avg = (float) perfResults.stream().mapToDouble(x -> x).average().orElse(0);
            log.info("Average: {} MT/s", avg);
        }
    }
 
Example #9
Source File: CHMLatencyTestMain.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public static void main(String... ignored) throws IOException {
        AffinityLock lock = AffinityLock.acquireCore();
        File file = File.createTempFile("testCHMLatency", "deleteme");
//        File file = new File("testCHMLatency.deleteme");
        file.delete();
        ChronicleMap<LongValue, LongValue> countersMap =
                ChronicleMapBuilder.of(LongValue.class, LongValue.class)
                        .entries(KEYS)
                        .createPersistedTo(file);

        // add keys
        LongValue key = Values.newHeapInstance(LongValue.class);
        LongValue value = Values.newNativeReference(LongValue.class);
        for (long i = 0; i < KEYS; i++) {
            key.setValue(i);
            countersMap.acquireUsing(key, value);
            value.setValue(0);
        }
        System.out.println("Keys created");
//        Monitor monitor = new Monitor();
        LongValue value2 = Values.newNativeReference(LongValue.class);
        for (int t = 0; t < 5; t++) {
            for (int rate : new int[]{2 * 1000 * 1000, 1000 * 1000, 500 * 1000/*, 250 * 1000, 100 * 1000, 50 * 1000*/}) {
                Histogram times = new Histogram();
                int u = 0;
                long start = System.nanoTime();
                long delay = 1000 * 1000 * 1000L / rate;
                long next = start + delay;
                for (long j = 0; j < RUN_TIME * rate; j += KEYS) {
                    int stride = Math.max(1, KEYS / (RUN_TIME * rate));
                    // the timed part
                    for (int i = 0; i < KEYS && u < RUN_TIME * rate; i += stride) {
                        // busy wait for next time.
                        while (System.nanoTime() < next - 12) ;
//                        monitor.sample = System.nanoTime();
                        long start0 = next;

                        // start the update.
                        key.setValue(i);
                        LongValue using = countersMap.getUsing(key, value2);
                        if (using == null)
                            assertNotNull(using);
                        value2.addAtomicValue(1);

                        // calculate the time using the time it should have started, not when it was able.
                        long elapse = System.nanoTime() - start0;
                        times.sample(elapse);
                        next += delay;
                    }
//                    monitor.sample = Long.MAX_VALUE;
                }
                System.out.printf("run %d %,9d : ", t, rate);
                times.printPercentiles(" micro-seconds.");
            }
            System.out.println();
        }
//        monitor.running = false;
        countersMap.close();
        file.delete();
    }