|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64165 [PATCH] RDTSC calibration totally wrong on Linux
Submitted: 2013-02-06 23:35 UTC Modified: 2017-10-24 08:25 UTC
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Assigned:
Status: Suspended Package: xhprof (PECL)
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2013-02-06 23:35 UTC]
get_cpu_frequency() calibrates RDTSC values by measuring the delta across a usleep(5000) call. On my Intel processor on Linux 3.5, the processor often halts for the majority of those 5 milliseconds. The RDTSC counter is not incremented during the halt. So the resulting calibration is off by a factor of 10 or so.

The solution is to do a busy loop instead of a usleep() call. Patch attached. The volatile loop counter prevents the loop from being optimised away, I checked the assembly generated by gcc.

I confirmed that with the patch, the reported CPU frequency is consistent with the physical clock frequency, and stable with a standard deviation of 0.007%.

Test script:
diff --git a/extension/xhprof.c b/extension/xhprof.c
index a053ede..52841d1 100644
--- a/extension/xhprof.c
+++ b/extension/xhprof.c
@@ -1351,6 +1351,7 @@ static void get_all_cpu_frequencies() {
     frequency = get_cpu_frequency();
+    printf("CPU %d: %g\n", id, frequency);
     if (frequency == 0.0) {


xhprof-rdtsc-busy-loop (last revision 2013-02-06 23:35 UTC by tstarling)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-24 08:25 UTC]
-Status: Open +Status: Suspended
 [2017-10-24 08:25 UTC]
This package has not had a release for over 4 years, and the last bit of git activity was over 2 years ago, so I think its safe to say this extension is no longer in active development. If development picks back up, then please re-open this report
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Dec 06 01:01:27 2023 UTC