From c13ba8541ba6701dc4a851ea0940884e2e6020f2 Mon Sep 17 00:00:00 2001
From: MilhouseVH <milhouseVH.github@nmacleod.com>
Date: Fri, 1 Mar 2019 15:58:44 +0000
Subject: [PATCH 1/5] add rpi4

---
 bcmstat.sh | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/bcmstat.sh b/bcmstat.sh
index fa8ee7e..b90392e 100755
--- a/bcmstat.sh
+++ b/bcmstat.sh
@@ -44,7 +44,7 @@ else:
 
 GITHUB = "https://raw.github.com/MilhouseVH/bcmstat/master"
 ANALYTICS = "http://goo.gl/edu1jG"
-VERSION = "0.5.1"
+VERSION = "0.5.2"
 
 VCGENCMD = None
 VCDBGCMD = None
@@ -60,7 +60,7 @@ SYSINFO = {}
 # NEW          23: will be 1 for the new scheme, 0 for the old scheme
 # MEMSIZE      20: 0=256M 1=512M 2=1G
 # MANUFACTURER 16: 0=SONY 1=EGOMAN 2=EMBEST 3=SONY JAPAN 4=EMBEST
-# PROCESSOR    12: 0=2835 1=2836 2=2837
+# PROCESSOR    12: 0=2835 1=2836 2=2837, 3=2838
 # TYPE         04: 0=MODELA 1=MODELB 2=MODELA+ 3=MODELB+ 4=Pi2 MODELB 5=ALPHA 6=CM 8=Pi3 9=Pi0 10=CM3 12=Pi0W
 # REV          00: 0=REV0 1=REV1 2=REV2 3=REV3
 
@@ -84,10 +84,10 @@ class RPIHardware():
 
     # Note: Some of these memory sizes and processors are fictional and relate to unannounced products - logic would
     #       dictate such products may exist at some point in the future, but it's only guesswork.
-    self.memsizes = ["256MB", "512MB", "1GB", "2GB", "4GB"]
+    self.memsizes = ["256MB", "512MB", "1GB", "2GB", "4GB", "8GB"]
     self.manufacturers = ["Sony UK", "Egoman", "Embest", "Sony Japan", "Embest", "Stadium"]
     self.processors = ["2835", "2836", "2837", "2838", "2839", "2840"]
-    self.models = ["Model A", "Model B", "Model A+", "Model B+", "Pi2 Model B", "Alpha", "CM1", "Unknown", "Pi3", "Pi0", "CM3", "Unknown", "Pi0 W", "Pi3 Model B+"]
+    self.models = ["Model A", "Model B", "Model A+", "Model B+", "Pi2 Model B", "Alpha", "CM1", "Unknown", "Pi3", "Pi0", "CM3", "Unknown", "Pi0 W", "Pi3 Model B+", "Pi3 Model A+", "Unknown", "CM3+", "Pi4 Model B"]
     self.pcbs = ["Unknown", "Pi3 Rev1.0", "Pi3 Rev1.2", "Pi2 2837 Rev1.1", "Pi2 2836", "Pi1 B+ Rev 1.1", "Pi0", "Pi1 B Rev2.0", "Pi2 (2837) Rev1.0", "Pi0 W", "Pi2 (2837) Rev1.2", "Pi3 B+"]
 
     self.set_rev_code(rev_code)
@@ -909,8 +909,8 @@ def ShowHeadings(display_flags, sysinfo):
       HDR1 = "%s RX kB/s TX kB/s" % HDR1
       HDR2 = "%s ======= =======" % HDR2
     else:
-      HDR1 = "%s     RX B/s     TX B/s" % HDR1
-      HDR2 = "%s ========== ==========" % HDR2
+      HDR1 = "%s      RX B/s      TX B/s" % HDR1
+      HDR2 = "%s =========== ===========" % HDR2
 
   if display_flags["utilisation"]:
     HDR1 = "%s  %%user  %%nice   %%sys  %%idle  %%iowt   %%irq %%s/irq %%total" % HDR1
