All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.red5.server.net.rtmp.event.AllocationDebugger Maven / Gradle / Ivy

Go to download

Ant Media Server supports RTMP, RTSP, MP4, HLS, WebRTC, Adaptive Streaming, etc.

There is a newer version: 2.11.3
Show newest version
/*
 * RED5 Open Source Media Server - https://github.com/Red5/
 * 
 * Copyright 2006-2016 by respective authors (see below). All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.red5.server.net.rtmp.event;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Simple allocation debugger for Event reference counting.
 * 
 * @author The Red5 Project
 * @author Steven Gong ([email protected]) on behalf of ([email protected])
 * @author Paul Gregoire ([email protected])
 */
public class AllocationDebugger {

    /**
     * Information on references count
     */
    private static class Info {
        /**
         * References count
         */
        public AtomicInteger refcount = new AtomicInteger(1);

        /** Constructs a new Info. */
        public Info() {
        }

    }

    /**
     * Allocation debugger istance
     */
    private static AllocationDebugger instance = new AllocationDebugger();

    /**
     * Logger
     */
    private Logger log;

    /**
     * Events-to-information objects map
     */
    private ConcurrentMap events;

    /**
     * Getter for instance
     * 
     * @return Allocation debugger instance
     */
    public static AllocationDebugger getInstance() {
        return instance;
    }

    /** Do not instantiate AllocationDebugger. */
    private AllocationDebugger() {
        log = LoggerFactory.getLogger(getClass());
        events = new ConcurrentHashMap();
    }

    /**
     * Add event to map
     * 
     * @param event
     *            Event
     */
    protected void create(BaseEvent event) {
        events.put(event, new Info());
    }

    /**
     * Retain event
     * 
     * @param event
     *            Event
     */
    protected void retain(BaseEvent event) {
        Info info = events.get(event);
        if (info != null) {
            info.refcount.incrementAndGet();
        } else {
            log.warn("Retain called on already released event.");
        }
    }

    /**
     * Release event if there's no more references to it
     * 
     * @param event
     *            Event
     */
    protected void release(BaseEvent event) {
        Info info = events.get(event);
        if (info != null) {
            if (info.refcount.decrementAndGet() == 0) {
                events.remove(event);
            }
        } else {
            log.warn("Release called on already released event.");
        }
    }

    /**
     * Dumps allocations
     */
    public synchronized void dump() {
        if (log.isDebugEnabled()) {
            log.debug("dumping allocations {}", events.size());
            for (Entry entry : events.entrySet()) {
                log.debug("{} {}", entry.getKey(), entry.getValue().refcount);
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy