casacore
MemoryTrace.h
Go to the documentation of this file.
1 //# MemoryTrace.h: Memory usage tracing mechanism
2 //# Copyright (C) 2015
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: Block.h 21120 2011-09-01 13:51:56Z gervandiepen $
27 
28 #ifndef CASA_MEMORYTRACE_H
29 #define CASA_MEMORYTRACE_H
30 
31 #include <casacore/casa/aips.h>
32 #include <casacore/casa/OS/Timer.h>
33 #include <fstream>
34 #include <string>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38  // <summary>memory usage tracing mechanism</summary>
39  // <use visibility=export>
40  //
41  // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
42  // </reviewed>
43  //
44  // <synopsis>
45  // The MemoryTrace class provides some means to trace the
46  // memory usage of a program. It logs malloc and free messages in
47  // a file which can be examined by the python script memorytrace.py.
48  // <br>The tracing is done using hooks for malloc and free as explained
49  // in 'man malloc_hook'.
50  //
51  // The tracing can be started and stopped at any time. On the first
52  // start the trace file is created. The file can be closed at any time,
53  // usually at the end of a program. Another start will recreate the file.
54  //
55  // The trace file consists of 3 types of lines:
56  // <ul>
57  // <li> An allocation line like "a <address> <caller> <size>"
58  // <li> A deallocation line like "f <address> <caller>"
59  // <li> A line like "begin/end <name>" telling the script the beginning
60  // or end of a code block. It makes it possible to see how memory usage
61  // develops. Such lines can be inserted using the class MemoryTraceBlock.
62  // </ul>
63  // All lines start with the number of milliseconds since the start of
64  // the program.
65  // <p>
66  // The script memorytrace.py can be used to interpret the log file and
67  // to show the memory usage.
68  // </synopsis>
69 
71  {
72  public:
73  // Start the tracing. Nothing is done if already started.
74  // On the first time, it opens the trace file. The name of the
75  // trace file can be given in the env.var. CASACORE_MEMORYTRACE.
76  // If undefined, the name casacore_memorytrace.log will be used.
77  static void start();
78 
79  // Stop the tracing.
80  static void stop();
81 
82  // Open the trace file if not open yet.
83  static void open();
84 
85  // Close the tracing output file.
86  static void close();
87 
88  // Is tracing on?
89  static Bool isOn()
90  { return theirDoTrace; }
91 
92  // Is the tracing file opened?
93  static Bool isOpen()
94  { return theirFile.is_open(); }
95 
96  // Write a block line in the output file.
97  static void writeBlock (const char* msg, const std::string& name);
98  static void writeBlock (const char* msg, const char* name);
99 
100  // Write an alloc or free message.
101  static std::ofstream& writeAlloc (const void* ptr, size_t);
102  static std::ofstream& writeFree (const void* ptr);
103 
104  // The hooks for malloc and free writing the trace messages.
105  static void* mallocHook (size_t, const void* caller);
106  static void freeHook (void*, const void* caller);
107 
108  // Make a string from a char* without tracing a possible malloc in
109  // the string constructor.
110  static std::string makeString (const char*);
111 
112  private:
114  static std::ofstream theirFile;
116  //# Variables to save original hooks.
117  static void* (*theirOldMallocHook)(size_t, const void*);
118  static void (*theirOldFreeHook)(void*, const void*);
119  };
120 
121 
122  // <summary> Class to write begin and end block message </summary>
123  // <synopsis>
124  // This class is meant to write memory trace messages indicating the
125  // beginning and end of a code block. In this way it is known that the
126  // (de)allocate messages between these messages belong to that code block.
127  //
128  // The constructor writes the begin message, while the destructor writes
129  // the end message. Because the destructor is called automatically by the
130  // compiler, the user does not have to worry about it; it will also
131  // work fine in case of a premature exit from a function.
132  //
133  // It is possible to nest blocks as deeply as one likes.
134  // </synopsis>
136  {
137  public:
138  // The constructor writes a block begin message.
139  MemoryTraceBlock (const std::string& name);
140  MemoryTraceBlock (const char* name);
141  // The constructor writes a block end message.
143  private:
144  std::string itsName;
145  };
146 
147 } //# NAMESPACE CASACORE - END
148 
149 
150 //# Trace memory (de)allocation.
151 #define traceMemoryAlloc(ptr,size,msg) \
152  if (casacore::MemoryTrace::isOpen()) { \
153  casacore::MemoryTrace::writeAlloc (ptr, size) << msg << std::endl; \
154  }
155 #define traceMemoryFree(ptr,msg) \
156  if (casacore::MemoryTrace::isOpen()) { \
157  casacore::MemoryTrace::writeFree (ptr) << msg << std::endl; \
158  }
159 
160 #define traceMemoryBlockBegin(name) \
161  if (casacore::MemoryTrace::isOpen()) { \
162  casacore::MemoryTrace::writeBlock(" begin ", name); \
163  }
164 #define traceMemoryBlockEnd(name) \
165  if (casacore::MemoryTrace::isOpen()) { \
166  casacore::MemoryTrace::writeBlock(" end ", name); \
167  }
168 
169 
170 #endif
Class to write begin and end block message.
Definition: MemoryTrace.h:136
~MemoryTraceBlock()
The constructor writes a block end message.
MemoryTraceBlock(const char *name)
MemoryTraceBlock(const std::string &name)
The constructor writes a block begin message.
static void open()
Open the trace file if not open yet.
static Bool isOn()
Is tracing on?
Definition: MemoryTrace.h:89
static Bool theirDoTrace
Definition: MemoryTrace.h:113
static std::string makeString(const char *)
Make a string from a char* without tracing a possible malloc in the string constructor.
static std::ofstream theirFile
Definition: MemoryTrace.h:114
static void * mallocHook(size_t, const void *caller)
The hooks for malloc and free writing the trace messages.
static Bool isOpen()
Is the tracing file opened?
Definition: MemoryTrace.h:93
static void(* theirOldFreeHook)(void *, const void *)
Definition: MemoryTrace.h:118
static std::ofstream & writeFree(const void *ptr)
static void writeBlock(const char *msg, const std::string &name)
Write a block line in the output file.
static Timer theirTimer
Definition: MemoryTrace.h:115
static void writeBlock(const char *msg, const char *name)
static void close()
Close the tracing output file.
static void freeHook(void *, const void *caller)
static std::ofstream & writeAlloc(const void *ptr, size_t)
Write an alloc or free message.
static void start()
Start the tracing.
static void stop()
Stop the tracing.
this file contains all the compiler specific defines
Definition: mainpage.dox:28
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42