use of net.openhft.affinity.AffinityLock in project HugeCollections-OLD by peter-lawrey.
the class SHMLatencyTestMain method main.
// TODO test passes but is under development.
public static void main(String... ignored) throws IOException {
AffinityLock lock = AffinityLock.acquireCore();
File file = File.createTempFile("testSHMLatency", "deleteme");
// File file = new File("testSHMLatency.deleteme");
file.delete();
SharedHashMap<LongValue, LongValue> countersMap = new SharedHashMapBuilder().entries(KEYS).entrySize(24).generatedKeyType(true).generatedValueType(true).file(file).kClass(LongValue.class).vClass(LongValue.class).create();
// add keys
LongValue key = DataValueClasses.newInstance(LongValue.class);
LongValue value = DataValueClasses.newInstance(LongValue.class);
for (long i = 0; i < KEYS; i++) {
key.setValue(i);
value.setValue(0);
countersMap.put(key, value);
}
System.out.println("Keys created");
// Monitor monitor = new Monitor();
LongValue value2 = DataValueClasses.newDirectReference(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();
}
use of net.openhft.affinity.AffinityLock in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueLatencyDistribution method runTest.
protected void runTest(@NotNull ChronicleQueue queue, int throughput) throws InterruptedException {
/*
Jvm.setExceptionHandlers(PrintExceptionHandler.ERR,
PrintExceptionHandler.OUT,
PrintExceptionHandler.OUT);
*/
Histogram histogramCo = new Histogram();
Histogram histogramIn = new Histogram();
Histogram histogramWr = new Histogram();
if (PRETOUCH) {
Thread pretoucher = new Thread(() -> {
ExcerptAppender appender = queue.acquireAppender();
while (!Thread.currentThread().isInterrupted()) {
appender.pretouch();
Jvm.pause(500);
}
});
pretoucher.setName("pret");
pretoucher.setDaemon(true);
pretoucher.start();
}
ExcerptAppender appender = queue.acquireAppender();
ExcerptTailer tailer = queue.createTailer();
String name = getClass().getName();
Thread tailerThread = new Thread(() -> {
AffinityLock lock = null;
try {
if (Jvm.getBoolean("enableTailerAffinity") || Jvm.getBoolean("enableAffinity")) {
lock = Affinity.acquireLock();
}
int counter = 0;
while (!Thread.currentThread().isInterrupted()) {
try {
// if (SAMPLING)
// sampler.thread(Thread.currentThread());
// boolean found = tailer.readDocument(myReadMarshallable);
boolean found;
try (DocumentContext dc = tailer.readingDocument()) {
found = dc.isPresent();
if (found) {
int count = counter++;
if (count == WARMUP) {
histogramCo.reset();
histogramIn.reset();
histogramWr.reset();
}
Bytes<?> bytes = dc.wire().bytes();
long startCo = bytes.readLong();
long startIn = bytes.readLong();
long now = System.nanoTime();
histogramCo.sample(now - startCo);
histogramIn.sample(now - startIn);
// if (count % 1_000_000 == 0) System.out.println("read " + count);
}
}
/*
if (SAMPLING) {
StackTraceElement[] stack = sampler.getAndReset();
if (stack != null) {
if (!stack[0].getClassName().equals(name) &&
!stack[0].getClassName().equals("java.lang.Thread")) {
StringBuilder sb = new StringBuilder();
Jvm.trimStackTrace(sb, stack);
// System.out.println(sb);
}
} else if (!found) {
Thread.yield();
}
}
*/
} catch (Exception e) {
break;
}
}
} finally {
if (lock != null) {
lock.release();
}
}
});
Thread appenderThread = new Thread(() -> {
AffinityLock lock = null;
try {
if (Jvm.getBoolean("enableAppenderAffinity") || Jvm.getBoolean("enableAffinity")) {
lock = Affinity.acquireLock();
}
long next = System.nanoTime();
long interval = 1_000_000_000 / throughput;
Map<String, Integer> stackCount = new LinkedHashMap<>();
BytesStore bytes24 = BytesStore.nativeStore(24);
for (int i = -WARMUP; i < ITERATIONS; i++) {
long s0 = System.nanoTime();
if (s0 < next) {
do ; while (System.nanoTime() < next);
// if we failed to come out of the spin loop on time, reset next.
next = System.nanoTime();
}
if (SAMPLING) {
sampler.thread(Thread.currentThread());
}
long start = System.nanoTime();
try (@NotNull DocumentContext dc = appender.writingDocument(false)) {
Bytes<?> bytes2 = dc.wire().bytes();
// when it should have started
bytes2.writeLong(next);
// when it actually started.
bytes2.writeLong(start);
bytes2.write(bytes24);
}
long time = System.nanoTime() - start;
histogramWr.sample(start - next);
if (SAMPLING && time > SAMPLE_THRESHOLD_NS && i > 0) {
StackTraceElement[] stack = sampler.getAndReset();
if (stack != null) {
if (!stack[0].getClassName().equals(name) && !stack[0].getClassName().equals("java.lang.Thread")) {
StringBuilder sb = new StringBuilder();
Jvm.trimStackTrace(sb, stack);
stackCount.compute(sb.toString(), (k, v) -> v == null ? 1 : v + 1);
}
}
}
next += interval;
// if (i % 1_000_000 == 0) System.out.println("wrote " + i);
}
stackCount.entrySet().stream().filter(e -> e.getValue() > 1).sorted(Comparator.comparingInt(Map.Entry::getValue)).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (lock != null) {
lock.release();
}
}
});
tailerThread.setName("tail");
tailerThread.start();
appenderThread.setName("appd");
appenderThread.start();
appenderThread.join();
// Pause to allow tailer to catch up (if needed)
Jvm.pause(500);
tailerThread.interrupt();
tailerThread.join();
// System.out.println("wr: " + histogramWr.toMicrosFormat());
// System.out.println("in: " + histogramIn.toMicrosFormat());
// System.out.println("co: " + histogramCo.toMicrosFormat());
}
use of net.openhft.affinity.AffinityLock in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueTwoThreads method doTest.
void doTest(boolean buffered) throws InterruptedException {
File name = getTmpDir();
AtomicLong counter = new AtomicLong();
Thread tailerThread = new Thread(() -> {
AffinityLock rlock = AffinityLock.acquireLock();
Bytes bytes = NativeBytes.nativeBytes(BYTES_LENGTH).unchecked(true);
try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).testBlockSize().build()) {
ExcerptTailer tailer = rqueue.createTailer();
while (!Thread.interrupted()) {
bytes.clear();
if (tailer.readBytes(bytes)) {
counter.incrementAndGet();
}
}
} finally {
if (rlock != null) {
rlock.release();
}
System.out.printf("Read %,d messages", counter.intValue());
}
}, "tailer thread");
long runs = 50_000;
Thread appenderThread = new Thread(() -> {
AffinityLock wlock = AffinityLock.acquireLock();
try {
ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).rollCycle(SMALL_DAILY).testBlockSize().buffered(buffered).build();
ExcerptAppender appender = wqueue.acquireAppender();
Bytes bytes = Bytes.allocateDirect(BYTES_LENGTH).unchecked(true);
long next = System.nanoTime() + INTERVAL_US * 1000;
for (int i = 0; i < runs; i++) {
while (System.nanoTime() < next) /* busy wait*/
;
long start = next;
bytes.readPositionRemaining(0, BYTES_LENGTH);
bytes.writeLong(0L, start);
appender.writeBytes(bytes);
next += INTERVAL_US * 1000;
}
wqueue.close();
} finally {
if (wlock != null) {
wlock.release();
}
}
}, "appender thread");
tailerThread.start();
Jvm.pause(100);
appenderThread.start();
appenderThread.join();
// Pause to allow tailer to catch up (if needed)
for (int i = 0; i < 10; i++) {
if (runs != counter.get())
Jvm.pause(Jvm.isDebug() ? 10000 : 100);
}
for (int i = 0; i < 10; i++) {
tailerThread.interrupt();
tailerThread.join(100);
}
assertEquals(runs, counter.get());
}
use of net.openhft.affinity.AffinityLock in project Chronicle-Core by OpenHFT.
the class JLBH method start.
/**
* Start benchmark
*/
public void start() {
long warmupStart = initStartOSJitterMonitorWarmup();
AffinityLock lock = jlbhOptions.acquireLock.get();
try {
for (int run = 0; run < jlbhOptions.runs; run++) {
long runStart = System.currentTimeMillis();
long startTimeNs = System.nanoTime();
for (int i = 0; i < jlbhOptions.iterations; i++) {
if (i == 0 && run == 0) {
warmupComplete(warmupStart);
runStart = System.currentTimeMillis();
startTimeNs = System.nanoTime();
} else if (jlbhOptions.accountForCoordinatedOmission) {
startTimeNs += latencyBetweenTasks;
long millis = (startTimeNs - System.nanoTime()) / 1000000 - 2;
if (millis > 0) {
Jvm.pause(millis);
}
Jvm.busyWaitUntil(startTimeNs);
} else {
if (latencyBetweenTasks > 2e6) {
long end = System.nanoTime() + latencyBetweenTasks;
Jvm.pause(latencyBetweenTasks / 1_000_000 - 1);
// account for jitter in Thread.sleep() and wait until a fixed point in time
Jvm.busyWaitUntil(end);
} else {
Jvm.busyWaitMicros(latencyBetweenTasks / 1000);
}
startTimeNs = System.nanoTime();
}
jlbhOptions.jlbhTask.run(startTimeNs);
}
endOfRun(run, runStart);
}
} finally {
Jvm.pause(5);
lock.release();
Jvm.pause(5);
}
endOfAllRuns();
}
use of net.openhft.affinity.AffinityLock in project Chronicle-Queue by OpenHFT.
the class QueueMultiThreadedJLBHBenchmark method init.
@Override
public void init(JLBH jlbh) {
this.jlbh = jlbh;
IOTools.deleteDirWithFiles(path, 10);
sourceQueue = createQueueInstance();
sinkQueue = useSingleQueueInstance ? sourceQueue : createQueueInstance();
appender = sourceQueue.acquireAppender().disableThreadSafetyCheck(true);
tailer = sinkQueue.createTailer().disableThreadSafetyCheck(true);
NanoSampler readProbe = jlbh.addProbe("read");
writeProbe = jlbh.addProbe("write");
if (usePretoucher) {
pretoucherExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("pretoucher", true));
pretoucherExecutorService.scheduleAtFixedRate(() -> sourceQueue.acquireAppender().pretouch(), 1, 200, TimeUnit.MILLISECONDS);
}
tailerThread = new Thread(() -> {
try (final AffinityLock affinityLock = AffinityLock.acquireLock(tailerAffinity)) {
Datum datum2 = new Datum(messageSize);
while (!stopped) {
long beforeReadNs = System.nanoTime();
try (DocumentContext dc = tailer.readingDocument()) {
if (!dc.isPresent())
continue;
datum2.readMarshallable(dc.wire().bytes());
long now = System.nanoTime();
jlbh.sample(now - datum2.ts);
readProbe.sampleNanos(now - beforeReadNs);
}
}
}
});
tailerThread.start();
}
Aggregations