mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 15:38:10 +00:00
SystemMonitor: Display processes and their threads in a tree :^)
This shows all non-main threads as children of the process they belong to. We also show the TID as that is important to distinguish the different threads in one process. Fixes #65 :skeleyak:
This commit is contained in:
parent
cd5ed44f64
commit
0a61b45b64
4 changed files with 265 additions and 54 deletions
|
@ -11,7 +11,10 @@
|
|||
#include <AK/NonnullOwnPtrVector.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Model.h>
|
||||
#include <LibGUI/ModelIndex.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
class GraphWidget;
|
||||
|
@ -20,9 +23,9 @@ class ProcessModel final : public GUI::Model {
|
|||
public:
|
||||
enum Column {
|
||||
Icon = 0,
|
||||
Name,
|
||||
PID,
|
||||
TID,
|
||||
Name,
|
||||
CPU,
|
||||
State,
|
||||
User,
|
||||
|
@ -57,10 +60,13 @@ public:
|
|||
static NonnullRefPtr<ProcessModel> create() { return adopt_ref(*new ProcessModel); }
|
||||
virtual ~ProcessModel() override = default;
|
||||
|
||||
virtual int tree_column() const override { return Column::Name; }
|
||||
virtual int row_count(GUI::ModelIndex const&) const override;
|
||||
virtual int column_count(GUI::ModelIndex const&) const override;
|
||||
virtual String column_name(int column) const override;
|
||||
virtual GUI::Variant data(GUI::ModelIndex const&, GUI::ModelRole) const override;
|
||||
virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const& parent = {}) const override;
|
||||
virtual GUI::ModelIndex parent_index(GUI::ModelIndex const&) const override;
|
||||
virtual bool is_searchable() const override { return true; }
|
||||
virtual Vector<GUI::ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, GUI::ModelIndex const& = GUI::ModelIndex()) override;
|
||||
virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
|
||||
|
@ -85,6 +91,8 @@ public:
|
|||
private:
|
||||
ProcessModel();
|
||||
|
||||
struct Process;
|
||||
|
||||
struct ThreadState {
|
||||
pid_t tid;
|
||||
pid_t pid;
|
||||
|
@ -96,6 +104,7 @@ private:
|
|||
bool kernel;
|
||||
String executable;
|
||||
String name;
|
||||
uid_t uid;
|
||||
String state;
|
||||
String user;
|
||||
String pledge;
|
||||
|
@ -120,16 +129,118 @@ private:
|
|||
unsigned file_write_bytes;
|
||||
float cpu_percent;
|
||||
float cpu_percent_kernel;
|
||||
Process& process;
|
||||
|
||||
ThreadState(Process& argument_process)
|
||||
: process(argument_process)
|
||||
{
|
||||
}
|
||||
ThreadState(ThreadState&& other) = default;
|
||||
ThreadState& operator=(ThreadState&& other)
|
||||
{
|
||||
this->tid = other.tid;
|
||||
this->pid = other.pid;
|
||||
this->ppid = other.ppid;
|
||||
this->pgid = other.pgid;
|
||||
this->sid = other.sid;
|
||||
this->time_user = other.time_user;
|
||||
this->time_kernel = other.time_kernel;
|
||||
this->kernel = other.kernel;
|
||||
this->executable = other.executable;
|
||||
this->name = other.name;
|
||||
this->uid = other.uid;
|
||||
this->state = other.state;
|
||||
this->user = other.user;
|
||||
this->pledge = other.pledge;
|
||||
this->veil = other.veil;
|
||||
this->cpu = other.cpu;
|
||||
this->priority = other.priority;
|
||||
this->amount_virtual = other.amount_virtual;
|
||||
this->amount_resident = other.amount_resident;
|
||||
this->amount_dirty_private = other.amount_dirty_private;
|
||||
this->amount_clean_inode = other.amount_clean_inode;
|
||||
this->amount_purgeable_volatile = other.amount_purgeable_volatile;
|
||||
this->amount_purgeable_nonvolatile = other.amount_purgeable_nonvolatile;
|
||||
this->syscall_count = other.syscall_count;
|
||||
this->inode_faults = other.inode_faults;
|
||||
this->zero_faults = other.zero_faults;
|
||||
this->cow_faults = other.cow_faults;
|
||||
this->unix_socket_read_bytes = other.unix_socket_read_bytes;
|
||||
this->unix_socket_write_bytes = other.unix_socket_write_bytes;
|
||||
this->ipv4_socket_read_bytes = other.ipv4_socket_read_bytes;
|
||||
this->ipv4_socket_write_bytes = other.ipv4_socket_write_bytes;
|
||||
this->file_read_bytes = other.file_read_bytes;
|
||||
this->file_write_bytes = other.file_write_bytes;
|
||||
this->cpu_percent = other.cpu_percent;
|
||||
this->cpu_percent_kernel = other.cpu_percent_kernel;
|
||||
this->process = other.process;
|
||||
|
||||
return *this;
|
||||
}
|
||||
~ThreadState() = default;
|
||||
};
|
||||
|
||||
struct Thread {
|
||||
struct Thread : public RefCounted<Thread> {
|
||||
ThreadState current_state;
|
||||
ThreadState previous_state;
|
||||
|
||||
Thread(Process& process)
|
||||
: current_state(process)
|
||||
, previous_state(process)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(Thread const& other) const
|
||||
{
|
||||
return current_state.tid == other.current_state.tid;
|
||||
}
|
||||
|
||||
bool is_main_thread() const
|
||||
{
|
||||
return current_state.tid == current_state.process.pid;
|
||||
}
|
||||
};
|
||||
|
||||
HashMap<int, NonnullOwnPtr<Thread>> m_threads;
|
||||
struct Process {
|
||||
pid_t pid;
|
||||
NonnullRefPtrVector<Thread> threads;
|
||||
|
||||
bool operator==(Process const& other) const
|
||||
{
|
||||
return this->pid == other.pid;
|
||||
}
|
||||
|
||||
NonnullRefPtr<Thread> main_thread() const
|
||||
{
|
||||
return *threads.first_matching([this](auto const thread) { return thread->current_state.tid == pid; }).value();
|
||||
}
|
||||
|
||||
// Return anything but the main thread; therefore, valid indices are anything up to threads.size()-1 exclusive.
|
||||
Thread const& non_main_thread(size_t index) const
|
||||
{
|
||||
auto main_thread_index = -1;
|
||||
for (size_t i = 0; i < threads.size(); ++i) {
|
||||
if (threads[i].is_main_thread()) {
|
||||
main_thread_index = static_cast<int>(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
VERIFY(main_thread_index >= 0);
|
||||
// Shift all indices starting from the main thread's index upwards, so that the user doesn't have to worry about index discontinuities.
|
||||
if (index >= static_cast<size_t>(main_thread_index))
|
||||
return threads[index + 1];
|
||||
return threads[index];
|
||||
}
|
||||
};
|
||||
|
||||
GUI::Icon icon_for(Thread const& thread) const;
|
||||
|
||||
int thread_model_row(Thread const& thread) const;
|
||||
|
||||
// The thread list contains the same threads as the Process structs.
|
||||
HashMap<int, NonnullRefPtr<Thread>> m_threads;
|
||||
NonnullOwnPtrVector<Process> m_processes;
|
||||
NonnullOwnPtrVector<CpuInfo> m_cpus;
|
||||
Vector<int> m_tids;
|
||||
RefPtr<Core::File> m_proc_all;
|
||||
GUI::Icon m_kernel_process_icon;
|
||||
u64 m_total_time_scheduled { 0 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue