use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class LatencyDistributionMain method runTest.
protected void runTest(@NotNull ChronicleQueue queue, @NotNull ChronicleQueue queue2) throws InterruptedException {
Histogram histogramCo = new Histogram();
Histogram histogramIn = new Histogram();
Histogram histogramWr = new Histogram();
Thread pretoucher = new Thread(() -> {
ExcerptAppender appender = queue.acquireAppender();
try {
while (!Thread.currentThread().isInterrupted()) {
appender.pretouch();
Jvm.pause(50);
}
} catch (Exception e) {
if (!appender.isClosed())
e.printStackTrace();
}
});
pretoucher.setDaemon(true);
pretoucher.start();
ExcerptAppender appender = queue.acquireAppender();
// two queues as most like in a different process.
ExcerptTailer tailer = queue2.createTailer();
String name = getClass().getName();
Thread tailerThread = new Thread(() -> {
AffinityLock lock = null;
try {
if (Jvm.getBoolean("enableTailerAffinity") || !Jvm.getBoolean("disableAffinity")) {
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 % INTLOG_INTERVAL == 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("disableAffinity")) {
lock = Affinity.acquireLock();
}
long next = System.nanoTime();
long interval = 1_000_000_000 / throughput;
Map<String, Integer> stackCount = new LinkedHashMap<>();
BytesStore<?, ?> bytes24 = BytesStore.nativeStoreFrom(new byte[Main.size - 16]);
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)) {
Wire wire = dc.wire();
Bytes<?> bytes2 = wire.bytes();
// when it should have started
bytes2.writeLong(next);
// when it actually started.
bytes2.writeLong(start);
bytes2.write(bytes24);
ThroughputMain.addToEndOfCache(wire);
}
long time = System.nanoTime() - start;
histogramWr.sample(start - next);
if (SAMPLING && time > 1e3 && 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 % INTLOG_INTERVAL == 0)
System.out.println("wrote " + i);
}
stackCount.entrySet().stream().filter(e -> e.getValue() > 1).forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (lock != null) {
lock.release();
}
}
});
tailerThread.start();
appenderThread.start();
appenderThread.join();
pretoucher.interrupt();
pretoucher.join();
// Pause to allow tailer to catch up (if needed)
Jvm.pause(500);
tailerThread.interrupt();
tailerThread.join();
System.out.println("wr: " + histogramWr.toLongMicrosFormat());
System.out.println("in: " + histogramIn.toLongMicrosFormat());
System.out.println("co: " + histogramCo.toLongMicrosFormat());
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueue method newIndex.
/**
* Creates a new Excerpt containing and index which will be 1L << 17L bytes long, This method is used for creating
* both the primary and secondary indexes. Chronicle Queue uses a root primary index ( each entry in the primary
* index points to a unique a secondary index. The secondary index only records the addressForRead of every 64th except,
* the except are linearly scanned from there on.
*
* @return the addressForRead of the Excerpt containing the usable index, just after the header
*/
long newIndex() {
final ByteableLongArrayValues array = longArray.get();
final long size = array.sizeInBytes(NUMBER_OF_ENTRIES_IN_EACH_INDEX);
final Bytes buffer = NativeBytes.nativeBytes(size);
buffer.zeroOut(0, size);
final Wire wire = WireUtil.createWire(this.builder.wireType(), buffer);
wire.write("index").int64array(NUMBER_OF_ENTRIES_IN_EACH_INDEX);
buffer.flip();
return appendMetaDataReturnAddress(buffer);
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class StagedPerformanceMain method producer.
public static void producer(int count, int intervalNS) {
IFacadeAll datum = Values.newNativeReference(IFacadeAll.class);
long datumSize = datum.maxSize();
assert datumSize >= 500 && datumSize % 8 == 0;
long start = System.nanoTime();
try (ExcerptAppender appender = q0.acquireAppender()) {
Thread pretoucher = new Thread(() -> {
ExcerptAppender appender0 = q0.acquireAppender();
Thread thread = Thread.currentThread();
while (!thread.isInterrupted()) {
appender0.pretouch();
Jvm.pause(10);
}
});
pretoucher.setDaemon(true);
pretoucher.start();
for (int i = 1; i <= count; i++) {
long end = start + (long) intervalNS * i;
while (System.nanoTime() <= end) Thread.yield();
try (DocumentContext dc = appender.writingDocument()) {
Wire wire = dc.wire();
wire.write("data");
Bytes<?> bytes = wire.bytes();
long wp = bytes.writePosition();
int paddingToAdd = (int) (-wp & 7);
wire.addPadding(paddingToAdd);
datum.bytesStore(bytes, bytes.writePosition(), datumSize);
bytes.writeSkip(datumSize);
// write the data
populateDatum(count, datum, i);
}
}
pretoucher.interrupt();
try {
pretoucher.join(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long time = System.nanoTime() - start;
if (!WARMUP) {
System.out.println("Producer wrote " + count + " messages in " + time / 1000 / 1e6 + " seconds");
}
}
Aggregations