coherence/lang/HeapAnalyzer.hpp

00001 /*
00002 * HeapAnalyzer.hpp
00003 *
00004 * Copyright 2001-2008 by Oracle. All rights reserved.
00005 *
00006 * Oracle is a registered trademarks of Oracle Corporation and/or its
00007 * affiliates.
00008 *
00009 * This software is the confidential and proprietary information of Oracle
00010 * Corporation. You shall not disclose such confidential and proprietary
00011 * information and shall use it only in accordance with the terms of the
00012 * license agreement you entered into with Oracle.
00013 *
00014 * This notice may not be removed or altered.
00015 */
00016 #ifndef COH_HEAP_ANALYZER_HPP
00017 #define COH_HEAP_ANALYZER_HPP
00018 
00019 #include "coherence/lang/interface_spec.hpp"
00020 #include "coherence/lang/Object.hpp"
00021 #include "coherence/lang/TypedHandle.hpp"
00022 
00023 COH_OPEN_NAMESPACE2(coherence,lang)
00024 
00025 
00026 /**
00027 * HeapAnalyzer provides a base diagnostics interface for tracking heap useage.
00028 *
00029 * There is at most one HeapAnalyzer registered with the system for the
00030 * lifetime of the process. The HeapAnalyzer impelmentation may be specified
00031 * via the "tangosol.coherence.heap.analyzer" system property. The property
00032 * can be set to one of the following values:
00033 * <ul>
00034 *  <li>none      No heap analysis will be performed.</li>
00035 *  <li>object    The coherence::lang::ObjectCountHeapAnalyzer will be used.</li>
00036 *  <li>class     The coherence::lang::ClassBasedHeapAnalyzer will be used.</li>
00037 *  <li>[custom]  The name of a class registered with the SystemClassLoader.</li>
00038 * </ul>
00039 *
00040 * In the case where a custom class is specified, it must implement this
00041 * interface.  The custom analyzer will be initialized as soon as the class
00042 * is registered with the SystemClassLoader. As static initalization order
00043 * cannot be guarenteed, this custom analyzer will not be notified of managed
00044 * objects created earlier in the static initialization order.
00045 *
00046 * The active analyzer may be obtained from the System::getHeapAnalyzer()
00047 * method.
00048 *
00049 * The HeapAnalyzer and Snapshot interfaces are intentionally narrow.
00050 * Implementations are expected to provide usefull information via the toStream
00051 * method, as well as by possibly augmenting the interfaces. The minimal
00052 * interface is sufficient for detecting memory leaks.
00053 *
00054 * HeapAnalyzer::Snapshot::View vSnap = hAnalyzer->capture();
00055 *   ...
00056 *   ...
00057 * std::cout << "Heap changed by: " << hAnalyzer->delta(vSnap) << std::endl;
00058 *
00059 * @see ObjectCountHeapAnalyzer
00060 * @see ClassBasedHeapAnalyzer
00061 *
00062 * @author mf  2008.04.27
00063 */
00064 class COH_EXPORT HeapAnalyzer
00065     : public interface_spec<HeapAnalyzer>
00066     {
00067     // ----- nested interface: Snapshot -------------------------------------
00068 
00069     public:
00070         /**
00071         * Snapshot provides a abstract mechanism for comparing successive
00072         * heap analysis points.
00073         */
00074         class COH_EXPORT Snapshot
00075             : public interface_spec<Snapshot>
00076             {
00077             // ----- Snapshot interface ---------------------------------
00078 
00079             public:
00080                 /**
00081                 * Return the number of registered objects reflected by this
00082                 * snapshot.
00083                 *
00084                 * @return the number of registered objects
00085                 */
00086                 virtual int64_t getObjectCount() const = 0;
00087             };
00088 
00089 
00090     // ----- HeapAnalyzer interface -----------------------------------------
00091 
00092     public:
00093         /**
00094         * Capture a Snapshot of the current state of the heap.
00095         *
00096         * @return a Snapshot of the current state of the heap.
00097         */
00098         virtual Snapshot::View capture() const = 0;
00099 
00100         /**
00101         * Compute the delta between the supplied Snapshot and the current heap
00102         * state.
00103         *
00104         * @param vThat  the snapshot to compare against.
00105         *
00106         * @return a snapshot containing the delta
00107         */
00108         virtual Snapshot::View delta(Snapshot::View vThat) const = 0;
00109 
00110         /**
00111         * Return the number of registered objects.
00112         *
00113         * @return the number of registered objects
00114         */
00115         virtual int64_t getObjectCount() const = 0;
00116 
00117     protected:
00118         /**
00119         * Register a newly created Object with the system.
00120         *
00121         * This method is called automatically by coherence::lang::Object once
00122         * the Object has finished construction.
00123         *
00124         * @param o  the newly created Object.
00125         */
00126         virtual void registerObject(const Object& o) = 0;
00127 
00128         /**
00129         * Unregister an Object with the system.
00130         *
00131         * This method is called automatically by coherence::lang::Object
00132         * just prior to the deletion of the Object.  No new handles or views
00133         * may be created to the object.
00134         *
00135         * @param o  the Object to unregister
00136         */
00137         virtual void unregisterObject(const Object& o) = 0;
00138 
00139 
00140     // ----- static helper methods ------------------------------------------
00141 
00142     public:
00143         /**
00144         * Ensure that the delta between the current heap and the supplied
00145         * snapshot is as expected.
00146         *
00147         * This method can be used to perform quick memory leak assertions.
00148         *
00149         * @code
00150         * HeapAnalyzer::Snapshot::View vSnapStart = System::getHeapAnalyzer()->capture();
00151         *   ...
00152         *   ...
00153         * HeapAnalyzer::ensureHeap(vSnapStart);
00154         * @endcode
00155         *
00156         * @param vSnap   the snapshot to ensure
00157         * @param cDelta  the allowable change in the heap's object count
00158         *
00159         * @throws IllegalStateException if the delta does not contain the
00160         *         expected amount. The text of the exception will include the
00161         *         output of the Snapshots toStream() method.
00162         */
00163         static void ensureHeap(Snapshot::View vSnap, int64_t cDelta = 0);
00164 
00165     // ----- friends --------------------------------------------------------
00166 
00167     friend class Object;
00168     };
00169 
00170 COH_CLOSE_NAMESPACE2
00171 
00172 #endif // COH_HEAP_ANALYZER_HPP
Copyright (c) 2000-2008 Oracle. All rights reserved.