Stxxl  1.2.1
iostats.h
1 /***************************************************************************
2  * include/stxxl/bits/io/iostats.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <dementiev@mpi-sb.mpg.de>
7  * Copyright (C) 2008 Andreas Beckmann <beckmann@mpi-inf.mpg.de>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_IOSTATS_HEADER
15 #define STXXL_IOSTATS_HEADER
16 
17 #ifndef STXXL_IO_STATS
18  #define STXXL_IO_STATS 1
19 #endif
20 
21 
22 #include <iostream>
23 
24 #include <stxxl/bits/namespace.h>
25 #include <stxxl/bits/common/mutex.h>
26 #include <stxxl/bits/common/types.h>
27 #include <stxxl/bits/common/utils.h>
28 #include <stxxl/bits/singleton.h>
29 
30 
31 __STXXL_BEGIN_NAMESPACE
32 
36 
39 class stats : public singleton<stats>
40 {
41  friend class singleton<stats>;
42 
43  unsigned reads, writes; // number of operations
44  int64 volume_read, volume_written; // number of bytes read/written
45  double t_reads, t_writes; // seconds spent in operations
46  double p_reads, p_writes; // seconds spent in parallel operations
47  double p_begin_read, p_begin_write; // start time of parallel operation
48  double p_ios; // seconds spent in all parallel I/O operations (read and write)
49  double p_begin_io;
50  double t_waits, p_waits; // seconds spent waiting for completion of I/O operations
51  double p_begin_wait;
52  int acc_reads, acc_writes; // number of requests, participating in parallel operation
53  int acc_ios;
54  int acc_waits;
55  double last_reset;
56  mutex read_mutex, write_mutex, io_mutex, wait_mutex;
57 
58  stats();
59 
60 public:
61  class scoped_write_timer
62  {
63  typedef unsigned_type size_type;
64 
65 #if STXXL_IO_STATS
66  bool running;
67 #endif
68 
69  public:
70  scoped_write_timer(size_type size)
71 #if STXXL_IO_STATS
72  : running(false)
73 #endif
74  {
75  start(size);
76  }
77 
78  ~scoped_write_timer()
79  {
80  stop();
81  }
82 
83  void start(size_type size)
84  {
85 #if STXXL_IO_STATS
86  if (!running) {
87  running = true;
88  stats::get_instance()->write_started(size);
89  }
90 #else
91  UNUSED(size);
92 #endif
93  }
94 
95  void stop()
96  {
97 #if STXXL_IO_STATS
98  if (running) {
99  stats::get_instance()->write_finished();
100  running = false;
101  }
102 #endif
103  }
104  };
105 
106  class scoped_read_timer
107  {
108  typedef unsigned_type size_type;
109 
110 #if STXXL_IO_STATS
111  bool running;
112 #endif
113 
114  public:
115  scoped_read_timer(size_type size)
116 #if STXXL_IO_STATS
117  : running(false)
118 #endif
119  {
120  start(size);
121  }
122 
123  ~scoped_read_timer()
124  {
125  stop();
126  }
127 
128  void start(size_type size)
129  {
130 #if STXXL_IO_STATS
131  if (!running) {
132  running = true;
133  stats::get_instance()->read_started(size);
134  }
135 #else
136  UNUSED(size);
137 #endif
138  }
139 
140  void stop()
141  {
142 #if STXXL_IO_STATS
143  if (running) {
144  stats::get_instance()->read_finished();
145  running = false;
146  }
147 #endif
148  }
149  };
150 
151  class scoped_wait_timer
152  {
153 #ifdef COUNT_WAIT_TIME
154  bool running;
155 #endif
156 
157  public:
158  scoped_wait_timer()
159 #ifdef COUNT_WAIT_TIME
160  : running(false)
161 #endif
162  {
163  start();
164  }
165 
166  ~scoped_wait_timer()
167  {
168  stop();
169  }
170 
171  void start()
172  {
173 #ifdef COUNT_WAIT_TIME
174  if (!running) {
175  running = true;
176  stats::get_instance()->wait_started();
177  }
178 #endif
179  }
180 
181  void stop()
182  {
183 #ifdef COUNT_WAIT_TIME
184  if (running) {
185  stats::get_instance()->wait_finished();
186  running = false;
187  }
188 #endif
189  }
190  };
191 
192 public:
195  unsigned get_reads() const
196  {
197  return reads;
198  }
199 
202  unsigned get_writes() const
203  {
204  return writes;
205  }
206 
209  int64 get_read_volume() const
210  {
211  return volume_read;
212  }
213 
216  int64 get_written_volume() const
217  {
218  return volume_written;
219  }
220 
223  double get_read_time() const
224  {
225  return t_reads;
226  }
227 
230  double get_write_time() const
231  {
232  return t_writes;
233  }
234 
237  double get_pread_time() const
238  {
239  return p_reads;
240  }
241 
244  double get_pwrite_time() const
245  {
246  return p_writes;
247  }
248 
251  double get_pio_time() const
252  {
253  return p_ios;
254  }
255 
261  double get_io_wait_time() const
262  {
263  return t_waits;
264  }
265 
268  double get_last_reset_time() const
269  {
270  return last_reset;
271  }
272 
274  void reset();
275 
277  __STXXL_DEPRECATED(void _reset_io_wait_time());
278 
279  // for library use
280  void write_started(unsigned size_);
281  void write_finished();
282  void read_started(unsigned size_);
283  void read_finished();
284  void wait_started();
285  void wait_finished();
286 };
287 
288 #if !STXXL_IO_STATS
289 inline void stats::write_started(unsigned size_)
290 {
291  UNUSED(size_);
292 }
293 inline void stats::write_finished() { }
294 inline void stats::read_started(unsigned size_)
295 {
296  UNUSED(size_);
297 }
298 inline void stats::read_finished() { }
299 #endif
300 #ifndef COUNT_WAIT_TIME
301 inline void stats::wait_started() { }
302 inline void stats::wait_finished() { }
303 #endif
304 
305 
306 class stats_data
307 {
308  unsigned reads, writes; // number of operations
309  int64 volume_read, volume_written; // number of bytes read/written
310  double t_reads, t_writes; // seconds spent in operations
311  double p_reads, p_writes; // seconds spent in parallel operations
312  double p_ios; // seconds spent in all parallel I/O operations (read and write)
313  double t_wait; // seconds spent waiting for completion of I/O operations
314  double elapsed;
315 
316 public:
317  stats_data() :
318  reads(0),
319  writes(0),
320  volume_read(0),
321  volume_written(0),
322  t_reads(0.0),
323  t_writes(0.0),
324  p_reads(0.0),
325  p_writes(0.0),
326  p_ios(0.0),
327  t_wait(0.0),
328  elapsed(0.0)
329  { }
330 
331  stats_data(const stats & s) :
332  reads(s.get_reads()),
333  writes(s.get_writes()),
334  volume_read(s.get_read_volume()),
335  volume_written(s.get_written_volume()),
336  t_reads(s.get_read_time()),
337  t_writes(s.get_write_time()),
338  p_reads(s.get_pread_time()),
339  p_writes(s.get_pwrite_time()),
340  p_ios(s.get_pio_time()),
341  t_wait(s.get_io_wait_time()),
342  elapsed(timestamp() - s.get_last_reset_time())
343  { }
344 
345  stats_data operator + (const stats_data & a) const
346  {
347  stats_data s;
348  s.reads = reads + a.reads;
349  s.writes = writes + a.writes;
350  s.volume_read = volume_read + a.volume_read;
351  s.volume_written = volume_written + a.volume_written;
352  s.t_reads = t_reads + a.t_reads;
353  s.t_writes = t_writes + a.t_writes;
354  s.p_reads = p_reads + a.p_reads;
355  s.p_writes = p_writes + a.p_writes;
356  s.p_ios = p_ios + a.p_ios;
357  s.t_wait = t_wait + a.t_wait;
358  s.elapsed = elapsed + a.elapsed;
359  return s;
360  }
361 
362  stats_data operator - (const stats_data & a) const
363  {
364  stats_data s;
365  s.reads = reads - a.reads;
366  s.writes = writes - a.writes;
367  s.volume_read = volume_read - a.volume_read;
368  s.volume_written = volume_written - a.volume_written;
369  s.t_reads = t_reads - a.t_reads;
370  s.t_writes = t_writes - a.t_writes;
371  s.p_reads = p_reads - a.p_reads;
372  s.p_writes = p_writes - a.p_writes;
373  s.p_ios = p_ios - a.p_ios;
374  s.t_wait = t_wait - a.t_wait;
375  s.elapsed = elapsed - a.elapsed;
376  return s;
377  }
378 
379  unsigned get_reads() const
380  {
381  return reads;
382  }
383 
384  unsigned get_writes() const
385  {
386  return writes;
387  }
388 
389  int64 get_read_volume() const
390  {
391  return volume_read;
392  }
393 
394  int64 get_written_volume() const
395  {
396  return volume_written;
397  }
398 
399  double get_read_time() const
400  {
401  return t_reads;
402  }
403 
404  double get_write_time() const
405  {
406  return t_writes;
407  }
408 
409  double get_pread_time() const
410  {
411  return p_reads;
412  }
413 
414  double get_pwrite_time() const
415  {
416  return p_writes;
417  }
418 
419  double get_pio_time() const
420  {
421  return p_ios;
422  }
423 
424  double get_elapsed_time() const
425  {
426  return elapsed;
427  }
428 
429  double get_io_wait_time() const
430  {
431  return t_wait;
432  }
433 };
434 
435 std::ostream & operator << (std::ostream & o, const stats_data & s);
436 
437 inline std::ostream & operator << (std::ostream & o, const stats & s)
438 {
439  o << stxxl::stats_data(s);
440  return o;
441 }
442 
444 
445 __STXXL_END_NAMESPACE
446 
447 #endif // !STXXL_IOSTATS_HEADER
448 // vim: et:ts=4:sw=4