From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 704ADA034F;
	Fri,  7 May 2021 04:14:11 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id E445740686;
	Fri,  7 May 2021 04:14:10 +0200 (CEST)
Received: from EUR02-AM5-obe.outbound.protection.outlook.com
 (mail-eopbgr00054.outbound.protection.outlook.com [40.107.0.54])
 by mails.dpdk.org (Postfix) with ESMTP id F008040040
 for <dev@dpdk.org>; Fri,  7 May 2021 04:14:08 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; 
 s=selector2-armh-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=ps/PnMe/8jjX6Kns9wtDbIDkoLXFUB1rXNfJdGBdFgU=;
 b=cJJsKvDMq/GiV7EW3mGAxtE0x9SEXgp28nPQ/a7vNHhZE2/cc4ebePXYfy+0nGbpJ9jLv6l7a4/jsGFAH2784KQcJnUqB0SmB4D8W76nI4dsxQN88X7wKgh/ys88dgBICPP/d0BzOaqN2zhTiLQYmrah3FHHIHWezxY8cdFi1X4=
Received: from AM6P191CA0074.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8a::15)
 by VI1PR0801MB1853.eurprd08.prod.outlook.com (2603:10a6:800:5a::22) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4108.25; Fri, 7 May
 2021 02:14:06 +0000
Received: from VE1EUR03FT031.eop-EUR03.prod.protection.outlook.com
 (2603:10a6:209:8a:cafe::7) by AM6P191CA0074.outlook.office365.com
 (2603:10a6:209:8a::15) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4108.24 via Frontend
 Transport; Fri, 7 May 2021 02:14:06 +0000
X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123)
 smtp.mailfrom=arm.com; dpdk.org; dkim=pass (signature was verified)
 header.d=armh.onmicrosoft.com;dpdk.org; dmarc=pass action=none
 header.from=arm.com;
Received-SPF: Pass (protection.outlook.com: domain of arm.com designates
 63.35.35.123 as permitted sender) receiver=protection.outlook.com;
 client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com;
Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by
 VE1EUR03FT031.mail.protection.outlook.com (10.152.18.69) with
 Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.4108.25 via Frontend Transport; Fri, 7 May 2021 02:14:05 +0000
Received: ("Tessian outbound aff50003470c:v91");
 Fri, 07 May 2021 02:14:05 +0000
X-CR-MTA-TID: 64aa7808
Received: from a7fe3dcceb9d.1
 by 64aa7808-outbound-1.mta.getcheckrecipient.com id
 3233DADA-D221-49C0-A666-01A2ED41B9B1.1; 
 Fri, 07 May 2021 02:13:59 +0000
Received: from EUR01-HE1-obe.outbound.protection.outlook.com
 by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id a7fe3dcceb9d.1
 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384);
 Fri, 07 May 2021 02:13:59 +0000
ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none;
 b=Pmg4b81Ec8ptpPJ58/gr7dNzBl5LMftjVjfQ7B9hmb6r23MQ1nPJOj32YlFwgCCXimsP30jbAqUU9xjWTyxTzeqNHrN63Bpsds/eTSvSJG6psQRDhESuK2dfRhzDhYqiKwe3pvgG4kNrslWf3l/apOrfq3rzL7YwjmpGY0e+KCcQv/N6y2K6OunGuHlFvH7+OoFjjtEyQoMk8trg8cvelIDJMDhFvTXdCDJlOJGtEoTy2FwqxtpsYHAx/R0rzp1mRE2kD6VmMP0uY0RMhzCan5ywxaJ2CJLpBs/VX15qI1Pfszxbe0Do2q0DfQU4ZEcP1EOPms00ZdeJ0aLZI7r9ww==
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-SenderADCheck;
 bh=ps/PnMe/8jjX6Kns9wtDbIDkoLXFUB1rXNfJdGBdFgU=;
 b=JNeRJ3aGF/OL/H3/0dbKOWBug50HGGizY9YHu7568pjAGORMESJZn5RPq0QjP2vJLQsOO7WESroOJO0fXywH3oHUUmw+YJCzEd8x3zUX1MtWIcQCzB36GbCVtxs/gOkg21OuweYXWapK1KRhBq3HOkHVSfZl6yZsBLHCbmCXAc+KXRXC4McrC+L5oag+CFlP4rZ/ucWgAcFjSABer0d8gv3BaTQ84mR8H+nvnLuOl6VopXUAKJwWeyM+glF43hrr1op/Fy7LiWrSTax3OlUR0Azqdu5Vq21jo7Xq7qIEd8OgXJJ2y0TZeVa9yfPH+cvpAuFc/O+V+u3MbynC8LjCPA==
ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass
 smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass
 header.d=arm.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; 
 s=selector2-armh-onmicrosoft-com;
 h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck;
 bh=ps/PnMe/8jjX6Kns9wtDbIDkoLXFUB1rXNfJdGBdFgU=;
 b=cJJsKvDMq/GiV7EW3mGAxtE0x9SEXgp28nPQ/a7vNHhZE2/cc4ebePXYfy+0nGbpJ9jLv6l7a4/jsGFAH2784KQcJnUqB0SmB4D8W76nI4dsxQN88X7wKgh/ys88dgBICPP/d0BzOaqN2zhTiLQYmrah3FHHIHWezxY8cdFi1X4=
Received: from AM8PR08MB5796.eurprd08.prod.outlook.com (2603:10a6:20b:1d1::22)
 by AM9PR08MB6982.eurprd08.prod.outlook.com (2603:10a6:20b:415::16)
 with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4108.25; Fri, 7 May
 2021 02:13:54 +0000
Received: from AM8PR08MB5796.eurprd08.prod.outlook.com
 ([fe80::8dde:97cd:c482:ef11]) by AM8PR08MB5796.eurprd08.prod.outlook.com
 ([fe80::8dde:97cd:c482:ef11%4]) with mapi id 15.20.4108.029; Fri, 7 May 2021
 02:13:53 +0000
From: Richael Zhuang <Richael.Zhuang@arm.com>
To: Anatoly Burakov <anatoly.burakov@intel.com>, "dev@dpdk.org" <dev@dpdk.org>
CC: "stephen@networkplumber.org" <stephen@networkplumber.org>,
 "reshma.pattan@intel.com" <reshma.pattan@intel.com>, "david.hunt@intel.com"
 <david.hunt@intel.com>, nd <nd@arm.com>, nd <nd@arm.com>
