Changeset 67b7feb


Ignore:
Timestamp:
Jul 6, 2018 4:59:14 PM (2 years ago)
Author:
zab2 <zab2@…>
Branches:
master
Children:
998931f
Parents:
9ac3157 (diff), f9c3f82b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

propagate from branch 'i2p.i2p' (head 6c6a7520cad38845cee83103c323782b426b3ff5)

to branch 'i2p.i2p.zab.2263' (head 727b9053d85f58811d1f5e05ccc697bfd9a454a2)

Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/transport/ntcp/EventPumper.java

    r9ac3157 r67b7feb  
    2929import net.i2p.router.RouterContext;
    3030import net.i2p.router.transport.FIFOBandwidthLimiter;
     31import net.i2p.util.TryCache;
    3132import net.i2p.util.Addresses;
    3233import net.i2p.util.ConcurrentHashSet;
     
    5455    private final ObjectCounter<ByteArray> _blockedIPs;
    5556    private long _expireIdleWriteTime;
    56     private boolean _useDirect;
     57    private static boolean _useDirect;
    5758   
    5859    /**
     
    6465    private static final int MAX_CACHE_SIZE = 64;
    6566
    66     /**
    67      *  Read buffers. (write buffers use wrap())
    68      *  Shared if there are multiple routers in the JVM
    69      *  Note that if the routers have different PROP_DIRECT settings this will have a mix,
    70      *  so don't do that.
    71      */
    72     private static final LinkedBlockingQueue<ByteBuffer> _bufCache = new LinkedBlockingQueue<ByteBuffer>(MAX_CACHE_SIZE);
     67    private static class BufferFactory implements TryCache.ObjectFactory<ByteBuffer> {
     68        public ByteBuffer newInstance() {
     69            if (_useDirect)
     70                return ByteBuffer.allocateDirect(BUF_SIZE);
     71            else
     72                return ByteBuffer.allocate(BUF_SIZE);
     73        }
     74    }
     75   
     76    private static final TryCache<ByteBuffer> _bufferCache = new TryCache<>(new BufferFactory(), MAX_CACHE_SIZE);
    7377
    7478    /**
     
    320324
    321325
    322                 // Clear the cache if the user changes the setting,
    323                 // so we can test the effect.
    324                 boolean newUseDirect = _context.getBooleanProperty(PROP_DIRECT);
    325                 if (_useDirect != newUseDirect) {
    326                     _useDirect = newUseDirect;
    327                     _bufCache.clear();
    328                 }
     326                _useDirect = _context.getBooleanProperty(PROP_DIRECT);
    329327            } catch (RuntimeException re) {
    330328                _log.error("Error in the event pumper", re);
     
    364362        _wantsRegister.clear();
    365363        _wantsWrite.clear();
    366         _bufCache.clear();
    367364    }
    368365   
     
    463460
    464461    /**
    465      *  How many to keep in reserve.
    466      *  Shared if there are multiple routers in the JVM
    467      */
    468     private static int _numBufs = MIN_BUFS;
    469     private static int __consecutiveExtra;
    470 
    471     /**
    472462     *  High-frequency path in thread.
    473463     */
    474464    private ByteBuffer acquireBuf() {
    475         ByteBuffer rv = _bufCache.poll();
    476         // discard buffer if _useDirect setting changes
    477         if (rv == null || rv.isDirect() != _useDirect) {
    478             if (_useDirect)
    479                 rv = ByteBuffer.allocateDirect(BUF_SIZE);
    480             else
    481                 rv = ByteBuffer.allocate(BUF_SIZE);
    482             _numBufs++;
    483         }
    484         return rv;
     465        return _bufferCache.acquire();
    485466    }
    486467   
     
    491472     */
    492473    public static void releaseBuf(ByteBuffer buf) {
    493         // double check
    494         if (buf.capacity() < BUF_SIZE) {
    495             I2PAppContext.getGlobalContext().logManager().getLog(EventPumper.class).error("Bad size " + buf.capacity(), new Exception());
    496             return;
    497         }
    498474        buf.clear();
    499         int extra = _bufCache.size();
    500         boolean cached = extra < _numBufs;
    501 
    502         // TODO always offer if direct?
    503         if (cached) {
    504             _bufCache.offer(buf);
    505             if (extra > MIN_BUFS) {
    506                 __consecutiveExtra++;
    507                 if (__consecutiveExtra >= 20) {
    508                     if (_numBufs > MIN_BUFS)
    509                         _numBufs--;
    510                     __consecutiveExtra = 0;
    511                 }
    512             }
    513         }
     475        _bufferCache.release(buf);
    514476    }
    515477   
  • router/java/src/net/i2p/router/transport/udp/UDPPacket.java

    r9ac3157 r67b7feb  
    1313import net.i2p.router.transport.FIFOBandwidthLimiter;
    1414import net.i2p.router.util.CDQEntry;
     15import net.i2p.util.TryCache;
    1516import net.i2p.util.Addresses;
    1617import net.i2p.util.Log;
     
    4647    private FIFOBandwidthLimiter.Request _bandwidthRequest;
    4748 
     49    private static class PacketFactory implements TryCache.ObjectFactory<UDPPacket> {
     50        static RouterContext context;
     51        public UDPPacket newInstance() {
     52            return new UDPPacket(context);
     53        }
     54    }
     55   
    4856    //  Warning - this mixes contexts in a multi-router JVM
    49     private static final Queue<UDPPacket> _packetCache;
     57    private static final TryCache<UDPPacket> _packetCache;
     58    private static final TryCache.ObjectFactory<UDPPacket> _packetFactory;
    5059    private static final boolean CACHE = true;
    51     private static final int MIN_CACHE_SIZE = 64;
    5260    private static final int MAX_CACHE_SIZE = 256;
    5361    static {
    5462        if (CACHE) {
    55             long maxMemory = SystemVersion.getMaxMemory();
    56             int csize = (int) Math.max(MIN_CACHE_SIZE, Math.min(MAX_CACHE_SIZE, maxMemory / (1024*1024)));
    57             _packetCache = new LinkedBlockingQueue<UDPPacket>(csize);
     63            _packetFactory = new PacketFactory();
     64            _packetCache = new TryCache<>(_packetFactory, MAX_CACHE_SIZE);
    5865        } else {
    5966            _packetCache = null;
     67            _packetFactory = null;
    6068        }
    6169    }
     
    399407        UDPPacket rv = null;
    400408        if (CACHE) {
    401             rv = _packetCache.poll();
    402             if (rv != null) {
    403                 synchronized(rv) {
    404                     if (!rv._released) {
    405                         Log log = rv._context.logManager().getLog(UDPPacket.class);
    406                         log.error("Unreleased cached packet", new Exception());
    407                         rv = null;
    408                     } else {
    409                         rv.init(ctx);
    410                     }
    411                 }
    412             }
     409            PacketFactory.context = ctx;
     410            rv = _packetCache.acquire();
     411            rv.init(ctx);
    413412        }
    414413        if (rv == null)
     
    441440        if (!CACHE)
    442441            return;
    443         _packetCache.offer(this);
     442        _packetCache.release(this);
    444443    }
    445444   
     
    449448     */
    450449    public static void clearCache() {
    451         if (CACHE)
     450        if (CACHE) {
     451            PacketFactory.context = null;
    452452            _packetCache.clear();
     453        }
    453454    }
    454455
Note: See TracChangeset for help on using the changeset viewer.