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