Chaste  Release::2018.1
Debug.cpp
1 /*
2 
3 Copyright (c) 2005-2018, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "Debug.hpp"
37 // If you ever need backtrace in Windows then the RTFM begins at "CaptureStackBackTrace"
38 #ifndef _MSC_VER
39 #include <execinfo.h> //For backtrace
40 #include <unistd.h> // For memory profiling
41 #include <sys/resource.h> // For memory profiling
42 #endif//_MSC_VER
43 //#include <cxxabi.h> // For demangling C++ C-style names
44 
45 double eMemoryAtMarker = 0.0;
46 
47 std::string FormDebugHead()
48 {
49  std::stringstream header;
50  header << "DEBUG: ";
52  {
53  header << "proc " << PetscTools::GetMyRank() << ": ";
54  }
55  return header.str();
56 }
57 
58 
60 {
61  // If you ever need backtrace in Windows then the RTFM begins at "CaptureStackBackTrace"
62  TRACE("Stack information");
63 #ifndef _MSC_VER
64  // storage array for stack trace address data
65  void* address_list[20u]; //20 is about the number of stack symbols we are going to print
66 
67  // retrieve current stack addresses
68  unsigned num_addresses = backtrace(address_list, sizeof(address_list) / sizeof(void*));
69 
70  char** symbol_list = backtrace_symbols(address_list, num_addresses);
71 
72  // iterate over the returned symbol lines.
73  for (unsigned i = 0; i < num_addresses; i++)
74  {
75  // We could demangle, but this may not be portable on GNU Linux versus Mac OSX
76  // char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
77  TRACE("Level " << i << ": " << symbol_list[i]);
78  }
79  free(symbol_list);
80 #endif //_MSC_VER
81 }
82 
83 void MarkMemory()
84 {
85  TRACE("PRINT_MEMORY will now be relative to here");
86 
87 #ifndef _MSC_VER
88  struct rusage rusage;
89  getrusage( RUSAGE_SELF, &rusage );
90 
91  // Store current memory footprint in Mb to allow calculations relative to this point
92  eMemoryAtMarker = double(rusage.ru_maxrss)/1024.0;
93 #endif //_MSC_VER
94 }
95 
97 {
98  TRACE("PRINT_MEMORY will now be absolute footprint");
99 
100 #ifndef _MSC_VER
101  eMemoryAtMarker = 0.0;
102 #endif //_MSC_VER
103 }
104 
106 {
107 #ifndef _MSC_VER
108  struct rusage rusage;
109  getrusage( RUSAGE_SELF, &rusage );
110 
111  // Memory as difference between now and the marker
112  double memory = double(rusage.ru_maxrss)/1024.0 - eMemoryAtMarker;
113 
114  if (fabs(eMemoryAtMarker) < 1e-6) // If marker is zero, this is the total memory footprint
115  {
116  std::cout << FormDebugHead() << "Total memory footprint: " << memory << " Mb" << std::endl << std::flush;
117  }
118  else // If marker non-zero, this is a change in memory
119  {
120  std::cout << FormDebugHead() << "Memory change from marker: " << memory << " Mb" << std::endl << std::flush;
121  }
122 #endif //_MSC_VER
123 }
std::string FormDebugHead()
Definition: Debug.cpp:47
void MarkMemory()
Definition: Debug.cpp:83
void PrintTheStack()
Definition: Debug.cpp:59
#define TRACE(stuff)
Definition: Debug.hpp:96
void PrintMemory()
Definition: Debug.cpp:105
static bool IsParallel()
Definition: PetscTools.cpp:97
double eMemoryAtMarker
Definition: Debug.cpp:45
static unsigned GetMyRank()
Definition: PetscTools.cpp:114
void UnmarkMemory()
Definition: Debug.cpp:96