public Stats getAndReset() {
   long putsNow = puts.getAndSet(0);
   long getsNow = gets.getAndSet(0);
   long removesNow = removes.getAndSet(0);
   Stats newOne = new Stats();
   newOne.puts.set(putsNow);
   newOne.gets.set(getsNow);
   newOne.removes.set(removesNow);
   return newOne;
 }
Пример #2
0
  /** Called by BlockWriter to actually write the block. */
  void writeFromBlockWriter() throws IOException {
    do {
      long dirty = _dirtyRange.getAndSet(INIT_DIRTY);

      int dirtyMax = (int) (dirty >> 32);
      int dirtyMin = (int) dirty;

      if (dirtyMin < dirtyMax) {
        if (log.isLoggable(Level.ALL))
          log.log(Level.ALL, "write db-block " + this + " [" + dirtyMin + ", " + dirtyMax + "]");

        boolean isPriority = false;

        writeImpl(dirtyMin, dirtyMax - dirtyMin, isPriority);
      }

      if (_dirtyRange.get() == INIT_DIRTY && !isDestroyed()) {
        toValid();
      }
    } while (_dirtyRange.get() != INIT_DIRTY);

    if (_useCount.get() <= 0) {
      freeImpl();
    }
  }
  /** Return the probe's next sample. */
  public final double sampleMax() {
    synchronized (_lock) {
      long max = _max.getAndSet(0);

      return _scale * max;
    }
  }
Пример #4
0
    /**
     * The background thread that adds sockets to the Poller, checks the poller for triggered events
     * and hands the associated socket off to an appropriate processor as events occur.
     */
    @Override
    public void run() {
      // Loop until destroy() is called
      while (true) {

        boolean hasEvents = false;

        try {
          if (!close) {
            hasEvents = events();
            if (wakeupCounter.getAndSet(-1) > 0) {
              // if we are here, means we have other stuff to do
              // do a non blocking select
              keyCount = selector.selectNow();
            } else {
              keyCount = selector.select(selectorTimeout);
            }
            wakeupCounter.set(0);
          }
          if (close) {
            events();
            timeout(0, false);
            try {
              selector.close();
            } catch (IOException ioe) {
              log.error(sm.getString("endpoint.nio.selectorCloseFail"), ioe);
            }
            break;
          }
        } catch (Throwable x) {
          ExceptionUtils.handleThrowable(x);
          log.error("", x);
          continue;
        }
        // either we timed out or we woke up, process events first
        if (keyCount == 0) hasEvents = (hasEvents | events());

        Iterator<SelectionKey> iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null;
        // Walk through the collection of ready keys and dispatch
        // any active event.
        while (iterator != null && iterator.hasNext()) {
          SelectionKey sk = iterator.next();
          NioSocketWrapper attachment = (NioSocketWrapper) sk.attachment();
          // Attachment may be null if another thread has called
          // cancelledKey()
          if (attachment == null) {
            iterator.remove();
          } else {
            iterator.remove();
            processKey(sk, attachment);
          }
        } // while

        // process timeouts
        timeout(keyCount, hasEvents);
      } // while

      stopLatch.countDown();
    }
Пример #5
0
 /* Never let the drift get greater than 200 ms. */
 private long checkForDrift(long time) {
   long delta = Math.abs(System.currentTimeMillis() - time);
   long lastDelta = lastDeltaTime.getAndSet(delta);
   if (delta > lastDelta + 200) {
     return getTheTime(time);
   }
   return time;
 }
 public long getVal() throws StatUnreachableException {
   StatUnreachableException ex;
   if ((ex = exceptionRef.get()) != null) {
     throw ex;
   }
   synchronized (value) {
     return value.getAndSet(0);
   }
 }
  /**
   * @return {@code true} if the watched file has probably been changed
   * @throws IOException If failed to query file data
   */
  public boolean checkReloadRequired() throws IOException {
    boolean exists = exists();
    // if existence state changed from last time
    if (exists != lastExisted.getAndSet(exists)) {
      return true;
    }

    if (!exists) {
      // file did not exist and still does not exist
      resetReloadAttributes();
      return false;
    }

    long size = size();
    if (size < 0L) {
      // means file no longer exists
      resetReloadAttributes();
      return true;
    }

    // if size changed then obviously need reload
    if (size != lastSize.getAndSet(size)) {
      return true;
    }

    FileTime modifiedTime = lastModified();
    if (modifiedTime == null) {
      // means file no longer exists
      resetReloadAttributes();
      return true;
    }

    long timestamp = modifiedTime.toMillis();
    if (timestamp != lastModified.getAndSet(timestamp)) {
      return true;
    }

    return false;
  }