Thread-Topic: [21.08 PATCH v4 2/2] power: refactor pstate and acpi code
Thread-Index: AQHXODBTxcRsGnSTxEuNCcgPn/DZ1arXVyPg
Date: Fri, 7 May 2021 02:13:53 +0000
Message-ID: <AM8PR08MB5796E0FB8FB2D995966913B292579@AM8PR08MB5796.eurprd08.prod.outlook.com>
References: <2fae7084c99dc8223e8d3a0aa9c2e5ba18bf1e6b.1619104102.git.anatoly.burakov@intel.com>
 <83b1e89d14834251d4d7e72fcc19d82dfb52686d.1619175790.git.anatoly.burakov@intel.com>
In-Reply-To: <83b1e89d14834251d4d7e72fcc19d82dfb52686d.1619175790.git.anatoly.burakov@intel.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-ts-tracking-id: E5D8664FB4879B4FB5755E29EBC553F4.0
x-checkrecipientchecked: true
Authentication-Results-Original: intel.com; dkim=none (message not signed)
 header.d=none;intel.com; dmarc=none action=none header.from=arm.com;
x-originating-ip: [203.126.0.112]
x-ms-publictraffictype: Email
X-MS-Office365-Filtering-Correlation-Id: 7774e08c-f37b-49d4-f0c4-08d910fdca8b
x-ms-traffictypediagnostic: AM9PR08MB6982:|VI1PR0801MB1853:
X-Microsoft-Antispam-PRVS: <VI1PR0801MB1853AD0C5E65FD2EDB82866292579@VI1PR0801MB1853.eurprd08.prod.outlook.com>
x-checkrecipientrouted: true
nodisclaimer: true
x-ms-oob-tlc-oobclassifiers: OLM:510;OLM:510;
X-MS-Exchange-SenderADCheck: 1
X-Microsoft-Antispam-Untrusted: BCL:0;
X-Microsoft-Antispam-Message-Info-Original: RlEP/9cKT5b/n2Uer8KHAQfxYBx3nkSdrJbLQPydoLXflnzlIHai+G1FZdkcw7BBxSqO1neDqLgDe/RajBQ+1HDYKeQWf1N6VXm1rxgIF58Evr/aQ4PrEdt3uzKIfzv7aWN+lQ4XsgjyXu80PRPBm6W9B8198rdtOMAx+rjrgs1UDh1p5qybRozRakr1T5qVfERzEJ/ea1zaxXAbh/1N7QySBTsECbQv0IIqVjyOmA9rbfMJ59Y434EpGjxIakh679LSj2H2TaVQKUBHt9RmDJGSJ705RO2fu607A+YuuNw8cd2HPVvKMRU2JR8y8V7VsuFPlyRWnDb1HlXxPAPoNbdhgWdoSfEAV8HnNawEHz6IKZESY95oR93srDDPHD/xFlTjUaQtM5aUxaYK6kNsdGyt+MlSUH39F/XNlIshZy8I5oijGl52oh4bCJrThZNy3dYS2vKglPltRErAePSpqsjqwBPOoVhDAXhIHVQm7E3V52mVXRjqP8/jk8pS1jL6I4+M6mUXLO2VEwZPnPFQ5cTjK0eBHW2QQt/bJU5UewQpBORLBbadySa5h/NjsvtzYpZwjQFo8HsZJqCyGn6VJKP6nEqJ7+JyhR9nMSPjos8=
X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en;
 SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM8PR08MB5796.eurprd08.prod.outlook.com;
 PTR:; CAT:NONE;
 SFS:(4636009)(136003)(346002)(39850400004)(396003)(366004)(376002)(66946007)(55016002)(71200400001)(54906003)(122000001)(110136005)(9686003)(6506007)(38100700002)(26005)(478600001)(66446008)(64756008)(66556008)(66476007)(316002)(53546011)(186003)(8676002)(2906002)(83380400001)(76116006)(7696005)(4326008)(33656002)(5660300002)(52536014)(30864003)(86362001)(8936002)(559001)(579004);
 DIR:OUT; SFP:1101; 
x-ms-exchange-antispam-messagedata: BXrF0biyzb058ICKqySUqZZcBOihj2VNFFZ3PnH0JRZzoR/2rjejRYtCkUBk5NwuY3Y+6lQ8GpXvDIafGKpYc70z3iEyBYxMHK5JUeqp1WiLzn67v3Zj1NFuBGyEiUtvRXH98dY3MH1Cthl2t/NroSGvFXlwkDd5hC8lQayePiv0kUPstJsZPdTksnGY9g6nNhNd2azrBvxhBwSbr9WEvWwLuwc7Uro1we9fOejSoimysI2lFiwVMh4YG+07sR9nSsz9/btY4OV9IoZSLK09UFPOcEh+zZ5o/bRX/tTfi/2VBlSP6QydlZm4bjnhv1ZG52iuOwlj4TAqTkKP7sONUDZzTYu6iPamz+QJvdFklOSE6EQajm0FDsfMgWIMBC+HkTa/1nUJSD+5yYd7bVjPP+EL+sz0UbgSMOfO7F4Ir36lDPUZkm4jGWXuX1DWt6GRKwnY2Bv81HGKyE3tz0XuPUcESUNeUkkPuvdTIL9XFSLpzFpq5ynGglDyr9J/fkkCmbAT07tis/sDtrEXbV7F2mNpx3CV01RpcmJ3X++wPsg/9g+WPXESgJIiR4DgYy4pF1BIMKY5y5jNdm3VNz0fr4z2xHLt0XAa9J99/DD19l+AXPuW+flv+atZEhCf++NQRuLtbQvMWgQDj/VKhU00f0KlxSBXJiO8MXCC7Hhmk0ti0DbuDKbR8jB2MhEaftDYKPxGGJOEJBV9N+EOjM9/e49lHA0olCbMEfmLXRGORRI=
x-ms-exchange-transport-forked: True
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0
X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB6982
Original-Authentication-Results: intel.com; dkim=none (message not signed)
 header.d=none;intel.com; dmarc=none action=none header.from=arm.com;
