From nobody Mon Feb 9 03:13:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1615237687; cv=none; d=zohomail.com; s=zohoarc; b=aTcrYJZYjHyMM4Ht4lNOduYwvu/Gyc+dkjOQi8VpoiA4rP9eMCEljGFlQ28CAeHiRtppbSrRJbWbGdIeSdK6HokivRXQKOypR3o0pJg3EfXvLmRaPfY5Fsf5dCD0w24/YRmgaVTMtywm6XOZJeyUqXdMjuV4oOYF5G/dqL752Ls= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615237687; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=PKJr8nrwfa99gzKdxy4i3QfsaQ+/kCEtBoAhhMKOwBc=; b=Y/zlrnPuIX2DHco/5YMaOdXhrnX8/QwWenv4Ga50oHqv53S6HyIfHBG6AuNRd9J8QOav147/rBsVrKFXR+ryH7JQ4gnpVRJLPBJr8kvAb7djymZuA/huJqW6btEGmlIgve0wFVXE0cdMztXnLSr8Nes03eETCKaFkcBYvHX0QqA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1615237687728594.2913667922672; Mon, 8 Mar 2021 13:08:07 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.95110.179472 (Exim 4.92) (envelope-from ) id 1lJN6V-0007cp-Sy; Mon, 08 Mar 2021 21:07:51 +0000 Received: by outflank-mailman (output) from mailman id 95110.179472; Mon, 08 Mar 2021 21:07:51 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lJN6V-0007ci-Pi; Mon, 08 Mar 2021 21:07:51 +0000 Received: by outflank-mailman (input) for mailman id 95110; Mon, 08 Mar 2021 21:07:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lJN2A-0006P0-Jq for xen-devel@lists.xenproject.org; Mon, 08 Mar 2021 21:03:22 +0000 Received: from mail-qk1-x733.google.com (unknown [2607:f8b0:4864:20::733]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id b51513b8-6bfb-48fc-bc0d-6441286aa7d3; Mon, 08 Mar 2021 21:02:47 +0000 (UTC) Received: by mail-qk1-x733.google.com with SMTP id b130so10829284qkc.10 for ; Mon, 08 Mar 2021 13:02:47 -0800 (PST) Received: from pm2-ws13.praxislan02.com ([2001:470:8:67e:a33e:daa7:46a1:a7eb]) by smtp.gmail.com with ESMTPSA id r2sm8514070qti.4.2021.03.08.13.02.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Mar 2021 13:02:46 -0800 (PST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: b51513b8-6bfb-48fc-bc0d-6441286aa7d3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PKJr8nrwfa99gzKdxy4i3QfsaQ+/kCEtBoAhhMKOwBc=; b=AGJqFitOs3SR/s3GKDOeCYaFSW2EOleleZyJ/l4r6z+1kV6iswUlvVlUw6jKFMrglP y7wfdqEI0ut4+sU2XdXaOGkaX6RYbehVdbBioKIX2lhOg5TMaQVXWXu1bkiHymevfWIp yqiw6SPbITy5LqVtlOZPNEHn66LgrB08HbB3OgHUKnX5kpatchhOc9aB4WkgI0JhOzxu BF02madFzY0pxaq3sUSmLDYIFNBjB6dLURnd8AJqxwGuOShxklWr3+LIYPyRstVB7LBm bq3c12d7MD5MEsq5G2neE1ai0E1zHYrwzwcAY103D9uIoeQnFz2uvS7qzz492tXf37Fd 3C9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PKJr8nrwfa99gzKdxy4i3QfsaQ+/kCEtBoAhhMKOwBc=; b=Fi4vYFSRSMUleKvffC1xxVcKDl0VSKi5xXuyuoeX8cyMV3qvqhwRskJ+mg9t131wRs kSTndLTbyMUarDGxy7vb3WcgIQKU015PDFkunNXkaude//ldLREccZJY90pbjFc7SK6M 63t0QikTiJz+YAWzpCVwF7O21oVd/QKaM5nO0SNL6YSXtYfr1vRGY4ZXjjq/HQD1/0vU g5CTQ5sKhEyfBSLvTlOFmBqwYLzo728gNmnCU3TOAVULKu8u8l4FD1D2xPQJxQ7V6TES sqKklU2GFWqt3r3VNnjs11k0u2pp7H2/ay3KmM/vLwBTrUtOwkE3g+hyAGuintF2frz1 W+rw== X-Gm-Message-State: AOAM532D2t/PqwO4YhMkcX6PKD4GuJvmS59QIVRztu8vOXosjt6UoEVs 2Ff6kqwWbjS6le8BI3GNMSgH9kV0E0o= X-Google-Smtp-Source: ABdhPJykpZdiAikj/fSZyShWHGRzVySaMqLE1EYhJjnKIDorwAsEFzY0mTfkgnKVTsqakCSIMg/7Tw== X-Received: by 2002:a37:9ed0:: with SMTP id h199mr22916899qke.8.1615237366768; Mon, 08 Mar 2021 13:02:46 -0800 (PST) From: Jason Andryuk To: xen-devel@lists.xenproject.org Cc: pawel@invisiblethingslab.com, marmarek@invisiblethingslab.com, Jason Andryuk , Ian Jackson , Wei Liu , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [RFC 12/12] xenpm: Add set-cpufreq-hwp subcommand Date: Mon, 8 Mar 2021 16:02:10 -0500 Message-Id: <20210308210210.116278-13-jandryuk@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210308210210.116278-1-jandryuk@gmail.com> References: <20210308210210.116278-1-jandryuk@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Type: text/plain; charset="utf-8" set-cpufreq-hwp allows setting the Hardware P-State (HWP) parameters. It can be run on all or just a snigle cpu. There are preset of balance, powersave & performance. Those can be further tweaked by param:val arguments as explained in the usage description. parameter names are just checked to the first 3 characters to shorten typing. Some options are hardware dependent, and ranges can be found in get-cpufreq-para. Signed-off-by: Jason Andryuk --- tools/misc/xenpm.c | 240 ++++++++++++++++++++++++++++++++ xen/arch/x86/acpi/cpufreq/hwp.c | 1 + 2 files changed, 241 insertions(+) diff --git a/tools/misc/xenpm.c b/tools/misc/xenpm.c index a686f8f46e..d3bcaf3b58 100644 --- a/tools/misc/xenpm.c +++ b/tools/misc/xenpm.c @@ -67,6 +67,25 @@ void show_help(void) " set-max-cstate |'unlimited' [|'unlimited']= \n" " set the C-State limitati= on ( >=3D 0) and\n" " optionally the C-sub-sta= te limitation ( >=3D 0)\n" + " set-cpufreq-hwp [cpuid] [balance|performance|powersave= ] *\n" + " set Hardware P-State (HW= P) parameters\n" + " optionally a preset of o= ne of\n" + " balance|performance|po= wersave\n" + " an optional list of para= m:val arguments\n" + " minimum:N hw_lowest .= .. hw_highest\n" + " maximum:N hw_lowest .= .. hw_highest\n" + " desired:N hw_lowest .= .. hw_highest\n" + " Set explicit perfo= rmance target.\n" + " non-zero disables = auto-HWP mode.\n" + " energy_perf:0-255 (or = 0-15)\n" + " energy/per= formance hint\n" + " lower favo= r performance\n" + " higher fav= or powersave\n" + " 127 (or 7)= balance\n" + " act_window:N{,m,u}s ra= nge 0us-1270s\n" + " window for interna= l calculations.\n" + " 0 lets the hardwar= e decide.\n" + " get-cpufreq-para returns= hw_lowest/highest.\n" " start [seconds] start collect Cx/Px stat= istics,\n" " output after CTRL-C or S= IGINT or several seconds.\n" " enable-turbo-mode [cpuid] enable Turbo Mode for pr= ocessors that support it.\n" @@ -1309,6 +1328,226 @@ void disable_turbo_mode(int argc, char *argv[]) errno, strerror(errno)); } =20 +/* + * Parse activity_window:NNN{us,ms,s} and validate range. + * + * Activity window is a 7bit mantissa (0-127) with a 3bit exponent (0-7) b= ase + * 10 in microseconds. So the range is 1 microsecond to 1270 seconds. A = value + * of 0 lets the hardware autonomously select the window. + * + * Return 0 on success + * -1 on error + * 1 Not activity_window. i.e. try parsing as another argument + */ +static int parse_activity_window(xc_set_hwp_para_t *set_hwp, char *p) +{ + char *param =3D NULL, *val =3D NULL, *suffix =3D NULL; + unsigned int u; + unsigned int exponent =3D 0; + unsigned int multiplier =3D 1; + int ret; + + ret =3D sscanf(p, "%m[a-z_A-Z]:%ms", ¶m, &val); + if ( ret !=3D 2 ) + { + return -1; + } + + if ( strncasecmp(param, "act", 3) !=3D 0 ) + { + ret =3D 1; + + goto out; + } + + free(param); + param =3D NULL; + + ret =3D sscanf(val, "%u%ms", &u, &suffix); + if ( ret !=3D 1 && ret !=3D 2 ) + { + fprintf(stderr, "invalid activity window: %s\n", val); + + ret =3D -1; + + goto out; + } + + if ( ret =3D=3D 2 && suffix ) + { + if ( strcasecmp(suffix, "s") =3D=3D 0 ) + { + multiplier =3D 1000 * 1000; + exponent =3D 6; + } + else if ( strcasecmp(suffix, "ms") =3D=3D 0 ) + { + multiplier =3D 1000; + exponent =3D 3; + } + else if ( strcasecmp(suffix, "us") =3D=3D 0 ) + { + multiplier =3D 1; + exponent =3D 0; + } + else + { + fprintf(stderr, "invalid activity window units: %s\n", suffix); + + ret =3D -1; + goto out; + } + } + + if ( u > 1270 * 1000 * 1000 / multiplier ) + { + fprintf(stderr, "activity window %s too large\n", val); + + ret =3D -1; + goto out; + } + + /* looking for 7 bits of mantissa and 3 bits of exponent */ + while ( u > 127 ) + { + u /=3D 10; + exponent +=3D 1; + } + + set_hwp->activity_window =3D ( exponent & 0x7 ) << 7 | ( u & 0x7f ); + set_hwp->set_params |=3D XEN_SYSCTL_HWP_SET_ACT_WINDOW; + + ret =3D 0; + + out: + free(suffix); + free(param); + free(val); + + return ret; +} + +static int parse_hwp_opts(xc_set_hwp_para_t *set_hwp, int *cpuid, + int argc, char *argv[]) +{ + int i =3D 0; + + if ( argc < 1 ) + return -1; + + if ( parse_cpuid_non_fatal(argv[i], cpuid) =3D=3D 0 ) + { + i++; + } + + if ( i =3D=3D argc ) + return -1; + + if ( strcasecmp(argv[i], "powersave") =3D=3D 0 ) + { + set_hwp->set_params =3D XEN_SYSCTL_HWP_SET_PRESET_POWERSAVE; + i++; + } + else if ( strcasecmp(argv[i], "performance") =3D=3D 0 ) + { + set_hwp->set_params =3D XEN_SYSCTL_HWP_SET_PRESET_PERFORMANCE; + i++; + } + else if ( strcasecmp(argv[i], "balance") =3D=3D 0 ) + { + set_hwp->set_params =3D XEN_SYSCTL_HWP_SET_PRESET_BALANCE; + i++; + } + + for ( ; i < argc; i++) + { + unsigned int val; + char *param; + int ret; + + ret =3D parse_activity_window(set_hwp, argv[i]); + switch ( ret ) + { + case -1: + return -1; + case 0: + continue; + break; + case 1: + /* try other parsing */ + break; + } + + /* sscanf can't handle split on ':' for "%ms:%u' */ + ret =3D sscanf(argv[i], "%m[a-zA-Z_]:%u", ¶m, &val); + if ( ret !=3D 2 ) + { + fprintf(stderr, "%s is an invalid hwp parameter.\n", argv[i]); + return -1; + } + + if ( val > 255 ) + { + fprintf(stderr, "%s value %u is out of range.\n", param, val); + return -1; + } + + if ( strncasecmp(param, "min", 3) =3D=3D 0 ) + { + set_hwp->minimum =3D val; + set_hwp->set_params |=3D XEN_SYSCTL_HWP_SET_MINIMUM; + } + else if ( strncasecmp(param, "max", 3) =3D=3D 0 ) + { + set_hwp->maximum =3D val; + set_hwp->set_params |=3D XEN_SYSCTL_HWP_SET_MAXIMUM; + } + else if ( strncasecmp(param, "des", 3) =3D=3D 0 ) + { + set_hwp->desired =3D val; + set_hwp->set_params |=3D XEN_SYSCTL_HWP_SET_DESIRED; + } + else if ( strncasecmp(param, "ene", 3) =3D=3D 0 ) + { + set_hwp->energy_perf =3D val; + set_hwp->set_params |=3D XEN_SYSCTL_HWP_SET_ENERGY_PERF; + } + else + { + fprintf(stderr, "%s is an invalid parameter\n.", param); + return -1; + } + + free(param); + } + + return 0; +} + +static void hwp_set_func(int argc, char *argv[]) +{ + xc_set_hwp_para_t set_hwp =3D {}; + int cpuid =3D -1; + int i =3D 0; + + if ( parse_hwp_opts(&set_hwp, &cpuid, argc, argv) ) + { + fprintf(stderr, "Missing, excess, or invalid argument(s)\n"); + exit(EINVAL); + } + + if ( cpuid !=3D -1 ) + { + i =3D cpuid; + max_cpu_nr =3D i + 1; + } + + for ( ; i < max_cpu_nr; i++ ) + if ( xc_set_cpufreq_hwp(xc_handle, i, &set_hwp) ) + fprintf(stderr, "[CPU%d] failed to set hwp params (%d - %s)\n", + i, errno, strerror(errno)); +} + struct { const char *name; void (*function)(int argc, char *argv[]); @@ -1319,6 +1558,7 @@ struct { { "get-cpufreq-average", cpufreq_func }, { "start", start_gather_func }, { "get-cpufreq-para", cpufreq_para_func }, + { "set-cpufreq-hwp", hwp_set_func }, { "set-scaling-maxfreq", scaling_max_freq_func }, { "set-scaling-minfreq", scaling_min_freq_func }, { "set-scaling-governor", scaling_governor_func }, diff --git a/xen/arch/x86/acpi/cpufreq/hwp.c b/xen/arch/x86/acpi/cpufreq/hw= p.c index 8f4b18d246..0fd70d76a8 100644 --- a/xen/arch/x86/acpi/cpufreq/hwp.c +++ b/xen/arch/x86/acpi/cpufreq/hwp.c @@ -584,6 +584,7 @@ int set_hwp_para(struct cpufreq_policy *policy, } =20 if ( set_hwp->set_params & XEN_SYSCTL_HWP_SET_DESIRED && + set_hwp->desired !=3D 0 && ( set_hwp->desired < data->hw_lowest || set_hwp->desired > data->hw_highest ) ) { --=20 2.29.2