Пример #8
0
 private void printStats() {
   // print every PRINT_INTERVAL milliseconds
   while (true) {
     long startTime = System.currentTimeMillis();
     try {
       Thread.sleep(PRINT_INTERVAL);
     } catch (InterruptedException e) {
       break;
     }
     long timeDelta = (System.currentTimeMillis() - startTime) / 1000;
     long cc = count.getAndSet(0);
     long objectsPerSec = cc / timeDelta;
     System.out.println("server inserts " + objectsPerSec + " objects per second");
   }
 }
 QueueOperationsCounter getAndReset() {
   QueueOperationsCounter newOne = new QueueOperationsCounter();
   newOne.offers.set(offers.getAndSet(0));
   newOne.polls.set(polls.getAndSet(0));
   newOne.rejectedOffers.set(rejectedOffers.getAndSet(0));
   newOne.emptyPolls.set(emptyPolls.getAndSet(0));
   newOne.others.set(others.getAndSet(0));
   newOne.events.set(events.getAndSet(0));
   newOne.startTime = this.startTime;
   newOne.endTime = now();
   this.startTime = newOne.endTime;
   return newOne;
 }
Пример #10
0
  private void incrementInBackground(int delta) {

    long currentTime = System.currentTimeMillis();
    long lastIncrementedTime = lastIncremented.getAndSet(currentTime);

    if (currentTime - lastIncrementedTime > getUpdateDelayInMs()
        || playerScore.getHistory().isEmpty()) {

      // if it's been awhile since the last time we incremented
      changeRecorder.onCallback(
          new RecordedChange(playerScore.getPlayerNumber(), Type.AddNew, delta));
      playerScore.getHistory().add(delta);
    } else {
      // else just update the most recent history item
      int lastIndex = playerScore.getHistory().size() - 1;
      int newValue = playerScore.getHistory().get(lastIndex) + delta;
      if (newValue == 0) { // don't add "0" to the list; just delete the
        // last history item
        int deletedValue = playerScore.getHistory().remove(lastIndex);
        changeRecorder.onCallback(
            new RecordedChange(playerScore.getPlayerNumber(), Type.DeleteLastZero, deletedValue));
        lastIncremented.set(0); // reset the lastIncremented time so we
        // don't update the
        // previous value later
      } else {
        playerScore.getHistory().set(lastIndex, newValue);
        changeRecorder.onCallback(
            new RecordedChange(playerScore.getPlayerNumber(), Type.ModifyLast, delta));
      }
    }

    playerScore.setScore(playerScore.getScore() + delta);

    shouldAutosave.set(true);

    // this runnable updates the history after 10 seconds and makes the
    // blibbet disappear
    createDelayedHistoryUpdateTask();

    // this runnable updates the history text view and the total score text
    // view
    handler.post(getUpdateViewsRunnable());
  }
Пример #11
0
  public CounterMetric calculateMetric() {
    long lastCount = counter.getAndSet(0);
    long currentTime = clock.getCurrentTime();

    CounterMetric metric = new CounterMetric();

    totalCount += lastCount;
    long totalElapsed = currentTime - startTime;
    metric.meanRate = (totalCount * 1000) / totalElapsed;

    metric.lastCount = lastCount;
    metric.totalCount = totalCount;

    long elapsed = currentTime - lastReportTime;
    if (elapsed > 0) {
      metric.lastRate = (lastCount * 1000) / elapsed;
    }

    lastReportTime = currentTime;

    snapshot = metric;

    return metric;
  }
Пример #12
0
 @Override
 public synchronized void clear() {
   value.getAndSet(0);
 }
 @Override
 public Long getCurrentSliceAndReset() {
   return counter.getAndSet(ZERO);
 }
Пример #14
0
 /** Number of checksum verification failures. It also clears the counter. */
 public static final long getChecksumFailuresCount() {
   return checksumFailures.getAndSet(0);
 }
Пример #15
0
 public static final long getWriteTimeMs() {
   return writeTimeNano.getAndSet(0) / 1000000;
 }
Пример #16
0
 public static final long getPreadTimeMs() {
   return preadTimeNano.getAndSet(0) / 1000000;
 }
 /** getAndSet returns previous value and sets to given value */
 public void testGetAndSet() {
   AtomicLong ai = new AtomicLong(1);
   assertEquals(1, ai.getAndSet(0));
   assertEquals(0, ai.getAndSet(-10));
   assertEquals(-10, ai.getAndSet(1));
 }