URI:
       tLinux.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tLinux.c (5143B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <regexp.h>
            5 #include "dat.h"
            6 
            7 void xapm(int);
            8 void xloadavg(int);
            9 void xmeminfo(int);
           10 void xnet(int);
           11 void xstat(int);
           12 void xvmstat(int);
           13 void xwireless(int);
           14 
           15 void (*statfn[])(int) =
           16 {
           17         xapm,
           18         xloadavg,
           19         xmeminfo,
           20         xnet,
           21         xstat,
           22         xvmstat,
           23         xwireless,
           24         0
           25 };
           26 
           27 void
           28 xapm(int first)
           29 {
           30         static int fd = -1;
           31         int curr = -1;
           32 
           33         if(first){
           34                 fd = open("/sys/class/power_supply/BAT0/capacity", OREAD);
           35                 return;
           36         }
           37         if(fd == -1)
           38                 return;
           39 
           40         readfile(fd);
           41         tokens(0);
           42         curr = atoi(tok[0]);
           43 
           44         if(curr != -1)
           45                 Bprint(&bout, "battery =%d 100\n", curr);
           46 
           47 }
           48 
           49 void
           50 xloadavg(int first)
           51 {
           52         static int fd = -1;
           53 
           54         if(first){
           55                 fd = open("/proc/loadavg", OREAD);
           56                 return;
           57         }
           58 
           59         readfile(fd);
           60         tokens(0);
           61         if(ntok >= 1)
           62                 Bprint(&bout, "load =%d 1000\n", (int)(atof(tok[0])*1000));
           63 }
           64 
           65 void
           66 xmeminfo(int first)
           67 {
           68         int i;
           69         vlong tot, used;
           70         vlong mtot, mfree;
           71         vlong stot, sfree;
           72         static int fd = -1;
           73 
           74         if(first){
           75                 fd = open("/proc/meminfo", OREAD);
           76                 return;
           77         }
           78 
           79         readfile(fd);
           80         mtot = 0;
           81         stot = 0;
           82         mfree = 0;
           83         sfree = 0;
           84         for(i=0; i<nline; i++){
           85                 tokens(i);
           86                 if(ntok < 3)
           87                         continue;
           88                 tot = atoll(tok[1]);
           89                 used = atoll(tok[2]);
           90                 if(strcmp(tok[0], "Mem:") == 0)
           91                         Bprint(&bout, "mem =%lld %lld\n", used/1024, tot/1024);
           92                 else if(strcmp(tok[0], "Swap:") == 0)
           93                         Bprint(&bout, "swap =%lld %lld\n", used/1024, tot/1024);
           94                 else if(strcmp(tok[0], "MemTotal:") == 0)
           95                         mtot = atoll(tok[1]);        /* kb */
           96                 else if(strcmp(tok[0], "MemFree:") == 0)
           97                         mfree += atoll(tok[1]);
           98                 else if(strcmp(tok[0], "Buffers:") == 0)
           99                         mfree += atoll(tok[1]);
          100                 else if(strcmp(tok[0], "Cached:") == 0){
          101                         mfree += atoll(tok[1]);
          102                         if(mtot < mfree)
          103                                 continue;
          104                         Bprint(&bout, "mem =%lld %lld\n", mtot-mfree, mtot);
          105                 }else if(strcmp(tok[0], "SwapTotal:") == 0)
          106                         stot = atoll(tok[1]);        /* kb */
          107                 else if(strcmp(tok[0], "SwapFree:") == 0){
          108                         sfree = atoll(tok[1]);
          109                         if(stot < sfree)
          110                                 continue;
          111                         Bprint(&bout, "swap =%lld %lld\n", stot-sfree, stot);
          112                 }
          113         }
          114 }
          115 
          116 void
          117 xnet(int first)
          118 {
          119         int i, n;
          120         vlong totb, totp, tote, totin, totou, totinb, totoub, b, p, e, in, ou, inb, oub;
          121         char *q;
          122         static int fd = -1;
          123         static Reprog *netdev = nil;
          124 
          125         if(first){
          126                 fd = open("/proc/net/dev", OREAD);
          127                 netdev = regcomp("^(eth[0-9]+|wlan[0-9]+|enp[0-9]+s[0-9]+f[0-9]+|wlp[0-9]+s[0-9]+)$");
          128                 return;
          129         }
          130 
          131         readfile(fd);
          132         n = 0;
          133         totb = 0;
          134         tote = 0;
          135         totp = 0;
          136         totin = 0;
          137         totou = 0;
          138         totinb = 0;
          139         totoub = 0;
          140         for(i=0; i<nline; i++){
          141                 if((q = strchr(line[i], ':')) != nil)
          142                         *q = ' ';
          143                 tokens(i);
          144                 if(ntok < 8+8)
          145                         continue;
          146                 if(regexec(netdev, tok[0], nil, 0) != 1)
          147                         continue;
          148                 inb = atoll(tok[1]);
          149                 oub = atoll(tok[9]);
          150                 in = atoll(tok[2]);
          151                 ou = atoll(tok[10]);
          152                 b = inb+oub;
          153                 p = in+ou;
          154                 e = atoll(tok[3])+atoll(tok[11]);
          155                 totb += b;
          156                 totp += p;
          157                 tote += e;
          158                 totin += in;
          159                 totou += ou;
          160                 totinb += inb;
          161                 totoub += oub;
          162                 n++;
          163         }
          164         Bprint(&bout, "etherb %lld %d\n", totb, n*1000000);
          165         Bprint(&bout, "ether %lld %d\n", totp, n*1000);
          166         Bprint(&bout, "ethererr %lld %d\n", tote, n*1000);
          167         Bprint(&bout, "etherin %lld %d\n", totin, n*1000);
          168         Bprint(&bout, "etherout %lld %d\n", totou, n*1000);
          169         Bprint(&bout, "etherinb %lld %d\n", totinb, n*1000);
          170         Bprint(&bout, "etheroutb %lld %d\n", totoub, n*1000);
          171 }
          172 
          173 void
          174 xstat(int first)
          175 {
          176         static int fd = -1;
          177         int i;
          178 
          179         if(first){
          180                 fd = open("/proc/stat", OREAD);
          181                 return;
          182         }
          183 
          184         readfile(fd);
          185         for(i=0; i<nline; i++){
          186                 tokens(i);
          187                 if(ntok < 2)
          188                         continue;
          189                 if(strcmp(tok[0], "cpu") == 0 && ntok >= 5){
          190                         Bprint(&bout, "user %lld 100\n", atoll(tok[1]));
          191                         Bprint(&bout, "sys %lld 100\n", atoll(tok[3]));
          192                         Bprint(&bout, "cpu %lld 100\n", atoll(tok[1])+atoll(tok[3]));
          193                         Bprint(&bout, "idle %lld 100\n", atoll(tok[4]));
          194                 }
          195         /*
          196                 if(strcmp(tok[0], "page") == 0 && ntok >= 3){
          197                         Bprint(&bout, "pagein %lld 500\n", atoll(tok[1]));
          198                         Bprint(&bout, "pageout %lld 500\n", atoll(tok[2]));
          199                         Bprint(&bout, "page %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
          200                 }
          201                 if(strcmp(tok[0], "swap") == 0 && ntok >= 3){
          202                         Bprint(&bout, "swapin %lld 500\n", atoll(tok[1]));
          203                         Bprint(&bout, "swapout %lld 500\n", atoll(tok[2]));
          204                         Bprint(&bout, "swap %lld 1000\n", atoll(tok[1])+atoll(tok[2]));
          205                 }
          206         */
          207                 if(strcmp(tok[0], "intr") == 0)
          208                         Bprint(&bout, "intr %lld 1000\n", atoll(tok[1]));
          209                 if(strcmp(tok[0], "ctxt") == 0)
          210                         Bprint(&bout, "context %lld 10000\n", atoll(tok[1]));
          211                 if(strcmp(tok[0], "processes") == 0)
          212                         Bprint(&bout, "fork %lld 1000\n", atoll(tok[1]));
          213         }
          214 }
          215 
          216 void
          217 xvmstat(int first)
          218 {
          219         static int fd = -1;
          220         int i;
          221 
          222         if(first){
          223                 fd = open("/proc/vmstat", OREAD);
          224                 return;
          225         }
          226 
          227         readfile(fd);
          228         for(i=0; i<nline; i++){
          229                 tokens(i);
          230                 if(ntok < 2)
          231                         continue;
          232                 if(strcmp(tok[0], "pgfault") == 0)
          233                         Bprint(&bout, "fault %lld 100000\n", atoll(tok[1]));
          234         }
          235 }
          236 
          237 void
          238 xwireless(int first)
          239 {
          240         static int fd = -1;
          241         static Reprog *wlan = nil;
          242         int i;
          243 
          244         if(first){
          245                 fd = open("/proc/net/wireless", OREAD);
          246                 wlan = regcomp("^(wlan[0-9]+|wlp[0-9]+s[0-9]+):$");
          247                 return;
          248         }
          249 
          250         readfile(fd);
          251         for(i=0; i<nline; i++){
          252                 tokens(i);
          253                 if(ntok < 3)
          254                         continue;
          255                 if(regexec(wlan, tok[0], nil, 0) == 1)
          256                         Bprint(&bout, "802.11 =%lld 100\n", atoll(tok[2]));
          257         }
          258 }