casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef CASA_MEMORYTRACE_H
27#define CASA_MEMORYTRACE_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/OS/Timer.h>
31#include <fstream>
32#include <string>
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36 // <summary>memory usage tracing mechanism</summary>
37 // <use visibility=export>
38 //
39 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
40 // </reviewed>
41 //
42 // <synopsis>
43 // The MemoryTrace class provides some means to trace the
44 // memory usage of a program. It logs malloc and free messages in
45 // a file which can be examined by the python script memorytrace.py.
46 // <br>The tracing is done using hooks for malloc and free as explained
47 // in 'man malloc_hook'.
48 //
49 // The tracing can be started and stopped at any time. On the first
50 // start the trace file is created. The file can be closed at any time,
51 // usually at the end of a program. Another start will recreate the file.
52 //
53 // The trace file consists of 3 types of lines:
54 // <ul>
55 // <li> An allocation line like "a <address> <caller> <size>"
56 // <li> A deallocation line like "f <address> <caller>"
57 // <li> A line like "begin/end <name>" telling the script the beginning
58 // or end of a code block. It makes it possible to see how memory usage
59 // develops. Such lines can be inserted using the class MemoryTraceBlock.
60 // </ul>
61 // All lines start with the number of milliseconds since the start of
62 // the program.
63 // <p>
64 // The script memorytrace.py can be used to interpret the log file and
65 // to show the memory usage.
66 // </synopsis>
67
69 {
70 public:
71 // Start the tracing. Nothing is done if already started.
72 // On the first time, it opens the trace file. The name of the
73 // trace file can be given in the env.var. CASACORE_MEMORYTRACE.
74 // If undefined, the name casacore_memorytrace.log will be used.
75 static void start();
76
77 // Stop the tracing.
78 static void stop();
79
80 // Open the trace file if not open yet.
81 static void open();
82
83 // Close the tracing output file.
84 static void close();
85
86 // Is tracing on?
87 static Bool isOn()
88 { return theirDoTrace; }
89
90 // Is the tracing file opened?
91 static Bool isOpen()
92 { return theirFile.is_open(); }
93
94 // Write a block line in the output file.
95 static void writeBlock (const char* msg, const std::string& name);
96 static void writeBlock (const char* msg, const char* name);
97
98 // Write an alloc or free message.
99 static std::ofstream& writeAlloc (const void* ptr, size_t);
100 static std::ofstream& writeFree (const void* ptr);
101
102 // The hooks for malloc and free writing the trace messages.
103 static void* mallocHook (size_t, const void* caller);
104 static void freeHook (void*, const void* caller);
105
106 // Make a string from a char* without tracing a possible malloc in
107 // the string constructor.
108 static std::string makeString (const char*);
109
110 private:
112 static std::ofstream theirFile;
114 //# Variables to save original hooks.
115 static void* (*theirOldMallocHook)(size_t, const void*);
116 static void (*theirOldFreeHook)(void*, const void*);
117 };
118
119
120 // <summary> Class to write begin and end block message </summary>
121 // <synopsis>
122 // This class is meant to write memory trace messages indicating the
123 // beginning and end of a code block. In this way it is known that the
124 // (de)allocate messages between these messages belong to that code block.
125 //
126 // The constructor writes the begin message, while the destructor writes
127 // the end message. Because the destructor is called automatically by the
128 // compiler, the user does not have to worry about it; it will also
129 // work fine in case of a premature exit from a function.
130 //
131 // It is possible to nest blocks as deeply as one likes.
132 // </synopsis>
134 {
135 public:
136 // The constructor writes a block begin message.
137 MemoryTraceBlock (const std::string& name);
138 MemoryTraceBlock (const char* name);
139 // The constructor writes a block end message.
141 private:
142 std::string itsName;
143 };
144
145} //# NAMESPACE CASACORE - END
146
147
148//# Trace memory (de)allocation.
149#define traceMemoryAlloc(ptr,size,msg) \
150 if (casacore::MemoryTrace::isOpen()) { \
151 casacore::MemoryTrace::writeAlloc (ptr, size) << msg << std::endl; \
152 }
153#define traceMemoryFree(ptr,msg) \
154 if (casacore::MemoryTrace::isOpen()) { \
155 casacore::MemoryTrace::writeFree (ptr) << msg << std::endl; \
156 }
157
158#define traceMemoryBlockBegin(name) \
159 if (casacore::MemoryTrace::isOpen()) { \
160 casacore::MemoryTrace::writeBlock(" begin ", name); \
161 }
162#define traceMemoryBlockEnd(name) \
163 if (casacore::MemoryTrace::isOpen()) { \
164 casacore::MemoryTrace::writeBlock(" end ", name); \
165 }
166
167
168#endif
Class to write begin and end block message.
~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:87
static std::ofstream & writeFree(const void *ptr)
static Bool theirDoTrace
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
static Bool isOpen()
Is the tracing file opened?
Definition MemoryTrace.h:91
static void(* theirOldFreeHook)(void *, const void *)
static std::ofstream & writeAlloc(const void *ptr, size_t)
Write an alloc or free message.
static void writeBlock(const char *msg, const std::string &name)
Write a block line in the output file.
static Timer theirTimer
static void writeBlock(const char *msg, const char *name)
static void * mallocHook(size_t, const void *caller)
The hooks for malloc and free writing the trace messages.
static void close()
Close the tracing output file.
static void freeHook(void *, const void *caller)
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:40