Search in sources :

Example 1 with Long2LongMap

use of it.unimi.dsi.fastutil.longs.Long2LongMap in project druid by druid-io.

the class AllocationMetricCollector method calculateDelta.

/**
 * Uses getThreadAllocatedBytes internally {@link com.sun.management.ThreadMXBean#getThreadAllocatedBytes}.
 *
 * Tests show the call to getThreadAllocatedBytes for a single thread ID out of 500 threads running takes around
 * 9000 ns (in the worst case), which for 500 IDs should take 500*9000/1000/1000 = 4.5 ms to the max.
 * AllocationMetricCollector takes linear time to calculate delta, for 500 threads it's negligible.
 * See the default emitting period {@link MonitorSchedulerConfig#getEmitterPeriod}.
 *
 * @return all threads summed allocated bytes delta
 */
long calculateDelta() {
    try {
        long[] allThreadIds = threadMXBean.getAllThreadIds();
        // the call time depends on number of threads, for 500 threads the estimated time is 4 ms
        long[] bytes = (long[]) getThreadAllocatedBytes.invoke(threadMXBean, (Object) allThreadIds);
        long sum = 0;
        Long2LongMap newResults = new Long2LongOpenHashMap();
        newResults.defaultReturnValue(NO_DATA);
        for (int i = 0; i < allThreadIds.length; i++) {
            long threadId = allThreadIds[i];
            long previous = previousResults.get(threadId);
            long current = bytes[i];
            newResults.put(threadId, current);
            // before
            if (previous == NO_DATA || previous > current) {
                sum += current;
            } else {
                sum += current - previous;
            }
        }
        previousResults = newResults;
        return sum;
    } catch (ReflectiveOperationException e) {
        // it doesn't make sense after initialization is complete
        log.error(e, "Cannot calculate delta");
    }
    return 0;
}
Also used : Long2LongMap(it.unimi.dsi.fastutil.longs.Long2LongMap) Long2LongOpenHashMap(it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap)

Example 2 with Long2LongMap

use of it.unimi.dsi.fastutil.longs.Long2LongMap in project snow-owl by b2ihealthcare.

the class LongKeyLongMapWrapper method create.

public static LongKeyLongMap create(LongKeyLongMap map) {
    if (map instanceof LongKeyLongMapWrapper) {
        final Long2LongMap sourceDelegate = ((LongKeyLongMapWrapper) map).delegate;
        return new LongKeyLongMapWrapper(clone(sourceDelegate));
    } else {
        final LongKeyLongMap result = createWithExpectedSize(map.size());
        final LongIterator keys = map.keySet().iterator();
        while (keys.hasNext()) {
            final long key = keys.next();
            result.put(key, map.get(key));
        }
        return result;
    }
}
Also used : Long2LongMap(it.unimi.dsi.fastutil.longs.Long2LongMap)

Example 3 with Long2LongMap

use of it.unimi.dsi.fastutil.longs.Long2LongMap in project druid by apache.

the class AllocationMetricCollector method calculateDelta.

/**
 * Uses getThreadAllocatedBytes internally {@link com.sun.management.ThreadMXBean#getThreadAllocatedBytes}.
 *
 * Tests show the call to getThreadAllocatedBytes for a single thread ID out of 500 threads running takes around
 * 9000 ns (in the worst case), which for 500 IDs should take 500*9000/1000/1000 = 4.5 ms to the max.
 * AllocationMetricCollector takes linear time to calculate delta, for 500 threads it's negligible.
 * See the default emitting period {@link MonitorSchedulerConfig#getEmitterPeriod}.
 *
 * @return all threads summed allocated bytes delta
 */
long calculateDelta() {
    try {
        long[] allThreadIds = threadMXBean.getAllThreadIds();
        // the call time depends on number of threads, for 500 threads the estimated time is 4 ms
        long[] bytes = (long[]) getThreadAllocatedBytes.invoke(threadMXBean, (Object) allThreadIds);
        long sum = 0;
        Long2LongMap newResults = new Long2LongOpenHashMap();
        newResults.defaultReturnValue(NO_DATA);
        for (int i = 0; i < allThreadIds.length; i++) {
            long threadId = allThreadIds[i];
            long previous = previousResults.get(threadId);
            long current = bytes[i];
            newResults.put(threadId, current);
            // before
            if (previous == NO_DATA || previous > current) {
                sum += current;
            } else {
                sum += current - previous;
            }
        }
        previousResults = newResults;
        return sum;
    } catch (ReflectiveOperationException e) {
        // it doesn't make sense after initialization is complete
        log.error(e, "Cannot calculate delta");
    }
    return 0;
}
Also used : Long2LongMap(it.unimi.dsi.fastutil.longs.Long2LongMap) Long2LongOpenHashMap(it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap)

Aggregations

Long2LongMap (it.unimi.dsi.fastutil.longs.Long2LongMap)3 Long2LongOpenHashMap (it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap)2