@@ -937,8 +937,12 @@ def ShowHeadings(display_flags, sysinfo):
     HDR2 = "%s ===========" % HDR2
 
   if display_flags["cpu_mem"]:
-    HDR1 = "%s Mem Free / %%used" % HDR1
-    HDR2 = "%s ================" % HDR2
+    if display_flags["human_readable"]:
+      HDR1 = "%s MemFreeMB / %%used" % HDR1
+      HDR2 = "%s =================" % HDR2
+    else:
+      HDR1 = "%s MemFreeKB / %%used" % HDR1
+      HDR2 = "%s =================" % HDR2
     if display_flags["swap"]:
       HDR1 = "%s(SwUse)" % HDR1
       HDR2 = "%s=======" % HDR2
@@ -1025,8 +1029,8 @@ def ShowStats(display_flags, sysinfo, threshold, bcm2385, irq, network, cpuload,
     else:
       LINE = "%s %s %s" % \
                (LINE,
-                colourise(network[0],         "%10s",     0.5e6,    2.5e6,    5.0e6, True),
-                colourise(network[1],         "%10s",     0.5e6,    2.5e6,    5.0e6, True))
+                colourise(network[0],         "%11s",     0.5e6,    2.5e6,    5.0e6, True),
+                colourise(network[1],         "%11s",     0.5e6,    2.5e6,    5.0e6, True))
 
   if display_flags["utilisation"]:
     LINE = "%s %s %s %s %s %s %s %s %s" % \
@@ -1071,12 +1075,12 @@ def ShowStats(display_flags, sysinfo, threshold, bcm2385, irq, network, cpuload,
     if display_flags["human_readable"]:
       LINE = "%s %s / %s" % \
                (LINE,
-                colourise(memory[1]/1024,  "%5s MB",   60, 75, 85, True,  compare=memory[2]),
-                colourise(memory[2],  "%4.1f%%",  60, 75, 85, False, compare=memory[2]))
+                colourise(memory[1]/1024,  "%9s",      60, 75, 85, True,  compare=memory[2]),
+                colourise(memory[2],       "%4.1f%%",  60, 75, 85, False, compare=memory[2]))
     else :
-      LINE = "%s %s/%s" % \
+      LINE = "%s %s / %s" % \
                (LINE,
-                colourise(memory[1],  "%7s kB",   60, 75, 85, True,  compare=memory[2]),
+                colourise(memory[1],  "%9s",      60, 75, 85, True,  compare=memory[2]),
                 colourise(memory[2],  "%4.1f%%",  60, 75, 85, False, compare=memory[2]))
 
     # Swap memory
-- 
2.14.1


From 390cc573319fd918dc23ee78e4ab914b30965304 Mon Sep 17 00:00:00 2001
From: MilhouseVH <milhouseVH.github@nmacleod.com>
Date: Wed, 6 Mar 2019 11:44:40 +0000
Subject: [PATCH 2/5] irqs: don't fail if missing

---
 bcmstat.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/bcmstat.sh b/bcmstat.sh
index b90392e..3e13c97 100755
--- a/bcmstat.sh
+++ b/bcmstat.sh
@@ -452,7 +452,8 @@ def getIRQ(storage):
 
   irq = 0
   for i in grep("dwc", readfile("/proc/interrupts"), 1).split("\n"):
-    irq += int(i)
+    if i:
+      irq += int(i)
 
   storage[1] = (time.time(), irq)
 
-- 
2.14.1


From 28ffb2a403af013ad2408dd6cfad485f01a9240b Mon Sep 17 00:00:00 2001
From: MilhouseVH <milhouseVH.github@nmacleod.com>
Date: Wed, 6 Mar 2019 12:29:29 +0000
Subject: [PATCH 3/5] irqs: accumulate all of them

---
 bcmstat.sh | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/bcmstat.sh b/bcmstat.sh
