Search in sources :

Example 16 with AtomicDouble

use of com.google.common.util.concurrent.AtomicDouble in project aic-praise by aic-sri-international.

the class UAIMARSolver method calculateCompressedEntries.

private static double calculateCompressedEntries(Expression compressedTableExpression) {
    AtomicDouble count = new AtomicDouble(0);
    visitCompressedTableEntries(compressedTableExpression, count);
    return count.doubleValue();
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble)

Example 17 with AtomicDouble

use of com.google.common.util.concurrent.AtomicDouble in project airlift by airlift.

the class QuantileDigest method getHistogram.

/*
     * Get the exponentially-decayed approximate counts of values in multiple buckets. The elements in
     * the provided list denote the upper bound each of the buckets and must be sorted in ascending
     * order.
     *
     * The approximate count in each bucket is guaranteed to be within 2 * totalCount * maxError of
     * the real count.
     */
public List<Bucket> getHistogram(List<Long> bucketUpperBounds) {
    checkArgument(Ordering.natural().isOrdered(bucketUpperBounds), "buckets must be sorted in increasing order");
    ImmutableList.Builder<Bucket> builder = ImmutableList.builder();
    PeekingIterator<Long> iterator = Iterators.peekingIterator(bucketUpperBounds.iterator());
    AtomicDouble sum = new AtomicDouble();
    AtomicDouble lastSum = new AtomicDouble();
    // for computing weighed average of values in bucket
    AtomicDouble bucketWeightedSum = new AtomicDouble();
    double normalizationFactor = weight(TimeUnit.NANOSECONDS.toSeconds(ticker.read()));
    postOrderTraversal(root, node -> {
        while (iterator.hasNext() && iterator.peek() <= upperBound(node)) {
            double bucketCount = sum.get() - lastSum.get();
            Bucket bucket = new Bucket(bucketCount / normalizationFactor, bucketWeightedSum.get() / bucketCount);
            builder.add(bucket);
            lastSum.set(sum.get());
            bucketWeightedSum.set(0);
            iterator.next();
        }
        bucketWeightedSum.addAndGet(middle(node) * counts[node]);
        sum.addAndGet(counts[node]);
        return iterator.hasNext();
    });
    while (iterator.hasNext()) {
        double bucketCount = sum.get() - lastSum.get();
        Bucket bucket = new Bucket(bucketCount / normalizationFactor, bucketWeightedSum.get() / bucketCount);
        builder.add(bucket);
        iterator.next();
    }
    return builder.build();
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ImmutableList(com.google.common.collect.ImmutableList) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 18 with AtomicDouble

use of com.google.common.util.concurrent.AtomicDouble in project cdap by caskdata.

the class LogSamplers method exponentialLimit.

/**
 * Returns a {@link LogSampler} that accepts once on every N calls, with N exponentially increased from
 * an initial value to a max value.
 */
public static LogSampler exponentialLimit(final int initialCount, final int maxCount, final double multiplier) {
    Preconditions.checkArgument(initialCount > 0, "Initial count must be >= 0");
    Preconditions.checkArgument(maxCount > 0, "Max count must be >= 0");
    Preconditions.checkArgument(multiplier >= 1.0d, "Multiplier must be >= 1.0");
    final AtomicDouble modular = new AtomicDouble(initialCount);
    return new CountBasedLogSampler() {

        @Override
        protected boolean accept(String message, int logLevel, long callCount) {
            double mod = modular.get();
            if (((callCount - 1) % Math.ceil(mod)) == 0) {
                modular.compareAndSet(mod, Math.min(mod * multiplier, maxCount));
                return true;
            }
            return false;
        }
    };
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble)

Example 19 with AtomicDouble

use of com.google.common.util.concurrent.AtomicDouble in project narchy by automenta.

the class NARTestOptimize method tests.

/**
 * HACK runs all Junit test methods, summing the scores.
 * TODO use proper JUnit5 test runner api but it is a mess to figure out right now
 */
static float tests(Supplier<NAR> s, Class<? extends NALTest>... c) {
    List<Method> methods = Stream.of(c).flatMap(cc -> Stream.of(cc.getMethods()).filter(x -> x.getAnnotation(Test.class) != null)).collect(toList());
    final CountDownLatch remain = new CountDownLatch(methods.size());
    final AtomicDouble sum = new AtomicDouble(0);
    methods.forEach(m -> {
        exe.submit(() -> {
            try {
                sum.addAndGet(test(s, m));
            } finally {
                remain.countDown();
            }
        });
    });
    try {
        remain.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return sum.floatValue();
}
Also used : NAL1MultistepTest(nars.nal.nal1.NAL1MultistepTest) NARS(nars.NARS) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) NAL1Test(nars.nal.nal1.NAL1Test) Supplier(java.util.function.Supplier) NARLoop(nars.NARLoop) NAL3Test(nars.nal.nal3.NAL3Test) Method(java.lang.reflect.Method) NAL2Test(nars.nal.nal2.NAL2Test) ExecutorService(java.util.concurrent.ExecutorService) NAL5Test(nars.nal.nal5.NAL5Test) MultiOutputStream(jcog.data.MultiOutputStream) Set(java.util.Set) NALTest(nars.util.NALTest) Field(java.lang.reflect.Field) Executors(java.util.concurrent.Executors) Result(jcog.optimize.Result) Param(nars.Param) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) java.io(java.io) NAR(nars.NAR) Tweaks(jcog.optimize.Tweaks) MetaGoal(nars.control.MetaGoal) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 20 with AtomicDouble

