~ubuntu-branches/ubuntu/vivid/deal.ii/vivid

« back to all changes in this revision

Viewing changes to base/source/log.cc

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV, Adam C. Powell, IV, Denis Barbier
  • Date: 2010-07-29 13:47:01 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100729134701-akb8jb3stwge8tcm
Tags: 6.3.1-1
[ Adam C. Powell, IV ]
* Changed to source format 3.0 (quilt).
* Changed maintainer to debian-science with Adam Powell as uploader.
* Added source lintian overrides about Adam Powell's name.
* Added Vcs info on git repository.
* Bumped Standards-Version.
* Changed stamp-patch to patch target and fixed its application criterion.
* Moved make_dependencies and expand_instantiations to a versioned directory
  to avoid shlib package conflicts.

[ Denis Barbier ]
* New upstream release (closes: #562332).
  + Added libtbb support.
  + Forward-ported all patches.
* Updates for new PETSc version, including workaround for different versions
  of petsc and slepc.
* Add debian/watch.
* Update to debhelper 7.
* Added pdebuild patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//---------------------------------------------------------------------------
2
 
//    $Id: log.cc 18724 2009-04-23 23:06:37Z bangerth $
3
 
//    Version: $Name$
4
 
//
5
 
//    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 by the deal.II authors
6
 
//
7
 
//    This file is subject to QPL and may not be  distributed
8
 
//    without copyright and license information. Please refer
9
 
//    to the file deal.II/doc/license.html for the  text  and
10
 
//    further information on this license.
11
 
//
12
 
//---------------------------------------------------------------------------
13
 
 
14
 
 
15
 
#include <base/logstream.h>
16
 
#include <base/job_identifier.h>
17
 
#include <base/memory_consumption.h>
18
 
#include <base/thread_management.h>
19
 
 
20
 
// include sys/resource.h for rusage(). Mac OS X needs sys/time.h then
21
 
// as well (strange), so include that, too.
22
 
#include <sys/time.h>
23
 
#include <sys/resource.h>
24
 
#include <sys/types.h>
25
 
#include <unistd.h>
26
 
#include <iostream>
27
 
#include <iomanip>
28
 
#include <fstream>
29
 
#include <sstream>
30
 
 
31
 
 
32
 
// on SunOS 4.x, getrusage is stated in the man pages and exists, but
33
 
// is not declared in resource.h. declare it ourselves
34
 
#ifdef NO_HAVE_GETRUSAGE
35
 
extern "C" { 
36
 
  int getrusage(int who, struct rusage* ru);
37
 
}
38
 
#endif
39
 
 
40
 
// When modifying the prefix list, we need to lock it just in case
41
 
// another thread tries to do the same.
42
 
DEAL_II_NAMESPACE_OPEN
43
 
 
44
 
namespace 
45
 
{
46
 
  Threads::ThreadMutex log_lock;
47
 
  Threads::ThreadMutex write_lock;
48
 
}
49
 
 
50
 
 
51
 
LogStream deallog;
52
 
 
53
 
 
54
 
LogStream::LogStream()
55
 
                :
56
 
                std_out(&std::cerr), file(0),
57
 
                std_depth(10000), file_depth(10000),
58
 
                print_utime(false), diff_utime(false),
59
 
                last_time (0.), double_threshold(0.), old_cerr(0)
60
 
{
61
 
  prefixes.push("DEAL:");
62
 
  std_out->setf(std::ios::showpoint | std::ios::left);
63
 
}
64
 
 
65
 
 
66
 
LogStream::~LogStream()
67
 
{
68
 
  if (old_cerr)
69
 
    std::cerr.rdbuf(old_cerr);
70
 
 
71
 
                                   // on some systems, destroying the
72
 
                                   // outstreams objects of deallog
73
 
                                   // triggers some sort of memory
74
 
                                   // corruption, in particular when
75
 
                                   // we also link with Trilinos;
76
 
                                   // since this happens at the very
77
 
                                   // end of the program, we take the
78
 
                                   // liberty to simply not do it by
79
 
                                   // putting that object into a
80
 
                                   // deliberate memory leak and
81
 
                                   // instead destroying an empty
82
 
                                   // object
83
 
#ifdef DEAL_II_USE_TRILINOS  
84
 
  if (this == &deallog)
85
 
    (new stream_map_type())->swap (outstreams);
86
 
#endif
87
 
}
88
 
 
89
 
 
90
 
LogStream &
91
 
LogStream::operator<< (std::ostream& (*p) (std::ostream&))
92
 
{
93
 
                                   // do the work that is common to
94
 
                                   // the operator<< functions
95
 
  print (p);
96
 
 
97
 
                                   // next check whether this is the
98
 
                                   // <tt>endl</tt> manipulator, and if so
99
 
                                   // set a flag
100
 
  std::ostream & (* const p_endl) (std::ostream&) = &std::endl;
101
 
  if (p == p_endl)
102
 
    {
103
 
      Threads::ThreadMutex::ScopedLock lock(write_lock);
104
 
      print_line_head();
105
 
      std::ostringstream& stream = get_stream();
106
 
      if (prefixes.size() <= std_depth)
107
 
        *std_out << stream.str();
108
 
      
109
 
      if (file && (prefixes.size() <= file_depth))
110
 
        *file << stream.str() << std::flush;
111
 
      
112
 
                                       // Start a new string
113
 
      stream.str("");
114
 
    }
115
 
  return *this;
116
 
}
117
 
 
118
 
 
119
 
std::ostringstream&
120
 
LogStream::get_stream()
121
 
{
122
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
123
 
  unsigned int id = Threads::this_thread_id();
124
 
 
125
 
  std_cxx1x::shared_ptr<std::ostringstream>& sptr = outstreams[id];
126
 
  if (sptr == 0)
127
 
    {
128
 
      sptr = std_cxx1x::shared_ptr<std::ostringstream> (new std::ostringstream());
129
 
      sptr->setf(std::ios::showpoint | std::ios::left);
130
 
    } 
131
 
  return *sptr;
132
 
}
133
 
 
134
 
 
135
 
 
136
 
void
137
 
LogStream::attach(std::ostream& o)
138
 
{
139
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
140
 
  file = &o;
141
 
  o.setf(std::ios::showpoint | std::ios::left);
142
 
  o << dealjobid();
143
 
}
144
 
 
145
 
 
146
 
void LogStream::detach ()
147
 
{
148
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
149
 
  file = 0;
150
 
}
151
 
 
152
 
 
153
 
void LogStream::log_cerr ()
154
 
{
155
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
156
 
  if (old_cerr == 0)
157
 
    {
158
 
      old_cerr = std::cerr.rdbuf(file->rdbuf());
159
 
    } else {
160
 
      std::cerr.rdbuf(old_cerr);
161
 
      old_cerr = 0;
162
 
    }
163
 
}
164
 
 
165
 
 
166
 
std::ostream&
167
 
LogStream::get_console()
168
 
{
169
 
  return *std_out;
170
 
}
171
 
 
172
 
 
173
 
std::ostream&
174
 
LogStream::get_file_stream()
175
 
{
176
 
  Assert(file, ExcNoFileStreamGiven());
177
 
  return *file;
178
 
}
179
 
 
180
 
 
181
 
bool
182
 
LogStream::has_file() const
183
 
{
184
 
  return (file != 0);
185
 
}
186
 
 
187
 
 
188
 
const std::string&
189
 
LogStream::get_prefix() const
190
 
{
191
 
  return prefixes.top();
192
 
}
193
 
 
194
 
 
195
 
void
196
 
LogStream::push (const std::string& text)
197
 
{
198
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
199
 
  std::string pre=prefixes.top();
200
 
  pre += text;
201
 
  pre += std::string(":");
202
 
  prefixes.push(pre);
203
 
}
204
 
 
205
 
 
206
 
void LogStream::pop ()
207
 
{
208
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
209
 
  if (prefixes.size() > 1)
210
 
    prefixes.pop();
211
 
}
212
 
 
213
 
 
214
 
unsigned int
215
 
LogStream::depth_console (const unsigned n)
216
 
{
217
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
218
 
  const unsigned int h = std_depth;
219
 
  std_depth = n;
220
 
  return h;
221
 
}
222
 
 
223
 
 
224
 
unsigned int
225
 
LogStream::depth_file (const unsigned n)
226
 
{
227
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
228
 
  const unsigned int h = file_depth;
229
 
  file_depth = n;
230
 
  return h;
231
 
}
232
 
 
233
 
 
234
 
void
235
 
LogStream::threshold_double (const double t)
236
 
{
237
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
238
 
  double_threshold = t;
239
 
}
240
 
 
241
 
 
242
 
bool
243
 
LogStream::log_execution_time (const bool flag)
244
 
{
245
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
246
 
  const bool h = print_utime;
247
 
  print_utime = flag;
248
 
  return h;
249
 
}
250
 
 
251
 
 
252
 
bool
253
 
LogStream::log_time_differences (const bool flag)
254
 
{
255
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
256
 
  const bool h = diff_utime;
257
 
  diff_utime = flag;
258
 
  return h;
259
 
}
260
 
 
261
 
 
262
 
bool
263
 
LogStream::log_thread_id (const bool flag)
264
 
{
265
 
  Threads::ThreadMutex::ScopedLock lock(log_lock);
266
 
  const bool h = print_thread_id;
267
 
  print_thread_id = flag;
268
 
  return h;
269
 
}
270
 
 
271
 
 
272
 
void
273
 
LogStream::print_line_head()
274
 
{
275
 
  rusage usage;
276
 
  double utime = 0.;
277
 
  if (print_utime)
278
 
    {
279
 
      getrusage(RUSAGE_SELF, &usage);
280
 
      utime = usage.ru_utime.tv_sec + 1.e-6 * usage.ru_utime.tv_usec;
281
 
      if (diff_utime)
282
 
        {
283
 
          double diff = utime - last_time;
284
 
          last_time = utime;
285
 
          utime = diff;
286
 
        }
287
 
    }
288
 
 
289
 
/*
290
 
 * The following lines were used for debugging a memory leak.
291
 
 * They work on Linux, not on Solaris, since the /proc filesystem
292
 
 * on Solaris is quite cryptic. For other systems, we don't know.
293
 
 *
294
 
 * Unfortunately, the information in /proc/pid/stat is updated slowly,
295
 
 * therefore, the information is quite unreliable.
296
 
 *
297
 
 * Furthermore, the constructor of ifstream caused another memory leak.
298
 
 *
299
 
 * Still, this code might be useful sometimes, so I kept it here.
300
 
 * When we have more information about the kernel, this should be
301
 
 * incorporated properly. Suggestions are welcome!
302
 
 */
303
 
  
304
 
#ifdef DEALII_MEMORY_DEBUG
305
 
  static const pid_t id = getpid();
306
 
  
307
 
  std::ostringstream statname;
308
 
  statname << "/proc/" << id << "/stat";
309
 
 
310
 
  static long size;
311
 
  static string dummy;
312
 
  ifstream stat(statname.str());
313
 
                                   // ignore 22 values
314
 
  stat >> dummy >> dummy >> dummy >> dummy >> dummy >> dummy >>
315
 
    dummy >> dummy >> dummy >> dummy >> dummy >>
316
 
    dummy >> dummy >> dummy >> dummy >> dummy >> dummy >>
317
 
    dummy >> dummy >> dummy >> dummy >> dummy >> size;
318
 
#endif
319
 
  
320
 
  const std::string& head = get_prefix();
321
 
  const unsigned int thread = Threads::this_thread_id();
322
 
  
323
 
  if (prefixes.size() <= std_depth)
324
 
    {
325
 
      if (print_utime)
326
 
        {
327
 
          int p = std_out->width(5);
328
 
          *std_out << utime << ':';
329
 
#ifdef DEALII_MEMORY_DEBUG
330
 
          *std_out << size << ':';
331
 
#endif
332
 
          std_out->width(p);
333
 
        }
334
 
      if (print_thread_id)
335
 
        *std_out << '[' << thread << ']';
336
 
      
337
 
      *std_out <<  head << ':';
338
 
    }
339
 
  
340
 
  if (file && (prefixes.size() <= file_depth))
341
 
    {
342
 
      if (print_utime)
343
 
        {
344
 
          int p = file->width(6);
345
 
          *file << utime << ':';
346
 
#ifdef DEALII_MEMORY_DEBUG
347
 
          *file << size << ':';
348
 
#endif
349
 
          file->width(p);
350
 
        }  
351
 
      if (print_thread_id)
352
 
        *file << '[' << thread << ']';
353
 
      
354
 
      *file << head << ':';
355
 
    }
356
 
}
357
 
 
358
 
 
359
 
unsigned int
360
 
LogStream::memory_consumption () const
361
 
{
362
 
  unsigned int mem = sizeof(*this);
363
 
                                   // to determine size of stack
364
 
                                   // elements, we have to copy the
365
 
                                   // stack since we can't access
366
 
                                   // elements from further below
367
 
  std::stack<std::string> tmp;
368
 
  while (tmp.size() > 0)
369
 
    {
370
 
      mem += MemoryConsumption::memory_consumption (tmp.top());
371
 
      tmp.pop ();
372
 
    };
373
 
  
374
 
  return mem;
375
 
}
376
 
 
377
 
DEAL_II_NAMESPACE_CLOSE