index 3e13c97..1db3df4 100755
--- a/bcmstat.sh
+++ b/bcmstat.sh
@@ -447,13 +447,19 @@ def colourise(display, nformat, green, yellow, red, withcomma, compare=None, add
 
   return nformat % cnum
 
-def getIRQ(storage):
+def getIRQ(storage, sysinfo):
   storage[2] = storage[1]
 
+  nproc = sysinfo["nproc"]
   irq = 0
-  for i in grep("dwc", readfile("/proc/interrupts"), 1).split("\n"):
-    if i:
-      irq += int(i)
+  for line in grep(":", readfile("/proc/interrupts")).split("\n"):
+    fields = line.split(" ")
+    if fields[0] == "FIQ:":
+      continue
+    if fields[0] == "Err:":
+      break
+    for n in range(1, nproc + 1):
+      irq += int(fields[n])
 
   storage[1] = (time.time(), irq)
 
@@ -1600,7 +1606,7 @@ def main(args):
     HARDWARE.GetThresholdValues(UFT, STATS_THRESHOLD_CLEAR)
 
   getBCM283X(BCM, STATS_WITH_VOLTS)
-  getIRQ(IRQ)
+  getIRQ(IRQ, sysinfo)
   getNetwork(NET, INTERFACE)
 
   STATS_NETWORK = (NET[1][1] != "")
@@ -1655,7 +1661,7 @@ def main(args):
       HARDWARE.GetThresholdValues(UFT, STATS_THRESHOLD_CLEAR)
 
     getBCM283X(BCM, STATS_WITH_VOLTS)
-    getIRQ(IRQ)
+    getIRQ(IRQ, sysinfo)
 
     if STATS_NETWORK:
       getNetwork(NET, INTERFACE)
-- 
2.14.1


From 19097fd9d91dd69ce05fe10957dfb72a2c470fcb Mon Sep 17 00:00:00 2001
From: MilhouseVH <milhouseVH.github@nmacleod.com>
Date: Fri, 26 Apr 2019 18:30:49 +0100
Subject: [PATCH 4/5] pmic: measure temp with option t

---
 bcmstat.sh | 74 ++++++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 16 deletions(-)

diff --git a/bcmstat.sh b/bcmstat.sh
index 1db3df4..50fa11c 100755
--- a/bcmstat.sh
+++ b/bcmstat.sh
@@ -51,7 +51,8 @@ VCDBGCMD = None
 GPU_ALLOCATED_R = None
 GPU_ALLOCATED_M = None
 SUDO = ""
-TMAX = 0.0
+TCMAX = 0.0
+TPMAX = 0.0
 LIMIT_TEMP = True
 COLOUR = False
 SYSINFO = {}
@@ -570,15 +571,27 @@ def getNetwork(storage, interface):
     dTX = cTX - pTX
     storage[0] = (dTime, [int(dRX/dTime), int(dTX/dTime), dRX, dTX])
 
-def getBCM283X(storage, STATS_WITH_VOLTS):
-  global TMAX, LIMIT_TEMP
+def getBCM283X(storage, STATS_WITH_VOLTS, STATS_WITH_PMIC_TEMP):
+  global TCMAX, LIMIT_TEMP, TPMAX
   #Grab temp - ignore temps of 85C as this seems to be an occasional aberration in the reading
   tCore = float(readfile("/sys/class/thermal/thermal_zone0/temp"))
   tCore = 0 if tCore < 0 else tCore
   if LIMIT_TEMP:
-    TMAX  = tCore if (tCore > TMAX and tCore < 85000) else TMAX
+    TCMAX = tCore if (tCore > TCMAX and tCore < 85000) else TCMAX
   else:
-    TMAX  = tCore if tCore > TMAX else TMAX
+    TCMAX = tCore if tCore > TCMAX else TCMAX
+
+  if STATS_WITH_PMIC_TEMP:
+    tPMIC = vcgencmd("measure_temp pmic", split=False)
+    if tPMIC.find("error") != -1:
+      tPMIC = None
+      tPMIC_MAX = None
+    else:
+      tPMIC = float(tPMIC.split("=")[1].replace("'C",""))
+      TPMAX = tPMIC if tPMIC > TPMAX else TPMAX
+  else:
+    tPMIC = None
+    tPMIC_MAX = None
 
   if STATS_WITH_VOLTS:
     volts = vcgencmd("measure_volts core")
@@ -594,8 +607,8 @@ def getBCM283X(storage, STATS_WITH_VOLTS):
                 [int(vcgencmd("measure_clock arm")) + 500000,
                  int(vcgencmd("measure_clock core")) + 500000,
                  int(vcgencmd("measure_clock h264")) + 500000,
-                 tCore,
-                 TMAX,
+                 tCore, TCMAX,
+                 tPMIC, TPMAX,
                  volts])
 
   if storage[2][0] != 0:
