From owner-svn-src-all@freebsd.org Tue May 21 21:14:24 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id EB35A158F88A; Tue, 21 May 2019 21:14:23 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 8E30280FEF; Tue, 21 May 2019 21:14:23 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 657D6B513; Tue, 21 May 2019 21:14:23 +0000 (UTC) (envelope-from allanjude@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x4LLENxv036683; Tue, 21 May 2019 21:14:23 GMT (envelope-from allanjude@FreeBSD.org) Received: (from allanjude@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x4LLENka036682; Tue, 21 May 2019 21:14:23 GMT (envelope-from allanjude@FreeBSD.org) Message-Id: <201905212114.x4LLENka036682@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: allanjude set sender to allanjude@FreeBSD.org using -f From: Allan Jude Date: Tue, 21 May 2019 21:14:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r348065 - head/sys/dev/acpica X-SVN-Group: head X-SVN-Commit-Author: allanjude X-SVN-Commit-Paths: head/sys/dev/acpica X-SVN-Commit-Revision: 348065 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 8E30280FEF X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.96)[-0.961,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 May 2019 21:14:24 -0000 Author: allanjude Date: Tue May 21 21:14:22 2019 New Revision: 348065 URL: https://svnweb.freebsd.org/changeset/base/348065 Log: Correct the way remaining battery life is calculated Previously, if a system had multiple batteries, the remaining life percentage was calculated as the average of each battery's percent remaining. This results in rather incorrect values when you consider the case of the Thinkpad X270 that has a small 3 cell internally battery, and a hot-swappable 9 cell battery that is used first. Battery 0 is at 100%, but battery 1 is at 10%, you do not infact have 55% of your capacity remaining. The new method calculates the percentage based on remaining capacity out of total capacity, giving a much more accurate reading. PR: 229818 Submitted by: Keegan Drake H.P. MFC after: 2 weeks Sponsored by: Klara Systems Event: Waterloo Hackathon 2019 Modified: head/sys/dev/acpica/acpi_battery.c Modified: head/sys/dev/acpica/acpi_battery.c ============================================================================== --- head/sys/dev/acpica/acpi_battery.c Tue May 21 20:38:48 2019 (r348064) +++ head/sys/dev/acpica/acpi_battery.c Tue May 21 21:14:22 2019 (r348065) @@ -119,7 +119,7 @@ int acpi_battery_get_battinfo(device_t dev, struct acpi_battinfo *battinfo) { int batt_stat, devcount, dev_idx, error, i; - int total_cap, total_min, valid_rate, valid_units; + int total_cap, total_lfcap, total_min, valid_rate, valid_units; devclass_t batt_dc; device_t batt_dev; struct acpi_bst *bst; @@ -152,6 +152,7 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_ba */ dev_idx = -1; batt_stat = valid_rate = valid_units = 0; + total_cap = total_lfcap = 0; for (i = 0; i < devcount; i++) { /* Default info for every battery is "not present". */ acpi_reset_battinfo(&bi[i]); @@ -210,17 +211,23 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_ba if (!acpi_battery_bif_valid(bif)) continue; - /* Calculate percent capacity remaining. */ - bi[i].cap = (100 * bst[i].cap) / bif->lfcap; - /* * Some laptops report the "design-capacity" instead of the * "real-capacity" when the battery is fully charged. That breaks * the above arithmetic as it needs to be 100% maximum. */ - if (bi[i].cap > 100) - bi[i].cap = 100; + if (bst[i].cap > bif->lfcap) + bst[i].cap = bif->lfcap; + /* Calculate percent capacity remaining. */ + bi[i].cap = (100 * bst[i].cap) / bif->lfcap; + + /* If this battery is not present, don't use its capacity. */ + if (bi[i].cap != -1) { + total_cap += bst[i].cap; + total_lfcap += bif->lfcap; + } + /* * On systems with more than one battery, they may get used * sequentially, thus bst.rate may only signify the one currently @@ -241,7 +248,7 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_ba } /* Pass 2: calculate capacity and remaining time for all batteries. */ - total_cap = total_min = 0; + total_min = 0; for (i = 0; i < devcount; i++) { /* * If any batteries are discharging, use the sum of the bst.rate @@ -253,10 +260,6 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_ba else bi[i].min = 0; total_min += bi[i].min; - - /* If this battery is not present, don't use its capacity. */ - if (bi[i].cap != -1) - total_cap += bi[i].cap; } /* @@ -265,7 +268,7 @@ acpi_battery_get_battinfo(device_t dev, struct acpi_ba */ if (valid_units > 0) { if (dev == NULL) { - battinfo->cap = total_cap / valid_units; + battinfo->cap = (total_cap * 100) / total_lfcap; battinfo->min = total_min; battinfo->state = batt_stat; battinfo->rate = valid_rate;