ps: Port to using CProcessStatisticsReader and /proc/all

Drop /proc/summary in the process.
We only needed one new field here, thankfully, so this was quite straightforward.
This commit is contained in:
Robin Burchell 2019-07-17 21:24:47 +02:00 committed by Andreas Kling
parent a9d1a86e6e
commit 57da716be0
Notes: sideshowbarker 2024-07-19 13:11:46 +09:00
4 changed files with 28 additions and 32 deletions

View File

@ -39,7 +39,6 @@ enum ProcFileType {
FI_Root_kmalloc,
FI_Root_all,
FI_Root_memstat,
FI_Root_summary,
FI_Root_cpuinfo,
FI_Root_inodes,
FI_Root_dmesg,
@ -541,29 +540,6 @@ ByteBuffer procfs$kmalloc(InodeIdentifier)
return builder.to_byte_buffer();
}
ByteBuffer procfs$summary(InodeIdentifier)
{
InterruptDisabler disabler;
auto processes = Process::all_processes();
StringBuilder builder;
builder.appendf("PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
for (auto* process : processes) {
builder.appendf("%-3u %-3u %-3u %-3u %-4u %-8s %-3u %-9u %-3u %-4s %s\n",
process->pid(),
process->tty() ? process->tty()->pgid() : 0,
process->pgid(),
process->sid(),
process->uid(),
to_string(process->state()),
process->ppid(),
process->main_thread().times_scheduled(), // FIXME(Thread): Bill all scheds to the process
process->number_of_open_file_descriptors(),
process->tty() ? strrchr(process->tty()->tty_name().characters(), '/') + 1 : "n/a",
process->name().characters());
}
return builder.to_byte_buffer();
}
ByteBuffer procfs$memstat(InodeIdentifier)
{
InterruptDisabler disabler;
@ -592,6 +568,7 @@ ByteBuffer procfs$all(InodeIdentifier)
process_object.set("pid", process.pid());
process_object.set("times_scheduled", process.main_thread().times_scheduled());
process_object.set("pgid", process.tty() ? process.tty()->pgid() : 0);
process_object.set("pgp", process.pgid());
process_object.set("sid", process.sid());
process_object.set("uid", process.uid());
process_object.set("gid", process.gid());
@ -1120,7 +1097,6 @@ ProcFS::ProcFS()
m_entries[FI_Root_kmalloc] = { "kmalloc", FI_Root_kmalloc, procfs$kmalloc };
m_entries[FI_Root_all] = { "all", FI_Root_all, procfs$all };
m_entries[FI_Root_memstat] = { "memstat", FI_Root_memstat, procfs$memstat };
m_entries[FI_Root_summary] = { "summary", FI_Root_summary, procfs$summary };
m_entries[FI_Root_cpuinfo] = { "cpuinfo", FI_Root_cpuinfo, procfs$cpuinfo };
m_entries[FI_Root_inodes] = { "inodes", FI_Root_inodes, procfs$inodes };
m_entries[FI_Root_dmesg] = { "dmesg", FI_Root_dmesg, procfs$dmesg };

View File

@ -28,6 +28,7 @@ HashMap<pid_t, CProcessStatistics> CProcessStatisticsReader::get_all()
process.pid = process_object.get("pid").to_u32();
process.times_scheduled = process_object.get("times_scheduled").to_u32();
process.pgid = process_object.get("pgid").to_u32();
process.pgp = process_object.get("pgp").to_u32();
process.sid = process_object.get("sid").to_u32();
process.uid = process_object.get("uid").to_u32();
process.gid = process_object.get("gid").to_u32();

View File

@ -9,6 +9,7 @@ struct CProcessStatistics {
pid_t pid;
unsigned times_scheduled;
unsigned pgid;
unsigned pgp;
unsigned sid;
uid_t uid;
gid_t gid;

View File

@ -1,4 +1,5 @@
#include <LibCore/CFile.h>
#include <LibCore/CProcessStatisticsReader.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
@ -8,13 +9,30 @@ int main(int argc, char** argv)
(void)argc;
(void)argv;
CFile f("/proc/summary");
if (!f.open(CIODevice::ReadOnly)) {
fprintf(stderr, "open: failed to open /proc/summary: %s", f.error_string());
return 1;
printf("PID TPG PGP SID OWNER STATE PPID NSCHED FDS TTY NAME\n");
auto all_processes = CProcessStatisticsReader::get_all();
for (const auto& it : all_processes) {
const auto& proc = it.value;
auto tty = proc.tty;
if (tty != "notty")
tty = strrchr(tty.characters(), '/') + 1;
printf("%-3u %-3u %-3u %-3u %-4u %-8s %-3u %-9u %-3u %-4s %s\n",
proc.pid,
proc.pgid,
proc.pgp,
proc.sid,
proc.uid,
proc.state.characters(),
proc.ppid,
proc.times_scheduled,
proc.nfds,
tty.characters(),
proc.name.characters());
}
const auto& b = f.read_all();
for (auto i = 0; i < b.size(); ++i)
putchar(b[i]);
return 0;
}