@@ -908,8 +921,15 @@ def ShowHeadings(display_flags, sysinfo):
     HDR1 = "%s Vcore " % HDR1
     HDR2 = "%s ======" % HDR2
 
-  HDR1 = "%s     ARM    Core    H264 Core Temp (Max)  IRQ/s" % HDR1
-  HDR2 = "%s ======= ======= ======= =============== ======" % HDR2
+  HDR1 = "%s     ARM    Core    H264 Core Temp (Max)" % HDR1
+  HDR2 = "%s ======= ======= ======= ===============" % HDR2
+
+  if display_flags["temp_pmic"]:
+    HDR1 = "%s PMIC Temp (Max)" % HDR1
+    HDR2 = "%s ===============" % HDR2
+
+  HDR1 = "%s  IRQ/s" % HDR1
+  HDR2 = "%s ======" % HDR2
 
   if display_flags["network"]:
     if display_flags["human_readable"]:
@@ -1016,15 +1036,26 @@ def ShowStats(display_flags, sysinfo, threshold, bcm2385, irq, network, cpuload,
   fTM = "%5.2fC" if bcm2385[4] < 100000 else "%5.1fC"
 
   if display_flags["core_volts"]:
-    LINE = "%s %s" % (LINE, bcm2385[5])
+    LINE = "%s %s" % (LINE, bcm2385[7])
 
-  LINE = "%s %s %s %s %s (%s) %s" % \
+  LINE = "%s %s %s %s %s (%s)" % \
            (LINE,
             colourise(bcm2385[0]/1000000, "%4dMhz", arm_min,     None,  arm_max, False),
             colourise(bcm2385[1]/1000000, "%4dMhz",core_min,     None, core_max, False),
             colourise(bcm2385[2]/1000000, "%4dMhz",       0, h264_min, h264_max, False),
             colourise(bcm2385[3]/1000,    fTC,         50.0,     70.0,     80.0, False),
-            colourise(bcm2385[4]/1000,    fTM,         50.0,     70.0,     80.0, False),
+            colourise(bcm2385[4]/1000,    fTM,         50.0,     70.0,     80.0, False))
+
+  if display_flags["temp_pmic"]:
+    fTC = "%5.2fC" if bcm2385[5] < 100000 else "%5.1fC"
+    fTM = "%5.2fC" if bcm2385[6] < 100000 else "%5.1fC"
+    LINE = "%s %s (%s)" % \
+             (LINE,
+              colourise(bcm2385[5],    fTC,         50.0,     70.0,     80.0, False),
+              colourise(bcm2385[6],    fTM,         50.0,     70.0,     80.0, False))
+
+  LINE = "%s %s" % \
+           (LINE,
             colourise(irq[0],             "%6s",        500,     2500,     5000, True))
 
   if display_flags["network"]:
@@ -1145,7 +1176,7 @@ def ShowStats(display_flags, sysinfo, threshold, bcm2385, irq, network, cpuload,
   printn("\n%s" % LINE)
 
 def ShowHelp():
-  print("Usage: %s [c|m] [d#] [H#] [i <iface>] [k] [L|N|M] [y|Y] [x|X|r|R] [p|P] [T] [g|G] [f|F] [D][A] [s|S] [q|Q] [V|U|W|C] [Z] [h]" % os.path.basename(__file__))
+  print("Usage: %s [c|m] [d#] [H#] [i <iface>] [k] [L|N|M] [y|Y] [x|X|r|R] [p|P] [T] [t] [g|G] [f|F] [D][A] [s|S] [q|Q] [V|U|W|C] [Z] [h]" % os.path.basename(__file__))
   print()
   print("c        Colourise output (white: minimal load or usage, then ascending through green, amber and red).")
   print("m        Monochrome output (no colourise)")
@@ -1168,6 +1199,7 @@ def ShowHelp():
   print("D        Show delta memory - negative: memory allocated, positive: memory freed")
   print("A        Show accumulated delta memory - negative: memory allocated, positive: memory freed")
   print("T        Maximum temperature is normally capped at 85C - use this option to disable temperature cap")
+  print("t        Show PMIC temperature (if available, ignore if not)")
   print()
   print("V        Check version")
   print("U        Update to latest version if an update is available")
@@ -1365,6 +1397,7 @@ def main(args):
   STATS_THRESHOLD = False
   STATS_THRESHOLD_CLEAR = False
   STATS_WITH_VOLTS = False
+  STATS_WITH_PMIC_TEMP = False
   STATS_CPU_MEM = False
   STATS_UTILISATION = False
   SIMPLE_UTILISATION = False
@@ -1495,6 +1528,9 @@ def main(args):
     elif a1 == "T":
       LIMIT_TEMP = False
 
+    elif a1 == "t":
+      STATS_WITH_PMIC_TEMP = True
+
     elif a1 == "D":
       STATS_DELTAS = True
 
@@ -1605,8 +1641,12 @@ def main(args):
   if STATS_THRESHOLD:
     HARDWARE.GetThresholdValues(UFT, STATS_THRESHOLD_CLEAR)
 
-  getBCM283X(BCM, STATS_WITH_VOLTS)
+  getBCM283X(BCM, STATS_WITH_VOLTS, STATS_WITH_PMIC_TEMP)
+  if BCM[1][1][5] == None:
+    STATS_WITH_PMIC_TEMP = False
+
   getIRQ(IRQ, sysinfo)
+
   getNetwork(NET, INTERFACE)
 
   STATS_NETWORK = (NET[1][1] != "")
@@ -1642,7 +1682,8 @@ def main(args):
                    "gpu_malloc":  STATS_GPU_M,
                    "swap":        (SWAP_ENABLED and INCLUDE_SWAP),
                    "deltas":      STATS_DELTAS,
-                   "accumulated": STATS_ACCUMULATED}
+                   "accumulated": STATS_ACCUMULATED,
+                   "temp_pmic":   STATS_WITH_PMIC_TEMP}
 
   #Store peak values
   PEAKVALUES = {"01#IRQ":0, "02#RX":0, "03#TX":0}
