tstats: make more stats work in Linux - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 3c6ab1854e92467a7309cf244339c6f10c2b0d7d
 (DIR) parent 0e9f3966da196eee3353154d7da04fafe90e5644
 (HTM) Author: Fazlul Shahriar <fshahriar@gmail.com>
       Date:   Sat,  8 Aug 2009 16:45:27 -0400
       
       stats: make more stats work in Linux
       
       http://codereview.appspot.com/96084
       
       Diffstat:
         M man/man1/stats.1                    |       5 +++++
         M src/cmd/auxstats/Linux.c            |      72 ++++++++++++++++++++++++++++---
         M src/cmd/draw/stats.c                |       7 ++++++-
       
       3 files changed, 77 insertions(+), 7 deletions(-)
       ---
 (DIR) diff --git a/man/man1/stats.1 b/man/man1/stats.1
       t@@ -105,6 +105,11 @@ number of system calls per second.
        number of valid pages on the swap device.
        The swap is displayed as a
        fraction of the number of swap pages configured by the machine.
       +.TP
       +.B "8 802.11b
       +display the signal strength detected by the 802.11b wireless ether card; the value
       +is usually below 50% unless the receiver is in the same room as the transmitter, so
       +a midrange value represents a strong signal.
        .PD
        .PP
        The graphs are plotted with time on the horizontal axis.
 (DIR) diff --git a/src/cmd/auxstats/Linux.c b/src/cmd/auxstats/Linux.c
       t@@ -8,6 +8,8 @@ void xloadavg(int);
        void xmeminfo(int);
        void xnet(int);
        void xstat(int);
       +void xvmstat(int);
       +void xwireless(int);
        
        void (*statfn[])(int) =
        {
       t@@ -16,6 +18,8 @@ void (*statfn[])(int) =
                xmeminfo,
                xnet,
                xstat,
       +        xvmstat,
       +        xwireless,
                0
        };
        
       t@@ -77,6 +81,7 @@ xmeminfo(int first)
                int i;
                vlong tot, used;
                vlong mtot, mfree;
       +        vlong stot, sfree;
                static int fd = -1;
        
                if(first){
       t@@ -86,6 +91,9 @@ xmeminfo(int first)
        
                readfile(fd);
                mtot = 0;
       +        stot = 0;
       +        mfree = 0;
       +        sfree = 0;
                for(i=0; i<nline; i++){
                        tokens(i);
                        if(ntok < 3)
       t@@ -98,11 +106,22 @@ xmeminfo(int first)
                                Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
                        else if(strcmp(tok[0], "MemTotal:") == 0)
                                mtot = atoll(tok[1]);        /* kb */
       -                else if(strcmp(tok[0], "MemFree:") == 0){
       -                        mfree = atoll(tok[1]);
       +                else if(strcmp(tok[0], "MemFree:") == 0)
       +                        mfree += atoll(tok[1]);
       +                else if(strcmp(tok[0], "Buffers:") == 0)
       +                        mfree += atoll(tok[1]);
       +                else if(strcmp(tok[0], "Cached:") == 0){
       +                        mfree += atoll(tok[1]);
                                if(mtot < mfree)
                                        continue;
                                Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
       +                }else if(strcmp(tok[0], "SwapTotal:") == 0)
       +                        stot = atoll(tok[1]);        /* kb */
       +                else if(strcmp(tok[0], "SwapFree:") == 0){
       +                        sfree = atoll(tok[1]);
       +                        if(stot < sfree)
       +                                continue;
       +                        Bprint(&bout, "swap =%lld %lld\n", stot-sfree, stot);
                        }
                }
        }
       t@@ -135,7 +154,7 @@ xnet(int first)
                        tokens(i);
                        if(ntok < 8+8)
                                continue;
       -                if(strncmp(tok[0], "eth", 3) != 0)
       +                if(strncmp(tok[0], "eth", 3) != 0 && strncmp(tok[0], "wlan", 4) != 0)
                                continue;
                        inb = atoll(tok[1]);
                        oub = atoll(tok[9]);
       t@@ -182,7 +201,7 @@ xstat(int first)
                                Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
                                Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
                                Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
       -                        Bprint(&bout, "idle %lld\n", atoll(tok[4]));
       +                        Bprint(&bout, "idle %lld 100\n", atoll(tok[4]));
                        }
                /*
                        if(strcmp(tok[0], "page") == 0 && ntok >= 3){
       t@@ -197,11 +216,52 @@ xstat(int first)
                        }
                */
                        if(strcmp(tok[0], "intr") == 0)
       -                        Bprint(&bout, "interrupt %lld 1000\n", atoll(tok[1]));
       +                        Bprint(&bout, "intr %lld 1000\n", atoll(tok[1]));
                        if(strcmp(tok[0], "ctxt") == 0)
       -                        Bprint(&bout, "context %lld 1000\n", atoll(tok[1]));
       +                        Bprint(&bout, "context %lld 10000\n", atoll(tok[1]));
                        if(strcmp(tok[0], "processes") == 0)
                                Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));
                }
        }
        
       +void
       +xvmstat(int first)
       +{
       +        static int fd = -1;
       +        int i;
       +
       +        if(first){
       +                fd = open("/proc/vmstat", OREAD);
       +                return;
       +        }
       +
       +        readfile(fd);
       +        for(i=0; i<nline; i++){
       +                tokens(i);
       +                if(ntok < 2)
       +                        continue;
       +                if(strcmp(tok[0], "pgfault") == 0)
       +                        Bprint(&bout, "fault %lld 100000\n", atoll(tok[1]));
       +        }
       +}
       +
       +void
       +xwireless(int first)
       +{
       +        static int fd = -1;
       +        int i;
       +        
       +        if(first){
       +                fd = open("/proc/net/wireless", OREAD);
       +                return;
       +        }
       +
       +        readfile(fd);
       +        for(i=0; i<nline; i++){
       +                tokens(i);
       +                if(ntok < 3)
       +                        continue;
       +                if(strcmp(tok[0], "wlan0:") == 0)
       +                        Bprint(&bout, "802.11 =%lld 100\n", atoll(tok[2]));
       +        }
       +}
 (DIR) diff --git a/src/cmd/draw/stats.c b/src/cmd/draw/stats.c
       t@@ -28,6 +28,7 @@ enum
        
        enum
        {
       +        V80211,
                Vbattery,
                Vcontext,
                Vcpu,
       t@@ -51,6 +52,7 @@ enum
        char*
        labels[Nvalue] = 
        {
       +        "802.11",
                "battery",
                "context",
                "cpu",
       t@@ -109,7 +111,7 @@ Machine        *mach;
        Font                *mediumfont;
        char                *fontname;
        char                *mysysname;
       -char                argchars[] = "bcCeEfiIlmnsw";
       +char                argchars[] = "8bcCeEfiIlmnsw";
        int                pids[1024];
        int                 parity;        /* toggled to avoid patterns in textured background */
        int                nmach;
       t@@ -748,6 +750,9 @@ threadmain(int argc, char *argv[])
                default:
                        fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
                        usage();
       +        case '8':
       +                addgraph(V80211);
       +                break;
                case 'b':
                        addgraph(Vbattery);
                        break;