1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
24 // Kudos to James Litsios <litsios@iis.ee.ethz.ch> for his tireless
25 // testing efforts and useful feedbacks. Certain part of Prop
26 // now incorporates some of James' ideas on reference counting and
28 //////////////////////////////////////////////////////////////////////////////
32 #include <AD/gc/gcconfig.h>
33 #include <AD/gc/gctimer.h>
35 // Use getrusage instead of times when the former is available
36 // since getrusage has higher resolution
38 #ifdef PROP_HAS_GETRUSAGE
39 # include <sys/time.h>
40 # include <sys/resource.h>
42 # ifdef PROP_HAS_TIMES
43 # include <sys/times.h>
47 GCTimer::GCTimer() : sec(0), usec(0) {}
48 GCTimer::GCTimer(long s
, long us
) : sec(s
), usec(us
) {}
49 void GCTimer::clear() { sec
= 0; usec
= 0; }
51 GCTimer
GCTimer::operator + (const GCTimer
& b
)
52 { long the_usec
= (usec
+ b
.usec
) % 1000000;
53 long the_sec
= sec
+ b
.sec
+ (usec
+ usec
) / 1000000;
54 return GCTimer(the_sec
,the_usec
);
57 GCTimer
GCTimer::operator - (const GCTimer
& b
)
58 { long the_usec
= (1000000 + usec
- b
.usec
) % 1000000;
59 long the_sec
= sec
- b
.sec
- (usec
< usec
);
60 return GCTimer(the_sec
,the_usec
);
63 void GCTimer::operator += (const GCTimer
& b
)
64 { *this = *this + b
; }
66 ostream
& operator << (ostream
& f
, const GCTimer
& t
)
67 { int ms
= t
.usec
/ 1000;
69 << (char)(ms
/100 + '0')
70 << (char)((ms
/10) % 10 + '0')
71 << (char)(ms
% 10 + '0')
76 void GCTimer::get_system_time()
78 #ifdef PROP_HAS_GETRUSAGE
80 getrusage(RUSAGE_SELF
,&usage
);
81 usec
= usage
.ru_stime
.tv_usec
;
82 sec
= usage
.ru_stime
.tv_sec
;
84 # ifdef PROP_HAS_TIMES
87 usec
= (timing
.tms_stime
% 60) * 1000000 / 60;
88 sec
= timing
.tms_stime
/ 60;
93 void GCTimer::get_user_time()
95 #ifdef PROP_HAS_GETRUSAGE
97 getrusage(RUSAGE_SELF
,&usage
);
98 usec
= usage
.ru_utime
.tv_usec
;
99 sec
= usage
.ru_utime
.tv_sec
;
101 # ifdef PROP_HAS_TIMES
104 usec
= (timing
.tms_utime
% 60) * 1000000 / 60;
105 sec
= timing
.tms_utime
/ 60;