coherence/lang/ClassBasedHeapAnalyzer.hpp

00001 /*
00002 * ClassBasedHeapAnalyzer.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_CLASS_BASED_HEAP_ANALYZER_HPP
00017 #define COH_CLASS_BASED_HEAP_ANALYZER_HPP
00018 
00019 #include "coherence/lang/compatibility.hpp"
00020 
00021 #include "coherence/lang/Comparable.hpp"
00022 #include "coherence/lang/AbstractHeapAnalyzer.hpp"
00023 
00024 #include <ostream>
00025 
00026 COH_OPEN_NAMESPACE2(coherence,util)
00027 class Map;
00028 COH_CLOSE_NAMESPACE2
00029 
00030 COH_OPEN_NAMESPACE2(coherence,lang)
00031 
00032 using coherence::util::Map;
00033 
00034 
00035 /**
00036 * ClassBasedHeapAnalyzer provides heap analysis at the class level, that is
00037 * it tracks the number of live instances of each class.
00038 *
00039 * The memory consumption of this heap analyzer is relative to the number of
00040 * classes used within the process. The CPU consumption is also very low, each
00041 * registration consists of roughly four compare-and-set operations. It is well
00042 * suited for development as well as many production envrionments.
00043 *
00044 * @see ObjectCountHeapAnalyzer for a lower overhead analyzer
00045 *
00046 * @author mf  2008.04.27
00047 */
00048 class COH_EXPORT ClassBasedHeapAnalyzer
00049     : public class_spec<ClassBasedHeapAnalyzer,
00050         extends<AbstractHeapAnalyzer> >
00051     {
00052     friend class factory<ClassBasedHeapAnalyzer>;
00053 
00054     // ----- constructor ----------------------------------------------------
00055 
00056     protected:
00057         /**
00058         * Create a new ClassBasedHeapAnalyzer.
00059         */
00060         ClassBasedHeapAnalyzer();
00061 
00062 
00063     // ----- nested class: ClassStats ---------------------------------------
00064 
00065     public:
00066         /**
00067         * Statistics relating to a class.
00068         */
00069         class COH_EXPORT ClassStats
00070             : public class_spec<ClassStats,
00071                 extends<Object>,
00072                 implements<Comparable> >
00073             {
00074             friend class factory<ClassStats>;
00075 
00076             protected:
00077                 /**
00078                 * Create a new ClassStats.
00079                 *
00080                 * @return the new ClassStats
00081                 */
00082                 ClassStats(int64_t cObjs, int64_t cBytes);
00083 
00084             // ----- ClassStats interface ---------------------------------
00085 
00086             public:
00087                 /**
00088                 * Return the instance count for the class.
00089                 */
00090                 virtual int64_t getObjectCount() const;
00091 
00092                 /**
00093                 * Return the byte count for the class.
00094                 */
00095                 virtual int64_t getByteCount() const;
00096 
00097             // ----- Comparable interface ---------------------------------
00098 
00099             public:
00100                 /**
00101                 * {@inheritDoc}
00102                 */
00103                 int32_t compareTo(Object::View v) const;
00104 
00105             // ----- Object interface -------------------------------------
00106 
00107             public:
00108                 /**
00109                 * {@inheritDoc}
00110                 */
00111                 virtual void toStream(std::ostream& out) const;
00112 
00113             // ----- data members -----------------------------------------
00114 
00115             protected:
00116                 /**
00117                 * The number of instanaces of the class.
00118                 */
00119                 int64_t m_cInstanceCount;
00120 
00121                 /**
00122                 * The byte size of all the instances.
00123                 */
00124                 int64_t m_cByteCount;
00125             };
00126 
00127     // ----- nested class: Snapshot -----------------------------------------
00128 
00129     public:
00130         /**
00131         * Snapshot containging the object count.
00132         */
00133         class COH_EXPORT Snapshot
00134             : public class_spec<Snapshot,
00135                 extends<Object>,
00136                 implements<HeapAnalyzer::Snapshot> >
00137             {
00138             friend class factory<Snapshot>;
00139 
00140             // ----- constructors ---------------------------------------
00141 
00142             protected:
00143                 /**
00144                 * Create a new Snapshot.
00145                 */
00146                 Snapshot(TypedHandle<const Map> vMap);
00147 
00148             // ----- Snapshot interface ---------------------------------
00149 
00150             public:
00151                 /**
00152                 * Return the Snapshots map of class names to ClassStats.
00153                 *
00154                 * The keys are class names, the values are ClassStats.
00155                 *
00156                 * @return the snapshots map.
00157                 */
00158                 virtual TypedHandle<const Map> getStatsMap() const;
00159 
00160                 /**
00161                 * {@inheritDoc}
00162                 */
00163                 virtual int64_t getObjectCount() const;
00164 
00165             // ----- Object interface: ----------------------------------
00166 
00167             public:
00168                 /**
00169                 * {@inheritDoc}
00170                 */
00171                 virtual void toStream(std::ostream& out) const;
00172 
00173             // ----- data members ---------------------------------------
00174 
00175             protected:
00176                 /**
00177                 * The map of class names to ClassStats.
00178                 */
00179                 Object::View m_vMapStats;
00180             };
00181 
00182 
00183     // ----- AbstractHeapAnalyzer interface ---------------------------------
00184 
00185     protected:
00186         /**
00187         * {@inheritDoc}
00188         */
00189         virtual void safeRegisterObject(const Object& o);
00190 
00191         /**
00192         * {@inheritDoc}
00193         */
00194         virtual void safeUnregisterObject(const Object& o);
00195 
00196 
00197     // ----- HeapAnalyzer interface -----------------------------------------
00198 
00199     public:
00200         /**
00201         * {@inheritDoc}
00202         */
00203         virtual HeapAnalyzer::Snapshot::View capture() const;
00204 
00205         /**
00206         * {@inheritDoc}
00207         */
00208         virtual HeapAnalyzer::Snapshot::View delta(
00209                 HeapAnalyzer::Snapshot::View vSnap) const;
00210 
00211         /**
00212         * {@inheritDoc}
00213         */
00214         virtual int64_t getObjectCount() const;
00215 
00216     protected:
00217         /**
00218         * {@inheritDoc}
00219         */
00220         virtual void registerObject(const Object& o);
00221 
00222         /**
00223         * {@inheritDoc}
00224         */
00225         virtual void unregisterObject(const Object& o);
00226     };
00227 
00228 COH_CLOSE_NAMESPACE2
00229 
00230 #endif // COH_CLASS_BASED_HEAP_ANALYZER_HPP
Copyright (c) 2000-2008 Oracle. All rights reserved.