aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/memstat.cc
blob: 71427889073d3c33926b5bca2be2691ee1571635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cctk.h>
#include <cctk_Arguments.h>
#include <cctk_Parameters.h>

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

#ifdef HAVE_MALLOC_H
#  include <malloc.h>
#endif

#include <sys/resource.h>
#include <sys/time.h>

#include "mem.hh"

#include "dh.hh"
#include "gdata.hh"
#include "ggf.hh"
#include "gh.hh"
#include "th.hh"



struct mstat {
  // Carpet object statistics
  double total_bytes;
  double total_objects;
  double max_bytes;
  double max_objects;
  // Carpet administrative data structure statistics
  double total_admin_bytes;
  // malloc statistics
  double malloc_used_bytes;
  double malloc_free_bytes;
};
int const mstat_entries = sizeof(mstat) / sizeof(double);



extern "C" void CarpetLib_printmemstats (CCTK_ARGUMENTS);

void CarpetLib_printmemstats (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  DECLARE_CCTK_PARAMETERS;
  
  int const ioproc = 0;
  
  static int next_output = 0;
  if ((print_memstats_every == 0 and cctk_iteration == 0) or
      (print_memstats_every > 0 and cctk_iteration >= next_output))
  {
    next_output = cctk_iteration + print_memstats_every;
    
    mstat mybuf;
    mybuf.total_bytes   = gmem::total_allocated_bytes;
    mybuf.total_objects = gmem::total_allocated_objects;
    mybuf.max_bytes     = gmem::max_allocated_bytes;
    mybuf.max_objects   = gmem::max_allocated_objects;
    mybuf.total_admin_bytes =
      gh::allmemory() + dh::allmemory() + th::allmemory() +
      ggf::allmemory() + gdata::allmemory();
#ifdef HAVE_MALLINFO
    // NOTE: struct mallinfo returns byte-counts as int, which can
    // overflow.  In this case, the information is incorrect.
    struct mallinfo const minfo = mallinfo ();
    mybuf.malloc_used_bytes = minfo.uordblks;
    mybuf.malloc_free_bytes = minfo.fordblks;
#else
    mybuf.malloc_used_bytes = 0;
    mybuf.malloc_free_bytes = 0;
#endif
    
    cout << "Memory statistics from CarpetLib:" << eol
         << "   Current number of objects: " << size_t(gmem::total_allocated_objects) << eol
         << "   Current allocated memory:  "
         << setprecision(3) << gmem::total_allocated_bytes / gmem::MEGA << " MB" << eol
         << "   Maximum number of objects: " << size_t(gmem::max_allocated_objects) << eol
         << "   Maximum allocated memory:  "
         << setprecision(3) << gmem::max_allocated_bytes / gmem::MEGA << " MB" << eol
         << "   Current administrative memory: "
         << setprecision(3) << mybuf.total_admin_bytes / gmem::MEGA << " MB" << eol
         << "   Total allocated used system memory: "
         << setprecision(3) << mybuf.malloc_used_bytes / gmem::MEGA << " MB" << eol
         << "   Total allocated free system memory: "
         << setprecision(3) << mybuf.malloc_free_bytes / gmem::MEGA << " MB" << endl;
    
    // TODO: improve this message
    cout << "   gh::allmemory:    " << gh   ::allmemory() << eol
         << "   dh::allmemory:    " << dh   ::allmemory() << eol
         << "   th::allmemory:    " << th   ::allmemory() << eol
         << "   ggf::allmemory:   " << ggf  ::allmemory() << eol
         << "   gdata::allmemory: " << gdata::allmemory() << endl;
    
    if (strcmp (memstat_file, "") != 0) {
      vector<mstat> allbuf (dist::size());
      MPI_Gather (& mybuf, mstat_entries, MPI_DOUBLE,
                  & allbuf.front(), mstat_entries, MPI_DOUBLE,
                  ioproc, dist::comm());
      
      if (dist::rank() == ioproc) {
        
        double max_total_bytes = 0;
        double avg_total_bytes = 0;
        double cnt_total_bytes = 0;
        double max_max_bytes = 0;
        double avg_max_bytes = 0;
        double cnt_max_bytes = 0;
        double max_admin_bytes = 0;
        double avg_admin_bytes = 0;
        double cnt_admin_bytes = 0;
        double max_used_bytes = 0;
        double avg_used_bytes = 0;
        double cnt_used_bytes = 0;
        double max_free_bytes = 0;
        double avg_free_bytes = 0;
        double cnt_free_bytes = 0;
        for (size_t n=0; n<allbuf.size(); ++n) {
          max_total_bytes = max (max_total_bytes, allbuf[n].total_bytes);
          avg_total_bytes += allbuf[n].total_bytes;
          ++ cnt_total_bytes;
          max_max_bytes = max (max_max_bytes, allbuf[n].max_bytes);
          avg_max_bytes += allbuf[n].max_bytes;
          ++ cnt_max_bytes;
          max_admin_bytes = max (max_admin_bytes, allbuf[n].total_admin_bytes);
          avg_admin_bytes += allbuf[n].total_admin_bytes;
          ++ cnt_admin_bytes;
          max_used_bytes = max (max_used_bytes, allbuf[n].malloc_used_bytes);
          avg_used_bytes += allbuf[n].malloc_used_bytes;
          ++ cnt_used_bytes;
          max_free_bytes = max (max_free_bytes, allbuf[n].malloc_free_bytes);
          avg_free_bytes += allbuf[n].malloc_free_bytes;
          ++ cnt_free_bytes;
        }
        avg_total_bytes /= cnt_total_bytes;
        avg_max_bytes /= cnt_max_bytes;
        avg_admin_bytes /= cnt_admin_bytes;
        avg_used_bytes /= cnt_used_bytes;
        avg_free_bytes /= cnt_free_bytes;
        
        ostringstream filenamebuf;
        filenamebuf << out_dir << "/" << memstat_file;
        string const filename = filenamebuf.str();
        ofstream file;
        static bool did_truncate = false;
        if (not did_truncate) {
          did_truncate = true;
          file.open (filename.c_str(), ios::out | ios::trunc);
          if (CCTK_IsFunctionAliased ("UniqueBuildID")) {
            char const * const build_id
              = static_cast<char const *> (UniqueBuildID (cctkGH));
            file << "# Build ID: " << build_id << eol;
          }
          if (CCTK_IsFunctionAliased ("UniqueSimulationID")) {
            char const * const job_id
              = static_cast<char const *> (UniqueSimulationID (cctkGH));
            file << "# Simulation ID: " << job_id << eol;
          }
          file << "# Running on " << dist::size() << " processes" << eol;
          file << "#" << eol;
          file << "# iteration   maxtotalbytes avgtotalbytes   maxmaxbytes avgm avgfreebytes" << eol;
        } else {
          file.open (filename.c_str(), ios::out | ios::app);
        }
        
        file << cctk_iteration
              << "\t "<< max_total_bytes << " " << avg_total_bytes
              << "\t "<< max_max_bytes << " " << avg_max_bytes
              << "\t "<< max_admin_bytes << " " << avg_admin_bytes
              << "\t "<< max_used_bytes << " " << avg_used_bytes
              << "\t "<< max_free_bytes << " " << avg_free_bytes
             << eol;
        
        file.close ();
        
      } // if on root process
    } // if output to file
    
  }
}