diff options
author | Ken Moore <ken@pcbsd.org> | 2015-05-22 12:11:01 -0400 |
---|---|---|
committer | Ken Moore <ken@pcbsd.org> | 2015-05-22 12:11:01 -0400 |
commit | 766980aeaba64e23001c7437220dd67afd70c052 (patch) | |
tree | dad60ac664a3598aee13d0801b3334aeea0d1220 /libLumina | |
parent | Add a few new functions to LuminaOS: (diff) | |
download | lumina-766980aeaba64e23001c7437220dd67afd70c052.tar.gz lumina-766980aeaba64e23001c7437220dd67afd70c052.tar.bz2 lumina-766980aeaba64e23001c7437220dd67afd70c052.zip |
Finish cleaning up the new CPU/Memory desction routines in libLumina, and add a new desktop plugin for monitoring system statistics (CPU/Mem usage, CPU temps).
Diffstat (limited to 'libLumina')
-rw-r--r-- | libLumina/LuminaOS-FreeBSD.cpp | 31 | ||||
-rw-r--r-- | libLumina/LuminaUtils.cpp | 28 |
2 files changed, 36 insertions, 23 deletions
diff --git a/libLumina/LuminaOS-FreeBSD.cpp b/libLumina/LuminaOS-FreeBSD.cpp index 0843b1ce..3bf857d9 100644 --- a/libLumina/LuminaOS-FreeBSD.cpp +++ b/libLumina/LuminaOS-FreeBSD.cpp @@ -257,25 +257,38 @@ QString LOS::FileSystemCapacity(QString dir) { //Return: percentage capacity as QStringList LOS::CPUTemperatures(){ //Returns: List containing the temperature of any CPU's ("50C" for example) QStringList temps = LUtils::getCmdOutput("sysctl -ai").filter(".temperature:"); + temps.sort(); for(int i=0; i<temps.length(); i++){ - temps[i] = temps[i].section(":",1,5).simplified(); //only pull out the value, not the variable + if(temps[i].contains(".acpi.") || temps[i].contains(".cpu")){ + temps[i] = temps[i].section(":",1,5).simplified(); //only pull out the value, not the variable + }else{ + //non CPU temperature - skip it + temps.removeAt(i); i--; + } } return temps; } int LOS::CPUUsagePercent(){ //Returns: Overall percentage of the amount of CPU cycles in use (-1 for errors) - QStringList info = LUtils::getCmdOutput("iostat -t proc -Cd"); - //Output: [cpu header, column headers, values(us ni sy in id)] - if(info.length()==3){ - //idle value is the last one, use 100% minus that (don't worry about usage breakdown) - return (100 - info[2].section(" ",4,4,QString::SectionSkipEmpty).toInt()); - }else{ - return -1; //error + //qDebug() << "Get CPU usage"; + QStringList info = LUtils::getCmdOutput("iostat",QStringList() <<"-c"<<"2"<<"-t"<<"proc"<<"-w"<<"0.2"); + if(info.length()<4){return -1;} + //Only need the idle percentage (last number on the 4th line) + info = info[3].split(" ",QString::SkipEmptyParts); + //qDebug() << "CPU Info:" << info; + if(info.isEmpty()){ return -1; } + QString idle = info.last(); + if(idle.isEmpty()){ return -1; } + else{ + return (100 - idle.toInt() ); } } int LOS::MemoryUsagePercent(){ - QStringList mem = LUtils::getCmdOutput("top -n 0").filter("Mem: ").first().section(":",1,50).split(", "); + //qDebug() << "Get Mem Usage"; + QStringList mem = LUtils::getCmdOutput("top -n 0").filter("Mem: ", Qt::CaseInsensitive); + if(mem.isEmpty()){ return -1; } + mem = mem.first().section(":",1,50).split(", "); //Memory Labels: "Active", "Inact", "Wired", "Cache", "Buf", "Free" (usually in that order) // Format of each entry: "<number><Unit> <Label>" double fB = 0; //Free Bytes diff --git a/libLumina/LuminaUtils.cpp b/libLumina/LuminaUtils.cpp index f1c007f0..8eee76ca 100644 --- a/libLumina/LuminaUtils.cpp +++ b/libLumina/LuminaUtils.cpp @@ -26,35 +26,33 @@ QString LUtils::LuminaDesktopVersion(){ } int LUtils::runCmd(QString cmd, QStringList args){ - QProcess *proc = new QProcess; - proc->setProcessChannelMode(QProcess::MergedChannels); + QProcess proc; + proc.setProcessChannelMode(QProcess::MergedChannels); if(args.isEmpty()){ - proc->start(cmd); + proc.start(cmd); }else{ - proc->start(cmd, args); + proc.start(cmd, args); } - while(!proc->waitForFinished(300)){ + while(!proc.waitForFinished(300)){ QCoreApplication::processEvents(); } - int ret = proc->exitCode(); - delete proc; + int ret = proc.exitCode(); return ret; } QStringList LUtils::getCmdOutput(QString cmd, QStringList args){ - QProcess *proc = new QProcess; - proc->setProcessChannelMode(QProcess::MergedChannels); + QProcess proc; + proc.setProcessChannelMode(QProcess::MergedChannels); if(args.isEmpty()){ - proc->start(cmd); + proc.start(cmd); }else{ - proc->start(cmd,args); + proc.start(cmd,args); } - while(!proc->waitForFinished(300)){ + while(!proc.waitForFinished(500)){ QCoreApplication::processEvents(); } - QStringList out = QString(proc->readAllStandardOutput()).split("\n"); - delete proc; + QStringList out = QString(proc.readAllStandardOutput()).split("\n"); return out; } @@ -148,6 +146,8 @@ void LUtils::LoadTranslation(QApplication *app, QString appname){ double LUtils::DisplaySizeToBytes(QString num){ num = num.toLower().simplified(); + num = num.remove(" "); + if(num.isEmpty()){ return 0.0; } if(num.endsWith("b")){ num.chop(1); } //remove the "bytes" marker (if there is one) QString lab = "b"; if(!num[num.size()-1].isNumber()){ |