Package com.yammer.metrics.core

Examples of com.yammer.metrics.core.TimerContext


    }

    private void loadBeforeUpdates(int interval, final ChiVertex<VertexDataType, EdgeDataType>[] vertices,  final MemoryShard<EdgeDataType> memShard,
                                   final int startVertex, final int endVertex) throws IOException {
        final Object terminationLock = new Object();
        final TimerContext _timer = loadTimer.time();
        // TODO: make easier to read
        synchronized (terminationLock) {

            final AtomicInteger countDown = new AtomicInteger(disableOutEdges ? 1 : nShards);

            if (!disableInEdges) {
                try {

                    logger.info("Memshard: " + startVertex + " -- " + endVertex);
                    memShard.loadVertices(startVertex, endVertex, vertices, disableOutEdges, parallelExecutor);
                    logger.info("Loading memory-shard finished." + Thread.currentThread().getName());

                    if (countDown.decrementAndGet() == 0) {
                        synchronized (terminationLock) {
                            terminationLock.notifyAll();
                        }
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                    throw new RuntimeException(ioe);
                catch (Exception err) {
                    err.printStackTrace();
                }
            }

            /* Load in parallel */
            if (!disableOutEdges) {
                for(int p=0; p < nShards; p++) {
                    if (p != interval || disableInEdges) {
                        final int _p = p;
                        final SlidingShard<EdgeDataType> shard = slidingShards.get(p);
                        loadingExecutor.submit(new Runnable() {

                            public void run() {
                                try {
                                    shard.readNextVertices(vertices, startVertex, false);
                                    if (countDown.decrementAndGet() == 0) {
                                        synchronized (terminationLock) {
                                            terminationLock.notifyAll();
                                        }
                                    }

                                } catch (IOException ioe) {
                                    ioe.printStackTrace();
                                    throw new RuntimeException(ioe);
                                catch (Exception err) {
                                    err.printStackTrace();
                                }
                            }
                        });
                    }
                }
            }

            // barrier
            try {
                while(countDown.get() > 0) {
                    terminationLock.wait(5000);
                    if (countDown.get() > 0) {
                        logger.info("Still waiting for loading, counter is: " + countDown.get());
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        _timer.stop();
    }
View Full Code Here



    }

    private int determineNextWindow(int subIntervalStart, int maxVertex) throws IOException, NoEdgesInIntervalException {
        final TimerContext _timer = determineNextWindowTimer.time();
        long totalDegree = 0;
        try {
            degreeHandler.load(subIntervalStart, maxVertex);

            if (useStaticWindowSize) {
                return maxVertex;
            }

            long memReq = 0;
            int maxInterval = maxVertex - subIntervalStart;
            int vertexDataSizeOf = (vertexDataConverter != null ? vertexDataConverter.sizeOf() : 0);
            int edataSizeOf = (onlyAdjacency ? 0 : edataConverter.sizeOf());

            logger.info("Memory budget: " + memBudget);

            for(int i=0; i< maxInterval; i++) {
                if (enableScheduler) {
                    if (!scheduler.isScheduled(i + subIntervalStart)) continue;
                }
                VertexDegree deg = degreeHandler.getDegree(i + subIntervalStart);
                int inc = deg.inDegree;
                int outc = deg.outDegree;

                if (inc + outc == 0 && skipZeroDegreeVertices) {
                    continue;
                }

                totalDegree += inc + outc;

                // Following calculation contains some perhaps reasonable estimates of the
                // overhead of Java objects.

                memReq += vertexDataSizeOf + 256 + (edataSizeOf + 4 + 4 + 4) * (inc + outc);
                if (memReq > memBudget) {
                    if (totalDegree == 0 && vertexDataConverter == null) {
                        throw new NoEdgesInIntervalException();
                    }
                    return subIntervalStart + i - 1; // Previous vertex was enough
                }
            }
            if (totalDegree == 0 && vertexDataConverter == null) {
                throw new NoEdgesInIntervalException();
            }
            return maxVertex;
        } finally {
            _timer.stop();
        }
    }
View Full Code Here

                snapshots[vertexId - fromVertex] = null;
            }

            @Override
            public void restoreUngrabbed() {
                final TimerContext _timer = restore.time();
                // Restore such walks that were not grabbed (because the vertex
                // was not initially scheduled)
                int v = fromVertex;
                int restoreCount = 0;
                for(int[] snapshot : snapshots) {
                    if (snapshot != null && !processedBits[v - fromVertex]) {
                        for(int i=0; i<snapshot.length; i++) {
                            int w = snapshot[i];
                            moveWalk(w, v, trackBit(w));
                            restoreCount++;
                        }
                    }
                    v++;
                }
                logger.info("Restored " + restoreCount);
                _timer.stop();
            }

            // Note: accurate number only before snapshot is being purged
            public long numWalks() {
                long sum = 0;
                for(int b=fromBucket; b <= toBucket; b++) {
                    sum += walks[b].length;
                }
                return sum;
            }

            @Override
            public WalkArray getWalksAtVertex(int vertexId, boolean processed) {
                int bucketIdx = vertexId / bucketSize;
                int localBucketIdx = bucketIdx - (fromVertex / bucketSize);

                processedBits[vertexId - fromVertex] = true;

                if (snapshotInitBits[localBucketIdx]) {
                    int[] array = snapshots[vertexId - fromVertex];
                    if (array == null) {
                        return null;
                    } else {
                        return new IntWalkArray(snapshots[vertexId - fromVertex]);
                    }
                } else {
                    final TimerContext _timer = grabTimer.time();

                    int[] bucketToConsume = null;
                    int len = 0;
                    synchronized (bucketLocks[bucketIdx]) {
                        if (!snapshotInitBits[localBucketIdx]) {

                            int bucketFirstVertex = bucketSize * bucketIdx;
                            len = walkIndices[bucketIdx];

                            bucketToConsume = walks[bucketIdx];

                            if (bucketToConsume != null) {
                                walks[bucketIdx] = null;
                                walkIndices[bucketIdx] = 0;
                                final int[] snapshotSizes = new int[bucketSize];
                                final int[] snapshotIdxs = new int[bucketSize];

                                /* Calculate vertex-walks sizes */
                                for(int i=0; i < len; i++) {
                                    int w = bucketToConsume[i];
                                    snapshotSizes[off(w)]++;
                                }

                                int offt = bucketFirstVertex - fromVertex;

                                for(int i=0; i < snapshotSizes.length; i++) {
                                    if (snapshotSizes[i] > 0 && i >= -offt && i + offt < snapshots.length)
                                        snapshots[i + offt] = new int[snapshotSizes[i]];
                                }

                                for(int i=0; i < len; i++) {
                                    int w = bucketToConsume[i];
                                    int vertex = bucketFirstVertex + off(w);

                                    if (vertex >= fromVertex && vertex <= toVertexInclusive) {
                                        int snapshotOff = vertex - fromVertex;
                                        int localOff = vertex - bucketFirstVertex;
                                        snapshots[snapshotOff][snapshotIdxs[localOff]] = w;
                                        snapshotIdxs[localOff]++;
                                    } else {
                                        // add back
                                        moveWalk(w, vertex, trackBit(w));
                                    }
                                }
                            }
                            snapshotInitBits[localBucketIdx] = true;
                        }
                    }
                    if (bucketConsumer != null && bucketToConsume != null && len > 0) {
                        bucketConsumer.consume(bucketIdx * bucketSize, new IntWalkArray(bucketToConsume), len);
                        if (len > 1000000) {
                            log((bucketIdx * bucketSize) + " - " + ((bucketIdx+1)) * bucketSize + ", " + len);
                        }
                    }
                    _timer.stop();
                    int[] array = snapshots[vertexId - fromVertex];
                    if (array == null) {
                        return null;
                    } else {
                        return new IntWalkArray(snapshots[vertexId - fromVertex]);
View Full Code Here

    }

    /** Dump to file all walks with more than 0 hop */
    @Override
    public void dumpToFile(WalkSnapshot snapshot, String filename) throws IOException {
        final TimerContext _timer = dumpTimer.time();
        synchronized (filename.intern()) {
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filename), true)));
            for(int i=snapshot.getFirstVertex(); i <= snapshot.getLastVertex(); i++) {
                int[] ws = ((IntWalkArray)snapshot.getWalksAtVertex(i, false)).getArray();
                if (ws != null) {
                    for(int j=0; j < ws.length; j++) {
                        int w = ws[j];
                        int source = sources[sourceIdx(w)];
                        dos.writeInt(source);
                        dos.writeInt(i);
                    }
                }
            }
            dos.flush();
            dos.close();
        }
        _timer.stop();
    }
View Full Code Here

        return sources[sourceIdx(walk)];
    }

    @Override
    public void populateSchedulerForInterval(Scheduler scheduler, VertexInterval interval) {
        final TimerContext _timer = schedulePopulate.time();
        int fromBucket = interval.getFirstVertex() / bucketSize;
        int toBucket = interval.getLastVertex() / bucketSize;

        for(int bucketIdx=fromBucket; bucketIdx <= toBucket; bucketIdx++) {
            int vertexBase = bucketIdx * bucketSize;
            int[] bucket = walks[bucketIdx];

            if (bucket != null) {
                BitSet alreadySeen = new BitSet(bucketSize);
                int counter = 0;
                for(int j=0; j<bucket.length; j++) {
                    int off = off(bucket[j]);
                    if (!alreadySeen.get(off))  {
                        alreadySeen.set(off, true);
                        counter++;
                        scheduler.addTask(vertexBase + off);
                        if (counter == bucketSize) break;
                    }
                }
            }
        }
        _timer.stop();
    }
View Full Code Here

                snapshots[vertexId - fromVertex] = null;
            }

            @Override
            public void restoreUngrabbed() {
                final TimerContext _timer = restore.time();
                // Restore such walks that were not grabbed (because the vertex
                // was not initially scheduled)
                int v = fromVertex;
                int restoreCount = 0;
                for(long[] snapshot : snapshots) {
                    if (snapshot != null && !processedBits[v - fromVertex]) {
                        for(int i=0; i<snapshot.length; i++) {
                            long w = snapshot[i];
                            moveWalk(w, v, trackBit(w));
                            restoreCount++;
                        }
                    }
                    v++;
                }
                logger.info("Restored " + restoreCount);
                _timer.stop();
            }

            // Note: accurate number only before snapshot is being purged
            public long numWalks() {
                long sum = 0;
                for(int b=fromBucket; b <= toBucket; b++) {
                    sum += walks[b].length;
                }
                return sum;
            }

            @Override
            public WalkArray getWalksAtVertex(int vertexId, boolean processed) {
                int bucketIdx = vertexId / bucketSize;
                int localBucketIdx = bucketIdx - (fromVertex / bucketSize);

                processedBits[vertexId - fromVertex] = true;

                if (snapshotInitBits[localBucketIdx]) {
                    long[] array = snapshots[vertexId - fromVertex];
                    if (array == null) {
                        return null;
                    } else {
                        return new LongWalkArray(snapshots[vertexId - fromVertex]);
                    }
                } else {
                    final TimerContext _timer = grabTimer.time();

                    long[] bucketToConsume = null;
                    int len = 0;
                    synchronized (bucketLocks[bucketIdx]) {
                        if (!snapshotInitBits[localBucketIdx]) {

                            int bucketFirstVertex = bucketSize * bucketIdx;
                            len = walkIndices[bucketIdx];

                            bucketToConsume = walks[bucketIdx];

                            if (bucketToConsume != null) {
                                walks[bucketIdx] = null;
                                walkIndices[bucketIdx] = 0;
                                final int[] snapshotSizes = new int[bucketSize];
                                final int[] snapshotIdxs = new int[bucketSize];

                                /* Calculate vertex-walks sizes */
                                for(int i=0; i < len; i++) {
                                    long w = bucketToConsume[i];
                                    snapshotSizes[off(w)]++;
                                }

                                int offt = bucketFirstVertex - fromVertex;

                                for(int i=0; i < snapshotSizes.length; i++) {
                                    if (snapshotSizes[i] > 0 && i >= -offt && i + offt < snapshots.length)
                                        snapshots[i + offt] = new long[snapshotSizes[i]];
                                }

                                for(int i=0; i < len; i++) {
                                    long w = bucketToConsume[i];
                                    int vertex = bucketFirstVertex + off(w);

                                    if (vertex >= fromVertex && vertex <= toVertexInclusive) {
                                        int snapshotOff = vertex - fromVertex;
                                        int localOff = vertex - bucketFirstVertex;
                                        snapshots[snapshotOff][snapshotIdxs[localOff]] = w;
                                        snapshotIdxs[localOff]++;
                                    } else {
                                        // add back
                                        moveWalk(w, vertex, trackBit(w));
                                    }
                                }
                            }
                            snapshotInitBits[localBucketIdx] = true;
                        }
                    }
                    if (bucketConsumer != null && bucketToConsume != null && len > 0) {
                        bucketConsumer.consume(bucketIdx * bucketSize, new LongWalkArray(bucketToConsume), len);
                        if (len > 1000000) {
                            log((bucketIdx * bucketSize) + " - " + ((bucketIdx+1)) * bucketSize + ", " + len);
                        }
                    }
                    _timer.stop();
                    long[] array = snapshots[vertexId - fromVertex];
                    if (array == null) {
                        return null;
                    } else {
                        return new LongWalkArray(snapshots[vertexId - fromVertex]);
View Full Code Here

    }

    /** Dump to file all walks with more than 0 hop */
    @Override
    public void dumpToFile(WalkSnapshot snapshot, String filename) throws IOException {
        final TimerContext _timer = dumpTimer.time();
        synchronized (filename.intern()) {
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filename), true)));
            for(int i=snapshot.getFirstVertex(); i <= snapshot.getLastVertex(); i++) {
                long[] ws = ((LongWalkArray)snapshot.getWalksAtVertex(i, false)).getArray();
                if (ws != null) {
                    for(int j=0; j < ws.length; j++) {
                        long w = ws[j];
                        int source = sources[sourceIdx(w)];
                        dos.writeLong(source);
                        dos.writeInt(i);
                    }
                }
            }
            dos.flush();
            dos.close();
        }
        _timer.stop();
    }
