STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
iostats.h
Go to the documentation of this file.
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 <[email protected]>
7  * Copyright (C) 2008-2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009, 2010 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_IO_IOSTATS_HEADER
16 #define STXXL_IO_IOSTATS_HEADER
17 
18 #ifndef STXXL_IO_STATS
19  #define STXXL_IO_STATS 1
20 #endif
21 
22 #include <stxxl/bits/namespace.h>
23 #include <stxxl/bits/deprecated.h>
28 #include <stxxl/bits/unused.h>
29 #include <stxxl/bits/singleton.h>
30 
31 #include <iostream>
32 #include <string>
33 
34 
36 
37 //! \addtogroup iolayer
38 //!
39 //! \{
40 
41 //! Collects various I/O statistics.
42 //! \remarks is a singleton
43 class stats : public singleton<stats>
44 {
45  friend class singleton<stats>;
46 
47  unsigned reads, writes; // number of operations
48  int64 volume_read, volume_written; // number of bytes read/written
49  unsigned c_reads, c_writes; // number of cached operations
50  int64 c_volume_read, c_volume_written; // number of bytes read/written from/to cache
51  double t_reads, t_writes; // seconds spent in operations
52  double p_reads, p_writes; // seconds spent in parallel operations
53  double p_begin_read, p_begin_write; // start time of parallel operation
54  double p_ios; // seconds spent in all parallel I/O operations (read and write)
55  double p_begin_io;
56  double t_waits, p_waits; // seconds spent waiting for completion of I/O operations
57  double p_begin_wait;
58  double t_wait_read, p_wait_read;
60  double t_wait_write, p_wait_write;
62  int acc_reads, acc_writes; // number of requests, participating in parallel operation
63  int acc_ios;
64  int acc_waits;
65  int acc_wait_read, acc_wait_write;
66  double last_reset;
67  mutex read_mutex, write_mutex, io_mutex, wait_mutex;
68 
69  stats();
70 
71 public:
72  enum wait_op_type {
75  WAIT_OP_WRITE
76  };
77 
79  {
81 
82  bool is_write;
83 #if STXXL_IO_STATS
84  bool running;
85 #endif
86 
87  public:
88  scoped_read_write_timer(size_type size, bool is_write = false)
89  : is_write(is_write)
91  , running(false)
92 #endif
93  {
94  start(size);
95  }
96 
98  {
99  stop();
100  }
101 
102  void start(size_type size)
103  {
104 #if STXXL_IO_STATS
105  if (!running) {
106  running = true;
107  if (is_write)
108  stats::get_instance()->write_started(size);
109  else
110  stats::get_instance()->read_started(size);
111  }
112 #else
113  STXXL_UNUSED(size);
114 #endif
115  }
116 
117  void stop()
118  {
119 #if STXXL_IO_STATS
120  if (running) {
121  if (is_write)
122  stats::get_instance()->write_finished();
123  else
124  stats::get_instance()->read_finished();
125  running = false;
126  }
127 #endif
128  }
129  };
130 
132  {
134 
135 #if STXXL_IO_STATS
136  bool running;
137 #endif
138 
139  public:
141 #if STXXL_IO_STATS
142  : running(false)
143 #endif
144  {
145  start(size);
146  }
147 
149  {
150  stop();
151  }
152 
153  void start(size_type size)
154  {
155 #if STXXL_IO_STATS
156  if (!running) {
157  running = true;
158  stats::get_instance()->write_started(size);
159  }
160 #else
161  STXXL_UNUSED(size);
162 #endif
163  }
164 
165  void stop()
166  {
167 #if STXXL_IO_STATS
168  if (running) {
169  stats::get_instance()->write_finished();
170  running = false;
171  }
172 #endif
173  }
174  };
175 
177  {
179 
180 #if STXXL_IO_STATS
181  bool running;
182 #endif
183 
184  public:
186 #if STXXL_IO_STATS
187  : running(false)
188 #endif
189  {
190  start(size);
191  }
192 
194  {
195  stop();
196  }
197 
198  void start(size_type size)
199  {
200 #if STXXL_IO_STATS
201  if (!running) {
202  running = true;
203  stats::get_instance()->read_started(size);
204  }
205 #else
206  STXXL_UNUSED(size);
207 #endif
208  }
209 
210  void stop()
211  {
212 #if STXXL_IO_STATS
213  if (running) {
214  stats::get_instance()->read_finished();
215  running = false;
216  }
217 #endif
218  }
219  };
220 
222  {
223 #ifndef STXXL_DO_NOT_COUNT_WAIT_TIME
224  bool running;
226 #endif
227 
228  public:
229  scoped_wait_timer(wait_op_type wait_op, bool measure_time = true)
230 #ifndef STXXL_DO_NOT_COUNT_WAIT_TIME
231  : running(false), wait_op(wait_op)
232 #endif
233  {
234  if (measure_time)
235  start();
236  }
237 
239  {
240  stop();
241  }
242 
243  void start()
244  {
245 #ifndef STXXL_DO_NOT_COUNT_WAIT_TIME
246  if (!running) {
247  running = true;
248  stats::get_instance()->wait_started(wait_op);
249  }
250 #endif
251  }
252 
253  void stop()
254  {
255 #ifndef STXXL_DO_NOT_COUNT_WAIT_TIME
256  if (running) {
257  stats::get_instance()->wait_finished(wait_op);
258  running = false;
259  }
260 #endif
261  }
262  };
263 
264 public:
265  //! Returns total number of reads.
266  //! \return total number of reads
267  unsigned get_reads() const
268  {
269  return reads;
270  }
271 
272  //! Returns total number of writes.
273  //! \return total number of writes
274  unsigned get_writes() const
275  {
276  return writes;
277  }
278 
279  //! Returns number of bytes read from disks.
280  //! \return number of bytes read
282  {
283  return volume_read;
284  }
285 
286  //! Returns number of bytes written to the disks.
287  //! \return number of bytes written
289  {
290  return volume_written;
291  }
292 
293  //! Returns total number of reads served from cache.
294  //! \return total number of cached reads
295  unsigned get_cached_reads() const
296  {
297  return c_reads;
298  }
299 
300  //! Returns total number of cached writes.
301  //! \return total number of cached writes
302  unsigned get_cached_writes() const
303  {
304  return c_writes;
305  }
306 
307  //! Returns number of bytes read from cache.
308  //! \return number of bytes read from cache
310  {
311  return c_volume_read;
312  }
313 
314  //! Returns number of bytes written to the cache.
315  //! \return number of bytes written to cache
317  {
318  return c_volume_written;
319  }
320 
321  //! Time that would be spent in read syscalls if all parallel reads were serialized.
322  //! \return seconds spent in reading
323  double get_read_time() const
324  {
325  return t_reads;
326  }
327 
328  //! Time that would be spent in write syscalls if all parallel writes were serialized.
329  //! \return seconds spent in writing
330  double get_write_time() const
331  {
332  return t_writes;
333  }
334 
335  //! Period of time when at least one I/O thread was executing a read.
336  //! \return seconds spent in reading
337  double get_pread_time() const
338  {
339  return p_reads;
340  }
341 
342  //! Period of time when at least one I/O thread was executing a write.
343  //! \return seconds spent in writing
344  double get_pwrite_time() const
345  {
346  return p_writes;
347  }
348 
349  //! Period of time when at least one I/O thread was executing a read or a write.
350  //! \return seconds spent in I/O
351  double get_pio_time() const
352  {
353  return p_ios;
354  }
355 
356  //! I/O wait time counter.
357  //! \return number of seconds spent in I/O waiting functions \link
358  //! request::wait request::wait \endlink, \c wait_any and \c wait_all
359  double get_io_wait_time() const
360  {
361  return t_waits;
362  }
363 
364  double get_wait_read_time() const
365  {
366  return t_wait_read;
367  }
368 
369  double get_wait_write_time() const
370  {
371  return t_wait_write;
372  }
373 
374  //! Return time of the last reset.
375  //! \return seconds passed from the last reset()
376  double get_last_reset_time() const
377  {
378  return last_reset;
379  }
380 
381 #ifndef STXXL_IO_STATS_RESET_FORBIDDEN
382  //! Resets I/O time counters (including I/O wait counter).
383  STXXL_DEPRECATED(void reset());
384 #endif
385 
386  //! Resets I/O wait time counter.
387  STXXL_DEPRECATED(void _reset_io_wait_time());
388 
389  // for library use
390  void write_started(unsigned_type size_, double now = 0.0);
391  void write_canceled(unsigned_type size_);
392  void write_finished();
393  void write_cached(unsigned_type size_);
394  void read_started(unsigned_type size_, double now = 0.0);
395  void read_canceled(unsigned_type size_);
396  void read_finished();
397  void read_cached(unsigned_type size_);
398  void wait_started(wait_op_type wait_op);
399  void wait_finished(wait_op_type wait_op);
400 };
401 
402 #if !STXXL_IO_STATS
403 inline void stats::write_started(unsigned_type size_, double now)
404 {
405  STXXL_UNUSED(size_);
406  STXXL_UNUSED(now);
407 }
408 inline void stats::write_cached(unsigned_type size_)
409 {
410  STXXL_UNUSED(size_);
411 }
412 inline void stats::write_finished() { }
413 inline void stats::read_started(unsigned_type size_, double now)
414 {
415  STXXL_UNUSED(size_);
416  STXXL_UNUSED(now);
417 }
418 inline void stats::read_cached(unsigned_type size_)
419 {
420  STXXL_UNUSED(size_);
421 }
422 inline void stats::read_finished() { }
423 #endif
424 #ifdef STXXL_DO_NOT_COUNT_WAIT_TIME
425 inline void stats::wait_started(wait_op_type) { }
426 inline void stats::wait_finished(wait_op_type) { }
427 #endif
428 
429 
431 {
432  unsigned reads, writes; // number of operations
433  int64 volume_read, volume_written; // number of bytes read/written
434  unsigned c_reads, c_writes; // number of cached operations
435  int64 c_volume_read, c_volume_written; // number of bytes read/written from/to cache
436  double t_reads, t_writes; // seconds spent in operations
437  double p_reads, p_writes; // seconds spent in parallel operations
438  double p_ios; // seconds spent in all parallel I/O operations (read and write)
439  double t_wait; // seconds spent waiting for completion of I/O operations
440  double t_wait_read, t_wait_write; //
441  double elapsed;
442 
443 public:
445  reads(0),
446  writes(0),
447  volume_read(0),
448  volume_written(0),
449  c_reads(0),
450  c_writes(0),
451  c_volume_read(0),
452  c_volume_written(0),
453  t_reads(0.0),
454  t_writes(0.0),
455  p_reads(0.0),
456  p_writes(0.0),
457  p_ios(0.0),
458  t_wait(0.0),
459  t_wait_read(0.0),
460  t_wait_write(0.0),
461  elapsed(0.0)
462  { }
463 
464  stats_data(const stats& s) :
465  reads(s.get_reads()),
466  writes(s.get_writes()),
467  volume_read(s.get_read_volume()),
468  volume_written(s.get_written_volume()),
469  c_reads(s.get_cached_reads()),
470  c_writes(s.get_cached_writes()),
471  c_volume_read(s.get_cached_read_volume()),
472  c_volume_written(s.get_cached_written_volume()),
473  t_reads(s.get_read_time()),
474  t_writes(s.get_write_time()),
475  p_reads(s.get_pread_time()),
476  p_writes(s.get_pwrite_time()),
477  p_ios(s.get_pio_time()),
478  t_wait(s.get_io_wait_time()),
479  t_wait_read(s.get_wait_read_time()),
480  t_wait_write(s.get_wait_write_time()),
481  elapsed(timestamp() - s.get_last_reset_time())
482  { }
483 
484  stats_data operator + (const stats_data& a) const
485  {
486  stats_data s;
487  s.reads = reads + a.reads;
488  s.writes = writes + a.writes;
489  s.volume_read = volume_read + a.volume_read;
490  s.volume_written = volume_written + a.volume_written;
491  s.c_reads = c_reads + a.c_reads;
492  s.c_writes = c_writes + a.c_writes;
493  s.c_volume_read = c_volume_read + a.c_volume_read;
494  s.c_volume_written = c_volume_written + a.c_volume_written;
495  s.t_reads = t_reads + a.t_reads;
496  s.t_writes = t_writes + a.t_writes;
497  s.p_reads = p_reads + a.p_reads;
498  s.p_writes = p_writes + a.p_writes;
499  s.p_ios = p_ios + a.p_ios;
500  s.t_wait = t_wait + a.t_wait;
501  s.t_wait_read = t_wait_read + a.t_wait_read;
502  s.t_wait_write = t_wait_write + a.t_wait_write;
503  s.elapsed = elapsed + a.elapsed;
504  return s;
505  }
506 
507  stats_data operator - (const stats_data& a) const
508  {
509  stats_data s;
510  s.reads = reads - a.reads;
511  s.writes = writes - a.writes;
512  s.volume_read = volume_read - a.volume_read;
513  s.volume_written = volume_written - a.volume_written;
514  s.c_reads = c_reads - a.c_reads;
515  s.c_writes = c_writes - a.c_writes;
516  s.c_volume_read = c_volume_read - a.c_volume_read;
517  s.c_volume_written = c_volume_written - a.c_volume_written;
518  s.t_reads = t_reads - a.t_reads;
519  s.t_writes = t_writes - a.t_writes;
520  s.p_reads = p_reads - a.p_reads;
521  s.p_writes = p_writes - a.p_writes;
522  s.p_ios = p_ios - a.p_ios;
523  s.t_wait = t_wait - a.t_wait;
524  s.t_wait_read = t_wait_read - a.t_wait_read;
525  s.t_wait_write = t_wait_write - a.t_wait_write;
526  s.elapsed = elapsed - a.elapsed;
527  return s;
528  }
529 
530  unsigned get_reads() const
531  {
532  return reads;
533  }
534 
535  unsigned get_writes() const
536  {
537  return writes;
538  }
539 
541  {
542  return volume_read;
543  }
544 
546  {
547  return volume_written;
548  }
549 
550  unsigned get_cached_reads() const
551  {
552  return c_reads;
553  }
554 
555  unsigned get_cached_writes() const
556  {
557  return c_writes;
558  }
559 
561  {
562  return c_volume_read;
563  }
564 
566  {
567  return c_volume_written;
568  }
569 
570  double get_read_time() const
571  {
572  return t_reads;
573  }
574 
575  double get_write_time() const
576  {
577  return t_writes;
578  }
579 
580  double get_pread_time() const
581  {
582  return p_reads;
583  }
584 
585  double get_pwrite_time() const
586  {
587  return p_writes;
588  }
589 
590  double get_pio_time() const
591  {
592  return p_ios;
593  }
594 
595  double get_elapsed_time() const
596  {
597  return elapsed;
598  }
599 
600  double get_io_wait_time() const
601  {
602  return t_wait;
603  }
604 
605  double get_wait_read_time() const
606  {
607  return t_wait_read;
608  }
609 
610  double get_wait_write_time() const
611  {
612  return t_wait_write;
613  }
614 };
615 
616 std::ostream& operator << (std::ostream& o, const stats_data& s);
617 
618 inline std::ostream& operator << (std::ostream& o, const stats& s)
619 {
620  o << stxxl::stats_data(s);
621  return o;
622 }
623 
624 std::string format_with_SI_IEC_unit_multiplier(uint64 number, const char* unit = "", int multiplier = 1000);
625 
626 inline std::string add_IEC_binary_multiplier(uint64 number, const char* unit = "")
627 {
628  return format_with_SI_IEC_unit_multiplier(number, unit, 1024);
629 }
630 
631 inline std::string add_SI_multiplier(uint64 number, const char* unit = "")
632 {
633  return format_with_SI_IEC_unit_multiplier(number, unit, 1000);
634 }
635 
636 //! \}
637 
639 
640 #endif // !STXXL_IO_IOSTATS_HEADER
641 // vim: et:ts=4:sw=4
int64 volume_written
Definition: iostats.h:48
int64 get_written_volume() const
Definition: iostats.h:545
double get_wait_write_time() const
Definition: iostats.h:369
double get_pio_time() const
Definition: iostats.h:590
double p_begin_wait
Definition: iostats.h:57
int64 get_cached_read_volume() const
Definition: iostats.h:560
unsigned get_cached_writes() const
Returns total number of cached writes.
Definition: iostats.h:302
double get_last_reset_time() const
Return time of the last reset.
Definition: iostats.h:376
int64 c_volume_read
Definition: iostats.h:435
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:231
unsigned get_reads() const
Definition: iostats.h:530
double t_wait_read
Definition: iostats.h:440
unsigned c_writes
Definition: iostats.h:434
double t_wait_read
Definition: iostats.h:58
long long int int64
Definition: types.h:40
double last_reset
Definition: iostats.h:66
double p_begin_wait_read
Definition: iostats.h:59
#define STXXL_IO_STATS
Definition: iostats.h:19
double get_read_time() const
Time that would be spent in read syscalls if all parallel reads were serialized.
Definition: iostats.h:323
int64 get_read_volume() const
Returns number of bytes read from disks.
Definition: iostats.h:281
unsigned long long int uint64
Definition: types.h:41
unsigned get_reads() const
Returns total number of reads.
Definition: iostats.h:267
int acc_writes
Definition: iostats.h:62
std::string format_with_SI_IEC_unit_multiplier(uint64 number, const char *unit="", int multiplier=1000)
Definition: iostats.cpp:329
double get_wait_read_time() const
Definition: iostats.h:364
double get_wait_read_time() const
Definition: iostats.h:605
double t_reads
Definition: iostats.h:436
unsigned get_writes() const
Returns total number of writes.
Definition: iostats.h:274
mutex write_mutex
Definition: iostats.h:67
int64 get_cached_written_volume() const
Returns number of bytes written to the cache.
Definition: iostats.h:316
int acc_waits
Definition: iostats.h:64
double t_waits
Definition: iostats.h:56
#define STXXL_DEPRECATED(x)
Definition: deprecated.h:30
stats_data(const stats &s)
Definition: iostats.h:464
double p_writes
Definition: iostats.h:437
unsigned get_cached_writes() const
Definition: iostats.h:555
double p_writes
Definition: iostats.h:52
void start(size_type size)
Definition: iostats.h:198
double get_wait_write_time() const
Definition: iostats.h:610
int64 volume_written
Definition: iostats.h:433
double get_io_wait_time() const
Definition: iostats.h:600
unsigned reads
Definition: iostats.h:432
double t_writes
Definition: iostats.h:436
double get_write_time() const
Time that would be spent in write syscalls if all parallel writes were serialized.
Definition: iostats.h:330
unsigned c_reads
Definition: iostats.h:434
double timestamp()
Returns number of seconds since the epoch, high resolution.
Definition: timer.h:42
double p_begin_write
Definition: iostats.h:53
std::string add_IEC_binary_multiplier(uint64 number, const char *unit="")
Definition: iostats.h:626
int acc_ios
Definition: iostats.h:63
double get_write_time() const
Definition: iostats.h:575
double get_pio_time() const
Period of time when at least one I/O thread was executing a read or a write.
Definition: iostats.h:351
double t_wait_write
Definition: iostats.h:60
Collects various I/O statistics.
Definition: iostats.h:43
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
unsigned c_writes
Definition: iostats.h:49
scoped_read_write_timer(size_type size, bool is_write=false)
Definition: iostats.h:88
double p_begin_wait_write
Definition: iostats.h:61
unsigned get_writes() const
Definition: iostats.h:535
int acc_wait_write
Definition: iostats.h:65
int64 c_volume_written
Definition: iostats.h:50
double p_ios
Definition: iostats.h:54
std::string add_SI_multiplier(uint64 number, const char *unit="")
Definition: iostats.h:631
double t_wait_write
Definition: iostats.h:440
unsigned get_cached_reads() const
Definition: iostats.h:550
double get_read_time() const
Definition: iostats.h:570
void start(size_type size)
Definition: iostats.h:102
int64 volume_read
Definition: iostats.h:433
double get_pwrite_time() const
Definition: iostats.h:585
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
double get_pwrite_time() const
Period of time when at least one I/O thread was executing a write.
Definition: iostats.h:344
double elapsed
Definition: iostats.h:441
int64 get_cached_written_volume() const
Definition: iostats.h:565
double p_begin_io
Definition: iostats.h:55
double p_reads
Definition: iostats.h:437
int64 get_read_volume() const
Definition: iostats.h:540
double get_elapsed_time() const
Definition: iostats.h:595
double get_io_wait_time() const
I/O wait time counter.
Definition: iostats.h:359
unsigned writes
Definition: iostats.h:432
unsigned writes
Definition: iostats.h:47
int64 c_volume_written
Definition: iostats.h:435
void start(size_type size)
Definition: iostats.h:153
double get_pread_time() const
Period of time when at least one I/O thread was executing a read.
Definition: iostats.h:337
int64 get_cached_read_volume() const
Returns number of bytes read from cache.
Definition: iostats.h:309
unsigned get_cached_reads() const
Returns total number of reads served from cache.
Definition: iostats.h:295
int64 get_written_volume() const
Returns number of bytes written to the disks.
Definition: iostats.h:288
double get_pread_time() const
Definition: iostats.h:580
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
double t_writes
Definition: iostats.h:51