Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
Debug.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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
45double eMemoryAtMarker = 0.0;
46
47std::string FormDebugHead()
48{
49 std::stringstream header;
50 header << "DEBUG: ";
52 {
53 header << "proc " << PetscTools::GetMyRank() << ": ";
54 }
55 return header.str();
56}
57
58
59void PrintTheStack()
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
83void 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
96void UnmarkMemory()
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
105void PrintMemory()
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}
#define TRACE(stuff)
Definition Debug.hpp:96
static bool IsParallel()
static unsigned GetMyRank()