fit-statUSB: Perl App for Displaying Memory Usage

programmable multi-color LED that plugs into a USB port
Post Reply
hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

fit-statUSB: Perl App for Displaying Memory Usage

Post by hassellbear »

Overview

System performance is often negatively impacted by having inadequate available memory. This is especially true when HDD virtual memory comes into play, or when poorly written software products don't properly release unused memory back to the system. Even older non-state of the art systems can often enjoy a performance boost when available system memory is increased either by adding ram or by using installed ram more efficiently.

Consequently, it is important to monitor memory usage, especially when poor system performance is encountered. The following perl app provides a simple visual method for monitoring memory usage via the brightness of fit-statUSB. The brighter fit-statUSB appears the higher the level of ram usage.



Examples

App Initialization : Maximum Brightness
InitializeMem.jpeg
InitializeMem.jpeg (22.39 KiB) Viewed 21175 times

Low Memory Usage: Dim fit-statUSB
LowMem.jpeg
LowMem.jpeg (20.21 KiB) Viewed 21175 times

Medium Memory Usage: Moderately Bright fit-statUSB
MedMem.jpeg
MedMem.jpeg (22.95 KiB) Viewed 21175 times



Perl App Listing

#!/usr/bin/perl -w
#
#fitletMemUse.pl

# Darryl Hassell 8/7/2018

use Tk;
use List::MoreUtils qw(first_index);
use Device::SerialPort;
use Time::HiRes ('sleep');

# Define & Initialize Variables

my $Mem_Use_Percent = 'Testing';
my $Mem_Used = 0;
my $Mem_Total = 1;
our $Mem_Use_BrightColor_Hex_fit = '#0000FF';
our $Mem_Use_BrightColor_Hex_screen = '#0000FF';
my $Bright_Dec_fit = 255;
my $Bright_Dec_scrren = 255;
my $Bright_Hex_fit = FF;
my $Bright_Hex_screen = FF;

# Configure Serial Port
my $port = Device::SerialPort -> new("/dev/ttyACM0");
$port -> baudrate(115200);
$port -> databits(8);
$port -> parity("none");
$port -> stopbits(1);
$port -> write_settings;

# Set Sleep Delay After Writes in Seconds
our $delay = 0.1;

# Initialize fit-statUSB to Max Brightness Red
$port -> write("#0000FF\n");
$port -> purge_rx;

# Create Main Window
$WinMain = MainWindow->new;
$WinMain -> resizable (0,0);

# Label Main Window
$myframe = $WinMain -> Frame ->pack();
$Mem_Use_Label = $myframe -> Label (-text => 'fitlet Memory Use Monitor'."\n"."$Mem_Use_Percent".' % ', -background => "$Mem_Use_BrightColor_Hex_screen", -foreground => "white", -font => "helvetica 9 bold")->pack(-ipadx => 20,-ipady => 20);


# Exit Program Button
$stopbutton = $myframe -> Button (-text => 'Exit', -command => \&Exit, -foreground => 'black', -background => 'lightblue');
$stopbutton -> pack (-side => 'bottom');


$WinMain -> repeat(2000, \&GetMemUse);

MainLoop();

################################ Exit Program ################################

sub Exit {

$port -> write("#000000\n");
$port -> purge_rx;
sleep($delay);
exit;
}

################################ Memory Usage Data ################################

sub GetMemUse {

$Mem_Use_Unfiltered1 = `free`;

# Read Unfiltered Memory Usage Data Into Array
@Mem_Use_Array1 = split ' ', $Mem_Use_Unfiltered1;

# Determine Array Index of String Mem:
my $ArrayIndexTotal1 = first_index { $_ eq 'Mem:' } @Mem_Use_Array1;

# Determine Array Index of Total Memory
my $ArrayIndexTotal2 = $ArrayIndexTotal1 + 1;

# Read Total Memory Out Of Array
$Mem_Total = $Mem_Use_Array1[$ArrayIndexTotal2];

# Determine Array Index of String buffers/cache:
my $ArrayIndexUsed1 = first_index { $_ eq 'buffers/cache:' } @Mem_Use_Array1;

# Determine Array Index of Memory Used
my $ArrayIndexUsed2 = $ArrayIndexUsed1 + 1;

# Read Memory Used Out Of Array
$Mem_Used = $Mem_Use_Array1[$ArrayIndexUsed2];

# Calculate Memory Usage Percentage Rounded to 1 Decimal Place
$Mem_Use_Percent = sprintf("%.1f",($Mem_Used/$Mem_Total)*100);

# Calculate Decimal fit-statUSB Brightness from 0 to 255
$Bright_Dec_fit = sprintf("%.0f",($Mem_Used/$Mem_Total)*255);

#Convert Decimal fit-statUSB Brightness to Hexidecimal
$Bright_Hex_fit = sprintf("%X", $Bright_Dec_fit);

# Generate fit-statUSB Color and Brightness Hex String
$Mem_Use_BrightColor_Hex_fit = '#'.'00'.'00'.$Bright_Hex_fit;

# Calculate Decimal Screen Window Brightness from 175 to 255
$Bright_Dec_screen = sprintf("%.0f",175 + ($Mem_Used/$Mem_Total)*80);

#Convert Decimal Screen Window Brightness to Hexidecimal
$Bright_Hex_screen = sprintf("%X", $Bright_Dec_screen);

# Generate fit-statUSB Color and Brightness Hex String
$Mem_Use_BrightColor_Hex_fit = '#'.'00'.'00'.$Bright_Hex_fit;

# Generate Screen Window Color and Brightness Hex String
$Mem_Use_BrightColor_Hex_screen = '#'.'00'.'00'.$Bright_Hex_screen;


# Send Color and Brightness to fit-statUSB
$port -> write("$Mem_Use_BrightColor_Hex_fit\n");
$port -> purge_rx;

# Prints of Values For Debugging
# print("$Bright_Dec\n");
# print("$Bright_Hex\n");
# print("$Mem_Use_BrightColor_Hex\n");
# print("\n");

$Mem_Use_Label -> configure (-text => 'fitlet Memory Use Monitor'."\n"."$Mem_Use_Percent".' % Used ', -background => "$Mem_Use_BrightColor_Hex_screen");

}




Conclusions

fit-statUSB provides a very usable visual indication of the memory usage of a system. fit-statUSB and its host fitlet RM system comprise a very versatile combination.

Post Reply

Return to “fit-statUSB”