Skip to content

Commit

Permalink
Fix slow processes list update (#130)
Browse files Browse the repository at this point in the history
* Fix slow processes list update
  After fix update time 20-60ms
  fix #129

Co-authored-by: Nathaniel van Diepen <[email protected]>
  • Loading branch information
coozoo and Eeems authored Jan 6, 2021
1 parent 59074e4 commit 6f3d9a9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
23 changes: 22 additions & 1 deletion applications/process-manager/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "controller.h"
#include "taskitem.h"

#include <QIODevice>
#include <QFile>
#include <QTextStream>
Expand All @@ -8,6 +9,8 @@
#include <QDir>
#include <QSet>
#include <QDebug>
#include <QMutableListIterator>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand All @@ -30,14 +33,16 @@ void Controller::sortBy(QString key){
}

QList<QObject*> Controller::getTasks(){
tasks.clear();
QDir directory("/proc");
if (!directory.exists() || directory.isEmpty()){
tasks.clear();
qCritical() << "Unable to access /proc";
return tasks;
}
directory.setFilter( QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
auto processes = directory.entryInfoList(QDir::NoFilter, QDir::SortFlag::Name);
// Get all pids we care about
QList<int> pids;
for(QFileInfo fi : processes){
std::string pid = fi.baseName().toStdString();
if(!is_uint(pid)){
Expand All @@ -55,6 +60,22 @@ QList<QObject*> Controller::getTasks(){
if(content == "0 0 0 0 0 0 0"){
continue;
}
pids.append(stoi(pid));
}
// Remove any pids that we've already got cached,
// and remove any cached items that no longer exist
QMutableListIterator<QObject*> i(tasks);
while(i.hasNext()){
auto taskItem = reinterpret_cast<TaskItem*>(i.next());
const auto pid = taskItem->pid();
if(!pids.contains(pid)){
i.remove();
}else{
pids.removeAll(pid);
}
}
// Create TaskItem instances for all new tasks
for(auto pid : pids){
auto taskItem = new TaskItem(pid);
auto taskPid = taskItem->pid();
taskItem->setKillable(taskPid != getpid() && taskPid != getppid() && taskPid != protectPid);
Expand Down
32 changes: 28 additions & 4 deletions applications/process-manager/taskitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,37 @@ string exec(const char* cmd) {
return result;
}

TaskItem::TaskItem(const std::string& pid) : QObject(nullptr), _pid(stoi(pid)){
std::string path = "/proc/" + to_string(_pid) + "/status";
_name = QString::fromStdString(exec(("cat " + path + " | grep Name: | awk '{print$2}'").c_str())).trimmed();
_ppid = stoi(exec(("cat " + path + " | grep PPid: | awk '{print$2}'").c_str()));
TaskItem::TaskItem(int pid) : QObject(nullptr), _pid(pid){
QString file_content=readFile(QString::fromStdString("/proc/" + to_string(_pid) + "/status"));
_name = parseRegex(file_content,QRegularExpression("^Name:\\t+(\\w+)"));
_ppid = parseRegex(file_content,QRegularExpression("^PPid:\\t+(\\d+)",QRegularExpression::MultilineOption)).toInt();
_killable = _ppid;
}

bool TaskItem::signal(int signal){
return kill(_pid, signal);
}

QString TaskItem::parseRegex(QString &file_content, const QRegularExpression &reg){
QRegularExpressionMatchIterator i = reg.globalMatch(file_content);
if (!i.isValid()){
return "";
}
QString result="";
while (i.hasNext()){
QRegularExpressionMatch match = i.next();
result=match.captured(1);
}
return result;
}

QString TaskItem::readFile(const QString &path){
QFile file(path);
if(!file.open(QIODevice::ReadOnly)){
qDebug()<<"Error reading file: " + path;
return "";
}
auto data = file.readAll();
file.close();
return data;
}
7 changes: 6 additions & 1 deletion applications/process-manager/taskitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <QObject>
#include <QFileInfo>
#include <string>
#include <QFile>
#include <QDebug>
#include <QRegularExpression>

class TaskItem : public QObject {
Q_OBJECT
public:
explicit TaskItem(const std::string& pid);
explicit TaskItem(int pid);
Q_PROPERTY(QString name MEMBER _name NOTIFY nameChanged);
Q_PROPERTY(int pid MEMBER _pid READ pid NOTIFY pidChanged);
Q_PROPERTY(int ppid MEMBER _ppid NOTIFY ppidChanged);
Expand All @@ -33,6 +36,8 @@ class TaskItem : public QObject {
int _pid;
int _ppid;
bool _killable;
QString readFile(const QString &path);
QString parseRegex(QString &file_content, const QRegularExpression &reg);
};

#endif // TASKITEM_H

0 comments on commit 6f3d9a9

Please sign in to comment.