Memory and CPU Usage Script

Kay C Lan lan.kc.macmail at gmail.com
Fri Jun 6 13:38:13 CEST 2014


For the first time, on OS X, I ran out of memory. Whilst Activity
Monitor is good, it doesn’t really give you a way to log memory usage
and trace areas that may be causing concern. So I googled the problem
and discovered a LOT of answers involving php,ruby,awk,grep,pipes and
a bunch of other stuff that suggested I could roll my own with LC. So
at the bottom you’ll find a script that will output* some nice tab
formatted memory and CPU info:

Physical Memory = 16 GB
  Free       Active     Inactive      Spec.       Wired      Page In    Page Out
 2.16 GB     2.64 GB     0.79 GB     9.08 GB     1.32 GB      236138           0
     Application           Time      % CPU    % Mem      Real      Virtual
                Dock      0:17.13      0.0      1.7     0.27 GB     2.71 GB
       LiveCode661GM    105:56.16     15.7      1.4     0.22 GB     0.97 GB
          WebContent      1:05.35      0.0      1.1     0.17 GB     4.01 GB
   IconServicesAgent      0:04.12      0.0      1.0     0.16 GB     2.55 GB
             Dropbox      0:35.55      0.0      0.5    81.09 MB     0.98 GB
              BBEdit    101:41.12      0.0      0.4    66.02 MB     1.11 GB
             ManOpen      0:18.63      0.0      0.2    28.92 MB     2.49 GB
          RegExhibit      0:53.09      0.0      0.2    27.37 MB     0.73 GB
              BBEdit      0:00.09      0.0      0.0     1.73 MB     0.66 GB

Currently you’ll note that my script is a switch structure with a
single case for OS X. Win and Linux users may wish to add appropriate
code to make this work for them.

*The output above isn’t exactly what you’ll get from the script below.
In my case, all my development tools live in a folder cryptically
called ‘Development’, so I have some extra code that filters the ps
output to the top 5 most % memory + all the running development apps.
Any duplicates are removed. It will be noted that there are two
records for BBEdit, but they are different, one is the program itself
and the other is it’s crash reporter.

For the curious: BBEdit is what I feed my log files into; ManOpen is a
man page reader where I found the options for id and ps; RegExhibit is
what I use to build and test regex entries for matchText etc.

Known errors: if an app's Time exceeds 999:99.9 mins it will not be Listed

!! be careful of line breaks, there's a very long regex entry for matchText() !!
SCRIPTSCRIPTSCRIPTSCRIPT

    switch (the platform)
      case ("MacOS")
         --get the system physical memory
         put shell("sysctl -n hw.memsize") into tData
         put round(tData/1024/1024/1024,1) into tPhyMem
         --memory titles
         put "Physical Memory = " & tPhyMem & " GB" & cr & "  Free  "
& tab & " Active " & tab & "Inactive" & tab & "  Spec. " & tab & \
               "  Wired " & tab & " Page In" & tab & "Page Out" & cr
into tOutput

         --get the virtual memory
         put shell("vm_stat") into tData

         put (word -2 of line 1 of tData)/1024/1024 into tPage
         put char 1 to -2 of word -1 of line 2 of tData * tPage into tFree
         put char 1 to -2 of word -1 of line 3 of tData * tPage into tActive
         put char 1 to -2 of word -1 of line 4 of tData * tPage into tInactive
         put char 1 to -2 of word -1 of line 5 of tData * tPage into tSpec
         put char 1 to -2 of word -1 of line 7 of tData * tPage into tWire
         put char 1 to -2 of word -1 of line 20 of tData into tPageIn
         put char 1 to -2 of word -1 of line 21 of tData  into tPageOut

         if (tFree > 99) then --we want the number in GB
            put format("%5.2f",tFree/1024) & " GB" & tab after tOutput
         else --we want the numbers in MB
            put format("%5.2f",tFree) & " MB" & tab after tOutput
         end if
         if (tActive > 99) then --we want the number in GB
            put format("%5.2f",tActive/1024) & " GB" & tab after tOutput
         else --we want the numbers in MB
            put round(tActive,2) & " MB" & tab after tOutput
         end if
         if (tInactive > 99) then --we want the number in GB
            put format("%5.2f",tInactive/1024) & " GB" & tab after tOutput
         else --we want the numbers in MB
            put format("%5.2f",tInactive) & " MB" & tab after tOutput
         end if
         if (tSpec > 99) then --we want the number in GB
            put format("%5.2f",tSpec/1024) & " GB" & tab after tOutput
         else --we want the numbers in MB
            put format("%5.2f",tSpec) & " MB" & tab after tOutput
         end if
         if (tWire > 99) then --we want the number in GB
            put format("%5.2f",tWire/1024) & " GB" & tab after tOutput
         else --we want the numbers in MB
            put format("%5.2f",tWire) & " MB" & tab after tOutput
         end if
         put format("%8s",tPageIn) & tab & format("%8s",tPageOut) & cr
after tOutput


         --virtual memory titles
         put "     Application    " & tab & "   Time  " & tab & "%
CPU" & tab & "% Mem" & tab & "  Real  " & tab & "Virtual" & cr after
tOutput

         --gets the current logged in users UID (normally 501)
         put shell("id -u") into tData
         --use the UID to list only their processes in order of memory used
         put shell("ps -mvU " & tData) into tData
         --we only want a copy of the top 5
         put line 2 to 6 of tData into tData
         --go through each line and extract the info you are interested in
         repeat for each line tLine in tData
            --some lines returned include a process ‘com.apple’ which
doesn't have a .app so to make it simpler for the
            --regex I add .app at the end of the line
            if (tLine contains "com.") then
               put tLine & ".app" into tLine
            end if
            --this matchText will extract the time in use, virtual
memory, real memory, % CPU, % memory,and Name
            if (matchText(tLine, \

"\s+\d{3,4}\s\w{1,2}\s+(\d{1,3}:\d{2}\.\d{2})\s+\d+\s+\d+\s+\d+\s+(\d*)\s+(\d*)\s+\W\s+\d+\s+(\d{1,3}\.\d)\s+(\d{1,3}\.\d)\s+.*[.|/](\w*).app",
\
                  tTime, tVirtMem, tRealMem, tCPU, tMem, tName) = true) then
               put format("%20s",tName) & tab & format("%9s",tTime) &
tab & format("%5s",tCPU) & tab  & \
                     format("%5s",tMem) & tab after tOutput
               if (tRealMem/1024 > 99) then --we want the number is GB
                  put format("%5.2f",tRealMem/1024/1024) & " GB" & tab
after tOutput
               else --we want the numbers in MB
                  put format("%5.2f",tRealMem/1024) & " MB" &  tab after tOutput
               end if
               if (tVirtMem/1024 > 99) then --we want the number is GB
                  put format("%5.2f",tVirtMem/1024/1024) & " GB" & cr
after tOutput
               else --we want the numbers in MB
                  put format("%5.2f",tVirtMem/1024) & " MB" & cr after tOutput
               end if
            end if --matchText true
         end repeat
         break
      —case() —Add Win/Linux here

    —break
   end switch
   put tOutput into msg
   --tOutput now contains nicely formatted CPU and Memory data that
will display correctly in any
   --app or field that uses mono spaced fonts.

SCRIPTSCRIPTSCRIPTSCRIPT



More information about the use-livecode mailing list