@@ -1660,7 +1701,8 @@ def main(args):
     if STATS_THRESHOLD:
       HARDWARE.GetThresholdValues(UFT, STATS_THRESHOLD_CLEAR)
 
-    getBCM283X(BCM, STATS_WITH_VOLTS)
+    getBCM283X(BCM, STATS_WITH_VOLTS, STATS_WITH_PMIC_TEMP)
+
     getIRQ(IRQ, sysinfo)
 
     if STATS_NETWORK:
-- 
2.14.1


From 987ad9c3dd102df5c7f027f8fb0cbc6d7e989d79 Mon Sep 17 00:00:00 2001
From: MilhouseVH <milhouseVH.github@nmacleod.com>
Date: Tue, 11 Jun 2019 02:31:07 +0100
Subject: [PATCH 5/5] memory: treat SReclaimable as free mem

---
 bcmstat.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bcmstat.sh b/bcmstat.sh
index 50fa11c..6a2a549 100755
--- a/bcmstat.sh
+++ b/bcmstat.sh
@@ -629,7 +629,7 @@ def getMemory(storage, include_swap):
 
   for line in readfile("/proc/meminfo").split("\n"):
     field_groups = re.search("^(.*):[ ]*([0-9]*) .*$", line)
-    if field_groups.group(1) in ["MemFree", "Buffers", "Cached"]:
+    if field_groups.group(1) in ["MemFree", "Buffers", "Cached", "SReclaimable"]:
       MEMFREE += int(field_groups.group(2))
     elif field_groups.group(1) == "MemTotal":
       MEMTOTAL = int(field_groups.group(2))
-- 
2.14.1

