From: Dan Magenheimer Subject: tsc_scale/cpu_khz imprecise and need fixing? Date: Thu, 29 Oct 2009 13:28:52 -0700 (PDT) Message-ID: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="__12568481371519918abhmt008.oracle.com" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "Xen-Devel (E-mail)" Cc: Keir Fraser , "Zhang, Xiantao" List-Id: xen-devel@lists.xenproject.org --__12568481371519918abhmt008.oracle.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable I observed with the attached patch (on a machine with invariant TSC) that cstate_restore_tsc() has very poor precision; the value that is written to TSC on C3 recovery seems like it should be within a handful of cycles of being accurate (which on a invariant TSC can be precisely compared). Instead, it was off by 200,000 cycles or more! This was counter-intuitive so I dug through the code a bit to see if there is an obvious bug. I *think* the reason is that tsc_scale, which I believe is set only once per processor at startup on machines with constant/invariant TSC, is set imprecisely using init_pit_and_calibrate_tsc(). I suspect the imprecision is compounded through the reciprocal operation. AND I wonder if an ill-timed power management event might render tsc_scale not just imprecise, but just plain wrong! Tsc_scale -- and cpu_khz which is tsc_scale/1000 -- are used in other places as well; one of interest to me is in hvm_gtsc_need_scale()... for TSC to work properly across certain migrations, this test needs to be very precise. There may be others. Anyway, I wonder if there is a more precise way of determining the exact TSC Hz rate, particularly on machines with constant/invariant TSC? I found one such method in Examples 9-5 and 9-6 in: http://www.intel.com/Assets/PDF/appnote/241618.pdf Or maybe there's a better way using ACPI tables or cpufreq? And hopefully there's a method that can work for both AMD and Intel processors? If nothing else, we should probably pick up the latest Linux native_calibrate_tsc() code which has grown considerably more complicated. Thanks, Dan P.S. Note that to reproduce my test, the hpetbroadcast Xen boot parameter must be set, else no C3 events occur. P.P.S. Yes, I realize that the write_tsc() on all processors in time_calibration_tsc_rendezvous() is intended to fix any synchronization lost by an imprecise setting in cstate_restore_tsc(). I was testing to see if the rendezvous'd write was really necessary (and perhaps counter- productive!) when I discovered this imprecision problem. --__12568481371519918abhmt008.oracle.com Content-Type: application/octet-stream; name="tsc_scale_prob.patch" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="tsc_scale_prob.patch" ZGlmZiAtciBkN2Q3Zjk3OGQ3MDQgeGVuL2FyY2gveDg2L3RpbWUuYwotLS0gYS94ZW4vYXJjaC94 ODYvdGltZS5jCVR1ZSBPY3QgMjAgMTQ6MzY6MDEgMjAwOSArMDEwMAorKysgYi94ZW4vYXJjaC94 ODYvdGltZS5jCVRodSBPY3QgMjkgMTM6NDg6MDkgMjAwOSAtMDYwMApAQCAtNjg2LDYgKzY4Niw4 IEBAIHN0YXRpYyB2b2lkIF9faW5pdCBpbml0X3BsYXRmb3JtX3RpbWVyKHYKICAgICAgICAgICAg ZnJlcV9zdHJpbmcocHRzLT5mcmVxdWVuY3kpLCBwdHMtPm5hbWUpOwogfQogCitzdGF0aWMgdTY0 IGMzX21heGRpZmYgPSAwOworCiB2b2lkIGNzdGF0ZV9yZXN0b3JlX3RzYyh2b2lkKQogewogICAg IHN0cnVjdCBjcHVfdGltZSAqdCA9ICZ0aGlzX2NwdShjcHVfdGltZSk7CkBAIC02OTMsOCArNjk1 LDEwIEBAIHZvaWQgY3N0YXRlX3Jlc3RvcmVfdHNjKHZvaWQpCiAgICAgc190aW1lX3Qgc3RpbWVf ZGVsdGE7CiAgICAgdTY0IG5ld190c2M7CiAKKy8qCiAgICAgaWYgKCBib290X2NwdV9oYXMoWDg2 X0ZFQVRVUkVfTk9OU1RPUF9UU0MpICkKICAgICAgICAgcmV0dXJuOworKi8KIAogICAgIHN0aW1l X2RlbHRhID0gcmVhZF9wbGF0Zm9ybV9zdGltZSgpIC0gdC0+c3RpbWVfbWFzdGVyX3N0YW1wOwog ICAgIGlmICggc3RpbWVfZGVsdGEgPCAwICkKQEAgLTcwMiw3ICs3MDYsMTcgQEAgdm9pZCBjc3Rh dGVfcmVzdG9yZV90c2Modm9pZCkKIAogICAgIG5ld190c2MgPSB0LT5sb2NhbF90c2Nfc3RhbXAg KyBzY2FsZV9kZWx0YShzdGltZV9kZWx0YSwgJnN5c190b190c2MpOwogCit7Cit1NjQgdHNjLCBk aWZmOworcmR0c2NsbCh0c2MpOworaWYgKHRzYyA+IG5ld190c2MpIGRpZmYgPSB0c2MgLSBuZXdf dHNjOworZWxzZSBkaWZmID0gbmV3X3RzYyAtIHRzYzsKK2lmIChjM19tYXhkaWZmIDwgZGlmZikg YzNfbWF4ZGlmZiA9IGRpZmY7Cit9CisKKy8qCiAgICAgd3JpdGVfdHNjKG5ld190c2MpOworKi8K IH0KIAogLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq KioqKioqKioqKioqKioqKioqKioqKioqKgpAQCAtMTYwOCw2ICsxNjIyLDcgQEAgc3RhdGljIHZv aWQgZHVtcF9zb2Z0dHNjKHVuc2lnbmVkIGNoYXIgawogICAgIGludCBkb21jbnQgPSAwOwogCiAg ICAgdHNjX2NoZWNrX3JlbGlhYmlsaXR5KCk7CitwcmludGsoImMzX21heGRpZmYgPSAlIlBSSXU2 NCJcbiIsYzNfbWF4ZGlmZik7CiAgICAgaWYgKCBib290X2NwdV9oYXMoWDg2X0ZFQVRVUkVfVFND X1JFTElBQkxFKSApCiAgICAgICAgIHByaW50aygiVFNDIG1hcmtlZCBhcyByZWxpYWJsZSwgIgog ICAgICAgICAgICAgICAgIndhcnAgPSAlbHUgKGNvdW50PSVsdSlcbiIsIHRzY19tYXhfd2FycCwg dHNjX2NoZWNrX2NvdW50KTsK --__12568481371519918abhmt008.oracle.com Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --__12568481371519918abhmt008.oracle.com--