@Override
  public void reset(int size) {
    // reset allocations/frees count first, as it's not used
    // for any functionality but to show information
    metrics.reset();

    // --- used
    try {
      usedLock.writeLock().lock();
      used.clear();
      usedMemorySize.set(0);
    } finally {
      usedLock.writeLock().unlock();
    }
    metrics.mark("vmtable.usedSize", usedMemorySize.longValue());
    metrics.mark("vmtable.usedBlocksCount", used.size());

    // --- free
    try {
      freeLock.writeLock().lock();
      free.clear();
      free.add(new TableBlock(0, size));
      freeMemorySize.set(size);
    } finally {
      freeLock.writeLock().unlock();
    }
    metrics.mark("vmtable.freeSize", freeMemorySize.longValue());
    metrics.mark("vmtable.freeBlocksCount", free.size());
  }
예제 #2
0
 /** longValue returns current value. */
 public void testLongValue() {
   AtomicInteger ai = new AtomicInteger();
   assertEquals(0L, ai.longValue());
   for (int x : VALUES) {
     ai.set(x);
     assertEquals((long) x, ai.longValue());
   }
 }
  @Override
  public Block allocate(int size) {
    if (size <= 0) {
      throw new OutOfBoundException("Size can't be negative or be zero: " + size);
    }

    metrics.increment("vmtable.totalAllocations");

    TimeContext timer = metrics.getTimer("vmtable.allocationTime");
    timer.start();

    final TableBlock freeBlock = findBlockToAllocateFrom(size);
    if (freeBlock != null) {
      final TableBlock result;
      try {
        result = new TableBlock(freeBlock.getAddress(), size);
        if (freeBlock.getSize() == size) {
          freeBlock.resize(0, 0);
          removeBlock(free, freeBlock, freeLock);
          metrics.decrement("vmtable.fragmentation");
        } else {
          freeBlock.resize(freeBlock.getAddress() + size, freeBlock.getSize() - size);
          metrics.increment("vmtable.fragmentation");
        }
        freeMemorySize.addAndGet(-size);
      } finally {
        // unlock asap
        freeBlock.unlock();
      }

      insertBlock(used, result, usedLock);

      usedMemorySize.addAndGet(size);

      timer.stop();
      metrics.mark("vmtable.freeSize", freeMemorySize.longValue());
      metrics.mark("vmtable.usedSize", usedMemorySize.longValue());
      metrics.mark("vmtable.freeBlocksCount", free.size());
      metrics.mark("vmtable.usedBlocksCount", used.size());

      return result;
    }

    timer.stop();

    metrics.increment("vmtable.failedAllocations");

    return null;
  }
예제 #4
0
 /** 1. 从Number中继承过来的几个方法, 并实现了它。 */
 public void testValue() {
   AtomicInteger ai = new AtomicInteger(22);
   System.out.println(ai.byteValue());
   System.out.println(ai.shortValue());
   System.out.println(ai.intValue());
   System.out.println(ai.longValue());
   System.out.println(ai.floatValue());
   System.out.println(ai.doubleValue());
 }
  @Override
  public boolean free(Block block) {
    if (block == null) {
      return false;
    }

    metrics.increment("vmtable.totalFrees");

    TimeContext timer = metrics.getTimer("vmtable.freeTime");
    timer.start();

    TableBlock tableBlock = getSimilarBlock(used, block, usedLock);
    if (tableBlock != null) {
      if (removeBlock(used, tableBlock, usedLock)) {
        int size = tableBlock.getSize();
        usedMemorySize.addAndGet(-size);

        addFreeBlock(new TableBlock(tableBlock.getAddress(), tableBlock.getSize()));

        freeMemorySize.addAndGet(size);

        tableBlock.resize(0, 0);
        tableBlock.unlock();

        timer.stop();

        metrics.mark("vmtable.freeSize", freeMemorySize.longValue());
        metrics.mark("vmtable.usedSize", usedMemorySize.longValue());
        metrics.mark("vmtable.freeBlocksCount", free.size());
        metrics.mark("vmtable.usedBlocksCount", used.size());

        return true;
      }
    }

    timer.stop();

    metrics.increment("vmtable.failedFrees");

    return false;
  }
  @Override
  public long synchronizeData(final Cache<Object, Object> cache) throws CacheException {
    int threads = Runtime.getRuntime().availableProcessors();
    PersistenceManager loaderManager = getPersistenceManager(cache);
    Set<CLInterfaceLoader> loaders = loaderManager.getStores(CLInterfaceLoader.class);

    for (CLInterfaceLoader loader : loaders) {
      MarshalledEntry loadedKnownKey = loader.load(KNOWN_KEY);
      if (loadedKnownKey != null) {
        Set<Object> keys;
        try {
          keys = jsonMapper.readValue((String) loadedKnownKey.getValue(), HashSet.class);
        } catch (IOException e) {
          throw new CacheException("Unable to read JSON value: " + loadedKnownKey.getValue(), e);
        }

        ExecutorService es = Executors.newFixedThreadPool(threads);
        final AtomicInteger count = new AtomicInteger(0);
        for (final Object key : keys) {
          es.submit(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    cache.get(key);
                    int i = count.getAndIncrement();
                    if (log.isDebugEnabled() && i % 100 == 0)
                      log.debugf(">>    Moved %s keys\n", i);
                  } catch (Exception e) {
                    log.keyMigrationFailed(Util.toStr(key), e);
                  }
                }
              });
        }
        es.shutdown();
        try {
          while (!es.awaitTermination(500, TimeUnit.MILLISECONDS)) ;
        } catch (InterruptedException e) {
          throw new CacheException(e);
        }
        return count.longValue();
      }
    }
    throw log.missingMigrationData(cache.getName());
  }
 long getDeadlockCount() {
   return deadlockCount.longValue();
 }