Return-Path: Received: from mail-pl0-f68.google.com ([209.85.160.68]:36364 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751378AbeBXTQT (ORCPT ); Sat, 24 Feb 2018 14:16:19 -0500 Received: by mail-pl0-f68.google.com with SMTP id v3so6895551plg.3 for ; Sat, 24 Feb 2018 11:16:19 -0800 (PST) From: Stephen Hemminger To: netdev@vger.kernel.org Cc: ikomyagin@gmail.com, Stephen Hemminger Subject: [PATCH iproute2 3/3] ss: update slabinfo names and sum IPv4 and IPv6 Date: Sat, 24 Feb 2018 11:16:07 -0800 Message-Id: <20180224191607.23892-4-stephen@networkplumber.org> In-Reply-To: <20180224191607.23892-1-stephen@networkplumber.org> References: <20180224191607.23892-1-stephen@networkplumber.org> Sender: netdev-owner@vger.kernel.org List-ID: For a long time the name of slabs for TCP sockets have changed. And ss summary was missing them. Also it never handled IPv6. Change the code to sum values from both protocols. Also, print error if slabinfo can not be opened. Typically this is attempt to run 'ss -s' as non-root user and /proc/slabinfo is now protected. Reported-by: Igor Komyagin Signed-off-by: Stephen Hemminger --- misc/ss.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 4c7057198eb2..f98acdba6e24 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -686,15 +686,14 @@ static int get_slabstat(struct slabstat *s) { char buf[256]; FILE *fp; - int cnt; static bool slabstat_valid; static const char * const slabstat_ids[] = { - "sock", + "sock_inode_cache", "tcp_bind_bucket", - "tcp_tw_bucket", - "tcp_open_request", + "tw_sock_TCP", + "request_sock_TCP", }; - unsigned int *vals = (unsigned int *) s; + unsigned int *slabstat_vals = (unsigned int *) s; if (slabstat_valid) return 0; @@ -702,27 +701,29 @@ static int get_slabstat(struct slabstat *s) memset(s, 0, sizeof(*s)); fp = slabinfo_open(); - if (!fp) + if (!fp) { + fprintf(stderr, "slabinfo unavailable: %s\n", + strerror(errno)); return -1; - - cnt = sizeof(*s)/sizeof(int); + } if (!fgets(buf, sizeof(buf), fp)) { fclose(fp); return -1; } + while (fgets(buf, sizeof(buf), fp) != NULL) { int i; for (i = 0; i < ARRAY_SIZE(slabstat_ids); i++) { - if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i])) == 0) { - sscanf(buf, "%*s%u", vals + i); - cnt--; - break; - } + unsigned long val; + + if (memcmp(buf, slabstat_ids[i], strlen(slabstat_ids[i]))) + continue; + + if (sscanf(buf, "%*s%lu", &val)) + slabstat_vals[i] += val; } - if (cnt <= 0) - break; } slabstat_valid = true; -- 2.16.1