All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Beulich <jbeulich@suse.com>
To: Teddy Astie <teddy.astie@vates.tech>
Cc: Anthony PERARD <anthony.perard@vates.tech>,
	xen-devel@lists.xenproject.org
Subject: Re: [RFC PATCH for-4.22 2/2] tools: Introduce xen-inteltemp tool
Date: Tue, 28 Oct 2025 10:24:13 +0100	[thread overview]
Message-ID: <6dcbb451-7c2e-434b-a125-1abf932ce0d0@suse.com> (raw)
In-Reply-To: <acb887d5df692aa143cb2b509dd362b7b46c158e.1761585640.git.teddy.astie@vates.tech>

On 27.10.2025 18:26, Teddy Astie wrote:
> Introduce a new tool to fetch Intel CPU temperatures through the
> Intel DTS interface using XENPF_resource_op hypercall.
> 
> Signed-off-by: Teddy Astie <teddy.astie@vates.tech>
> ---
>  tools/misc/.gitignore      |  1 +
>  tools/misc/Makefile        |  4 ++
>  tools/misc/xen-inteltemp.c | 98 ++++++++++++++++++++++++++++++++++++++
>  3 files changed, 103 insertions(+)
>  create mode 100644 tools/misc/xen-inteltemp.c

Instead of introducing a new tool, might this not fit in xenpm?

> --- /dev/null
> +++ b/tools/misc/xen-inteltemp.c
> @@ -0,0 +1,98 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * xen-inteltemp.c
> + *
> + * Get the CPU temperature of Intel processors.
> + *
> + * Copyright 2025 Teddy Astie <teddy.astie@vates.tech>
> + */
> +
> +#include <stdio.h>
> +#include <errno.h>
> +#include <xenctrl.h>
> +
> +#define MSR_IA32_THERM_STATUS         0x0000019c
> +#define MSR_IA32_TEMPERATURE_TARGET   0x000001a2
> +#define MSR_IA32_PACKAGE_THERM_STATUS 0x000001b1
> +
> +int fetch_dts_temp(xc_interface *xch, uint32_t cpu, bool package, int *temp)
> +{
> +    xc_resource_entry_t entries[2] = {
> +        (xc_resource_entry_t){
> +            .idx = package ? MSR_IA32_PACKAGE_THERM_STATUS : MSR_IA32_THERM_STATUS
> +        },
> +        (xc_resource_entry_t){ .idx = MSR_IA32_TEMPERATURE_TARGET },
> +    };
> +    struct xc_resource_op ops = {
> +        .cpu = cpu,
> +        .entries = entries,
> +        .nr_entries = 2,
> +    };
> +    int tjmax;
> +
> +    int ret = xc_resource_op(xch, 1, &ops);
> +
> +    if ( ret <= 0 )
> +        /* This CPU isn't online or can't query this MSR */
> +        return ret ?: -EOPNOTSUPP;
> +
> +    if ( ret == 2 )
> +        tjmax = (entries[1].val >> 16) & 0xff;
> +    else
> +    {
> +        /*
> +         * The CPU doesn't support MSR_IA32_TEMPERATURE_TARGET, we assume it's 100 which
> +         * is correct aside a few selected Atom CPUs. Check coretemp source code for more
> +         * information.
> +         */
> +        fprintf(stderr, "CPU%d doesn't support MSR_IA32_TEMPERATURE_TARGET, assume "
> +               "tjmax=100°C, readings may be incorrect\n", cpu);
> +        tjmax = 100;
> +    }
> +    
> +    *temp = tjmax - ((entries[0].val >> 16) & 0xff);
> +    return 0;
> +}
> +
> +int main(void)
> +{
> +    int rc = 0, temp, cpu, socket;
> +    bool has_data = false;
> +    xc_interface *xch = xc_interface_open(0, 0, 0);
> +    xc_physinfo_t info;
> +
> +    if ( (rc = xc_physinfo(xch, &info)) < 0 )
> +    {
> +        perror("Getting physinfo failed");
> +        return rc;
> +    }
> +
> +    /* Per socket measurement */
> +    for ( socket = 0, cpu = 0;
> +          cpu < (info.max_cpu_id + 1);
> +          socket++, cpu += info.cores_per_socket * info.threads_per_core )
> +    {
> +        if ( !fetch_dts_temp(xch, cpu, true, &temp) )
> +        {
> +            has_data = true;
> +            printf("Package%d: %d°C\n", socket, temp);
> +        }
> +    }
> +
> +    printf("\n");
> +
> +    for ( cpu = 0; cpu < (info.max_cpu_id + 1); cpu += info.threads_per_core )
> +    {
> +        if ( fetch_dts_temp(xch, cpu, false, &temp) )
> +            continue;
> +
> +        has_data = true;
> +        printf("CPU%d: %d°C\n", cpu, temp);
> +    }
> +
> +    if ( !has_data )
> +        printf("No data\n");
> +
> +    xc_interface_close(xch);
> +    return 0;
> +}
> \ No newline at end of file

Please never introduce files without trailing newline.

Jan


  reply	other threads:[~2025-10-28  9:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-27 17:26 [RFC PATCH for-4.22 0/2] Support for Intel temperature sensors (DTS) Teddy Astie
2025-10-27 17:26 ` [RFC PATCH for-4.22 2/2] tools: Introduce xen-inteltemp tool Teddy Astie
2025-10-28  9:24   ` Jan Beulich [this message]
2025-10-27 17:26 ` [RFC PATCH for-4.22 1/2] x86/platform: Expose DTS sensors MSR Teddy Astie
2025-10-27 19:38   ` Andrew Cooper
2025-10-28  9:20     ` Jan Beulich
2025-10-29 16:06       ` Andrew Cooper
2025-10-30  7:07         ` Jan Beulich
2025-10-28  9:22   ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6dcbb451-7c2e-434b-a125-1abf932ce0d0@suse.com \
    --to=jbeulich@suse.com \
    --cc=anthony.perard@vates.tech \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.