From patchwork Mon Feb 11 20:24:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zachary Cook X-Patchwork-Id: 1040135 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SExdaVLG"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TVgmpU03"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43yy695L26z9s7h for ; Tue, 12 Feb 2019 07:28:17 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=HgelZaM/ws7OR9w6MZigxjnHOlamBZbqj5YMxQU9WB0=; b=SExdaVLGqtj/rZ 26QdRBwLwEPKcPCIQBZknRlKPaaVpIjaI6dGIRwRBkDLNhlkZJHKd2dVloBGu9+6UaL4VYcTeaAQ3 FoBCIX43IAc109vRGOfOz1y6BpXW07GYt7I4rOeGoTpUMkxLoGV8ix2hmdvq4sF0kYl6JfoAt7kRb PHRF6M3zIB1Zwn+zpSAxo+9KtKudzdjf4OH6dTrHFcwY6mm3/u7slH8hz96ETIqvlo73reQ42qBSJ RScBsgcko+wVlDS6MID85DmAV/pzo336c7frLjbiO2J9k5YsmaFugbk/MYlLbr/dn6cNf6e5nOKWh cmqspEgcRSxDIGe6UKPw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gtIBQ-0001dT-So; Mon, 11 Feb 2019 20:28:04 +0000 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gtIBO-0001d8-3O for openwrt-devel@lists.openwrt.org; Mon, 11 Feb 2019 20:28:03 +0000 Received: by mail-qt1-x843.google.com with SMTP id e5so245175qtr.12 for ; Mon, 11 Feb 2019 12:28:01 -0800 (PST) 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=CLLaFQ9cOVUQSvDlfls5WSMQgQyzAcqfUQ7h0eExiuQ=; b=TVgmpU03GsWCMiK4/Ly60c46EIhyvGLniKr7FmIUa9ACP1EGYFP+jOXNn7BJECL8PO 8q/oloYt5kTtHuecEjBLWX3EmgduycE/l4ASOnR5a1PlpzBM9KXzs1673WlXf4xvU/YO OXAptDWIXbgMqGJ1LC+/zQDmoGjZjhJHolcA0UB1qlcu8cYAMPFqfxJIRyaVP2sM8xHz JwyoRrCpffNx/gxLMBSOSVhW3RyR0LHNz7SyilG6j3Nr0GYewvGPnB6zDcwpo6+lSF8V mC9WT7u5ap8eQGmFTsbLW/ft5OLH0ac82ISY9FOeoa1kWL6bMdrS+Px+zhm0EvzGiAgY EodQ== 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=CLLaFQ9cOVUQSvDlfls5WSMQgQyzAcqfUQ7h0eExiuQ=; b=ua95W3xde9ueiwEA0xf/ca08iWQq/ccJKaUsE6xskbO0XJkMKwbqqawXf3ZTzhDhhM aEMughPnI9MvrJqfaVetaFpi9GrjBJZfMVdV4nesPCEhiFIDY5grDg1cwOUEflf6wfbe fRgVwqms+ShZ/CeWwtNYx0ZNWPgmyz95l0iSUN7fJOePjfkp3Lp6H3c11YSu8NMV73SE O3m53MfE4ad1700p8BPlL9Pb0yx2jfTPjL6UpKU/hluSZYjKWE+p2eCceamc/M7dDtJZ U7JxxDa/GfHHCY1gMVtuqLjnhE4EJIVJ007/FOPtv1qLJILfzXLV3fzeW6TQhWirHx7p FaMQ== X-Gm-Message-State: AHQUAuZ+oTtcIC+p4NKRdbtjC4yWeZcWLcjYyrDjvU0QsSeiwzJQyXxa /pyygZTgFI4wcOy1/PmH9L5zFC17 X-Google-Smtp-Source: AHgI3IZq/QBlNW2VM8VRemux+j7KgNRHfRxefGvYKiDdgQurXWGOG2chbXpkAFf/f7q4qXeZg0fo9A== X-Received: by 2002:ac8:1625:: with SMTP id p34mr29213qtj.95.1549916880406; Mon, 11 Feb 2019 12:28:00 -0800 (PST) Received: from localhost.localdomain ([142.47.212.2]) by smtp.gmail.com with ESMTPSA id 10sm5536455qtx.40.2019.02.11.12.27.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 12:27:59 -0800 (PST) From: Zachary Cook To: openwrt-devel@lists.openwrt.org Date: Mon, 11 Feb 2019 15:24:59 -0500 Message-Id: <20190211202459.6014-1-zachcook1991@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211140033.15166-1-zachcook1991@gmail.com> References: <20190211140033.15166-1-zachcook1991@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190211_122802_169846_2DD984C5 X-CRM114-Status: GOOD ( 13.11 ) X-Spam-Score: 0.1 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:843 listed in] [list.dnswl.org] 0.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (zachcook1991[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (zachcook1991[at]gmail.com) -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH v2] procd: Add cached and available to memory table X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mhei@heimpold.de, Zachary Cook Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This should allow Luci to show a better measure of system memory usage Cached memory will be used to add a new progress bar MemAvailable is the kernel's estimate of memory that is useable by userspace without swapping, and is more accurate than the current memory.free + memory.buffered calculation for total memory available Changes in v2: reorganized code to not leak a file descriptor if early return stay consistent with current comment style use atoll instead of atol to better match the data size Signed-off-by: Zachary Cook --- system.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/system.c b/system.c index 8ed3f93..79f0ca8 100644 --- a/system.c +++ b/system.c @@ -228,9 +228,36 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj, #ifdef linux struct sysinfo info; void *c; + char line[256]; + char *key, *val; + unsigned long long available, cached; + FILE *f; if (sysinfo(&info)) return UBUS_STATUS_UNKNOWN_ERROR; + + if ((f = fopen("/proc/meminfo", "r")) == NULL) + return UBUS_STATUS_UNKNOWN_ERROR; + + /* if linux < 3.14 MemAvailable is not in meminfo */ + available = 0; + cached = 0; + + while(fgets(line, sizeof(line), f)) + { + key = strtok(line, " :"); + val = strtok(NULL, " "); + + if (!key || !val) + continue; + + if (!strcasecmp(key, "MemAvailable")) + available = 1024 * atoll(val); + else if (!strcasecmp(key, "Cached")) + cached = 1024 * atoll(val); + } + + fclose(f); #endif now = time(NULL); @@ -256,6 +283,8 @@ static int system_info(struct ubus_context *ctx, struct ubus_object *obj, blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram); blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram); blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram); + blobmsg_add_u64(&b, "available",available); + blobmsg_add_u64(&b, "cached", cached); blobmsg_close_table(&b, c); c = blobmsg_open_table(&b, "swap");