Chaste Commit::fa89f2b838c1edb21a1eaec92ee3a2eacc9255dd
Debug.cpp
1/*
2
3Copyright (c) 2005-2026, 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#include <execinfo.h> //For backtrace
38#include <unistd.h> // For memory profiling
39#include <sys/resource.h> // For memory profiling
40//#include <cxxabi.h> // For demangling C++ C-style names
41
42double eMemoryAtMarker = 0.0;
43
44std::string FormDebugHead()
45{
46 std::stringstream header;
47 header << "DEBUG: ";
49 {
50 header << "proc " << PetscTools::GetMyRank() << ": ";
51 }
52 return header.str();
53}
54
55
56void PrintTheStack()
57{
58 TRACE("Stack information");
59 // storage array for stack trace address data
60 void* address_list[20u]; //20 is about the number of stack symbols we are going to print
61
62 // retrieve current stack addresses
63 unsigned num_addresses = backtrace(address_list, sizeof(address_list) / sizeof(void*));
64
65 char** symbol_list = backtrace_symbols(address_list, num_addresses);
66
67 // iterate over the returned symbol lines.
68 for (unsigned i = 0; i < num_addresses; i++)
69 {
70 // We could demangle, but this may not be portable on GNU Linux versus Mac OSX
71 // char* ret = abi::__cxa_demangle(begin_name, funcname, &funcnamesize, &status);
72 TRACE("Level " << i << ": " << symbol_list[i]);
73 }
74 free(symbol_list);
75}
76
77void MarkMemory()
78{
79 TRACE("PRINT_MEMORY will now be relative to here");
80
81 struct rusage rusage;
82 getrusage( RUSAGE_SELF, &rusage );
83
84 // Store current memory footprint in Mb to allow calculations relative to this point
85 eMemoryAtMarker = double(rusage.ru_maxrss)/1024.0;
86}
87
88void UnmarkMemory()
89{
90 TRACE("PRINT_MEMORY will now be absolute footprint");
91
92 eMemoryAtMarker = 0.0;
93}
94
95void PrintMemory()
96{
97 struct rusage rusage;
98 getrusage( RUSAGE_SELF, &rusage );
99
100 // Memory as difference between now and the marker
101 double memory = double(rusage.ru_maxrss)/1024.0 - eMemoryAtMarker;
102
103 if (fabs(eMemoryAtMarker) < 1e-6) // If marker is zero, this is the total memory footprint
104 {
105 std::cout << FormDebugHead() << "Total memory footprint: " << memory << " Mb" << std::endl << std::flush;
106 }
107 else // If marker non-zero, this is a change in memory
108 {
109 std::cout << FormDebugHead() << "Memory change from marker: " << memory << " Mb" << std::endl << std::flush;
110 }
111}
#define TRACE(stuff)
Definition Debug.hpp:96
static bool IsParallel()
static unsigned GetMyRank()