use of com.google.common.util.concurrent.AtomicDouble in project Singularity by HubSpot.

the class SingularityUsagePoller method collectSlaveUage.

private void collectSlaveUage(SingularitySlave slave, long now, Map<String, RequestUtilization> utilizationPerRequestId, Map<SingularitySlaveUsage, List<TaskIdWithUsage>> overLoadedHosts, AtomicLong totalMemBytesUsed, AtomicLong totalMemBytesAvailable, AtomicDouble totalCpuUsed, AtomicDouble totalCpuAvailable, AtomicLong totalDiskBytesUsed, AtomicLong totalDiskBytesAvailable) {
    Map<ResourceUsageType, Number> longRunningTasksUsage = new HashMap<>();
    longRunningTasksUsage.put(ResourceUsageType.MEMORY_BYTES_USED, 0);
    longRunningTasksUsage.put(ResourceUsageType.CPU_USED, 0);
    longRunningTasksUsage.put(ResourceUsageType.DISK_BYTES_USED, 0);
    Optional<Long> memoryMbTotal = Optional.absent();
    Optional<Double> cpusTotal = Optional.absent();
    Optional<Long> diskMbTotal = Optional.absent();
    long memoryMbReservedOnSlave = 0;
    double cpuReservedOnSlave = 0;
    long diskMbReservedOnSlave = 0;
    long memoryBytesUsedOnSlave = 0;
    double cpusUsedOnSlave = 0;
    long diskMbUsedOnSlave = 0;
    try {
        List<MesosTaskMonitorObject> allTaskUsage = mesosClient.getSlaveResourceUsage(slave.getHost());
        MesosSlaveMetricsSnapshotObject slaveMetricsSnapshot = mesosClient.getSlaveMetricsSnapshot(slave.getHost());
        double systemMemTotalBytes = 0;
        double systemMemFreeBytes = 0;
        double systemLoad1Min = 0;
        double systemLoad5Min = 0;
        double systemLoad15Min = 0;
        double slaveDiskUsed = 0;
        double slaveDiskTotal = 0;
        double systemCpusTotal = 0;
        if (slaveMetricsSnapshot != null) {
            systemMemTotalBytes = slaveMetricsSnapshot.getSystemMemTotalBytes();
            systemMemFreeBytes = slaveMetricsSnapshot.getSystemMemFreeBytes();
            systemLoad1Min = slaveMetricsSnapshot.getSystemLoad1Min();
            systemLoad5Min = slaveMetricsSnapshot.getSystemLoad5Min();
            systemLoad15Min = slaveMetricsSnapshot.getSystemLoad15Min();
            slaveDiskUsed = slaveMetricsSnapshot.getSlaveDiskUsed();
            slaveDiskTotal = slaveMetricsSnapshot.getSlaveDiskTotal();
            systemCpusTotal = slaveMetricsSnapshot.getSystemCpusTotal();
        }
        double systemLoad;
        switch(configuration.getMesosConfiguration().getScoreUsingSystemLoad()) {
            case LOAD_1:
                systemLoad = systemLoad1Min;
                break;
            case LOAD_15:
                systemLoad = systemLoad15Min;
                break;
            case LOAD_5:
            default:
                systemLoad = systemLoad5Min;
                break;
        }
        boolean slaveOverloaded = systemCpusTotal > 0 && systemLoad / systemCpusTotal > 1.0;
        List<TaskIdWithUsage> possibleTasksToShuffle = new ArrayList<>();
        for (MesosTaskMonitorObject taskUsage : allTaskUsage) {
            String taskId = taskUsage.getSource();
            SingularityTaskId task;
            try {
                task = SingularityTaskId.valueOf(taskId);
            } catch (InvalidSingularityTaskIdException e) {
                LOG.error("Couldn't get SingularityTaskId for {}", taskUsage);
                continue;
            }
            SingularityTaskUsage latestUsage = getUsage(taskUsage);
            List<SingularityTaskUsage> pastTaskUsages = usageManager.getTaskUsage(taskId);
            clearOldUsage(taskId);
            usageManager.saveSpecificTaskUsage(taskId, latestUsage);
            Optional<SingularityTask> maybeTask = taskManager.getTask(task);
            Optional<Resources> maybeResources = Optional.absent();
            if (maybeTask.isPresent()) {
                maybeResources = maybeTask.get().getTaskRequest().getPendingTask().getResources().or(maybeTask.get().getTaskRequest().getDeploy().getResources());
                if (maybeResources.isPresent()) {
                    Resources taskResources = maybeResources.get();
                    double memoryMbReservedForTask = taskResources.getMemoryMb();
                    double cpuReservedForTask = taskResources.getCpus();
                    double diskMbReservedForTask = taskResources.getDiskMb();
                    memoryMbReservedOnSlave += memoryMbReservedForTask;
                    cpuReservedOnSlave += cpuReservedForTask;
                    diskMbReservedOnSlave += diskMbReservedForTask;
                    updateRequestUtilization(utilizationPerRequestId, pastTaskUsages, latestUsage, task, memoryMbReservedForTask, cpuReservedForTask, diskMbReservedForTask);
                }
            }
            memoryBytesUsedOnSlave += latestUsage.getMemoryTotalBytes();
            diskMbUsedOnSlave += latestUsage.getDiskTotalBytes();
            SingularityTaskCurrentUsage currentUsage = null;
            if (pastTaskUsages.isEmpty()) {
                Optional<SingularityTaskHistoryUpdate> maybeStartingUpdate = taskManager.getTaskHistoryUpdate(task, ExtendedTaskState.TASK_STARTING);
                if (maybeStartingUpdate.isPresent()) {
                    long startTimestampSeconds = TimeUnit.MILLISECONDS.toSeconds(maybeStartingUpdate.get().getTimestamp());
                    double usedCpusSinceStart = latestUsage.getCpuSeconds() / (latestUsage.getTimestamp() - startTimestampSeconds);
                    if (isLongRunning(task) || isConsideredLongRunning(task)) {
                        updateLongRunningTasksUsage(longRunningTasksUsage, latestUsage.getMemoryTotalBytes(), usedCpusSinceStart, latestUsage.getDiskTotalBytes());
                    }
                    currentUsage = new SingularityTaskCurrentUsage(latestUsage.getMemoryTotalBytes(), now, usedCpusSinceStart, latestUsage.getDiskTotalBytes());
                    usageManager.saveCurrentTaskUsage(taskId, currentUsage);
                    cpusUsedOnSlave += usedCpusSinceStart;
                }
            } else {
                SingularityTaskUsage lastUsage = pastTaskUsages.get(pastTaskUsages.size() - 1);
                double taskCpusUsed = ((latestUsage.getCpuSeconds() - lastUsage.getCpuSeconds()) / (latestUsage.getTimestamp() - lastUsage.getTimestamp()));
                if (isLongRunning(task) || isConsideredLongRunning(task)) {
                    updateLongRunningTasksUsage(longRunningTasksUsage, latestUsage.getMemoryTotalBytes(), taskCpusUsed, latestUsage.getDiskTotalBytes());
                }
                currentUsage = new SingularityTaskCurrentUsage(latestUsage.getMemoryTotalBytes(), now, taskCpusUsed, latestUsage.getDiskTotalBytes());
                usageManager.saveCurrentTaskUsage(taskId, currentUsage);
                cpusUsedOnSlave += taskCpusUsed;
            }
            if (configuration.isShuffleTasksForOverloadedSlaves() && currentUsage != null && currentUsage.getCpusUsed() > 0) {
                if (isLongRunning(task) && !configuration.getDoNotShuffleRequests().contains(task.getRequestId())) {
                    Optional<SingularityTaskHistoryUpdate> maybeCleanupUpdate = taskManager.getTaskHistoryUpdate(task, ExtendedTaskState.TASK_CLEANING);
                    if (maybeCleanupUpdate.isPresent() && isTaskAlreadyCleanedUpForShuffle(maybeCleanupUpdate.get())) {
                        LOG.trace("Task {} already being cleaned up to spread cpu usage, skipping", taskId);
                    } else {
                        if (maybeResources.isPresent()) {
                            possibleTasksToShuffle.add(new TaskIdWithUsage(task, maybeResources.get(), currentUsage));
                        }
                    }
                }
            }
        }
        if (!slave.getResources().isPresent() || !slave.getResources().get().getMemoryMegaBytes().isPresent() || !slave.getResources().get().getNumCpus().isPresent()) {
            LOG.debug("Could not find slave or resources for slave {}", slave.getId());
        } else {
            memoryMbTotal = Optional.of(slave.getResources().get().getMemoryMegaBytes().get().longValue());
            cpusTotal = Optional.of(slave.getResources().get().getNumCpus().get().doubleValue());
            diskMbTotal = Optional.of(slave.getResources().get().getDiskSpace().get());
        }
        SingularitySlaveUsage slaveUsage = new SingularitySlaveUsage(cpusUsedOnSlave, cpuReservedOnSlave, cpusTotal, memoryBytesUsedOnSlave, memoryMbReservedOnSlave, memoryMbTotal, diskMbUsedOnSlave, diskMbReservedOnSlave, diskMbTotal, longRunningTasksUsage, allTaskUsage.size(), now, systemMemTotalBytes, systemMemFreeBytes, systemCpusTotal, systemLoad1Min, systemLoad5Min, systemLoad15Min, slaveDiskUsed, slaveDiskTotal);
        if (slaveOverloaded) {
            overLoadedHosts.put(slaveUsage, possibleTasksToShuffle);
        }
        List<Long> slaveTimestamps = usageManager.getSlaveUsageTimestamps(slave.getId());
        if (slaveTimestamps.size() + 1 > configuration.getNumUsageToKeep()) {
            usageManager.deleteSpecificSlaveUsage(slave.getId(), slaveTimestamps.get(0));
        }
        if (slaveUsage.getMemoryBytesTotal().isPresent() && slaveUsage.getCpusTotal().isPresent()) {
            totalMemBytesUsed.getAndAdd(slaveUsage.getMemoryBytesUsed());
            totalCpuUsed.getAndAdd(slaveUsage.getCpusUsed());
            totalDiskBytesUsed.getAndAdd(slaveUsage.getDiskBytesUsed());
            totalMemBytesAvailable.getAndAdd(slaveUsage.getMemoryBytesTotal().get());
            totalCpuAvailable.getAndAdd(slaveUsage.getCpusTotal().get());
            totalDiskBytesAvailable.getAndAdd(slaveUsage.getDiskBytesTotal().get());
        }
        LOG.debug("Saving slave {} usage {}", slave.getHost(), slaveUsage);
        usageManager.saveSpecificSlaveUsageAndSetCurrent(slave.getId(), slaveUsage);
    } catch (Throwable t) {
        String message = String.format("Could not get slave usage for host %s", slave.getHost());
        LOG.error(message, t);
        exceptionNotifier.notify(message, t);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MesosTaskMonitorObject(com.hubspot.mesos.json.MesosTaskMonitorObject) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) ResourceUsageType(com.hubspot.singularity.SingularitySlaveUsage.ResourceUsageType) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityTaskUsage(com.hubspot.singularity.SingularityTaskUsage) SingularityTaskCurrentUsage(com.hubspot.singularity.SingularityTaskCurrentUsage) SingularitySlaveUsage(com.hubspot.singularity.SingularitySlaveUsage) MesosSlaveMetricsSnapshotObject(com.hubspot.mesos.json.MesosSlaveMetricsSnapshotObject) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) InvalidSingularityTaskIdException(com.hubspot.singularity.InvalidSingularityTaskIdException) SingularityTask(com.hubspot.singularity.SingularityTask) AtomicLong(java.util.concurrent.atomic.AtomicLong) Resources(com.hubspot.mesos.Resources)

Aggregations

AtomicDouble (com.google.common.util.concurrent.AtomicDouble)28 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2 LoggerFactory (com.graphaware.common.log.LoggerFactory)2 Resources (com.hubspot.mesos.Resources)2 MesosTaskMonitorObject (com.hubspot.mesos.json.MesosTaskMonitorObject)2 InvalidSingularityTaskIdException (com.hubspot.singularity.InvalidSingularityTaskIdException)2 SingularityAgentUsage (com.hubspot.singularity.SingularityAgentUsage)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskCurrentUsage (com.hubspot.singularity.SingularityTaskCurrentUsage)2 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 SingularityTaskUsage (com.hubspot.singularity.SingularityTaskUsage)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2