Skip to contentSkip to conversation

Patch: cpu-all.h for better cycle counter

Qemu-devel Todd T. Fries 3 participants 6 messages Mar 21, 2007 – Mar 27, 2007

lore
Share & tools
lore
#0 lore raw

This is relative to the 20070319 snapshot.

cpu-all.h renamed from cpu-all.h.orig +14 -5
@@ -1012,13 +1012,22 @@ static inline int64_t cpu_get_real_ticks
 #endif
 }
 #else
-/* The host CPU doesn't have an easily accessible cycle counter.
-   Just return a monotonically increasing vlue.  This will be totally wrong,
-   but hopefully better than nothing.  */
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;
+	struct timeval tv;
+	static int64_t i = 0;
+	int64_t j;
+        
+	gettimeofday(&tv, NULL);
+	do {
+	   j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+	} while (i == j);
+	i = j;
+	return j;
 }
 #endif
 
Signature · 12 lines
Todd Fries .. [email protected]

 _____________________________________________
|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| "..in support of free software solutions."  \          250797 (FWD)
|                                             \
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                                 
              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt
#1 lore raw

Hi,

Quoted 19 lines · Todd T. Fries

This is relative to the 20070319 snapshot.

cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +4 -5
@@ -1012,13 +1012,22 @@ static inline int64_t cpu_get_real_ticks
 #endif
 }
 #else
-/* The host CPU doesn't have an easily accessible cycle counter.
-   Just return a monotonically increasing vlue.  This will be totally wrong,
-   but hopefully better than nothing.  */
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;

cpu-all.h Tue Mar 20 21:14:10 2007 static inline int64_t cpu_get_real_ticks · line 1030

Quoted 10 lines
cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +10 -0
+       struct timeval tv;
+       static int64_t i = 0;
+       int64_t j;
+
+       gettimeofday(&tv, NULL);
+       do {
+          j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+       } while (i == j);
+       i = j;
+       return j;

Isn't this an infinite loop? gettimeofday() was left out of the loop.

How about "return j + (ticks++)" instead of the loop? If I understand correctly it may slow things down to below 1Hz.

Quoted 12 lines
cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +0 -1
 }
 #endif
 
--

Todd Fries .. [email protected]

_____________________________________________

|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| "..in support of free software solutions."  \          250797 (FWD)
|                                             \
Quoted 8 lines

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt

Regards,
Andrew

#2 lore raw
Quoted 35 lines · andrzej zaborowski

Hi,

Quoted 19 lines · Todd T. Fries

This is relative to the 20070319 snapshot.

cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +4 -5
@@ -1012,13 +1012,22 @@ static inline int64_t cpu_get_real_ticks
 #endif
 }
 #else
-/* The host CPU doesn't have an easily accessible cycle counter.
-   Just return a monotonically increasing vlue.  This will be totally wrong,
-   but hopefully better than nothing.  */
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;

cpu-all.h Tue Mar 20 21:14:10 2007 static inline int64_t cpu_get_real_ticks · line 1030

Quoted 10 lines
cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +10 -0
+       struct timeval tv;
+       static int64_t i = 0;
+       int64_t j;
+
+       gettimeofday(&tv, NULL);
+       do {
+          j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+       } while (i == j);
+       i = j;
+       return j;

Isn't this an infinite loop? gettimeofday() was left out of the loop.

Quoted 2 lines

How about "return j + (ticks++)" instead of the loop? If I understand correctly it may slow things down to below 1Hz.

(I wanted to say MHz)

Quoted 25 lines
Quoted 13 lines
cpu-all.h Tue Mar 20 21:14:10 2007 renamed from cpu-all.h.orig Fri Mar 16 18:58:11 2007 +0 -1
 }
 #endif
 
--

Todd Fries .. [email protected]

_____________________________________________

|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| "..in support of free software solutions."  \          250797 (FWD)
|                                             \
Quoted 8 lines

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt

Regards,
Andrew

#3 lore raw
Quoted 1 line · andrzej zaborowski

On 26/03/07, andrzej zaborowski <[email protected]> wrote:

<snip>

Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
cpu-all.h renamed from cpu-all.h.orig +4 -2
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;

cpu-all.h static inline int64_t cpu_get_real_ticks · line 1030

Quoted 10 lines
cpu-all.h renamed from cpu-all.h.orig +10 -0
+       struct timeval tv;
+       static int64_t i = 0;
+       int64_t j;
+
+       gettimeofday(&tv, NULL);
+       do {
+          j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+       } while (i == j);
+       i = j;
+       return j;

