1 /* Definitions of simple functions for printing timings.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #include "ppl-config.h"
25 #include "timings.hh"
26 #include <cassert>
27 #include <ctime>
28 #include <iostream>
29 #include <iomanip>
30 #include <cstring>
31 #include <cerrno>
32 #include <cstdlib>
33 
34 #ifdef PPL_HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
37 
38 #ifdef PPL_HAVE_SYS_RESOURCE_H
39 // This should be included after <time.h> and <sys/time.h> so as to make
40 // sure we have the definitions for, e.g., `ru_utime'.
41 # include <sys/resource.h>
42 #endif
43 
44 #ifdef PPL_HAVE_TIMEVAL
45 // To save the time when start_clock is called.
46 static struct timeval saved_ru_utime;
47 #endif
48 
49 void
start_clock()50 start_clock() {
51 #if PPL_HAVE_DECL_GETRUSAGE && defined(PPL_HAVE_TIMEVAL)
52   struct rusage usage;
53   if (getrusage(RUSAGE_SELF, &usage) != 0) {
54     std::cerr << "getrusage failed: " << strerror(errno) << std::endl;
55     exit(1);
56   }
57   else {
58     saved_ru_utime = usage.ru_utime;
59   }
60 #endif
61 }
62 
63 void
print_clock(std::ostream & s)64 print_clock(std::ostream& s) {
65 #if PPL_HAVE_DECL_GETRUSAGE && defined(PPL_HAVE_TIMEVAL)
66   struct rusage usage;
67   if (getrusage(RUSAGE_SELF, &usage) != 0) {
68     std::cerr << "getrusage failed: " << strerror(errno) << std::endl;
69     exit(1);
70   }
71   else {
72     const time_t current_secs = usage.ru_utime.tv_sec;
73     const time_t current_usecs = usage.ru_utime.tv_usec;
74     const time_t saved_secs = saved_ru_utime.tv_sec;
75     const time_t saved_usecs = saved_ru_utime.tv_usec;
76     time_t secs;
77     time_t csecs;
78     secs = current_secs - saved_secs;
79     if (current_usecs < saved_usecs) {
80       csecs = (((1000000 + current_usecs) - saved_usecs) + 5000) / 10000;
81       if (csecs < 100) {
82         --secs;
83       }
84       else {
85         csecs = 0;
86       }
87     }
88     else {
89       csecs = ((current_usecs - saved_usecs) + 5000) / 10000;
90       if (csecs == 100) {
91         ++secs;
92         csecs = 0;
93       }
94     }
95     assert(csecs >= 0 && csecs < 100 && secs >= 0);
96     const char fill_char = s.fill();
97     s << secs << "." << std::setfill('0') << std::setw(2) << csecs;
98     s.fill(fill_char);
99   }
100 #else
101   s << "(no clock available)";
102 #endif
103 }
104