COPASI API  4.16.103
CopasiTime.cpp
Go to the documentation of this file.
1 /* Begin CVS Header
2  $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/utilities/CopasiTime.cpp,v $
3  $Revision: 1.17 $
4  $Name: $
5  $Author: shoops $
6  $Date: 2012/04/10 12:32:09 $
7  End CVS Header */
8 
9 // Copyright (C) 2012 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc. and EML Research, gGmbH.
16 // All rights reserved.
17 
18 #include <time.h>
19 
20 #ifdef WIN32
21 # include <windows.h>
22 # include <winbase.h>
23 #else
24 # ifdef Darwin
25 # include <mach/mach_init.h>
26 # include <mach/task.h>
27 # endif
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 # ifndef RUSAGE_THREAD
31 # define RUSAGE_THREAD 1 /* only the calling thread */
32 # endif
33 #endif // WIN32
34 
35 #include <sstream>
36 
37 #include "copasi.h"
38 #include "CopasiTime.h"
39 #include "utility.h"
40 
42  mTime(LLONG_CONST(0))
43 {}
44 
46  mTime(src.mTime)
47 {}
48 
50  mTime(value)
51 {}
52 
54 
56 {return mTime + value.mTime;}
57 
59 {return mTime - value.mTime;}
60 
62 {
63  mTime = rhs.mTime;
64  return *this;
65 }
66 
68 {
69  mTime = value;
70  return *this;
71 }
72 
74 {return (mTime < value.mTime);}
75 
76 std::string CCopasiTimeVariable::isoFormat() const
77 {
78  std::stringstream Iso;
79  bool first = true;
80 
81  if (mTime < LLONG_CONST(0))
82  {
84  Iso << "-";
85  Iso << Tmp.isoFormat();
86 
87  return Iso.str();
88  }
89 
90  if (mTime >= LLONG_CONST(86400000000))
91  {
92  Iso << LL2String(getDays()) << ":";
93  first = false;
94  }
95 
96  if (mTime >= LLONG_CONST(3600000000))
97  Iso << LL2String(getHours(true), first ? 0 : 2) << ":";
98 
99  if (mTime >= LLONG_CONST(60000000))
100  Iso << LL2String(getMinutes(true), first ? 0 : 2) << ":";
101 
102  if (mTime >= LLONG_CONST(1000000))
103  Iso << LL2String(getSeconds(true), first ? 0 : 2) << ".";
104  else
105  Iso << "0.";
106 
107  Iso << LL2String(getMilliSeconds(true), 3) << LL2String(getMicroSeconds(true), 3);
108 
109  return Iso.str();
110 }
111 
113 {
114  if (bounded) return mTime % LLONG_CONST(1000);
115  else return mTime;
116 }
117 
119 {
120  C_INT64 MilliSeconds = mTime / LLONG_CONST(1000);
121 
122  if (bounded) return MilliSeconds % LLONG_CONST(1000);
123  else return MilliSeconds;
124 }
125 
126 C_INT64 CCopasiTimeVariable::getSeconds(const bool & bounded) const
127 {
128  C_INT64 Seconds = mTime / LLONG_CONST(1000000);
129 
130  if (bounded) return Seconds % LLONG_CONST(60);
131  else return Seconds;
132 }
133 
134 C_INT64 CCopasiTimeVariable::getMinutes(const bool & bounded) const
135 {
136  C_INT64 Minutes = mTime / LLONG_CONST(60000000);
137 
138  if (bounded) return Minutes % LLONG_CONST(60);
139  else return Minutes;
140 }
141 
142 C_INT64 CCopasiTimeVariable::getHours(const bool & bounded) const
143 {
144  C_INT64 Hours = mTime / LLONG_CONST(3600000000);
145 
146  if (bounded) return Hours % LLONG_CONST(24);
147  else return Hours;
148 }
149 
151 {
152  C_INT64 Days = mTime / LLONG_CONST(86400000000);
153 
154  return Days;
155 }
156 
157 #ifndef WIN32
158 
159 //static
161 {
162  timeval ttt;
163  gettimeofday(&ttt, 0);
164  C_INT64 time;
165  time = ((C_INT64) ttt.tv_sec) * LLONG_CONST(1000000) + (C_INT64) ttt.tv_usec;
166  return time;
167 }
168 #else
169 
170 //static
172 {
173  LARGE_INTEGER SystemTime;
174  GetSystemTimeAsFileTime((FILETIME *) &SystemTime);
175 
176  return SystemTime.QuadPart / LLONG_CONST(10);
177 }
178 #endif
179 
181 {
182 #ifdef WIN32
183  LARGE_INTEGER CreationTime;
184  LARGE_INTEGER ExitTime;
185  LARGE_INTEGER KernelTime;
186  LARGE_INTEGER UserTime;
187 
188  GetProcessTimes(GetCurrentProcess(),
189  (FILETIME *) &CreationTime,
190  (FILETIME *) &ExitTime,
191  (FILETIME *) &KernelTime,
192  (FILETIME *) &UserTime);
193 
194  return (KernelTime.QuadPart + UserTime.QuadPart) / LLONG_CONST(10);
195 
196 #else
197  struct rusage ResourceUsage;
198 
199  getrusage(RUSAGE_SELF, &ResourceUsage);
200 
201  return ((C_INT64) ResourceUsage.ru_utime.tv_sec) * LLONG_CONST(1000000)
202  + (C_INT64) ResourceUsage.ru_utime.tv_usec;
203 #endif // WIN32
204 }
205 
207 {
208 #ifdef WIN32
209  LARGE_INTEGER CreationTime;
210  LARGE_INTEGER ExitTime;
211  LARGE_INTEGER KernelTime;
212  LARGE_INTEGER UserTime;
213 
214  GetThreadTimes(GetCurrentThread(),
215  (FILETIME *) &CreationTime,
216  (FILETIME *) &ExitTime,
217  (FILETIME *) &KernelTime,
218  (FILETIME *) &UserTime);
219 
220  return (KernelTime.QuadPart + UserTime.QuadPart) / LLONG_CONST(10);
221 
222 #elif defined Darwin
223 
224  struct thread_basic_info t_info;
225  mach_msg_type_number_t t_info_count = THREAD_BASIC_INFO_COUNT;
226 
227  if (KERN_SUCCESS != task_info(mach_task_self(),
228  THREAD_BASIC_INFO, (task_info_t) & t_info, & t_info_count))
229  {
230  return -1;
231  }
232 
233  return ((C_INT64) t_info.user_time.seconds) * LLONG_CONST(1000000)
234  + (C_INT64) t_info.user_time.microseconds;
235 
236 #else
237  struct rusage ResourceUsage;
238 
239  getrusage(RUSAGE_THREAD, &ResourceUsage);
240 
241  return ((C_INT64) ResourceUsage.ru_utime.tv_sec) * LLONG_CONST(1000000)
242  + (C_INT64) ResourceUsage.ru_utime.tv_usec;
243 #endif // WIN32
244 }
245 
246 std::string CCopasiTimeVariable::LL2String(const C_INT64 & value,
247  const C_INT32 & digits)
248 {
249  std::string format;
250 
251  if (digits > 0)
252  format = "%0" + StringPrint("%d", digits);
253  else
254  format = "%";
255 
256 #ifdef WIN32
257  format += "I64d";
258 #else
259  format += "lld";
260 #endif
261 
262  return StringPrint(format.c_str(), value);
263 }
std::string isoFormat() const
Definition: CopasiTime.cpp:76
static CCopasiTimeVariable getThreadTime()
Definition: CopasiTime.cpp:206
static CCopasiTimeVariable getProcessTime()
Definition: CopasiTime.cpp:180
C_INT64 getHours(const bool &bounded=false) const
Definition: CopasiTime.cpp:142
#define C_INT64
Definition: copasi.h:88
#define C_INT32
Definition: copasi.h:90
bool operator<(const CCopasiTimeVariable &value)
Definition: CopasiTime.cpp:73
static std::string LL2String(const C_INT64 &value, const C_INT32 &digits=0)
Definition: CopasiTime.cpp:246
C_INT64 getMinutes(const bool &bounded=false) const
Definition: CopasiTime.cpp:134
C_INT64 getDays() const
Definition: CopasiTime.cpp:150
static CCopasiTimeVariable getCurrentWallTime()
Definition: CopasiTime.cpp:160
#define LLONG_CONST(n)
Definition: copasi.h:89
CCopasiTimeVariable operator+(const CCopasiTimeVariable &value)
Definition: CopasiTime.cpp:55
C_INT64 getSeconds(const bool &bounded=false) const
Definition: CopasiTime.cpp:126
std::string StringPrint(const char *format,...)
Definition: utility.cpp:87
#define RUSAGE_THREAD
Definition: CopasiTime.cpp:31
C_INT64 getMicroSeconds(const bool &bounded=false) const
Definition: CopasiTime.cpp:112
CCopasiTimeVariable & operator=(const CCopasiTimeVariable &rhs)
Definition: CopasiTime.cpp:61
CCopasiTimeVariable operator-(const CCopasiTimeVariable &value)
Definition: CopasiTime.cpp:58
C_INT64 getMilliSeconds(const bool &bounded=false) const
Definition: CopasiTime.cpp:118