From patchwork Sat Nov 11 18:55:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Radoslaw Biernacki X-Patchwork-Id: 118616 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp58435qgn; Sat, 11 Nov 2017 10:56:09 -0800 (PST) X-Google-Smtp-Source: AGs4zMYnc2XL2JKVhDFGc5JeG9V4QO3Sf71zodbMoKCGVANnYU5b40DQZ75KZpetd/oLOnasdDO2 X-Received: by 10.223.184.42 with SMTP id h39mr3537288wrf.38.1510426569882; Sat, 11 Nov 2017 10:56:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510426569; cv=none; d=google.com; s=arc-20160816; b=z3zUsv49AxF0Vt5TY6k49ceaL5YBrT4dHzZgo+dW6BdFhjxcSxe6k6whg/Ti+JTu3F 4UqiR60bzBNkerkr6wLWgDwJbUwqYeWw0KH2OW9Ke86Kk1HxwFrKopwX/tMrWlt0ggRN SIN6OXCl9dKLzhnjvEILpXJ1MnOJMdD8QQNre0N4N4tbBVk/zg+nhVt1uGpx7UM1vMdp lfyhUEpBC18d0QhnC7vfsWDVAV9lyuhyzs1Lkvt2xs4uLlCk0r4L1F5eYhZC8fNmUsZe IP+TwxlLc1HWd6QLLGOjknCXvk/cQtobLHdpY5I1IGNcozlceKQfc81l8vf+dKCSH9mP PA8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:cc:to:from:dkim-signature :arc-authentication-results; bh=vUQCCJSN8CGiB4UZE3IjW0aIFc0qwXtMv5b8I8XzhgA=; b=gcb1kxu92Ed7bRZKigbblMWNVkVPWyJGwc2QLEGK37lLJoSVT8RnBzJhlE8E+TGcKM IYjU4/ayVFfCfg85BP2JdHExoOLUq1aa3pX19EGjSPPTmIwjYACMtoKOn3HioBZIU+TB jCQ6yszo9HHitvbIYtravlS1h55epAjlQu9+snof7oznOL7Bg8yUUYER2McofSVTOP3N 5ftf634djOq05zMNpA3RNWLd7lgWGC3uTsrAFNmmxQMEO6TOwgzDZSAgQyUKlXpA0Dog y61fUWQs39hPS1mHyb5xrhWFRzR/joFnxHoa5g6ZWQ7nmHMW/ZJJ33iAW2SrsqRsAX1Q NJFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=VNHJIk1D; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from dpdk.org (dpdk.org. [92.243.14.124]) by mx.google.com with ESMTP id b78si10196246wrd.92.2017.11.11.10.56.09; Sat, 11 Nov 2017 10:56:09 -0800 (PST) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) client-ip=92.243.14.124; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=VNHJIk1D; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1D0B61B218; Sat, 11 Nov 2017 19:56:04 +0100 (CET) Received: from mail-lf0-f68.google.com (mail-lf0-f68.google.com [209.85.215.68]) by dpdk.org (Postfix) with ESMTP id 4FA3F1B20F for ; Sat, 11 Nov 2017 19:56:02 +0100 (CET) Received: by mail-lf0-f68.google.com with SMTP id s16so14104372lfs.1 for ; Sat, 11 Nov 2017 10:56:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vUQCCJSN8CGiB4UZE3IjW0aIFc0qwXtMv5b8I8XzhgA=; b=VNHJIk1DnqYBa3jfeghu5D9wgKaMu7jOxSqFbukRENtDhNM/551FuaeLqzO9WQR2e4 pzLxEgMBwt6jovlcBqdQ1SUjnNQOqCLP4mMwx50veAgTdyGzg/rfVqyzNd9atnP0rnbH 3Elrf6+6mdHoQEzantWPjPkr+DFZwd+WAqtQI= 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; bh=vUQCCJSN8CGiB4UZE3IjW0aIFc0qwXtMv5b8I8XzhgA=; b=IOKjnVPJVIEuJtec0uUNwsAEQ3WkJI/w16ESI2QCTImsntp9cpPPVGSuxayK9FZ188 +ZKdpGZ4ttiCNuhDJhDsYbYYp10Ss8j155UmXt3Ya6e5IqqPqQ58Dp6TgQLGOpQWSGR6 cdCD0SQk6jXdU8cnmrv0BLpq23ZPg+gl0H1VCQDjoS+TGxpne4aBFdKqHFniURtfjG4B 9pXghLY9PjvVReYPhBkNU9dsr1cBlhb0FG5l6vqfwCFAI4X8FXghROjFXxkKH3s5xoOM EPPbzdSdwOGCDOxCjg1i4BeyAA050SOpfPvFFfoIv1K0YYmHsAq/ERUC2lxx801OfV9u 8bJw== X-Gm-Message-State: AJaThX7HSQPpbxGBRX6pgj0SgHH58sTz4armqF89NXJnESnZl+vGdL5B iSk/wsc7POiiS9bUfmgGjUBXQLzL0S8= X-Received: by 10.46.67.207 with SMTP id z76mr999246lje.61.1510426561433; Sat, 11 Nov 2017 10:56:01 -0800 (PST) Received: from rad-H81M-S1.semihalf.local (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id x90sm2342107ljb.86.2017.11.11.10.56.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 11 Nov 2017 10:56:00 -0800 (PST) From: Radoslaw Biernacki To: dev@dpdk.org, david.hunt@intel.com Cc: stable@dpdk.org, alan.carew@intel.com, pablo.de.lara.guarch@intel.com Date: Sat, 11 Nov 2017 19:55:05 +0100 Message-Id: <1510426507-28245-2-git-send-email-radoslaw.biernacki@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> References: <1508161628-4265-1-git-send-email-radoslaw.biernacki@linaro.org> <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> Subject: [dpdk-dev] [PATCH 1/3] power: removing code macros X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This fix implements David Hunt suggestion to remove error checking code macros. Together with improved code readability it removes checkpatch warnings. Fixes: 445c6528b55f ("power: common interface for guest and host") CC: stable@dpdk.org Signed-off-by: Radoslaw Biernacki --- lib/librte_power/rte_power_acpi_cpufreq.c | 81 ++++++++++++++++++------------- 1 file changed, 48 insertions(+), 33 deletions(-) -- 2.7.4 diff --git a/lib/librte_power/rte_power_acpi_cpufreq.c b/lib/librte_power/rte_power_acpi_cpufreq.c index 01ac5ac..3d0872f 100644 --- a/lib/librte_power/rte_power_acpi_cpufreq.c +++ b/lib/librte_power/rte_power_acpi_cpufreq.c @@ -55,27 +55,6 @@ #define POWER_DEBUG_TRACE(fmt, args...) #endif -#define FOPEN_OR_ERR_RET(f, retval) do { \ - if ((f) == NULL) { \ - RTE_LOG(ERR, POWER, "File not openned\n"); \ - return retval; \ - } \ -} while (0) - -#define FOPS_OR_NULL_GOTO(ret, label) do { \ - if ((ret) == 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 @@ -172,10 +151,16 @@ power_set_governor_userspace(struct rte_power_info *pi) snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR, pi->lcore_id); f = fopen(fullpath, "rw+"); - FOPEN_OR_ERR_RET(f, ret); + if (!f) { + RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); + return ret; + } s = fgets(buf, sizeof(buf), f); - FOPS_OR_NULL_GOTO(s, out); + if (!s) { + RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + goto out; + } /* Check if current governor is userspace */ if (strncmp(buf, POWER_GOVERNOR_USERSPACE, @@ -190,10 +175,16 @@ power_set_governor_userspace(struct rte_power_info *pi) /* Write 'userspace' to the governor */ val = fseek(f, 0, SEEK_SET); - FOPS_OR_ERR_GOTO(val, out); + if (val < 0) { + RTE_LOG(ERR, POWER, "fseek failed\n"); + goto out; + } val = fputs(POWER_GOVERNOR_USERSPACE, f); - FOPS_OR_ERR_GOTO(val, out); + if (val < 0) { + RTE_LOG(ERR, POWER, "fputs failed\n"); + goto out; + } ret = 0; RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been " @@ -222,10 +213,16 @@ power_get_available_freqs(struct rte_power_info *pi) snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ, pi->lcore_id); f = fopen(fullpath, "r"); - FOPEN_OR_ERR_RET(f, ret); + if (!f) { + RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); + return ret; + } s = fgets(buf, sizeof(buf), f); - FOPS_OR_NULL_GOTO(s, out); + if (!s) { + RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + goto out; + } /* Strip the line break if there is */ p = strchr(buf, '\n'); @@ -290,10 +287,16 @@ power_init_for_setting_freq(struct rte_power_info *pi) snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED, pi->lcore_id); f = fopen(fullpath, "rw+"); - FOPEN_OR_ERR_RET(f, -1); + if (!f) { + RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); + return -1; + } s = fgets(buf, sizeof(buf), f); - FOPS_OR_NULL_GOTO(s, out); + if (!s) { + RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + goto out; + } freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL); for (i = 0; i < pi->nb_freqs; i++) { @@ -387,10 +390,16 @@ power_set_governor_original(struct rte_power_info *pi) snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR, pi->lcore_id); f = fopen(fullpath, "rw+"); - FOPEN_OR_ERR_RET(f, ret); + if (!f) { + RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); + return ret; + } s = fgets(buf, sizeof(buf), f); - FOPS_OR_NULL_GOTO(s, out); + if (!s) { + RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + goto out; + } /* Check if the governor to be set is the same as current */ if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) { @@ -403,10 +412,16 @@ power_set_governor_original(struct rte_power_info *pi) /* Write back the original governor */ val = fseek(f, 0, SEEK_SET); - FOPS_OR_ERR_GOTO(val, out); + if (val < 0) { + RTE_LOG(ERR, POWER, "fseek failed\n"); + goto out; + } val = fputs(pi->governor_ori, f); - FOPS_OR_ERR_GOTO(val, out); + if (val < 0) { + RTE_LOG(ERR, POWER, "fputs failed\n"); + goto out; + } ret = 0; RTE_LOG(INFO, POWER, "Power management governor of lcore %u " From patchwork Sat Nov 11 18:55:06 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Radoslaw Biernacki X-Patchwork-Id: 118617 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp58571qgn; Sat, 11 Nov 2017 10:56:22 -0800 (PST) X-Google-Smtp-Source: AGs4zMYFVCZloaFrBcragxqxXgdbRkdXniQDHJICJj9mizez3GrEg4x+n/64ved511zwl7PYJPoD X-Received: by 10.28.169.198 with SMTP id s189mr2750031wme.65.1510426582572; Sat, 11 Nov 2017 10:56:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510426582; cv=none; d=google.com; s=arc-20160816; b=DXGxz6RrLxSly1eg8VQhpRYoSCyz9PQqwus3UZ9pvgfSfI8qY3YiKTvvDV0mg3Osax prkjhmaUIILXQLfKGrMnDpmoobiTKfNBWGKYb9Vti2uMqclxuDL9Hwc7+Vl/wLHStuNY vmrrEQGmrm5r9gcwrkfCbRuiiPV7irsv1twFo05b5SfN1yee5OG/HgMvnHUqLCcaZg+M xUf8AGC0sCk3rt2TqoNbRBkcVtvB8Q6jgdYE8InOa5DxJLov0s2SZtsTFQ6JJetscq7/ iyiIbU4VMvJrojwMOrdsYCe4qcZ2NMa2aGVcpZPgHzTSK9mnCH+O4fY4YmdCKKrrq38V kt5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:cc:to:from:dkim-signature :arc-authentication-results; bh=tEYyt+uhESeeqgHaaGbE0FMroNwwgkQqyt5ZOMN0KP4=; b=mkE89XWYcWPhPStLCM6wfsYfRA6Rj5IGY5O/eb7A3QAjMdKVsdTEW35afuymXKDsPP zXtSpM27YKpDvqiZLbhles1cLiMdL6o/llIoIcQ95nJmDBWl42ZCfGqvgxw+ZAIkzPas bc/fMS3dOUGR44t3HE8JX0b4VpWVjjMnbDTOc0I4izNdKDyHEm5vac7Bdca7ZPc2TVAF rJJHJYy5Q/m+JpP41K1rjWH+6yb5Q/vCQwPtS6YLsVN9ZD5iikwCj+xxgEeiw1OKlsIq cOBPVmud2oNh1KzzskTjhBYD4dCZ4hUx06xo0eISAaZu0SX/xGPthhiwKkYSvnkwqEB8 I3tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Q1NqAf8a; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from dpdk.org (dpdk.org. [92.243.14.124]) by mx.google.com with ESMTP id y4si11019183wrg.306.2017.11.11.10.56.22; Sat, 11 Nov 2017 10:56:22 -0800 (PST) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) client-ip=92.243.14.124; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Q1NqAf8a; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 203751B221; Sat, 11 Nov 2017 19:56:06 +0100 (CET) Received: from mail-lf0-f65.google.com (mail-lf0-f65.google.com [209.85.215.65]) by dpdk.org (Postfix) with ESMTP id 2F3CE1B21D for ; Sat, 11 Nov 2017 19:56:05 +0100 (CET) Received: by mail-lf0-f65.google.com with SMTP id b190so14360115lfg.9 for ; Sat, 11 Nov 2017 10:56:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tEYyt+uhESeeqgHaaGbE0FMroNwwgkQqyt5ZOMN0KP4=; b=Q1NqAf8apyoyTo3h+EtQArfe/ll8i0L/79D5rMFZupZWd258wpswzPxhzhMvT+mUPA jdbzQWM5uK8lvRekRvWq3YBt5nGW6G5QK3TWTVdIFzlDgSgJo59fLJo8UeOUqcr84k/D uC9J3SK2MRZwduTpMxFgI02TN5/C3t0d+fjGU= 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; bh=tEYyt+uhESeeqgHaaGbE0FMroNwwgkQqyt5ZOMN0KP4=; b=i432zjfltsArN88Uw7+1oUqoqxzncXO6IQjUoJJLYaaIZG+xv0fpaGXJOrYc+AnpaD 2jEChFRCyhEOonRp2JRS/wxmNE0YtXlLqagO4czFfrkoMCa5f8qwpk9v6HriqjhczaN6 sLZISwkiiQk4ZTOVHChEHx1cHxPvWcxaM/nr+4W0FxsadfJg27Cmz6L1CTRrCYJzFW/l fcnCXzGv6HBUquLleDedrJj/s14mrKE54J84lQCNWpfvsITpQ25YbVMNs8WXUTgWTMOy dafwXHwv1L/XK6RVeBu2pfXUfwEUtOyrCGJDCpHWSft87Yqkledqom7SwlPcSuaAZBle 1fqQ== X-Gm-Message-State: AJaThX48AkWhCFTip48hK/PLxxN44wKtf4zKqbBZLYtJSduFYW/R3+LX 4sg2qldfv98j6DWZRFS5bvbsimPa748= X-Received: by 10.46.22.20 with SMTP id w20mr1510811ljd.69.1510426564559; Sat, 11 Nov 2017 10:56:04 -0800 (PST) Received: from rad-H81M-S1.semihalf.local (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id x90sm2342107ljb.86.2017.11.11.10.56.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 11 Nov 2017 10:56:03 -0800 (PST) From: Radoslaw Biernacki To: dev@dpdk.org, david.hunt@intel.com Cc: stable@dpdk.org, alan.carew@intel.com, pablo.de.lara.guarch@intel.com Date: Sat, 11 Nov 2017 19:55:06 +0100 Message-Id: <1510426507-28245-3-git-send-email-radoslaw.biernacki@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> References: <1508161628-4265-1-git-send-email-radoslaw.biernacki@linaro.org> <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> Subject: [dpdk-dev] [PATCH 2/3] power: switching to unbuffered access for /sys files X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This patch fixes the bug caused by improper use of buffered stdio file access for switching the CPU frequency and governor. When using buffered stdio, each fwrite() must use fflush() and the return code must be verified. Also fseek() is needed. Therefore it is better to use unbuffered mode or use plain open()/write() functions. This fix use second approach. This not only remove need for ffush() but also remove need for fseek() operations. This patch also reuse some code around power_set_governor_userspace() and power_set_governor_userspace() functions. Fixes: 445c6528b55f ("power: common interface for guest and host") CC: stable@dpdk.org Signed-off-by: Radoslaw Biernacki --- lib/librte_power/rte_power_acpi_cpufreq.c | 211 +++++++++++++----------------- 1 file changed, 91 insertions(+), 120 deletions(-) -- 2.7.4 diff --git a/lib/librte_power/rte_power_acpi_cpufreq.c b/lib/librte_power/rte_power_acpi_cpufreq.c index 3d0872f..f811bd3 100644 --- a/lib/librte_power/rte_power_acpi_cpufreq.c +++ b/lib/librte_power/rte_power_acpi_cpufreq.c @@ -30,7 +30,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - +#include #include #include #include @@ -47,6 +47,12 @@ #include "rte_power_acpi_cpufreq.h" #include "rte_power_common.h" +#define min(_x, _y) ({ \ + typeof(_x) _min1 = (_x); \ + typeof(_y) _min2 = (_y); \ + (void) (&_min1 == &_min2); \ + _min1 < _min2 ? _min1 : _min2; }) + #ifdef RTE_LIBRTE_POWER_DEBUG #define POWER_DEBUG_TRACE(fmt, args...) do { \ RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \ @@ -88,7 +94,7 @@ struct rte_power_info { unsigned lcore_id; /**< Logical core id */ uint32_t freqs[RTE_MAX_LCORE_FREQS]; /**< Frequency array */ uint32_t nb_freqs; /**< number of available freqs */ - FILE *f; /**< FD of scaling_setspeed */ + int fd; /**< FD of scaling_setspeed */ char governor_ori[32]; /**< Original governor name */ uint32_t curr_idx; /**< Freq index in freqs array */ volatile uint32_t state; /**< Power in use state */ @@ -105,6 +111,9 @@ static struct rte_power_info lcore_power_info[RTE_MAX_LCORE]; static int set_freq_internal(struct rte_power_info *pi, uint32_t idx) { + char buf[BUFSIZ]; + int count, ret; + if (idx >= RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) { RTE_LOG(ERR, POWER, "Invalid frequency index %u, which " "should be less than %u\n", idx, pi->nb_freqs); @@ -117,17 +126,14 @@ set_freq_internal(struct rte_power_info *pi, uint32_t idx) POWER_DEBUG_TRACE("Freqency[%u] %u to be set for lcore %u\n", idx, pi->freqs[idx], pi->lcore_id); - if (fseek(pi->f, 0, SEEK_SET) < 0) { - RTE_LOG(ERR, POWER, "Fail to set file position indicator to 0 " - "for setting frequency for lcore %u\n", pi->lcore_id); + count = snprintf(buf, sizeof(buf), "%u", pi->freqs[idx]); + assert((size_t)count < sizeof(buf)-1); + ret = write(pi->fd, buf, count); + if (ret != count) { + RTE_LOG(ERR, POWER, "Fail to write new frequency (%s) for " + "lcore %u\n", buf, pi->lcore_id); return -1; } - if (fprintf(pi->f, "%u", pi->freqs[idx]) < 0) { - RTE_LOG(ERR, POWER, "Fail to write new frequency for " - "lcore %u\n", pi->lcore_id); - return -1; - } - fflush(pi->f); pi->curr_idx = idx; return 1; @@ -139,90 +145,109 @@ set_freq_internal(struct rte_power_info *pi, uint32_t idx) * governor will be saved for rolling back. */ static int -power_set_governor_userspace(struct rte_power_info *pi) +power_set_governor(unsigned int lcore_id, const char *new_gov, char *old_gov, + size_t old_gov_len) { - FILE *f; + int fd; + int count, len; int ret = -1; char buf[BUFSIZ]; char fullpath[PATH_MAX]; - char *s; - int val; snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR, - pi->lcore_id); - f = fopen(fullpath, "rw+"); - if (!f) { + lcore_id); + fd = open(fullpath, O_RDWR); + if (fd < 0) { RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); return ret; } - s = fgets(buf, sizeof(buf), f); - if (!s) { - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + count = read(fd, buf, sizeof(buf)); + if (count < 0) { + RTE_LOG(ERR, POWER, "Failed to read from %s\n", fullpath); goto out; } + buf[min((size_t)count, sizeof(buf)-1)] = '\0'; - /* Check if current governor is userspace */ - if (strncmp(buf, POWER_GOVERNOR_USERSPACE, - sizeof(POWER_GOVERNOR_USERSPACE)) == 0) { + /* Check if current governor is as requested */ + if (!strcmp(buf, new_gov)) { ret = 0; POWER_DEBUG_TRACE("Power management governor of lcore %u is " - "already userspace\n", pi->lcore_id); - goto out; - } - /* Save the original governor */ - snprintf(pi->governor_ori, sizeof(pi->governor_ori), "%s", buf); - - /* Write 'userspace' to the governor */ - val = fseek(f, 0, SEEK_SET); - if (val < 0) { - RTE_LOG(ERR, POWER, "fseek failed\n"); + "already %s\n", lcore_id, new_gov); goto out; } + /* Save the old governor */ + if (old_gov) + snprintf(old_gov, old_gov_len, "%s", buf); - val = fputs(POWER_GOVERNOR_USERSPACE, f); - if (val < 0) { - RTE_LOG(ERR, POWER, "fputs failed\n"); + /* Set new governor */ + len = strlen(new_gov); + count = write(fd, new_gov, len); + if (count != len) { + RTE_LOG(ERR, POWER, "Failed to set %s governor\n", new_gov); goto out; } ret = 0; RTE_LOG(INFO, POWER, "Power management governor of lcore %u has been " - "set to user space successfully\n", pi->lcore_id); + "set to user space successfully\n", lcore_id); out: - fclose(f); + if (close(fd)) + RTE_LOG(ERR, POWER, "Error while closing file %s\n", fullpath); return ret; } /** + * It is to check the current scaling governor by reading sys file, and then + * set it into 'userspace' if it is not by writing the sys file. The original + * governor will be saved for rolling back. + */ +static int +power_set_governor_userspace(struct rte_power_info *pi) +{ + 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 the sys file. + */ +static int +power_set_governor_original(struct rte_power_info *pi) +{ + return power_set_governor(pi->lcore_id, pi->governor_ori, NULL, 0); +} + +/** * It is to get the available frequencies of the specific lcore by reading the * sys file. */ static int power_get_available_freqs(struct rte_power_info *pi) { - FILE *f; + int fd; int ret = -1, i, count; char *p; char buf[BUFSIZ]; char fullpath[PATH_MAX]; char *freqs[RTE_MAX_LCORE_FREQS]; - char *s; snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_AVAIL_FREQ, - pi->lcore_id); - f = fopen(fullpath, "r"); - if (!f) { + pi->lcore_id); + fd = open(fullpath, O_RDONLY); + if (fd < 0) { RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); return ret; } - s = fgets(buf, sizeof(buf), f); - if (!s) { - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + count = read(fd, buf, sizeof(buf)); + if (count < 0) { + RTE_LOG(ERR, POWER, "Failed to read from %s\n", fullpath); goto out; } + buf[min((size_t)count, sizeof(buf)-1)] = '\0'; /* Strip the line break if there is */ p = strchr(buf, '\n'); @@ -267,7 +292,8 @@ power_get_available_freqs(struct rte_power_info *pi) POWER_DEBUG_TRACE("%d frequencie(s) of lcore %u are available\n", count, pi->lcore_id); out: - fclose(f); + if (close(fd)) + RTE_LOG(ERR, POWER, "Error while closing file %s\n", fullpath); return ret; } @@ -278,37 +304,39 @@ power_get_available_freqs(struct rte_power_info *pi) static int power_init_for_setting_freq(struct rte_power_info *pi) { - FILE *f; + int fd; + int count; + uint32_t i, freq; char fullpath[PATH_MAX]; char buf[BUFSIZ]; - uint32_t i, freq; - char *s; snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_SETSPEED, pi->lcore_id); - f = fopen(fullpath, "rw+"); - if (!f) { + fd = open(fullpath, O_RDWR); + if (fd < 0) { RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); return -1; } - s = fgets(buf, sizeof(buf), f); - if (!s) { - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); + count = read(fd, buf, sizeof(buf)); + if (count < 0) { + RTE_LOG(ERR, POWER, "Failed to read from %s\n", fullpath); goto out; } + buf[min((size_t)count, sizeof(buf)-1)] = '\0'; freq = strtoul(buf, NULL, POWER_CONVERT_TO_DECIMAL); for (i = 0; i < pi->nb_freqs; i++) { if (freq == pi->freqs[i]) { pi->curr_idx = i; - pi->f = f; + pi->fd = fd; return 0; } } out: - fclose(f); + if (close(fd)) + RTE_LOG(ERR, POWER, "Error while closing file %s\n", fullpath); return -1; } @@ -373,66 +401,6 @@ rte_power_acpi_cpufreq_init(unsigned lcore_id) return -1; } -/** - * It is to check the governor and then set the original governor back if - * needed by writing the the sys file. - */ -static int -power_set_governor_original(struct rte_power_info *pi) -{ - FILE *f; - int ret = -1; - char buf[BUFSIZ]; - char fullpath[PATH_MAX]; - char *s; - int val; - - snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR, - pi->lcore_id); - f = fopen(fullpath, "rw+"); - if (!f) { - RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); - return ret; - } - - s = fgets(buf, sizeof(buf), f); - if (!s) { - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); - goto out; - } - - /* Check if the governor to be set is the same as current */ - if (strncmp(buf, pi->governor_ori, sizeof(pi->governor_ori)) == 0) { - ret = 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 = fseek(f, 0, SEEK_SET); - if (val < 0) { - RTE_LOG(ERR, POWER, "fseek failed\n"); - goto out; - } - - val = fputs(pi->governor_ori, f); - if (val < 0) { - RTE_LOG(ERR, POWER, "fputs failed\n"); - goto out; - } - - ret = 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 rte_power_acpi_cpufreq_exit(unsigned lcore_id) { @@ -452,8 +420,11 @@ rte_power_acpi_cpufreq_exit(unsigned lcore_id) } /* Close FD of setting freq */ - fclose(pi->f); - pi->f = NULL; + if (close(pi->fd)) { + RTE_LOG(ERR, POWER, "Error while closing governor file\n"); + return -1; + } + pi->fd = -1; /* Set the governor back to the original */ if (power_set_governor_original(pi) < 0) { From patchwork Sat Nov 11 18:55:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Radoslaw Biernacki X-Patchwork-Id: 118618 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp58787qgn; Sat, 11 Nov 2017 10:56:37 -0800 (PST) X-Google-Smtp-Source: AGs4zMZLIdn9yBwWGqD+/yEzTsO4Ej1IHE9KbcXbTX22j/4GFfW03N5wlibXpwZmgKwnmSy65GFq X-Received: by 10.28.90.85 with SMTP id o82mr2939266wmb.69.1510426597581; Sat, 11 Nov 2017 10:56:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510426597; cv=none; d=google.com; s=arc-20160816; b=tmr4ftnXFcWMFDBAaiSBYRf+g9SZigSCtAAqhTYKtMU6vtoVL3B+kRMtPvKMey4yoJ iCdRWyej9tfr/dJiQRawUEKqOdvrw29JQCvzdz927MbJ7ue+Oupn29QTnM0KXXDCucsJ jioikQohvnfhr3xSyXQd2z/3gAKEYy8cVivLyBnT2TalLRGNHZwYX5YqdPihR2li+gop DbADUlOyxjaVtOMFNiUU94PeF03FKch83negrKY1a/7m1OncPW1CnLLJyBHC6CCcVnKm 6f+wRC4gvYzYKiRFKfRlFzR3haS0uucPyctriJxuffVPhoM0m+QaIfxb3yyQ+pkHpszU 6CRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:cc:to:from:dkim-signature :arc-authentication-results; bh=ZCvXBggHUGQnUYfuG8wV9TMa2MRJADnDqHmf5te56Es=; b=B89eapPK1g3/Ry6xlefEliZWI4de8V1/NJGF3weLFas9+tZjCwIx0NjYvLS5boD30Y 8ETk+z05OzKulB/pKrYLF2u+gsIfDdx0xbIP+cyUU6cb/tlryPLPOsKFgB9zGiOvW8W1 SztUs4UL0QpN4w6HDwvI9YKhz8HCPhbqTjWGNoLvdeZGATlS6xcbFx3NLTTW1AG2yORI M8GmpHYNurmYd4D8olzlZ78keHtosTQNKBI0IFwZbmRzL3OiQqTW24PIpSVRYjtS+JKx CC19EbiXLKfIh8vnveIWqbnmp0bKCbqJdLM1FwPEEFG3eA3Ruco3Wx5B//p2h1dAHjta 44Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Epyg7F6j; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from dpdk.org (dpdk.org. [92.243.14.124]) by mx.google.com with ESMTP id g13si10735592wrh.413.2017.11.11.10.56.37; Sat, 11 Nov 2017 10:56:37 -0800 (PST) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) client-ip=92.243.14.124; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Epyg7F6j; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 019A21B226; Sat, 11 Nov 2017 19:56:08 +0100 (CET) Received: from mail-lf0-f68.google.com (mail-lf0-f68.google.com [209.85.215.68]) by dpdk.org (Postfix) with ESMTP id 1AA9D1B225 for ; Sat, 11 Nov 2017 19:56:07 +0100 (CET) Received: by mail-lf0-f68.google.com with SMTP id a2so14352551lfh.11 for ; Sat, 11 Nov 2017 10:56:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZCvXBggHUGQnUYfuG8wV9TMa2MRJADnDqHmf5te56Es=; b=Epyg7F6jxZJzZ7epLoOKOcMQSiSZ1stc2mzOPO7EriTJfYroeSV5WV5ihRfEqdmYM6 Ll0On3XBiwCDsTHigO3Lx17sZRH4tYz68OY70OUwLRJbEkRG0Yh4rxNIZ+l8t14DxUPk NfKfSuzYIUtyVtlk6XdiM15eJ8vjCqJhx1pEE= 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; bh=ZCvXBggHUGQnUYfuG8wV9TMa2MRJADnDqHmf5te56Es=; b=Kh9FnIJYzaeV9tvRY054xeDsOERm6GYXCbrjJjFx7JXc0sMLmWlZVF0A9oPKM1X7eb NEVe7gMbg6uUukOjw6Q458e0ykpVeeRD+zNfluLLcVHPsu6RjaC2pxbIRcBuS2WBJXZ6 Vm22vCSb9VLUzzCgtSycslMo59EsDY8Cozaa15stt+oGSRPkCG6264eSnhqazbu6p4U2 LC+r3zVzMVigbrHDVW+8UFweraAKat9dZjgar3oWCEFMcb1T2XDOMtVi3oJ+QYgWWs7m EOk+u5vChsMd9P3hgh25tvahOsNO7dyN7J8vd76KoRJrjcCJTxWZZcyTIVicon5dpMSZ IFOA== X-Gm-Message-State: AJaThX6VD+w2lmfP98OD+X5mGfCFnnZuHiueAosee118/kTH1InxpaOH L0Ei1VwCOk3kp26QmsXEZKmKAzNDsz0= X-Received: by 10.25.43.9 with SMTP id r9mr1304565lfr.128.1510426566551; Sat, 11 Nov 2017 10:56:06 -0800 (PST) Received: from rad-H81M-S1.semihalf.local (31-172-191-173.noc.fibertech.net.pl. [31.172.191.173]) by smtp.gmail.com with ESMTPSA id x90sm2342107ljb.86.2017.11.11.10.56.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 11 Nov 2017 10:56:05 -0800 (PST) From: Radoslaw Biernacki To: dev@dpdk.org, david.hunt@intel.com Cc: stable@dpdk.org, alan.carew@intel.com, pablo.de.lara.guarch@intel.com Date: Sat, 11 Nov 2017 19:55:07 +0100 Message-Id: <1510426507-28245-4-git-send-email-radoslaw.biernacki@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> References: <1508161628-4265-1-git-send-email-radoslaw.biernacki@linaro.org> <1510426507-28245-1-git-send-email-radoslaw.biernacki@linaro.org> Subject: [dpdk-dev] [PATCH 3/3] power: check if userspace governor is available X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Since for new Intel CPU's kernel use intel_pstate driver, which does not offer userspace governor, it is wise to check the userspace governor availability before trying to perform governor switch. The outcome from this patch is direct information for user about the root cause of failure during the rte_power_acpi_cpufreq_init(). Signed-off-by: Radoslaw Biernacki --- lib/librte_power/rte_power_acpi_cpufreq.c | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) -- 2.7.4 diff --git a/lib/librte_power/rte_power_acpi_cpufreq.c b/lib/librte_power/rte_power_acpi_cpufreq.c index f811bd3..0d9a2a5 100644 --- a/lib/librte_power/rte_power_acpi_cpufreq.c +++ b/lib/librte_power/rte_power_acpi_cpufreq.c @@ -65,6 +65,11 @@ #define POWER_CONVERT_TO_DECIMAL 10 #define POWER_GOVERNOR_USERSPACE "userspace" +#define POWER_SCALING_DRIVER_INTEL_PSTATE "intel_pstate" +#define POWER_SYSFILE_SCALING_DRIVER \ + "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver" +#define POWER_SYSFILE_AVAIL_GOVERNOR \ + "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_governors" #define POWER_SYSFILE_GOVERNOR \ "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor" #define POWER_SYSFILE_AVAIL_FREQ \ @@ -139,6 +144,70 @@ set_freq_internal(struct rte_power_info *pi, uint32_t idx) return 1; } +/* check /sys file for presence of given string */ +static int +power_check_sysfile_string(const char *fullpath, const char *string) +{ + int fd; + int count; + int ret = -1; + char buf[BUFSIZ]; + + fd = open(fullpath, O_RDONLY); + if (fd < 0) { + RTE_LOG(ERR, POWER, "Failed to open %s\n", fullpath); + return ret; + } + + count = read(fd, buf, sizeof(buf)); + if (count < 0) { + RTE_LOG(ERR, POWER, "Failed to read from %s\n", fullpath); + goto out; + } + buf[min((size_t)count, sizeof(buf)-1)] = '\0'; + + if (strstr(buf, string)) + ret = 0; /* substring found */ + +out: + if (close(fd)) { + RTE_LOG(ERR, POWER, "Error while closing file %s\n", fullpath); + return -1; + } + + return ret; +} + +/* check if userspace governor is offered by the kernel */ +static int power_check_gov_userspace(unsigned int lcore_id) +{ + char fullpath[PATH_MAX]; + + snprintf(fullpath, sizeof(fullpath), + POWER_SYSFILE_AVAIL_GOVERNOR, lcore_id); + if (power_check_sysfile_string( + fullpath, POWER_GOVERNOR_USERSPACE)) { + + RTE_LOG(ERR, POWER, "Userspace governor is not available\n"); + + snprintf(fullpath, sizeof(fullpath), + POWER_SYSFILE_SCALING_DRIVER, lcore_id); + if (!power_check_sysfile_string( + fullpath, POWER_SCALING_DRIVER_INTEL_PSTATE)) { + + RTE_LOG(WARNING, POWER, + POWER_SCALING_DRIVER_INTEL_PSTATE "was detected" + " which does not offer userspace power governor" + ". \"intel_pstate=disable\" needs to be added " + "to the kernel boot parameters.\n"); + } + + return -1; + } + + return 0; +} + /** * It is to check the current scaling governor by reading sys file, and then * set it into 'userspace' if it is not by writing the sys file. The original @@ -154,6 +223,11 @@ power_set_governor(unsigned int lcore_id, const char *new_gov, char *old_gov, char buf[BUFSIZ]; char fullpath[PATH_MAX]; + /* check if userspace governor is available */ + if (power_check_gov_userspace(lcore_id)) + return ret; + + /* store current governor and set userspace governor */ snprintf(fullpath, sizeof(fullpath), POWER_SYSFILE_GOVERNOR, lcore_id); fd = open(fullpath, O_RDWR);