]> AND Private Git Repository - loba.git/commitdiff
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
timestamp: add possibility to choose clock type.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Fri, 6 May 2011 09:42:40 +0000 (11:42 +0200)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Fri, 6 May 2011 12:30:23 +0000 (14:30 +0200)
main.cpp
timer.h

index 102122dc7eab8012aa056eab515f351ad496707a..91e08a2a51f3808748f3c199acf83eb47c1a8058 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -166,7 +166,7 @@ int main(int argc, char* argv[])
     // Note: variables used after THROW must be declared as volatile.
     volatile int exit_status = 0;   // global exit status
     volatile double simulated_time = -1.0;
     // Note: variables used after THROW must be declared as volatile.
     volatile int exit_status = 0;   // global exit status
     volatile double simulated_time = -1.0;
-    timestamp simulation_time;
+    timestamp simulation_time(timestamp::cpu_time);
     xbt_ex_t ex;
     MSG_error_t res;
 
     xbt_ex_t ex;
     MSG_error_t res;
 
diff --git a/timer.h b/timer.h
index ccb115bcba15736b7012599aa9f5713e1b5ef15e..b9a25099630063bd41d851cabaed8764fb012222 100644 (file)
--- a/timer.h
+++ b/timer.h
@@ -51,7 +51,9 @@ struct timeval operator-(const struct timeval& a, const struct timeval& b)
 
 class timestamp {
 public:
 
 class timestamp {
 public:
-    timestamp();
+    enum clock_type { wallclock_time, cpu_time };
+
+    timestamp(clock_type ct);
     ~timestamp();
     void reset();
 
     ~timestamp();
     void reset();
 
@@ -62,16 +64,19 @@ public:
     double duration() const;
 
 private:
     double duration() const;
 
 private:
-    struct rusage before;
-    struct rusage after;
+    clock_type clk;
+    struct timeval before;
+    struct timeval after;
     struct timeval difference;
 
     struct timeval difference;
 
+    void get_time(struct timeval& tv);
+
     static void tv_clear(struct timeval& a);
     static double timertod(const struct timeval& a);
 };
 
 inline
     static void tv_clear(struct timeval& a);
     static double timertod(const struct timeval& a);
 };
 
 inline
-timestamp::timestamp()
+timestamp::timestamp(clock_type ct): clk(ct)
 {
     reset();
 }
 {
     reset();
 }
@@ -84,25 +89,22 @@ timestamp::~timestamp()
 inline
 void timestamp::reset()
 {
 inline
 void timestamp::reset()
 {
-    tv_clear(before.ru_utime);
-    tv_clear(before.ru_stime);
-    tv_clear(after.ru_utime);
-    tv_clear(after.ru_stime);
+    tv_clear(before);
+    tv_clear(after);
     tv_clear(difference);
 }
 
 inline
 void timestamp::start()
 {
     tv_clear(difference);
 }
 
 inline
 void timestamp::start()
 {
-    getrusage(RUSAGE_SELF, &before);
+    get_time(before);
 }
 
 inline
 void timestamp::stop()
 {
 }
 
 inline
 void timestamp::stop()
 {
-    getrusage(RUSAGE_SELF, &after);
-    difference = difference + ((after.ru_utime + after.ru_stime) -
-                               (before.ru_utime + before.ru_stime));
+    get_time(after);
+    difference = difference + (after - before);
 }
 
 inline
 }
 
 inline
@@ -117,6 +119,22 @@ double timestamp::duration() const
     return timertod(difference);
 }
 
     return timertod(difference);
 }
 
+inline
+void timestamp::get_time(struct timeval& tv)
+{
+    switch (clk) {
+    case cpu_time: {
+        struct rusage usage;
+        getrusage(RUSAGE_SELF, &usage);
+        tv = usage.ru_utime + usage.ru_stime;
+        break;
+    }
+    case wallclock_time:
+        gettimeofday(&tv, NULL);
+        break;
+    }
+}
+
 inline
 void timestamp::tv_clear(struct timeval& a)
 {
 inline
 void timestamp::tv_clear(struct timeval& a)
 {