Skip to content

Commit

Permalink
Keep files open while running.
Browse files Browse the repository at this point in the history
Instead of opening and closing files in /proc, just keep them open
and seek to the beginning each time their data is needed.  Close the
files when exiting.
  • Loading branch information
bdubbs committed Oct 16, 2023
1 parent 015b0b9 commit 6a0e50e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

moc*
*Make*
make-release.sh
src/sysmon-qt
*.sav*
notes
Expand Down
Binary file removed src/sysmon-qt
Binary file not shown.
54 changes: 32 additions & 22 deletions src/sysmon-qt.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <QAction>
#include <QApplication>
#include <QDateTime>
#include <QFile>
#include <QHostInfo>
#include <QRegularExpression>
#include <QTime>
#include <QTimer>
// #include <QMessageBox>

#include "sysmon-qt.h"
#include "sm_config.h"
Expand Down Expand Up @@ -36,6 +34,19 @@ sysmon_qt::sysmon_qt()
font_size = settings.value( "fontSize" , 12 ).toInt();
font_normal = QFont( font_family, font_size );

// Files
fUptime = new QFile( "/proc/uptime" );
fUptime ->open( QIODevice::ReadOnly | QIODevice::Text);

fLoadAvg = new QFile( "/proc/loadavg" );
fLoadAvg->open( QIODevice::ReadOnly | QIODevice::Text);

fMemInfo = new QFile( "/proc/meminfo" );
fMemInfo->open( QIODevice::ReadOnly | QIODevice::Text);

fStat = new QFile( "/proc/stat" );
fStat ->open( QIODevice::ReadOnly | QIODevice::Text);

// Set up palettes
set_palettes();

Expand Down Expand Up @@ -98,6 +109,11 @@ sysmon_qt::~sysmon_qt()
settings.setValue( "positionX", QString::number( position.x() ) );
settings.setValue( "positionY", QString::number( position.y() ) );
settings.sync();

if ( fUptime ->isOpen() ) fUptime ->close();
if ( fLoadAvg->isOpen() ) fLoadAvg->close();
if ( fMemInfo->isOpen() ) fMemInfo->close();
if ( fStat ->isOpen() ) fStat ->close();
}

void sysmon_qt::setup_all()
Expand Down Expand Up @@ -178,12 +194,10 @@ void sysmon_qt::update_uptime( void )
if ( lbl_uptime == nullptr ) return;
if ( tick % 60 != 0 ) return; // Update once a minute

QFile file( "/proc/uptime" );
file.open( QIODevice::ReadOnly | QIODevice::Text);
QByteArray ba = file.readLine();
file.close();
fUptime->seek( 0 );
QByteArray baUptime = fUptime->readLine();

QString lineString = QString( ba );
QString lineString = QString( baUptime );
QString upSecs = lineString.left( lineString.indexOf(".") );

uint secs = upSecs.toInt();
Expand All @@ -204,7 +218,8 @@ void sysmon_qt::setup_cpuLoad()
bool useCPU = settings.value( "useCPU", true ).toBool();
bool useCPUbar = settings.value( "useCPUbar", true ).toBool();

lbl_cpu = nullptr;
lbl_cpu = nullptr;
lbl_loads = nullptr;

if ( ! useCPU && ! useCPUbar ) return;

Expand Down Expand Up @@ -247,12 +262,10 @@ void sysmon_qt::update_cpu( void )

if ( lbl_loads != nullptr )
{
QFile file( "/proc/loadavg" );
file.open( QIODevice::ReadOnly | QIODevice::Text);
QByteArray ba2 = file.readLine();
file.close();
fLoadAvg->seek( 0 );
QByteArray ba = fLoadAvg->readLine();

QString lineString = QString( ba2 );
QString lineString = QString( ba );
QStringList loadList = lineString.split( QLatin1Char(' ') );
QString CPU_loads = loadList[0] + " " +
loadList[1] + " " +
Expand Down Expand Up @@ -297,9 +310,8 @@ void sysmon_qt::update_memory( void )
if ( lbl_memory == nullptr ) return;
if ( tick % memory_refresh != 0 ) return;

QFile file( "/proc/meminfo" );
file.open( QIODevice::ReadOnly | QIODevice::Text);
QTextStream textStream( &file );
fMemInfo->seek( 0 );
QTextStream textStream( fMemInfo );

QString MemTotal;
QString MemAvailable;
Expand All @@ -314,7 +326,6 @@ void sysmon_qt::update_memory( void )
break;
}
}
file.close();

QRegularExpression re( "\\d+" );
QRegularExpressionMatch match = re.match( MemTotal );
Expand Down Expand Up @@ -591,7 +602,6 @@ void sysmon_qt::update_cpu_percentage( void )
// Could possibly do it every cpu_refresh time (3 seconds for now)
static qint64 previousJiffiesWork;
static qint64 previousJiffiesTotal;
static QFile* file;

QString cpu; // Not really used -- always "cpu"
qint64 user, // jiffies
Expand All @@ -602,16 +612,16 @@ void sysmon_qt::update_cpu_percentage( void )
// First time through only
if ( previousJiffiesWork == 0 )
{
file = new QFile( "/proc/stat" );
file->open( QIODevice::ReadOnly | QIODevice::Text);
QTextStream stream( file );
fStat->seek( 0 );
QTextStream stream( fStat );
stream >> cpu >> user >> nice >> system >> idle;

previousJiffiesWork = user + nice + system;
previousJiffiesTotal = previousJiffiesWork + idle;
return;
}

QTextStream stream( file );
QTextStream stream( fStat );
stream.seek( 0 ); // Just read the first line each time
stream >> cpu >> user >> nice >> system >> idle;

Expand Down
44 changes: 25 additions & 19 deletions src/sysmon-qt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SYSMONQT_H

#include <QFont>
#include <QFile>
#include <QLabel>
#include <QLayout>
#include <QMainWindow>
Expand Down Expand Up @@ -29,25 +30,30 @@ public slots:
QLabel* textlabel( const QString&, int = -1, int = QFont::Normal );
QLabel* banner ( const QString&, int = 0, int = QFont::Bold );

int tick = 0; // tick every second
int cpu_refresh = 3;
int memory_refresh = 3;
int temp_refresh = 5;
int tempsCount;
int cpuPercentage;
bool pauseUpdate = false;
bool mFrame = false; // turn frame on and off
bool pauseTempUpdate = false; // turn temperature update on and off
QString TZ; // time zone
QPoint position; // location of widget

QLabel* lbl_hostname;
QLabel* lbl_time; // time of day
QLabel* lbl_date;
QLabel* lbl_uptime;
QLabel* lbl_cpu; // title
QLabel* lbl_loads; // label for memory
QLabel* lbl_memory; // title
int tick = 0; // tick every second
int cpu_refresh = 3;
int memory_refresh = 3;
int temp_refresh = 5;
int tempsCount;
int cpuPercentage;
bool pauseUpdate = false;
bool mFrame = false; // turn frame on and off
bool pauseTempUpdate = false; // turn temperature update on and off
QString TZ; // time zone
QPoint position; // location of widget

QLabel* lbl_hostname;
QLabel* lbl_time; // time of day
QLabel* lbl_date;
QLabel* lbl_uptime;
QLabel* lbl_cpu; // title
QLabel* lbl_loads; // label for memory
QLabel* lbl_memory; // title

QFile* fUptime;
QFile* fLoadAvg;
QFile* fMemInfo;
QFile* fStat;

QProgressBar* memory;
QProgressBar* load;
Expand Down

0 comments on commit 6a0e50e

Please sign in to comment.