Isn't this an infinite loop? gettimeofday() was left out of the loop.

Quoted 4 lines
Quoted 2 lines

How about "return j + (ticks++)" instead of the loop? If I understand correctly it may slow things down to below 1Hz.

(I wanted to say MHz)

I dont think so, in the loop "j" is set and the while-condition is "j==i", so unless "(tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec" always computes the same value the do-while block gets only executed once.

Quoted 1 line

<snip>

Signature · 20 lines
Freundliche Gruesse,
Marc Lörner

Tel.: +49 (0)9103-715-30046
Fax.: +49 (0)9103-715-271
Internet: http://www.hob.de
E-Mail: [email protected]

HOB GmbH & Co. KG
Schwadermühlstraße 3
90556 Cadolzburg

Geschaeftsfuehrer Klaus Brandstaetter - Franz Wiedenmann

AG Fuerth  HRA 5180
Steuer-Nr. 218/163/00107
USt-ID-Nr. DE 132747002

Komplementaerin HOB electronic BeteiligungsGmbH
AG Fuerth HRB 3416
#4 lore raw
Quoted 24 lines · Marc Lörner
Quoted 1 line · andrzej zaborowski

On 26/03/07, andrzej zaborowski <[email protected]> wrote:

<snip>

Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
cpu-all.h renamed from cpu-all.h.orig +4 -2
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;

cpu-all.h static inline int64_t cpu_get_real_ticks · line 1030

Quoted 10 lines
cpu-all.h renamed from cpu-all.h.orig +10 -0
+       struct timeval tv;
+       static int64_t i = 0;
+       int64_t j;
+
+       gettimeofday(&tv, NULL);
+       do {
+          j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+       } while (i == j);
+       i = j;
+       return j;

Isn't this an infinite loop? gettimeofday() was left out of the loop.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

How about "return j + (ticks++)" instead of the loop? If I understand correctly it may slow things down to below 1Hz.

(I wanted to say MHz)

I dont think so, in the loop "j" is set and the while-condition is "j==i", so unless "(tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec" always computes the same value the do-while block gets only executed once.

Well, it does always compute the same value - inside the loop. Doesn't it? And across one microsecond also the same value in all calls to cpu_get_real_ticks.

<snip>

Regards

#5 lore raw
Quoted 36 lines · andrzej zaborowski
Quoted 26 lines · Marc Lörner
Quoted 1 line · andrzej zaborowski

On 26/03/07, andrzej zaborowski <[email protected]> wrote:

<snip>

Quoted 21 lines
Quoted 21 lines
Quoted 8 lines
cpu-all.h renamed from cpu-all.h.orig +4 -2
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
 static inline int64_t cpu_get_real_ticks (void)
 {
-    static int64_t ticks = 0;
-    return ticks++;

cpu-all.h static inline int64_t cpu_get_real_ticks · line 1030

Quoted 10 lines
cpu-all.h renamed from cpu-all.h.orig +10 -0
+       struct timeval tv;
+       static int64_t i = 0;
+       int64_t j;
+
+       gettimeofday(&tv, NULL);
+       do {
+          j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+       } while (i == j);
+       i = j;
+       return j;

Isn't this an infinite loop? gettimeofday() was left out of the loop.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

How about "return j + (ticks++)" instead of the loop? If I understand correctly it may slow things down to below 1Hz.

(I wanted to say MHz)

I dont think so, in the loop "j" is set and the while-condition is "j==i", so unless "(tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec" always computes the same value the do-while block gets only executed once.

Quoted 7 lines

Well, it does always compute the same value - inside the loop. Doesn't it? And across one microsecond also the same value in all calls to cpu_get_real_ticks.

<snip>

Regards

okay, sometimes one doesn't see the wood for the trees. The over and over computation of the same value doesn't make sense, so the call of gettimeofday should be in the loop (i think).

Signature · 20 lines
Freundliche Gruesse,
Marc Lörner

Tel.: +49 (0)9103-715-30046
Fax.: +49 (0)9103-715-271
Internet: http://www.hob.de
E-Mail: [email protected]

HOB GmbH & Co. KG
Schwadermühlstraße 3
90556 Cadolzburg

Geschaeftsfuehrer Klaus Brandstaetter - Franz Wiedenmann

AG Fuerth  HRA 5180
Steuer-Nr. 218/163/00107
USt-ID-Nr. DE 132747002

Komplementaerin HOB electronic BeteiligungsGmbH
AG Fuerth HRB 3416