Search in sources :

Example 1 with UnsafeDirectLittleEndian

use of io.netty.buffer.UnsafeDirectLittleEndian in project drill by apache.

the class BaseAllocator method dumpBuffers.

private void dumpBuffers(final StringBuilder sb, final Set<BufferLedger> ledgerSet) {
    for (final BufferLedger ledger : ledgerSet) {
        if (!ledger.isOwningLedger()) {
            continue;
        }
        final UnsafeDirectLittleEndian udle = ledger.getUnderlying();
        sb.append("UnsafeDirectLittleEndian[dentityHashCode == ");
        sb.append(Integer.toString(System.identityHashCode(udle)));
        sb.append("] size ");
        sb.append(Integer.toString(udle.capacity()));
        sb.append('\n');
    }
}
Also used : UnsafeDirectLittleEndian(io.netty.buffer.UnsafeDirectLittleEndian) BufferLedger(org.apache.drill.exec.memory.AllocationManager.BufferLedger)

Example 2 with UnsafeDirectLittleEndian

use of io.netty.buffer.UnsafeDirectLittleEndian in project drill by apache.

the class BaseAllocator method verifyAllocator.

/**
   * Verifies the accounting state of the allocator. Only works for DEBUG.
   *
   * <p>
   * This overload is used for recursive calls, allowing for checking that DrillBufs are unique across all allocators
   * that are checked.
   * </p>
   *
   * @param buffersSeen
   *          a map of buffers that have already been seen when walking a tree of allocators
   * @throws IllegalStateException
   *           when any problems are found
   */
private void verifyAllocator(final IdentityHashMap<UnsafeDirectLittleEndian, BaseAllocator> buffersSeen) {
    synchronized (DEBUG_LOCK) {
        // The remaining tests can only be performed if we're in debug mode.
        if (!DEBUG) {
            return;
        }
        final long allocated = getAllocatedMemory();
        // verify my direct descendants
        final Set<BaseAllocator> childSet = childAllocators.keySet();
        for (final BaseAllocator childAllocator : childSet) {
            childAllocator.verifyAllocator(buffersSeen);
        }
        /*
       * Verify my relationships with my descendants.
       *
       * The sum of direct child allocators' owned memory must be <= my allocated memory; my allocated memory also
       * includes DrillBuf's directly allocated by me.
       */
        long childTotal = 0;
        for (final BaseAllocator childAllocator : childSet) {
            childTotal += Math.max(childAllocator.getAllocatedMemory(), childAllocator.reservation);
        }
        if (childTotal > getAllocatedMemory()) {
            historicalLog.logHistory(logger);
            logger.debug("allocator[" + name + "] child event logs BEGIN");
            for (final BaseAllocator childAllocator : childSet) {
                childAllocator.historicalLog.logHistory(logger);
            }
            logger.debug("allocator[" + name + "] child event logs END");
            throw new IllegalStateException("Child allocators own more memory (" + childTotal + ") than their parent (name = " + name + " ) has allocated (" + getAllocatedMemory() + ')');
        }
        // Furthermore, the amount I've allocated should be that plus buffers I've allocated.
        long bufferTotal = 0;
        final Set<BufferLedger> ledgerSet = childLedgers.keySet();
        for (final BufferLedger ledger : ledgerSet) {
            if (!ledger.isOwningLedger()) {
                continue;
            }
            final UnsafeDirectLittleEndian udle = ledger.getUnderlying();
            /*
         * Even when shared, DrillBufs are rewrapped, so we should never see the same instance twice.
         */
            final BaseAllocator otherOwner = buffersSeen.get(udle);
            if (otherOwner != null) {
                throw new IllegalStateException("This allocator's drillBuf already owned by another allocator");
            }
            buffersSeen.put(udle, this);
            bufferTotal += udle.capacity();
        }
        // Preallocated space has to be accounted for
        final Set<Reservation> reservationSet = reservations.keySet();
        long reservedTotal = 0;
        for (final Reservation reservation : reservationSet) {
            if (!reservation.isUsed()) {
                reservedTotal += reservation.getSize();
            }
        }
        if (bufferTotal + reservedTotal + childTotal != getAllocatedMemory()) {
            final StringBuilder sb = new StringBuilder();
            sb.append("allocator[");
            sb.append(name);
            sb.append("]\nallocated: ");
            sb.append(Long.toString(allocated));
            sb.append(" allocated - (bufferTotal + reservedTotal + childTotal): ");
            sb.append(Long.toString(allocated - (bufferTotal + reservedTotal + childTotal)));
            sb.append('\n');
            if (bufferTotal != 0) {
                sb.append("buffer total: ");
                sb.append(Long.toString(bufferTotal));
                sb.append('\n');
                dumpBuffers(sb, ledgerSet);
            }
            if (childTotal != 0) {
                sb.append("child total: ");
                sb.append(Long.toString(childTotal));
                sb.append('\n');
                for (final BaseAllocator childAllocator : childSet) {
                    sb.append("child allocator[");
                    sb.append(childAllocator.name);
                    sb.append("] owned ");
                    sb.append(Long.toString(childAllocator.getAllocatedMemory()));
                    sb.append('\n');
                }
            }
            if (reservedTotal != 0) {
                sb.append(String.format("reserved total : %d bytes.", reservedTotal));
                for (final Reservation reservation : reservationSet) {
                    reservation.historicalLog.buildHistory(sb, 0, true);
                    sb.append('\n');
                }
            }
            logger.debug(sb.toString());
            final long allocated2 = getAllocatedMemory();
            if (allocated2 != allocated) {
                throw new IllegalStateException(String.format("allocator[%s]: allocated t1 (%d) + allocated t2 (%d). Someone released memory while in verification.", name, allocated, allocated2));
            }
            throw new IllegalStateException(String.format("allocator[%s]: buffer space (%d) + prealloc space (%d) + child space (%d) != allocated (%d)", name, bufferTotal, reservedTotal, childTotal, allocated));
        }
    }
}
Also used : UnsafeDirectLittleEndian(io.netty.buffer.UnsafeDirectLittleEndian) BufferLedger(org.apache.drill.exec.memory.AllocationManager.BufferLedger)

Aggregations

UnsafeDirectLittleEndian (io.netty.buffer.UnsafeDirectLittleEndian)2 BufferLedger (org.apache.drill.exec.memory.AllocationManager.BufferLedger)2