2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-02-16 20:24:22 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-02-10 12:28:48 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-28 10:20:04 +01:00
|
|
|
#pragma once
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-02-28 10:20:04 +01:00
|
|
|
#include <AK/HashMap.h>
|
2020-06-27 22:52:13 -06:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2019-02-28 10:20:04 +01:00
|
|
|
#include <AK/Vector.h>
|
2022-04-05 00:32:37 +02:00
|
|
|
#include <LibGUI/Icon.h>
|
2020-02-06 20:33:02 +01:00
|
|
|
#include <LibGUI/Model.h>
|
2022-04-05 00:32:37 +02:00
|
|
|
#include <LibGUI/ModelIndex.h>
|
|
|
|
#include <sys/types.h>
|
2019-02-28 10:20:04 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-05-06 03:21:23 +02:00
|
|
|
class GraphWidget;
|
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class ProcessModel final : public GUI::Model {
|
2019-02-28 10:20:04 +01:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Column {
|
2019-03-09 13:33:52 +01:00
|
|
|
Icon = 0,
|
2022-04-05 00:32:37 +02:00
|
|
|
Name,
|
2021-04-05 13:36:43 +02:00
|
|
|
PID,
|
2022-04-05 00:23:01 +02:00
|
|
|
TID,
|
2019-03-09 13:33:52 +01:00
|
|
|
CPU,
|
|
|
|
State,
|
|
|
|
User,
|
2019-06-07 12:44:29 +02:00
|
|
|
Virtual,
|
2019-12-29 12:28:32 +01:00
|
|
|
DirtyPrivate,
|
2021-04-05 13:36:43 +02:00
|
|
|
Pledge,
|
|
|
|
Physical,
|
2019-12-29 12:45:58 +01:00
|
|
|
CleanInode,
|
2019-12-09 19:16:58 +01:00
|
|
|
PurgeableVolatile,
|
|
|
|
PurgeableNonvolatile,
|
2020-01-21 18:56:23 +01:00
|
|
|
Veil,
|
2021-04-05 13:36:43 +02:00
|
|
|
Processor,
|
|
|
|
Priority,
|
|
|
|
PPID,
|
|
|
|
PGID,
|
|
|
|
SID,
|
2019-04-17 14:48:55 +02:00
|
|
|
Syscalls,
|
2019-10-02 14:13:49 +02:00
|
|
|
InodeFaults,
|
|
|
|
ZeroFaults,
|
|
|
|
CowFaults,
|
2019-12-01 17:36:06 +01:00
|
|
|
FileReadBytes,
|
|
|
|
FileWriteBytes,
|
|
|
|
UnixSocketReadBytes,
|
|
|
|
UnixSocketWriteBytes,
|
|
|
|
IPv4SocketReadBytes,
|
|
|
|
IPv4SocketWriteBytes,
|
2022-10-29 21:49:51 +02:00
|
|
|
Command,
|
2019-03-09 13:33:52 +01:00
|
|
|
__Count
|
|
|
|
};
|
|
|
|
|
2019-10-02 20:26:19 +02:00
|
|
|
static ProcessModel& the();
|
|
|
|
|
2021-04-23 16:46:57 +02:00
|
|
|
static NonnullRefPtr<ProcessModel> create() { return adopt_ref(*new ProcessModel); }
|
2022-02-10 12:28:48 -07:00
|
|
|
virtual ~ProcessModel() override = default;
|
2019-02-28 10:20:04 +01:00
|
|
|
|
2022-04-05 00:32:37 +02:00
|
|
|
virtual int tree_column() const override { return Column::Name; }
|
2021-09-07 18:14:47 +02:00
|
|
|
virtual int row_count(GUI::ModelIndex const&) const override;
|
|
|
|
virtual int column_count(GUI::ModelIndex const&) const override;
|
2022-12-04 18:02:33 +00:00
|
|
|
virtual DeprecatedString column_name(int column) const override;
|
2021-09-07 18:14:47 +02:00
|
|
|
virtual GUI::Variant data(GUI::ModelIndex const&, GUI::ModelRole) const override;
|
2022-04-05 00:32:37 +02:00
|
|
|
virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const& parent = {}) const override;
|
|
|
|
virtual GUI::ModelIndex parent_index(GUI::ModelIndex const&) const override;
|
2021-09-03 21:30:45 +02:00
|
|
|
virtual bool is_searchable() const override { return true; }
|
2021-11-11 00:55:02 +01:00
|
|
|
virtual Vector<GUI::ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, GUI::ModelIndex const& = GUI::ModelIndex()) override;
|
2021-05-15 00:15:09 +01:00
|
|
|
virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
|
2021-05-25 14:13:19 +00:00
|
|
|
void update();
|
2022-08-30 15:25:11 +03:00
|
|
|
bool is_default_column(int index) const;
|
2019-02-28 10:20:04 +01:00
|
|
|
|
2020-07-11 21:08:40 +01:00
|
|
|
struct CpuInfo {
|
2020-06-27 22:52:13 -06:00
|
|
|
u32 id;
|
2020-07-11 21:08:40 +01:00
|
|
|
float total_cpu_percent { 0.0 };
|
2021-01-04 20:14:52 -07:00
|
|
|
float total_cpu_percent_kernel { 0.0 };
|
2020-07-11 21:08:40 +01:00
|
|
|
|
2021-02-16 20:24:22 +01:00
|
|
|
explicit CpuInfo(u32 id)
|
2020-07-11 21:08:40 +01:00
|
|
|
: id(id)
|
2020-06-27 22:52:13 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 18:14:47 +02:00
|
|
|
Function<void(NonnullOwnPtrVector<CpuInfo> const&)> on_cpu_info_change;
|
2021-04-04 20:46:46 +02:00
|
|
|
Function<void(int process_count, int thread_count)> on_state_update;
|
2020-06-27 22:52:13 -06:00
|
|
|
|
2021-09-07 18:14:47 +02:00
|
|
|
NonnullOwnPtrVector<CpuInfo> const& cpus() const { return m_cpus; }
|
2019-05-06 03:21:23 +02:00
|
|
|
|
2019-10-02 20:26:19 +02:00
|
|
|
private:
|
|
|
|
ProcessModel();
|
2019-03-20 03:27:07 +01:00
|
|
|
|
2022-04-05 00:32:37 +02:00
|
|
|
struct Process;
|
|
|
|
|
2019-11-26 21:25:45 +01:00
|
|
|
struct ThreadState {
|
2022-04-06 21:02:40 +02:00
|
|
|
pid_t tid { 0 };
|
|
|
|
pid_t pid { 0 };
|
|
|
|
pid_t ppid { 0 };
|
|
|
|
pid_t pgid { 0 };
|
|
|
|
pid_t sid { 0 };
|
|
|
|
u64 time_user { 0 };
|
|
|
|
u64 time_kernel { 0 };
|
|
|
|
bool kernel { false };
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString executable { "" };
|
|
|
|
DeprecatedString name { "" };
|
|
|
|
DeprecatedString command { "" };
|
2022-04-06 21:02:40 +02:00
|
|
|
uid_t uid { 0 };
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString state { "" };
|
|
|
|
DeprecatedString user { "" };
|
|
|
|
DeprecatedString pledge { "" };
|
|
|
|
DeprecatedString veil { "" };
|
2022-04-06 21:02:40 +02:00
|
|
|
u32 cpu { 0 };
|
|
|
|
u32 priority { 0 };
|
|
|
|
size_t amount_virtual { 0 };
|
|
|
|
size_t amount_resident { 0 };
|
|
|
|
size_t amount_dirty_private { 0 };
|
|
|
|
size_t amount_clean_inode { 0 };
|
|
|
|
size_t amount_purgeable_volatile { 0 };
|
|
|
|
size_t amount_purgeable_nonvolatile { 0 };
|
|
|
|
unsigned syscall_count { 0 };
|
|
|
|
unsigned inode_faults { 0 };
|
|
|
|
unsigned zero_faults { 0 };
|
|
|
|
unsigned cow_faults { 0 };
|
|
|
|
unsigned unix_socket_read_bytes { 0 };
|
|
|
|
unsigned unix_socket_write_bytes { 0 };
|
|
|
|
unsigned ipv4_socket_read_bytes { 0 };
|
|
|
|
unsigned ipv4_socket_write_bytes { 0 };
|
|
|
|
unsigned file_read_bytes { 0 };
|
|
|
|
unsigned file_write_bytes { 0 };
|
|
|
|
float cpu_percent { 0 };
|
|
|
|
float cpu_percent_kernel { 0 };
|
2022-04-05 00:32:37 +02:00
|
|
|
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;
|
2022-10-29 21:49:51 +02:00
|
|
|
this->command = other.command;
|
2022-04-05 00:32:37 +02:00
|
|
|
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;
|
2019-02-28 10:20:04 +01:00
|
|
|
};
|
|
|
|
|
2022-04-05 00:32:37 +02:00
|
|
|
struct Thread : public RefCounted<Thread> {
|
2019-11-26 21:25:45 +01:00
|
|
|
ThreadState current_state;
|
|
|
|
ThreadState previous_state;
|
2022-04-05 00:32:37 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Process {
|
|
|
|
pid_t pid;
|
|
|
|
NonnullRefPtrVector<Thread> threads;
|
|
|
|
|
|
|
|
bool operator==(Process const& other) const
|
|
|
|
{
|
|
|
|
return this->pid == other.pid;
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:55:20 +02:00
|
|
|
Optional<NonnullRefPtr<Thread>> main_thread() const
|
2022-04-05 00:32:37 +02:00
|
|
|
{
|
2022-04-06 14:55:20 +02:00
|
|
|
return threads.first_matching([this](auto const thread) { return thread->current_state.tid == pid; });
|
2022-04-05 00:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
2019-02-28 10:20:04 +01:00
|
|
|
};
|
|
|
|
|
2022-04-05 00:32:37 +02:00
|
|
|
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;
|
2020-06-27 22:52:13 -06:00
|
|
|
NonnullOwnPtrVector<CpuInfo> m_cpus;
|
2021-01-03 10:13:33 -07:00
|
|
|
RefPtr<Core::File> m_proc_all;
|
2021-04-06 17:18:43 +02:00
|
|
|
GUI::Icon m_kernel_process_icon;
|
2021-07-14 21:46:32 -06:00
|
|
|
u64 m_total_time_scheduled { 0 };
|
|
|
|
u64 m_total_time_scheduled_kernel { 0 };
|
|
|
|
bool m_has_total_scheduled_time { false };
|
2019-02-28 10:20:04 +01:00
|
|
|
};
|