X-EOPAttributedMessage: 0
X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT031.eop-EUR03.prod.protection.outlook.com
X-MS-Office365-Filtering-Correlation-Id-Prvs: 0bef8ce5-e33b-466f-edc8-08d910fdc34a
X-Microsoft-Antispam: BCL:0;
X-Microsoft-Antispam-Message-Info: q1mrYkHRCpdS3jXTKIY9O6L8oEqFGTbY10k8n6kEaSXmYGvTr5YzeWz56Tfwao529MR3KxaqCAO4gOFdkbkz59wqN0UXepvASgxv+SQoB3cS9uYfZEQs0wVAPBI6wIrb0UZlqtSc+9MVyhKdGMYw2SS2BvNLFPg5hjESoVpciUEY4kIUujNAvSkVD+GNWOUnjUTvTQwvndW9Xpx2292Q+UqxBoeCXl+HhgI/b7dFqTl2MPbH1Gs6o1G4UYyQL+xSPMZrxCNiWSPILRZBCZrDtT4a9pAc/PpSBfBjUFZb0d42dx4SIGyUYTdwWlnrBDnqh0PFDt0akt/BFQXIB0NEI04Un4qxn+hrMGLWHnl4rPMYtyZ7OWDH//5YfZ0DZ5cRa6zwvYZsI2QlULQwtdcg2douEUYevdcit9TYTsY+5mPYJpFSp+/dcwBRH6850+lltxNe+2i8MotKA8XcbaPHRUq7kMhZLadiK/qo0Gpt51lrC3seH4Jp1vjkxviio29ruR41/v/S5iiif8qGwqGCChkap/HSECZK2dpa4k+MLe7MAOcFEEG7SXe1vNSoy60uDrbglRSm46oM3beJRMISDhz0lTi3R96CDngkqcKqSVwVvdJZqfeW8Xky5RS4LK485FuAkRZuNdPPWStBpbimqo8PbcljRpLPmZUvNxk/M6E=
X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:;
 IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com;
 PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE;
 SFS:(4636009)(346002)(136003)(39850400004)(376002)(396003)(46966006)(36840700001)(82740400003)(70586007)(47076005)(9686003)(81166007)(70206006)(30864003)(54906003)(4326008)(356005)(110136005)(316002)(55016002)(82310400003)(86362001)(8676002)(53546011)(83380400001)(478600001)(33656002)(36860700001)(52536014)(26005)(6506007)(5660300002)(8936002)(7696005)(186003)(336012)(2906002);
 DIR:OUT; SFP:1101; 
X-OriginatorOrg: arm.com
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 May 2021 02:14:05.8865 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: 7774e08c-f37b-49d4-f0c4-08d910fdca8b
X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d
X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123];
 Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com]
X-MS-Exchange-CrossTenant-AuthSource: VE1EUR03FT031.eop-EUR03.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem
X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0801MB1853
Subject: Re: [dpdk-dev] [21.08 PATCH v4 2/2] power: refactor pstate and acpi
 code
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>