View Full Code Here

        return sources[sourceIdx(walk)];
    }

    @Override
    public void populateSchedulerForInterval(Scheduler scheduler, VertexInterval interval) {
        final TimerContext _timer = schedulePopulate.time();
        int fromBucket = interval.getFirstVertex() / bucketSize;
        int toBucket = interval.getLastVertex() / bucketSize;

        for(int bucketIdx=fromBucket; bucketIdx <= toBucket; bucketIdx++) {
            int vertexBase = bucketIdx * bucketSize;
            long[] bucket = walks[bucketIdx];

            if (bucket != null) {
                BitSet alreadySeen = new BitSet(bucketSize);
                int counter = 0;
                for(int j=0; j<bucket.length; j++) {
                    int off = off(bucket[j]);
                    if (!alreadySeen.get(off))  {
                        alreadySeen.set(off, true);
                        counter++;
                        scheduler.addTask(vertexBase + off);
                        if (counter == bucketSize) break;
                    }
                }
            }
        }
        _timer.stop();
    }
View Full Code Here

        curWalkSnapshot.restoreUngrabbed();
        curWalkSnapshot = null; // Release memory

        /* Purge local buffers */
        synchronized (localBuffers) {
            final TimerContext _timer = purgeTimer.time();
            for (LocalWalkBuffer buf : localBuffers) {
                buf.purge(job.getWalkManager());
            }
            localBuffers.clear();
            _timer.stop();
        }
    }
View Full Code Here

    private static final com.yammer.metrics.core.Timer putTimer =
            Metrics.newTimer(TextEntityResource.class, "puts", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);

    @POST
    public String echo(final String text) {
        final TimerContext timer = postTimer.time();
        try {
            return text;
        } finally {
            timer.stop();
        }
    }
View Full Code Here

TOP

Related Classes of com.yammer.metrics.core.TimerContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.