Update ooo320-m1
[ooovba.git] / framework / inc / macros / debug / memorymeasure.hxx
blob19d510fae9b3f1284e9cbeef927e6b00e872f315
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: memorymeasure.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_MACROS_DEBUG_MEMORYMEASURE_HXX_
32 #define __FRAMEWORK_MACROS_DEBUG_MEMORYMEASURE_HXX_
34 //*************************************************************************************************************
35 // special macros for time measures
36 // 1) LOGFILE_MEMORYMEASURE used it to define log file for this operations (default will be set automaticly)
37 // 2) MAKE_MEMORY_SNAPSHOT make snapshot of currently set memory informations of OS
38 // 3) LOG_MEMORYMEASURE write measured time to logfile
39 //*************************************************************************************************************
41 #ifdef ENABLE_MEMORYMEASURE
43 #if !defined( WIN ) && !defined( WNT )
44 #error "Macros to measure memory access not available under platforms different from windows!"
45 #endif
47 //_________________________________________________________________________________________________________________
48 // includes
49 //_________________________________________________________________________________________________________________
51 #ifndef _RTL_STRBUF_HXX_
52 #include <rtl/strbuf.hxx>
53 #endif
55 #ifndef __SGI_STL_VECTOR
56 #include <vector>
57 #endif
59 /*_____________________________________________________________________________________________________________
60 LOGFILE_MEMORYMEASURE
62 For follow macros we need a special log file. If user forget to specify anyone, we must do it for him!
63 _____________________________________________________________________________________________________________*/
65 #ifndef LOGFILE_MEMORYMEASURE
66 #define LOGFILE_MEMORYMEASURE "memorymeasure.log"
67 #endif
69 /*_____________________________________________________________________________________________________________
70 class MemoryMeasure
72 We use this baseclass to collect all snapshots in one object and analyze this information at one point.
73 Macros of this file are used to enable using of this class by special compile-parameter only!
74 _____________________________________________________________________________________________________________*/
76 class _DBGMemoryMeasure
78 //---------------------------------------------------------------------------------------------------------
79 private:
80 struct _MemoryInfo
82 MEMORYSTATUS aStatus ;
83 ::rtl::OString sComment ;
86 //---------------------------------------------------------------------------------------------------------
87 public:
88 //_____________________________________________________________________________________________________
89 inline _DBGMemoryMeasure()
93 //_____________________________________________________________________________________________________
94 // clear used container!
95 inline ~_DBGMemoryMeasure()
97 ::std::vector< _MemoryInfo >().swap( m_lSnapshots );
100 //_____________________________________________________________________________________________________
101 inline void makeSnapshot( const ::rtl::OString& sComment )
103 _MemoryInfo aInfo;
104 aInfo.sComment = sComment;
105 GlobalMemoryStatus ( &(aInfo.aStatus) );
106 m_lSnapshots.push_back( aInfo );
109 //_____________________________________________________________________________________________________
110 inline ::rtl::OString getLog()
112 ::rtl::OStringBuffer sBuffer( 10000 );
114 if( !m_lSnapshots.empty() )
116 // Write informations to return buffer
117 ::std::vector< _MemoryInfo >::const_iterator pItem1;
118 ::std::vector< _MemoryInfo >::const_iterator pItem2;
120 pItem1 = m_lSnapshots.begin();
121 pItem2 = pItem1;
122 ++pItem2;
124 while( pItem1!=m_lSnapshots.end() )
126 sBuffer.append( "snap [ " );
127 sBuffer.append( pItem1->sComment );
128 sBuffer.append( " ]\n\tavail phys\t=\t" );
129 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailPhys );
130 sBuffer.append( "\n\tavail page\t=\t" );
131 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailPageFile );
132 sBuffer.append( "\n\tavail virt\t=\t" );
133 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailVirtual );
134 sBuffer.append( "\n\tdifference\t=\t[ " );
136 if( pItem1 == m_lSnapshots.begin() )
138 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailPhys );
139 sBuffer.append( ", " );
140 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailPageFile );
141 sBuffer.append( ", " );
142 sBuffer.append( (sal_Int32)pItem1->aStatus.dwAvailVirtual );
143 sBuffer.append( " ]\n\n" );
145 else if( pItem2 != m_lSnapshots.end() )
147 sBuffer.append( (sal_Int32)(pItem2->aStatus.dwAvailPhys - pItem1->aStatus.dwAvailPhys ) );
148 sBuffer.append( ", " );
149 sBuffer.append( (sal_Int32)(pItem2->aStatus.dwAvailPageFile - pItem1->aStatus.dwAvailPageFile ) );
150 sBuffer.append( ", " );
151 sBuffer.append( (sal_Int32)(pItem2->aStatus.dwAvailVirtual - pItem1->aStatus.dwAvailVirtual ) );
152 sBuffer.append( " ]\n\n" );
154 else
156 sBuffer.append( "0, 0, 0 ]\n\n" );
158 if( pItem1!=m_lSnapshots.end() ) ++pItem1;
159 if( pItem2!=m_lSnapshots.end() ) ++pItem2;
161 // clear current list ... make it empty for further snapshots!
162 ::std::vector< _MemoryInfo >().swap( m_lSnapshots );
165 return sBuffer.makeStringAndClear();
168 //---------------------------------------------------------------------------------------------------------
169 private:
170 ::std::vector< _MemoryInfo > m_lSnapshots;
173 /*_____________________________________________________________________________________________________________
174 START_MEMORY_MEASURE
176 Create new object to measure memory access.
177 _____________________________________________________________________________________________________________*/
179 #define START_MEMORYMEASURE( AOBJECT ) \
180 _DBGMemoryMeasure AOBJECT;
182 /*_____________________________________________________________________________________________________________
183 MAKE_MEMORY_SNAPSHOT
185 Make snapshot of currently set memory informations of OS.
186 see _DBGMemoryMeasure for further informations
187 _____________________________________________________________________________________________________________*/
189 #define MAKE_MEMORY_SNAPSHOT( AOBJECT, SCOMMENT ) \
190 AOBJECT.makeSnapshot( SCOMMENT );
192 /*_____________________________________________________________________________________________________________
193 LOG_MEMORYMEASURE( SOPERATION, SCOMMENT, AOBJECT )
195 Write measured values to logfile.
196 _____________________________________________________________________________________________________________*/
198 #define LOG_MEMORYMEASURE( SOPERATION, SCOMMENT, AOBJECT ) \
200 ::rtl::OStringBuffer _sBuffer( 256 ); \
201 _sBuffer.append( SOPERATION ); \
202 _sBuffer.append( "\n" ); \
203 _sBuffer.append( SCOMMENT ); \
204 _sBuffer.append( "\n\n" ); \
205 _sBuffer.append( AOBJECT.getLog() ); \
206 WRITE_LOGFILE( LOGFILE_MEMORYMEASURE, _sBuffer.makeStringAndClear() ) \
209 #else // #ifdef ENABLE_MEMORYMEASURE
211 /*_____________________________________________________________________________________________________________
212 If right testmode is'nt set - implements these macros empty!
213 _____________________________________________________________________________________________________________*/
215 #undef LOGFILE_MEMORYMEASURE
216 #define START_MEMORYMEASURE( AOBJECT )
217 #define MAKE_MEMORY_SNAPSHOT( AOBJECT, SCOMMENT )
218 #define LOG_MEMORYMEASURE( SOPERATION, SCOMMENT, AOBJECT )
220 #endif // #ifdef ENABLE_MEMORYMEASURE
222 //*****************************************************************************************************************
223 // end of file
224 //*****************************************************************************************************************
226 #endif // #ifndef __FRAMEWORK_MACROS_DEBUG_MEMORYMEASURE_HXX_