> -----Original Message-----
> From: Anatoly Burakov <anatoly.burakov@intel.com>
> Sent: Friday, April 23, 2021 7:03 PM
> To: dev@dpdk.org
> Cc: stephen@networkplumber.org; Richael Zhuang
> <Richael.Zhuang@arm.com>; reshma.pattan@intel.com;
> david.hunt@intel.com
> Subject: [21.08 PATCH v4 2/2] power: refactor pstate and acpi code
>=20
> Currently, ACPI and PSTATE modes have lots of code duplication, confusing
> logic, and a bunch of other issues that can, and have, led to various bug=
s and
> resource leaks.
>=20
> This commit factors out the common parts of sysfs reading/writing for ACP=
I
> and PSTATE drivers.
>=20
> Signed-off-by: Anatoly Burakov <anatoly.burakov@intel.com>
> ---
>  lib/power/meson.build            |   7 +
>  lib/power/power_acpi_cpufreq.c   | 178 +++--------------
>  lib/power/power_common.c         | 133 +++++++++++++
>  lib/power/power_common.h         |  46 +++++
>  lib/power/power_pstate_cpufreq.c | 332 ++++++++-----------------------
>  5 files changed, 293 insertions(+), 403 deletions(-)
>=20
> diff --git a/lib/power/meson.build b/lib/power/meson.build index
> a2cc9fe2ef..85324d48d2 100644
> --- a/lib/power/meson.build
> +++ b/lib/power/meson.build
> @@ -5,6 +5,13 @@ if not is_linux
>      build =3D false
>      reason =3D 'only supported on Linux'
>  endif
> +
> +# we do some snprintf magic so silence format-nonliteral
> +flag_nonliteral =3D '-Wno-format-nonliteral'
> +if cc.has_argument(flag_nonliteral)
> +	cflags +=3D flag_nonliteral
> +endif
> +
>  sources =3D files(
>          'guest_channel.c',
>          'power_acpi_cpufreq.c',
> diff --git a/lib/power/power_acpi_cpufreq.c
> b/lib/power/power_acpi_cpufreq.c index 1b8c69cc8b..97f1d302c9 100644
> --- a/lib/power/power_acpi_cpufreq.c
> +++ b/lib/power/power_acpi_cpufreq.c
> @@ -19,41 +19,10 @@
>  #include "power_acpi_cpufreq.h"
>  #include "power_common.h"
>=20
> -#ifdef RTE_LIBRTE_POWER_DEBUG
> -#define POWER_DEBUG_TRACE(fmt, args...) do { \
> -		RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
> -} while (0)
> -#else
> -#define POWER_DEBUG_TRACE(fmt, args...) -#endif
> -
> -#define FOPEN_OR_ERR_RET(f, retval) do { \
> -		if ((f) =3D=3D NULL) { \
> -			RTE_LOG(ERR, POWER, "File not opened\n"); \
> -			return retval; \
> -		} \
> -} while (0)
> -
> -#define FOPS_OR_NULL_GOTO(ret, label) do { \
> -		if ((ret) =3D=3D NULL) { \
> -			RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
> -			goto label; \
> -		} \
> -} while (0)
> -
> -#define FOPS_OR_ERR_GOTO(ret, label) do { \
> -		if ((ret) < 0) { \
> -			RTE_LOG(ERR, POWER, "File operations failed\n"); \
> -			goto label; \
> -		} \
> -} while (0)
> -
>  #define STR_SIZE     1024
>  #define POWER_CONVERT_TO_DECIMAL 10
>=20
>  #define POWER_GOVERNOR_USERSPACE "userspace"
> -#define POWER_SYSFILE_GOVERNOR   \
> -		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
>  #define POWER_SYSFILE_AVAIL_FREQ \
>=20
> 	"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequen
> cies"
>  #define POWER_SYSFILE_SETSPEED   \
> @@ -135,53 +104,18 @@ set_freq_internal(struct acpi_power_info *pi,
> uint32_t idx)  static int  power_set_governor_userspace(struct
> acpi_power_info *pi)  {
> -	FILE *f;
> -	int ret =3D -1;
> -	char buf[BUFSIZ];
> -	char fullpath[PATH_MAX];
> -	char *s;
> -	int val;
> -
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "rw+");
> -	FOPEN_OR_ERR_RET(f, ret);
> -
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> -	/* Strip off terminating '\n' */
> -	strtok(buf, "\n");
> -
> -	/* Save the original governor */
> -	rte_strscpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
> -
> -	/* Check if current governor is userspace */
> -	if (strncmp(buf, POWER_GOVERNOR_USERSPACE,
> -			sizeof(POWER_GOVERNOR_USERSPACE)) =3D=3D 0) {
> -		ret =3D 0;
> -		POWER_DEBUG_TRACE("Power management governor of
> lcore %u is "
> -				"already userspace\n", pi->lcore_id);
> -		goto out;
> -	}
> -
> -	/* Write 'userspace' to the governor */
> -	val =3D fseek(f, 0, SEEK_SET);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	val =3D fputs(POWER_GOVERNOR_USERSPACE, f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	/* We need to flush to see if the fputs succeeds */
> -	val =3D fflush(f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	ret =3D 0;
> -	RTE_LOG(INFO, POWER, "Power management governor of lcore %u
> has been "
> -			"set to user space successfully\n", pi->lcore_id);
> -out:
> -	fclose(f);
> -
> -	return ret;
> +	return power_set_governor(pi->lcore_id,
> POWER_GOVERNOR_USERSPACE,
> +			pi->governor_ori, sizeof(pi->governor_ori)); }
> +
> +/**
> + * It is to check the governor and then set the original governor back
> +if
> + * needed by writing the sys file.
> + */
> +static int
> +power_set_governor_original(struct acpi_power_info *pi) {
> +	return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
>  }
>=20
>  /**
> @@ -195,22 +129,14 @@ power_get_available_freqs(struct
> acpi_power_info *pi)
>  	int ret =3D -1, i, count;
>  	char *p;
>  	char buf[BUFSIZ];
> -	char fullpath[PATH_MAX];
>  	char *freqs[RTE_MAX_LCORE_FREQS];
> -	char *s;
>=20
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "r");
> -	FOPEN_OR_ERR_RET(f, ret);
> +	open_core_sysfs_file(POWER_SYSFILE_AVAIL_FREQ, pi->lcore_id, "r",
> +			&f);
> +	FOPEN_OR_ERR_GOTO(f, out);
>=20
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> -
> -	/* Strip the line break if there is */
> -	p =3D strchr(buf, '\n');
> -	if (p !=3D NULL)
> -		*p =3D 0;
> +	ret =3D read_core_sysfs_s(f, buf, sizeof(buf));
> +	FOPS_OR_ERR_GOTO(ret, out);
>=20
>  	/* Split string into at most RTE_MAX_LCORE_FREQS frequencies */
>  	count =3D rte_strsplit(buf, sizeof(buf), freqs, @@ -250,7 +176,8 @@
> power_get_available_freqs(struct acpi_power_info *pi)
>  	POWER_DEBUG_TRACE("%d frequency(s) of lcore %u are
> available\n",
>  			count, pi->lcore_id);
>  out:
> -	fclose(f);
> +	if (f !=3D NULL)
> +		fclose(f);
>=20
>  	return ret;
>  }
> @@ -262,18 +189,16 @@ static int
>  power_init_for_setting_freq(struct acpi_power_info *pi)  {
>  	FILE *f;
> -	char fullpath[PATH_MAX];
>  	char buf[BUFSIZ];
>  	uint32_t i, freq;
> -	char *s;
> +	int ret;
>=20
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "rw+");
> -	FOPEN_OR_ERR_RET(f, -1);
> +	open_core_sysfs_file(POWER_SYSFILE_SETSPEED, pi->lcore_id, "r",
> +			&f);
Hi Anatoly,
I tried to verify your patch on my arm platform,  and I found several bugs.

Here it should be "rw+", for it will write freq to POWER_SYSFILE_SETSPEED l=
ater.

Best Regards,
Richael
> +	FOPEN_OR_ERR_GOTO(f, err);
>=20
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> +	ret =3D read_core_sysfs_s(f, buf, sizeof(buf));
> +	FOPS_OR_ERR_GOTO(ret, err);
>=20
>  	freq =3D strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
>  	for (i =3D 0; i < pi->nb_freqs; i++) {
> @@ -284,8 +209,9 @@ power_init_for_setting_freq(struct acpi_power_info
> *pi)
>  		}
>  	}
>=20
> -out:
> -	fclose(f);
> +err:
> +	if (f !=3D NULL)
> +		fclose(f);
>=20
>  	return -1;
>  }
> @@ -369,54 +295,6 @@ power_acpi_cpufreq_init(unsigned int lcore_id)
>  	return -1;
>  }
>=20
> -/**
> - * It is to check the governor and then set the original governor back i=
f
> - * needed by writing the sys file.
> - */
> -static int
> -power_set_governor_original(struct acpi_power_info *pi) -{
> -	FILE *f;
> -	int ret =3D -1;
> -	char buf[BUFSIZ];
> -	char fullpath[PATH_MAX];
> -	char *s;
> -	int val;
> -
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "rw+");
> -	FOPEN_OR_ERR_RET(f, ret);
> -
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> -
> -	/* Check if the governor to be set is the same as current */
> -	if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) =3D=3D 0) =
{
> -		ret =3D 0;
> -		POWER_DEBUG_TRACE("Power management governor of
> lcore %u "
> -				"has already been set to %s\n",
> -				pi->lcore_id, pi->governor_ori);
> -		goto out;
> -	}
> -
> -	/* Write back the original governor */
> -	val =3D fseek(f, 0, SEEK_SET);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	val =3D fputs(pi->governor_ori, f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	ret =3D 0;
> -	RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
> -			"has been set back to %s successfully\n",
> -			pi->lcore_id, pi->governor_ori);
> -out:
> -	fclose(f);
> -
> -	return ret;
> -}
> -
>  int
>  power_acpi_cpufreq_exit(unsigned int lcore_id)  { diff --git
> a/lib/power/power_common.c b/lib/power/power_common.c index
> 67e3318ec7..f06e9e5fa9 100644
> --- a/lib/power/power_common.c
> +++ b/lib/power/power_common.c
> @@ -3,13 +3,20 @@
>   */
>=20
>  #include <limits.h>
> +#include <stdlib.h>
>  #include <stdio.h>
>  #include <string.h>
>=20
> +#include <rte_log.h>
> +#include <rte_string_fns.h>
> +
>  #include "power_common.h"
>=20
>  #define POWER_SYSFILE_SCALING_DRIVER   \
>  		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
> +#define POWER_SYSFILE_GOVERNOR  \
> +		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
> +#define POWER_CONVERT_TO_DECIMAL 10
>=20
>  int
>  cpufreq_check_scaling_driver(const char *driver_name) @@ -58,3 +65,129
> @@ cpufreq_check_scaling_driver(const char *driver_name)
>  	 */
>  	return 1;
>  }
> +
> +int
> +open_core_sysfs_file(const char *template, unsigned int core, const char
> *mode,
> +		FILE **f)
> +{
> +	char fullpath[PATH_MAX];
> +	FILE *tmpf;
> +
> +	/* silenced -Wformat-nonliteral here */
> +	snprintf(fullpath, sizeof(fullpath), template, core);
> +	tmpf =3D fopen(fullpath, mode);
> +	if (tmpf =3D=3D NULL)
> +		return -1;
> +	*f =3D tmpf;

When the file that open_core_sysfs_file() opens doesn't exist, there's segm=
entation fault.
Moving *f=3Dtmpf above runs OK.

> +
> +	return 0;
> +}
> +
> +int
> +read_core_sysfs_u32(FILE *f, uint32_t *val) {
> +	char buf[BUFSIZ];
> +	uint32_t fval;
> +	char *s;
> +
> +	s =3D fgets(buf, sizeof(buf), f);
> +	if (s =3D=3D NULL)
> +		return -1;
> +
> +	/* fgets puts null terminator in, but do this just in case */
> +	buf[BUFSIZ - 1] =3D '\0';
> +
> +	/* strip off any terminating newlines */
> +	*strchrnul(buf, '\n') =3D '\0';
> +
> +	fval =3D strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL);
> +
> +	/* write the value */
> +	*val =3D fval;
> +
> +	return 0;
> +}
> +
> +int
> +read_core_sysfs_s(FILE *f, char *buf, unsigned int len) {
> +	char *s;
> +
> +	s =3D fgets(buf, len, f);
> +	if (s =3D=3D NULL)
> +		return -1;
> +
> +	/* fgets puts null terminator in, but do this just in case */
> +	buf[len - 1] =3D '\0';
> +
> +	/* strip off any terminating newlines */
> +	*strchrnul(buf, '\n') =3D '\0';
> +
> +	return 0;
> +}
> +
> +int
> +write_core_sysfs_s(FILE *f, const char *str) {
> +	int ret;
> +
> +	ret =3D fseek(f, 0, SEEK_SET);
> +	if (ret !=3D 0)
> +		return -1;
> +
> +	ret =3D fputs(str, f);
> +	if (ret !=3D 0)

ret >=3D0 if success, EOF means failure.

> +		return -1;
> +
> +	/* flush the output */
> +	ret =3D fflush(f);
> +	if (ret !=3D 0)
> +		return -1;
> +
> +	return 0;
> +}
> +
> +/**
> + * It is to check the current scaling governor by reading sys file, and
> +then
> + * set it into 'performance' if it is not by writing the sys file. The
> +original
> + * governor will be saved for rolling back.
> + */
> +int
> +power_set_governor(unsigned int lcore_id, const char *new_governor,
> +		char *orig_governor, size_t orig_governor_len) {
> +	FILE *f_governor =3D NULL;
> +	int ret =3D -1;
> +	char buf[BUFSIZ];
> +
> +	open_core_sysfs_file(POWER_SYSFILE_GOVERNOR, lcore_id, "rw+",
> &f_governor);
> +	FOPEN_OR_ERR_GOTO(f_governor, out);
> +
> +	ret =3D read_core_sysfs_s(f_governor, buf, sizeof(buf));
> +	FOPS_OR_ERR_GOTO(ret, out);
> +
> +	/* Save the original governor, if it was provided */
> +	if (orig_governor)
> +		rte_strscpy(orig_governor, buf, orig_governor_len);
> +
> +	/* Check if current governor is already what we want */
> +	if (strcmp(buf, new_governor) =3D=3D 0) {
> +		ret =3D 0;
> +		POWER_DEBUG_TRACE("Power management governor of
> lcore %u is "
> +				"already %s\n", lcore_id, new_governor);
> +		goto out;
> +	}
> +
> +	/* Write the new governor */
> +	ret =3D write_core_sysfs_s(f_governor, new_governor);
> +	FOPS_OR_ERR_GOTO(ret, out);
> +
> +	ret =3D 0;
> +	RTE_LOG(INFO, POWER, "Power management governor of lcore %u
> has been "
> +			"set to '%s' successfully\n", lcore_id, new_governor);
> +out:
> +	if (f_governor !=3D NULL)
> +		fclose(f_governor);
> +
> +	return ret;
> +}
> diff --git a/lib/power/power_common.h b/lib/power/power_common.h
> index fab3ca995a..729ae8cbf4 100644
> --- a/lib/power/power_common.h
> +++ b/lib/power/power_common.h
> @@ -5,9 +5,55 @@
>  #ifndef _POWER_COMMON_H_
>  #define _POWER_COMMON_H_
>=20
> +#include <inttypes.h>
> +
>  #define RTE_POWER_INVALID_FREQ_INDEX (~0)
>=20
> +
> +#ifdef RTE_LIBRTE_POWER_DEBUG
> +#define POWER_DEBUG_TRACE(fmt, args...) do { \
> +		RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \ }
> while (0)
> +#else #define POWER_DEBUG_TRACE(fmt, args...) #endif
> +
> +#define FOPEN_OR_ERR_RET(f, retval) do { \
> +		if ((f) =3D=3D NULL) { \
> +			RTE_LOG(ERR, POWER, "File not opened\n"); \
> +			return retval; \
> +		} \
> +} while (0)
> +
> +#define FOPEN_OR_ERR_GOTO(f, label) do { \
> +		if ((f) =3D=3D NULL) { \
> +			RTE_LOG(ERR, POWER, "File not opened\n"); \
> +			goto label; \
> +		} \
> +} while (0)
> +
> +#define FOPS_OR_NULL_GOTO(ret, label) do { \
> +		if ((ret) =3D=3D NULL) { \
> +			RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
> +			goto label; \
> +		} \
> +} while (0)
> +
> +#define FOPS_OR_ERR_GOTO(ret, label) do { \
> +		if ((ret) < 0) { \
> +			RTE_LOG(ERR, POWER, "File operations failed\n"); \
> +			goto label; \
> +		} \
> +} while (0)
> +
>  /* check if scaling driver matches one we want */  int
> cpufreq_check_scaling_driver(const char *driver);
> +int power_set_governor(unsigned int lcore_id, const char *new_governor,
> +		char *orig_governor, size_t orig_governor_len); int
> +open_core_sysfs_file(const char *template, unsigned int core,
> +			 const char *mode, FILE **f);
> +int read_core_sysfs_u32(FILE *f, uint32_t *val); int
> +read_core_sysfs_s(FILE *f, char *buf, unsigned int len); int
> +write_core_sysfs_s(FILE *f, const char *str);
>=20
>  #endif /* _POWER_COMMON_H_ */
> diff --git a/lib/power/power_pstate_cpufreq.c
> b/lib/power/power_pstate_cpufreq.c
> index 2cfc54acf3..2a365988a9 100644
> --- a/lib/power/power_pstate_cpufreq.c
> +++ b/lib/power/power_pstate_cpufreq.c
> @@ -21,46 +21,13 @@
>  #include "power_pstate_cpufreq.h"
>  #include "power_common.h"
>=20
> -
> -#ifdef RTE_LIBRTE_POWER_DEBUG
> -#define POWER_DEBUG_TRACE(fmt, args...) do { \
> -		RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \
> -} while (0)
> -#else
> -#define POWER_DEBUG_TRACE(fmt, args...) -#endif
> -
> -#define FOPEN_OR_ERR_RET(f, retval) do { \
> -		if ((f) =3D=3D NULL) { \
> -			RTE_LOG(ERR, POWER, "File not opened\n"); \
> -			return retval; \
> -		} \
> -} while (0)
> -
> -#define FOPS_OR_NULL_GOTO(ret, label) do { \
> -		if ((ret) =3D=3D NULL) { \
> -			RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \
> -			goto label; \
> -		} \
> -} while (0)
> -
> -#define FOPS_OR_ERR_GOTO(ret, label) do { \
> -		if ((ret) < 0) { \
> -			RTE_LOG(ERR, POWER, "File operations failed\n"); \
> -			goto label; \
> -		} \
> -} while (0)
> -
>  /* macros used for rounding frequency to nearest 100000 */  #define
> FREQ_ROUNDING_DELTA 50000  #define ROUND_FREQ_TO_N_100000
> 100000
>=20
> -#define POWER_CONVERT_TO_DECIMAL 10
>  #define BUS_FREQ     100000
>=20
>  #define POWER_GOVERNOR_PERF "performance"
> -#define POWER_SYSFILE_GOVERNOR  \
> -		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor"
>  #define POWER_SYSFILE_MAX_FREQ \
>=20
> 	"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_max_freq"
>  #define POWER_SYSFILE_MIN_FREQ  \
> @@ -154,91 +121,58 @@ out:	close(fd);
>  static int
>  power_init_for_setting_freq(struct pstate_power_info *pi)  {
> -	FILE *f_min, *f_max, *f_base =3D NULL, *f_base_max;
> -	char fullpath_min[PATH_MAX];
> -	char fullpath_max[PATH_MAX];
> -	char fullpath_base[PATH_MAX];
> -	char fullpath_base_max[PATH_MAX];
> -	char buf_base[BUFSIZ];
> -	char *s_base;
> -	char *s_base_max;
> -	uint32_t base_ratio =3D 0;
> -	uint32_t base_max_ratio =3D 0;
> -	uint64_t max_non_turbo =3D 0;
> -	int  ret_val =3D 0;
> -
> -	snprintf(fullpath_base_max,
> -			sizeof(fullpath_base_max),
> -			POWER_SYSFILE_BASE_MAX_FREQ,
> -			pi->lcore_id);
> -	f_base_max =3D fopen(fullpath_base_max, "r");
> -	FOPEN_OR_ERR_RET(f_base_max, -1);
> -	if (f_base_max !=3D NULL) {
> -		s_base_max =3D fgets(buf_base, sizeof(buf_base),
> f_base_max);
> -
> -		/* close the file unconditionally */
> -		fclose(f_base_max);
> -		f_base_max =3D NULL;
> -
> -		FOPS_OR_NULL_GOTO(s_base_max, out);
> -
> -		buf_base[BUFSIZ-1] =3D '\0';
> -		if (strlen(buf_base))
> -			/* Strip off terminating '\n' */
> -			strtok(buf_base, "\n");
> -
> -		base_max_ratio =3D
> -			strtoul(buf_base, NULL,
> POWER_CONVERT_TO_DECIMAL)
> -				/ BUS_FREQ;
> -	}
> -
> -	snprintf(fullpath_min, sizeof(fullpath_min),
> POWER_SYSFILE_MIN_FREQ,
> -			pi->lcore_id);
> -	f_min =3D fopen(fullpath_min, "rw+");
> -	FOPEN_OR_ERR_RET(f_min, -1);
> -
> -	snprintf(fullpath_max, sizeof(fullpath_max),
> POWER_SYSFILE_MAX_FREQ,
> -			pi->lcore_id);
> -	f_max =3D fopen(fullpath_max, "rw+");
> -	if (f_max =3D=3D NULL)
> -		fclose(f_min);
> -	FOPEN_OR_ERR_RET(f_max, -1);
> -
> -	pi->f_cur_min =3D f_min;
> -	pi->f_cur_max =3D f_max;
> -
> -	snprintf(fullpath_base, sizeof(fullpath_base),
> POWER_SYSFILE_BASE_FREQ,
> -			pi->lcore_id);
> -
> -	f_base =3D fopen(fullpath_base, "r");
> -	if (f_base =3D=3D NULL) {
> -		/* No sysfs base_frequency, that's OK, continue without */
> -		base_ratio =3D 0;
> +	FILE *f_base =3D NULL, *f_base_max =3D NULL, *f_min =3D NULL, *f_max =
=3D
> NULL;
> +	uint32_t base_ratio, base_max_ratio;
> +	uint64_t max_non_turbo;
> +	int ret;
> +
> +	/* open all files we expect to have open */
> +	open_core_sysfs_file(POWER_SYSFILE_BASE_MAX_FREQ, pi-
> >lcore_id, "r",
> +			&f_base_max);
> +	FOPEN_OR_ERR_GOTO(f_base_max, err);
> +
> +	open_core_sysfs_file(POWER_SYSFILE_MIN_FREQ, pi->lcore_id,
> "rw+",
> +			&f_min);
> +	FOPEN_OR_ERR_GOTO(f_min, err);
> +
> +	open_core_sysfs_file(POWER_SYSFILE_MAX_FREQ, pi->lcore_id,
> "rw+",
> +			&f_max);
> +	FOPEN_OR_ERR_GOTO(f_max, err);
> +
> +	open_core_sysfs_file(POWER_SYSFILE_BASE_FREQ, pi->lcore_id, "r",
> +			&f_base);
> +	/* base ratio file may not exist in some kernels, so no error check */
> +
> +	/* read base max ratio */
> +	ret =3D read_core_sysfs_u32(f_base_max, &base_max_ratio);
> +	FOPS_OR_ERR_GOTO(ret, err);
> +
> +	/* base ratio may not exist */
> +	if (f_base !=3D NULL) {
> +		ret =3D read_core_sysfs_u32(f_base, &base_ratio);
> +		FOPS_OR_ERR_GOTO(ret, err);
>  	} else {
> -		s_base =3D fgets(buf_base, sizeof(buf_base), f_base);
> -		FOPS_OR_NULL_GOTO(s_base, out);
> -
> -		buf_base[BUFSIZ-1] =3D '\0';
> -		if (strlen(buf_base))
> -			/* Strip off terminating '\n' */
> -			strtok(buf_base, "\n");
> -
> -		base_ratio =3D strtoul(buf_base, NULL,
> POWER_CONVERT_TO_DECIMAL)
> -				/ BUS_FREQ;
> +		base_ratio =3D 0;
>  	}
>=20
>  	/* Add MSR read to detect turbo status */
> +	if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) <
> 0)
> +		goto err;
> +	/* no errors after this point */
>=20
> -	if (power_rdmsr(PLATFORM_INFO, &max_non_turbo, pi->lcore_id) <
> 0) {
> -		ret_val =3D -1;
> -		goto out;
> -	}
> +	/* convert ratios to bins */
> +	base_max_ratio /=3D BUS_FREQ;
> +	base_ratio /=3D BUS_FREQ;
> +
> +	/* assign file handles */
> +	pi->f_cur_min =3D f_min;
> +	pi->f_cur_max =3D f_max;
>=20
>  	max_non_turbo =3D
> (max_non_turbo&NON_TURBO_MASK)>>NON_TURBO_OFFSET;
>=20
>  	POWER_DEBUG_TRACE("no turbo perf %"PRIu64"\n",
> max_non_turbo);
>=20
> -	pi->non_turbo_max_ratio =3D max_non_turbo;
> +	pi->non_turbo_max_ratio =3D (uint32_t)max_non_turbo;
>=20
>  	/*
>  	 * If base_frequency is reported as greater than the maximum @@ -
> 264,7 +198,20 @@ power_init_for_setting_freq(struct pstate_power_info
> *pi)
>  out:
>  	if (f_base !=3D NULL)
>  		fclose(f_base);
> -	return ret_val;
> +	fclose(f_base_max);
> +	/* f_min and f_max are stored, no need to close */
> +	return 0;
> +
> +err:
> +	if (f_base !=3D NULL)
> +		fclose(f_base);
> +	if (f_base_max !=3D NULL)
> +		fclose(f_base_max);
> +	if (f_min !=3D NULL)
> +		fclose(f_min);
> +	if (f_max !=3D NULL)
> +		fclose(f_max);
> +	return -1;
>  }
>=20
>  static int
> @@ -369,53 +316,8 @@ set_freq_internal(struct pstate_power_info *pi,
> uint32_t idx)  static int  power_set_governor_performance(struct
> pstate_power_info *pi)  {
> -	FILE *f;
> -	int ret =3D -1;
> -	char buf[BUFSIZ];
> -	char fullpath[PATH_MAX];
> -	char *s;
> -	int val;
> -
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "rw+");
> -	FOPEN_OR_ERR_RET(f, ret);
> -
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> -	/* Strip off terminating '\n' */
> -	strtok(buf, "\n");
> -
> -	/* Save the original governor */
> -	rte_strscpy(pi->governor_ori, buf, sizeof(pi->governor_ori));
> -
> -	/* Check if current governor is performance */
> -	if (strncmp(buf, POWER_GOVERNOR_PERF,
> -			sizeof(POWER_GOVERNOR_PERF)) =3D=3D 0) {
> -		ret =3D 0;
> -		POWER_DEBUG_TRACE("Power management governor of
> lcore %u is "
> -				"already performance\n", pi->lcore_id);
> -		goto out;
> -	}
> -
> -	/* Write 'performance' to the governor */
> -	val =3D fseek(f, 0, SEEK_SET);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	val =3D fputs(POWER_GOVERNOR_PERF, f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	/* We need to flush to see if the fputs succeeds */
> -	val =3D fflush(f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	ret =3D 0;
> -	RTE_LOG(INFO, POWER, "Power management governor of lcore %u
> has been "
> -			"set to performance successfully\n", pi->lcore_id);
> -out:
> -	fclose(f);
> -
> -	return ret;
> +	return power_set_governor(pi->lcore_id, POWER_GOVERNOR_PERF,
> +			pi->governor_ori, sizeof(pi->governor_ori));
>  }
>=20
>  /**
> @@ -425,45 +327,7 @@ power_set_governor_performance(struct
> pstate_power_info *pi)  static int  power_set_governor_original(struct
> pstate_power_info *pi)  {
> -	FILE *f;
> -	int ret =3D -1;
> -	char buf[BUFSIZ];
> -	char fullpath[PATH_MAX];
> -	char *s;
> -	int val;
> -
> -	snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR,
> -			pi->lcore_id);
> -	f =3D fopen(fullpath, "rw+");
> -	FOPEN_OR_ERR_RET(f, ret);
> -
> -	s =3D fgets(buf, sizeof(buf), f);
> -	FOPS_OR_NULL_GOTO(s, out);
> -
> -	/* Check if the governor to be set is the same as current */
> -	if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) =3D=3D 0) =
{
> -		ret =3D 0;
> -		POWER_DEBUG_TRACE("Power management governor of
> lcore %u "
> -				"has already been set to %s\n",
> -				pi->lcore_id, pi->governor_ori);
> -		goto out;
> -	}
> -
> -	/* Write back the original governor */
> -	val =3D fseek(f, 0, SEEK_SET);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	val =3D fputs(pi->governor_ori, f);
> -	FOPS_OR_ERR_GOTO(val, out);
> -
> -	ret =3D 0;
> -	RTE_LOG(INFO, POWER, "Power management governor of lcore %u "
> -			"has been set back to %s successfully\n",
> -			pi->lcore_id, pi->governor_ori);
> -out:
> -	fclose(f);
> -
> -	return ret;
> +	return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0);
>  }
>=20
>  /**
> @@ -473,51 +337,26 @@ power_set_governor_original(struct
> pstate_power_info *pi)  static int  power_get_available_freqs(struct
> pstate_power_info *pi)  {
> -	FILE *f_min, *f_max;
> +	FILE *f_min =3D NULL, *f_max =3D NULL;
>  	int ret =3D -1;
> -	char *p_min, *p_max;
> -	char buf_min[BUFSIZ];
> -	char buf_max[BUFSIZ];
> -	char fullpath_min[PATH_MAX];
> -	char fullpath_max[PATH_MAX];
> -	char *s_min, *s_max;
>  	uint32_t sys_min_freq =3D 0, sys_max_freq =3D 0, base_max_freq =3D 0;
>  	uint32_t i, num_freqs =3D 0;
>=20
> -	snprintf(fullpath_max, sizeof(fullpath_max),
> -			POWER_SYSFILE_BASE_MAX_FREQ,
> -			pi->lcore_id);
> -	snprintf(fullpath_min, sizeof(fullpath_min),
> -			POWER_SYSFILE_BASE_MIN_FREQ,
> -			pi->lcore_id);
> +	/* open all files */
> +	open_core_sysfs_file(POWER_SYSFILE_BASE_MAX_FREQ, pi-
> >lcore_id, "r",
> +			&f_max);
> +	FOPEN_OR_ERR_GOTO(f_max, out);
>=20
> -	f_min =3D fopen(fullpath_min, "r");
> -	FOPEN_OR_ERR_RET(f_min, ret);
> +	open_core_sysfs_file(POWER_SYSFILE_BASE_MIN_FREQ, pi->lcore_id,
> "r",
> +			     &f_min);
> +	FOPEN_OR_ERR_GOTO(f_max, out);
>=20
> -	f_max =3D fopen(fullpath_max, "r");
> -	if (f_max =3D=3D NULL)
> -		fclose(f_min);
> +	/* read base ratios */
> +	ret =3D read_core_sysfs_u32(f_max, &sys_max_freq);
> +	FOPS_OR_ERR_GOTO(ret, out);
>=20
> -	FOPEN_OR_ERR_RET(f_max, ret);
> -
> -	s_min =3D fgets(buf_min, sizeof(buf_min), f_min);
> -	FOPS_OR_NULL_GOTO(s_min, out);
> -
> -	s_max =3D fgets(buf_max, sizeof(buf_max), f_max);
> -	FOPS_OR_NULL_GOTO(s_max, out);
> -
> -
> -	/* Strip the line break if there is */
> -	p_min =3D strchr(buf_min, '\n');
> -	if (p_min !=3D NULL)
> -		*p_min =3D 0;
> -
> -	p_max =3D strchr(buf_max, '\n');
> -	if (p_max !=3D NULL)
> -		*p_max =3D 0;
> -
> -	sys_min_freq =3D strtoul(buf_min, &p_min,
> POWER_CONVERT_TO_DECIMAL);
> -	sys_max_freq =3D strtoul(buf_max, &p_max,
> POWER_CONVERT_TO_DECIMAL);
> +	ret =3D read_core_sysfs_u32(f_min, &sys_min_freq);
> +	FOPS_OR_ERR_GOTO(ret, out);
>=20
>  	if (sys_max_freq < sys_min_freq)
>  		goto out;
> @@ -576,27 +415,14 @@ power_get_cur_idx(struct pstate_power_info *pi)
> {
>  	FILE *f_cur;
>  	int ret =3D -1;
> -	char *p_cur;
> -	char buf_cur[BUFSIZ];
> -	char fullpath_cur[PATH_MAX];
> -	char *s_cur;
>  	uint32_t sys_cur_freq =3D 0;
>  	unsigned int i;
>=20
> -	snprintf(fullpath_cur, sizeof(fullpath_cur),
> -			POWER_SYSFILE_CUR_FREQ,
> -			pi->lcore_id);
> -	f_cur =3D fopen(fullpath_cur, "r");
> -	FOPEN_OR_ERR_RET(f_cur, ret);
> +	open_core_sysfs_file(POWER_SYSFILE_CUR_FREQ, pi->lcore_id, "r",
> &f_cur);
> +	FOPEN_OR_ERR_GOTO(f_cur, fail);
>=20
> -	/* initialize the cur_idx to matching current frequency freq index */
> -	s_cur =3D fgets(buf_cur, sizeof(buf_cur), f_cur);
> -	FOPS_OR_NULL_GOTO(s_cur, fail);
> -
> -	p_cur =3D strchr(buf_cur, '\n');
> -	if (p_cur !=3D NULL)
> -		*p_cur =3D 0;
> -	sys_cur_freq =3D strtoul(buf_cur, &p_cur,
> POWER_CONVERT_TO_DECIMAL);
> +	ret =3D read_core_sysfs_u32(f_cur, &sys_cur_freq);
> +	FOPS_OR_ERR_GOTO(ret, fail);
>=20
>  	/* convert the frequency to nearest 100000 value
>  	 * Ex: if sys_cur_freq=3D1396789 then freq_conv=3D1400000 @@ -615,10
> +441,10 @@ power_get_cur_idx(struct pstate_power_info *pi)
>  		}
>  	}
>=20
> -	fclose(f_cur);
> -	return 0;
> +	ret =3D 0;
>  fail:
> -	fclose(f_cur);
> +	if (f_cur !=3D NULL)
> +		fclose(f_cur);
>  	return ret;
>  }
>=20
> --
> 2.25.1