[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] x86/time: further improve TSC / CPU freq calibration accuracy
- To: Jan Beulich <jbeulich@xxxxxxxx>
- From: Roger Pau Monné <roger.pau@xxxxxxxxxx>
- Date: Tue, 18 Jan 2022 13:45:17 +0100
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=H3IhpHkgH3+YnSr4acbdkM+taRDBNemAk9nA1ypjnBc=; b=R/7RcgkVDR72VElFDueOLDug33lEzemEd9CuQyQ1nrcbel8SYlcBkPyB4PzQ5p2Og7obYalD9A3vt17bZywrQHngdslWDRNVVDpypnkOZXHoEgKWI2khlYcFEod1XpqlyR8tcf7C2H21gEGtXEfGxXwbn3GeKvT/ux1PKZbCkQ86wRB0e/kKGOd8v72ikVAXdiO5btcEqzzsZURT6CZg7fkWX14kvqDHHBoiVtIoKfYUNAj2bpBYG6Q4KO6eY2kcmCivK0bq3GwW6DllY7P/ex8CMocs4YyLnoZu/XiX2K6tOx42r7F4dA6PBKY4dxhZyyK0KplSKYy1H7/FB1jC+g==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CZ1wLv2+jV4t7ot+cvRriOhk0NXbGeicT5mcydezrl015T/4K5s/ZBweJrS3rXBFgfCDTGgbtkYfImecD5rjfpgtCSO4v/7+RsLIBcesu87CmW3nOBsWGKWFJ3JRhMDS9j5/UEMGsgcyMrB19FgMccbvnulJy76sGnEsMD1Hs4Jx/RfVqNLS7jnjvjWJ+RMF37W4Wu7zQ3fyHtD2tUZnDQifRQ78JQn2DcMsnVVJv/mg+MnfeHtDRt9u9YcCtkhiS7PU80FJKh74W8ffpeI9T5E9TULLbuVJwkW34hHjWAHGWcW8TWbNSSBJ3exunSnt9wdzcdAiAhU5EZUMMH4ZAQ==
- Authentication-results: esa4.hc3370-68.iphmx.com; dkim=pass (signature verified) header.i=@citrix.onmicrosoft.com
- Cc: "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>
- Delivery-date: Tue, 18 Jan 2022 12:45:53 +0000
- Ironport-data: A9a23:FVWnx6vo1DQHGQC33Bxo88ERh+fnVLdZMUV32f8akzHdYApBsoF/q tZmKW+Cb/mDYzT0L9p+PNvj80oG7ZWAyd5qGQBuqSlkFXsa+JbJXdiXEBz9bniYRiHhoOOLz Cm8hv3odp1coqr0/0/1WlTZQP0VOZigHtIQMsadUsxKbVIiGHdJZS5LwbZj2NYx2IjhWmthh PupyyHhEA79s9JLGjp8B5Kr8HuDa9yr5Vv0FnRnDRx6lAe2e0s9VfrzFonoR5fMeaFGH/bSe gr25OrRElU1XfsaIojNfr7TKiXmS1NJVOSEoiI+t6OK2nCuqsGuu0qS2TV1hUp/0l20c95NJ Npl5LiAFh4PY5b3gu0EUhdSCjN3P/Jb0eqSSZS/mZT7I0zudnLtx7NlDV0sPJ1e8eFyaY1M3 aVGcnZXNEnF3r/ohuLgIgVvrp1LwM3DJoQQt2sm1TjEJf0nXYrCU+PB4towMDIY2JwVQaeEP ZJxhTxHbwiRYRtuMUYsMIsyjsOP3XzvYSZFtwfAzUYwyzeKl1EguFT3C/LKfvSaSMMTmVyXz krW8mK8DhwEOdi3zTue7mnqluLJhTn8Wo8ZCPu/7PECqF+Zy3EXCRYWfUCmuvT/gUm7M++zM GRNpHBo9/JrshX2EJ+tBHVUvUJooDZNf+oPCstn0zqXw7PV7QyAFGEGESNoPYlOWNANeRQm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KjlzEnrv5an854Ed3AUV0gK1XYeNu/0KDwTeOEQbqBJaK Z0q/A37waISDAyVICWF3MFV+/Mq5nik4/nWwcPA1oC5BOVhT2lxbbmG1zAty1uGQ9n8PMHyy zoggb57qKsv7WSzQLd7Xba69BzlMH6wtVOKcSQgow+KynqiCyveIN9Mofy9wwdkaWK0hIHgd PMqxAvM4Ba7G7QRHi8pV/X1wzpwF8Vmgjf4G7dpUGmjd3yRTo8BcYEr5leaAHl500pu8w5+L 5X3kqC3qAnQi/orWDY3ZzlRhtqnk27rT4JiugIlUFSVoMYdft4sZEfxkVIC50NdRiKpLzPKN MeTf002cwmMW9zNxvizypSKZ2XLzkO9y69MwY/Upf/6UkVoJh7p3FosPD30E1wsa7VcKM0l9 gsBJ4Y4Y2mfvVmHJ6VO91xMvdfKla9Ny4kY1jiaGgOKsk8SgDwQtjMkfEI2N0=
- Ironport-sdr: z/U7XjDqLD4W0NKUCmKjsVj6PupdXDh2sFgP8BQ0h0E0SzOwKil+jk1x71EA8FihYBeqIecE6O MNR2+2lMxP3vmECvAYqc1srOWMPHkB9nnVQC7Ls9bzljdZ2PVAJWQUutgQg4+88E6UnxqChyyP GRiHBWCGrzMw2fJ0kwwCk6zU69Ej747j9weet5KvKMUB0454fUKo1lePCN4II3wvXMqWjYd+sd JUHu9t94ktwcWViyZxn0K/8WBsBiPbT3W2JxE3+FLpxBVWYoWaHbKSBa6+Jyti01uGDoFfxda8 6btH+YnsUAduhQ7vFSovt4+q
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On Thu, Jan 13, 2022 at 02:41:31PM +0100, Jan Beulich wrote:
> Calibration logic assumes that the platform timer (HPET or ACPI PM
> timer) and the TSC are read at about the same time. This assumption may
> not hold when a long latency event (e.g. SMI or NMI) occurs between the
> two reads. Reduce the risk of reading uncorrelated values by doing at
> least four pairs of reads, using the tuple where the delta between the
> enclosing TSC reads was smallest. From the fourth iteration onwards bail
> if the new TSC delta isn't better (smaller) than the best earlier one.
Do you have some measurements as to whether this makes the frequency
detection any more accurate?
> Signed-off-by: Jan Beulich <jbeulich@xxxxxxxx>
> ---
> Obviously (I think) instead of having both read_{hpet,pmtmr}_and_tsc()
> the calibration logic could be folded between HPET and PMTMR, at the
As an intermediate solution you could have a read_counter_and_tsc I
would think?
> expense of a couple more indirect calls (which may not be that much of a
> problem as this is all boot-time only). Really such folding would have
> been possible already before, just that the amount of duplicate code
> hasn't been this large so far. IOW if so desired, then perhaps the
> folding should be a separate prereq patch.
You could even pass a platform_timesource as a parameter to that
generic read counter and TSC helper.
> --- a/xen/arch/x86/time.c
> +++ b/xen/arch/x86/time.c
> @@ -392,9 +392,36 @@ static u64 read_hpet_count(void)
> return hpet_read32(HPET_COUNTER);
> }
>
> +static uint32_t __init read_hpet_and_tsc(uint64_t *tsc)
> +{
> + uint64_t tsc_prev = *tsc = rdtsc_ordered(), tsc_min = ~0;
> + uint32_t best = best;
> + unsigned int i;
> +
> + for ( i = 0; ; ++i )
> + {
> + uint32_t hpet = hpet_read32(HPET_COUNTER);
> + uint64_t tsc_cur = rdtsc_ordered();
> + uint64_t tsc_delta = tsc_cur - tsc_prev;
> +
> + if ( tsc_delta < tsc_min )
> + {
> + tsc_min = tsc_delta;
> + *tsc = tsc_cur;
> + best = hpet;
> + }
> + else if ( i > 2 )
> + break;
> +
> + tsc_prev = tsc_cur;
Would it be better to set tsc_prev to the current TSC value here in
order to minimize the window you are measuring? Or else tsc_delta will
also account for the time in the loop code, and there's more
likeliness from being interrupted?
I guess being interrupted four times so that all loops are bad is very
unlikely.
Since this loop could in theory run multiple times, do we need to
check that the counter doesn't overflow? Or else the elapsed counter
value would be miscalculated.
Thanks, Roger.
|