File: journal.cc

package info (click to toggle)
dynare 5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,852 kB
  • sloc: cpp: 94,481; ansic: 28,551; pascal: 14,532; sh: 5,453; objc: 4,671; yacc: 4,442; makefile: 2,923; lex: 1,612; python: 677; ruby: 469; lisp: 156; xml: 22
file content (239 lines) | stat: -rw-r--r-- 6,734 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright © 2004-2011 Ondra Kamenik
 * Copyright © 2019-2020 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "journal.hh"
#include "kord_exception.hh"

#include <iomanip>
#include <cmath>
#include <ctime>
#include <limits>
#include <thread>

#ifndef _WIN32
# include <sys/time.h>     // For getrusage()
# include <sys/resource.h> // For getrusage()
# include <sys/utsname.h>  // For uname()
# include <cstdlib>        // For getloadavg()
# include <unistd.h>       // For sysconf()
# ifdef __APPLE__
#  include <sys/types.h>
#  include <sys/sysctl.h>
# endif
#else
# ifndef NOMINMAX
#  define NOMINMAX         // Do not define "min" and "max" macros
# endif
# include <windows.h>      // For GlobalMemoryStatus()
#endif

const std::chrono::time_point<std::chrono::high_resolution_clock> SystemResources::start = std::chrono::high_resolution_clock::now();

#ifndef _WIN32
long
SystemResources::pageSize()
{
  return sysconf(_SC_PAGESIZE);
}
#endif

long
SystemResources::availableMemory()
{
#if !defined(_WIN32) && !defined(__APPLE__)
  return sysconf(_SC_AVPHYS_PAGES)*pageSize();
#elif defined(__APPLE__)
  unsigned long usermem = 0;
  size_t len = sizeof usermem;
  static int mib[2] = { CTL_HW, HW_USERMEM };
  int retval = sysctl(mib, 2, &usermem, &len, NULL, 0);
  if (retval == 0)
    return static_cast<long>(usermem);
  return 0;
#else // _WIN32
  MEMORYSTATUS memstat;
  GlobalMemoryStatus(&memstat);
  return memstat.dwAvailPhys;
#endif
}

SystemResources::SystemResources()
{
  auto now = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> duration = now - start;
  elapsed = duration.count();

#ifndef _WIN32
  struct rusage rus;
  getrusage(RUSAGE_SELF, &rus);
  utime = rus.ru_utime.tv_sec+rus.ru_utime.tv_usec*1.0e-6;
  stime = rus.ru_stime.tv_sec+rus.ru_stime.tv_usec*1.0e-6;
  idrss = rus.ru_idrss;
  majflt = rus.ru_majflt * pageSize();
#else
  utime = std::numeric_limits<double>::quiet_NaN();
  stime = std::numeric_limits<double>::quiet_NaN();
  idrss = -1;
  majflt = -1;
#endif

#ifndef _WIN32
  getloadavg(&load_avg, 1);
#else
  load_avg = std::numeric_limits<double>::quiet_NaN();
#endif

  mem_avail = availableMemory();
}

void
SystemResources::diff(const SystemResources &pre)
{
  utime -= pre.utime;
  stime -= pre.stime;
  elapsed -= pre.elapsed;
  idrss -= pre.idrss;
  majflt -= pre.majflt;
}

// JournalRecord::operator<<() symmetry code
JournalRecord &
JournalRecord::operator<<(const IntSequence &s)
{
  operator<<('[');
  for (int i = 0; i < s.size(); i++)
    {
      operator<<(s[i]);
      if (i < s.size()-1)
        operator<<(',');
    }
  operator<<(']');
  return *this;
}

void
JournalRecord::writeFloatTabular(std::ostream &s, double d, int width)
{
  // Number of digits of integer part
  int intdigits = std::max(static_cast<int>(std::floor(log10(d))+1), 1);

  int prec = std::max(width - 1 - intdigits, 0);
  s << std::fixed << std::setw(width) << std::setprecision(prec) << d;
}

void
JournalRecord::writePrefix(const SystemResources &f)
{
  constexpr double mb = 1024*1024;
  std::ostringstream s;
  s << std::setfill('0');
  writeFloatTabular(s, f.elapsed, 7);
  s << u8"│" << recChar << std::setw(5) << ord << u8"│";
  writeFloatTabular(s, f.load_avg, 3);
  s << u8"│";
  writeFloatTabular(s, f.mem_avail/mb, 5);
  s << u8"│      │ ";
  for (int i = 0; i < 2*journal.getDepth(); i++)
    s << ' ';
  prefix = s.str();
}

void
JournalRecordPair::writePrefixForEnd(const SystemResources &f)
{
  constexpr double mb = 1024*1024;
  SystemResources difnow;
  difnow.diff(f);
  std::ostringstream s;
  s << std::setfill('0');
  writeFloatTabular(s, f.elapsed+difnow.elapsed, 7);
  s << u8"│E" << std::setw(5) << ord << u8"│";
  writeFloatTabular(s, difnow.load_avg, 3);
  s << u8"│";
  writeFloatTabular(s, difnow.mem_avail/mb, 5);
  s << u8"│";
  writeFloatTabular(s, difnow.majflt/mb, 6);
  s << u8"│ ";
  for (int i = 0; i < 2*journal.getDepth(); i++)
    s << ' ';
  prefix_end = s.str();
}

JournalRecordPair::~JournalRecordPair()
{
  journal.decrementDepth();
  writePrefixForEnd(flash);
  journal << prefix_end;
  journal << mes;
  journal << std::endl;
  journal.flush();
}

JournalRecord &
endrec(JournalRecord &rec)
{
  rec.journal << rec.prefix;
  rec.journal << rec.mes;
  rec.journal << std::endl;
  rec.journal.flush();
  rec.journal.incrementOrd();
  return rec;
}

void
Journal::printHeader()
{
  *this << "Dynare++ v. " << VERSION << '\n'
        << '\n'
        << u8"Copyright © 2004-2011 Ondra Kamenik\n"
        << u8"Copyright © 2019-2020 Dynare Team\n"
        << "Dynare++ comes with ABSOLUTELY NO WARRANTY and is distributed under the GNU GPL,\n"
        << "version 3 or later (see https://www.gnu.org/licenses/gpl.html)\n"
        << "\n\n"
        << "System info: ";
#ifndef _WIN32
  utsname info;
  uname(&info);
  *this << info.sysname << " " << info.release << " " << info.version << " "
        << info.machine;
#else
  *this << "Windows";
#endif
  *this << ", processors online: " << std::thread::hardware_concurrency()
        << "\n\nStart time: ";
  std::time_t t = std::time(nullptr);
  // NB: in the date/time string, we avoid using locale-specific strings (#1751)
  *this << std::put_time(std::localtime(&t),
#ifndef _WIN32
                         "%F %T %z"
#else
                         // Specifiers introduced in C++11 don’t work under Windows…
                         "%Y-%m-%d %H:%M:%S"
#endif
                         )
        << "\n\n"
        << u8"  ┌────╼ elapsed time (seconds)                     \n"
        << u8"  │       ┌────╼ record unique identifier           \n"
        << u8"  │       │     ┌────╼ load average                 \n"
        << u8"  │       │     │    ┌────╼ available memory (MB)   \n"
        << u8"  │       │     │    │     ┌─────╼ major faults (MB)\n"
        << u8"  │       │     │    │     │                        \n"
        << u8"  ╽       ╽     ╽    ╽     ╽                        \n";
}