From 1d8d69bf0692fd80781593312189256c98f34478 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R." Date: Tue, 5 Aug 2025 05:34:15 +0200 Subject: [PATCH] Add sort by PID option --- doc/nethogs.8 | 3 +++ src/cui.cpp | 11 +++++++---- src/main.cpp | 6 +++++- src/nethogs.cpp | 1 + 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/nethogs.8 b/doc/nethogs.8 index 928fad7..cc9fca9 100644 --- a/doc/nethogs.8 +++ b/doc/nethogs.8 @@ -78,6 +78,9 @@ Show only processes with the specified pid(s). \fB-j\fP Output in JSON format. .TP +\fB-z\fP +Sort by PID. +.TP \fB-f\fP EXPERIMENTAL: specify string pcap filter (like tcpdump). This may be removed or changed in a future version. .TP diff --git a/src/cui.cpp b/src/cui.cpp index e975d2f..f8fd0ae 100644 --- a/src/cui.cpp +++ b/src/cui.cpp @@ -44,6 +44,7 @@ extern Process *unknownudp; extern Process *unknownip; extern bool sortRecv; +extern bool sortPID; extern int viewMode; extern bool showcommandline; @@ -97,8 +98,6 @@ public: double sent_value; double recv_value; const char *devicename; - -private: const char *m_name; const char *m_cmdline; pid_t m_pid; @@ -300,14 +299,18 @@ int GreatestFirst(const void *ma, const void *mb) { Line *a = *pa; Line *b = *pb; double aValue; - if (sortRecv) { + if (sortPID) { + aValue = a->m_pid; + } else if (sortRecv) { aValue = a->recv_value; } else { aValue = a->sent_value; } double bValue; - if (sortRecv) { + if (sortPID) { + bValue = (double)b->m_pid; + } else if (sortRecv) { bValue = b->recv_value; } else { bValue = b->sent_value; diff --git a/src/main.cpp b/src/main.cpp index 5cc9316..1ee9658 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,6 +70,7 @@ static void help(bool iserror) { output << " m: switch between total (kB, bytes, MB) and throughput (kB/s, " " MB/s, GB/s) mode\n"; output << " j: json output\n"; + output << " z: sort by PIDn"; } void quit_cb(int /* i */) { @@ -154,7 +155,7 @@ int main(int argc, char **argv) { int garbage_collection_period = 50; int opt; - while ((opt = getopt(argc, argv, "Vhxtpsd:v:c:laf:Cbg:P:j")) != -1) { + while ((opt = getopt(argc, argv, "Vhxtpsd:v:c:laf:Cbg:P:jz")) != -1) { switch (opt) { case 'V': versiondisplay(); @@ -208,6 +209,9 @@ int main(int argc, char **argv) { case 'j': output_json = true; break; + case 'z': + sortPID = true; + break; default: help(true); exit(EXIT_FAILURE); diff --git a/src/nethogs.cpp b/src/nethogs.cpp index b120699..ebab94f 100644 --- a/src/nethogs.cpp +++ b/src/nethogs.cpp @@ -59,6 +59,7 @@ bool tracemode = false; bool bughuntmode = false; // sort on sent or received? bool sortRecv = true; +bool sortPID = false; bool showcommandline = false; bool showBasename = false; // viewMode: kb/s or total