Commit Graph

2642 Commits

Author SHA1 Message Date
Jim Warner
f11f43b4f7 top: overlap some more processing for separate threads
In an effort to improve the performance & decrease the
cost of our separate background update threads we will
relocate some overhead so that it might be overlapped.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-26 14:47:21 +10:00
Jim Warner
d89047659c top: modify semaphore destruction for separate threads
This commit just ensures that at program end all those
semaphores are removed in the same order for symmetry.
From thread's view of importance, we do least to most.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-26 14:47:21 +10:00
Jim Warner
476327048c top: reverse the #define defaults for separate threads
Rather than having those 3 separate background threads
enabled by default, we'll turn them off until somebody
chooses to activate them. That seems more appropriate.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-20 20:05:44 +10:00
Jim Warner
29f0a674a8 top: introduce background updates via separate threads
After the stage had been set in the previous patch, in
this patch we will actually implement those background
updates via 3 separate threads. The design was simple:

. the do-while loops have now been made truly infinite
. 2 semaphores per thread allow needed synchronization
. 1 semaphore will provide for each thread to sem_wait
. 1 semaphore will provide for display o/p to sem_wait
. and all 3 thread's program name was made descriptive

A complication was the potential for a signal directed
to one of our new threads. Rather than having a thread
try to deal with such signals, we pass a mask with all
signals blocked at pthread_create time. Thereafter any
subsequent signals are forwarded to the parent thread.

[ also sigprocmask was exchanged for pthread_sigmask ]
[ since warned about use "in multithreaded process". ]

[ plus we also modified each of those POSIX comments ]
[ about 2004 to agree with current signal-safety(7). ]

Sadly, after all this effort there were no performance
benefits to having separate threads. In fact there was
a measurable performance degradation when running with
ever smaller delay intervals. But even with a delay of
1/10 second the 'real' cost increase is only about 1%.

There is one way whereby any additional costs might be
eliminated (at least seemingly). One could introduce 2
separate sets of contexts for each of those 3 threads.
Then retrieval & display could be overlapped. However,
the resulting display wouldn't represent the real-time
results. Rather it would be stale by 1 delay interval.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-20 20:05:44 +10:00
Jim Warner
b4b68f11d6 top: refactor 3 routines ahead of the separate threads
This commit sets the stage for turning three functions
into code that can support a separate thread to update
their respective data while working in the background.

It involved relocating 1 function, renaming 2 routines
and adding parameters plus return addresses to each of
three functions. Those latter changes will be required
when issuing 'pthread_create' calls in the next patch.

The final step was organizing this code into what will
become the infinite do-while loop supporting a thread.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-20 20:05:44 +10:00
Jim Warner
1d1c1b84df top: tweak end-of-job processing if invoked via signal
The master branch top is pretty well insulated against
signal-unsafe function calls during end-of-job action.

However, this newlib branch top had exposed himself to
a bunch of such unsafe activity in the form of 'unref'
function calls to the new library. In those guys there
will be several 'free' invocations which are not safe.

So, this commit will simply correct such an oversight.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-20 20:05:44 +10:00
Jim Warner
6049c13b4d top: justify those header #define description comments
This commit is an example of what can happen when this
author is waiting around for the release of our newlib
& otherwise runs out of legitimate changes to be made.

[ on the other hand, such changes surely make us run ]
[ quite a bit more efficiently, if i am not mistaken ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-20 20:05:44 +10:00
Craig Small
75ba637182 docs: Add note in sysctl.conf.5 for line length
References:
 procps-ng/procps#77
2021-09-15 21:45:26 +10:00
Craig Small
fdab991bb2 docs: Fix comment for priority
Adjust the pri field for ps.1 because a higher pri field means a
higher priority.

References:
 procps-ng/procps#115
2021-09-15 21:36:40 +10:00
Jan Rybar
08aa15ce4b sysctl: Return non-zero if param setting failed
Real usecase:
'sysctl -w user.max_uts_namespaces=2147483648; echo $?'
returns 0 even though it failed with EINVAL

The close_stream() realised there was an issue and printed an
error but didn't change the return value.

Slightly modified merge request.

References:
 procps-ng/procps!76

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2021-09-15 21:07:43 +10:00
Craig Small
e886010ef7 misc: Add NEWS for previous
References:
 procps-ng/procps!91
2021-09-15 20:54:30 +10:00
Tom Levy
2737850e66 pgrep: Add a newline after regex error messages
The message from 'regerror' does not include a newline.

Test with `pgrep '*'`.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2021-09-15 20:53:12 +10:00
Craig Small
4706f225c9 testsuite: Add sysctl test for directory checks
Adds a test to try to write a sysctl parameter above /proc
and passes if it gives an error message.

References:
 commit f25d462166
2021-09-15 20:16:06 +10:00
Craig Small
2a7c909a1b sysctl: Update previous patch for newlib
This change was cherry picked from old library branch but
missed the change to check the path.

References:
 commit 6389deca5b
2021-09-15 20:09:44 +10:00
Craig Small
6389deca5b sysctl: Support systemd glob patterns
systemd-sysctl handles glob patterns along with overrides and
exceptions. Now the procps sysctl does it too.

The return value for sysctl is consistently either 0 or 1.

Added tests to check sysctl functions.

References:
 procps-ng/procps#191

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2021-09-15 20:07:32 +10:00
Jim Warner
8c23dfdcd4 top: some minor tweaks supporting long cmdline options
This darn patch began as 1 correction to an extraneous
comma in the man document. Then, it grew to include an
adjustment for a couple of additional man page things.

After that, I figured I might as well tighten up logic
dealing with those awful gaps in the getopt_long code.

[ the error_exit mentioned in the associated comment ]
[ will only be taken when '=' ends the argv vectors! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-08 21:35:20 +10:00
Jim Warner
2d75b2eb96 NEWS: update to acknowledge top's long cmdline options
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-07 08:02:43 +10:00
Jim Warner
922696eb65 top: update man document with new long cmdline options
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-07 08:02:43 +10:00
Jim Warner
c91b371485 top: utilize getopt and introduce long cmdline options
For quite some time now, top has stood out like a sore
thumb regarding the approach to cmdline options & help
text.  Only short options were used and that same help
text was displayed for '-h' (help) plus 'v' (version).

[ also, top 'rolled his own' when it came to parsing ]
[ options while avoiding that getopt implementation. ]

Well, with this commit all of that has changed and top
now has added a long form of his options. Additionally
he employs getopt_long() for the bulk of that parsing.

[ however, top will still avoid separate fputs calls ]
[ characteristic of other procps-ng programs when it ]
[ comes to help. rather all such text is one string. ]

Along the way, the following major getopt deficiencies
were addressed, assuming the absence of a new #define:

* an equals sign ('=') is allowed on both option forms

* whitespace is allowed before & after the equals sign

* optional arguments needn't abut their related option
for short form nor is an '=' required with either form

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-07 08:02:43 +10:00
Jim Warner
b8118b9fee top: some minor organizational tweaks for man document
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-09-07 08:02:43 +10:00
Jim Warner
9a5a155e75 library: trade 2 strstr & atoi calls for 1 sscanf call
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-24 20:52:10 +10:00
Jim Warner
2a539477b7 library: just eliminate a couple of unnecessary braces
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-24 20:52:10 +10:00
Jim Warner
1c00486728 top: and just eliminate a couple of unnecessary braces
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-24 20:52:10 +10:00
Jim Warner
40cf72b7b9 top: reposition call to cpus_refresh() for consistency
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-24 20:52:10 +10:00
Jim Warner
01fd743d86 top: corrected single man document typographical error
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-24 20:52:10 +10:00
Jim Warner
95294d6e4c top: man page now reflects change for the 'select' i/f
Reference(s):
https://www.freelists.org/post/procps/important-extension-to-newlib,4
. 'select' i/f top response
commit 23776571a7
. 'select' i/f library change
commit af34cc964a

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-17 22:04:51 +10:00
Jim Warner
96e2220068 library: address major bug in the 'listed_nextpid' guy
Ever since 2003, the 'listed_nextpid' routine has been
misrepresenting its duties. Far from finding processes
in a list given to openproc, it just inserted the next
pid in that list into the passed proc_t as BOTH a tgid
and tid. There was no attempt to validate such values.

The net result is that tid & tgid were valid only with
a thread group leader. When called with a pid for some
sibling thread, the resulting tgid would be incorrect.

With this commit, our little function will now attempt
to validate both the tid and tgid. If this should fail
then the fallback position will be the same as what we
inherited. So we're no worse off & likely much better.

[ note that calling the function with a thread's pid ]
[ likely stems from 2011 when a 'readeither' routine ]
[ was added which dealt with both tasks and threads! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-12 21:34:11 +10:00
Jim Warner
f1aad95cd1 library: disabled some code that now serves no purpose
In the patch shown below, two lines involving the flag
PROC_UID were uncommented (enabled). However given the
construct of the readeither function, it is impossible
for the simple_readtask guy to be called when its TGID
leader has already been ignored. So, let's disable it.

[ it's only now true that the lines serve no purpose ]
[ after the commit shown below tweaked readeither to ]
[ access the base directory of the tgid leader. but, ]
[ before that, the 2 lines should have been enabled! ]

Reference(s):
. two lines uncommented
commit af34cc964a
. tweaked readeither
commit a375262609

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-12 21:34:11 +10:00
Jim Warner
243bca8ab0 top: thread mode transitions should remove focus ('F')
The commit referenced below forced a return to row one
whenever there was a thread mode transition. Now, with
our new focus ('F') feature, we should adopt a similar
philosophy so as to avoid potential display anomalies.

Reference(s)
. Jun 2018, force row 1 w/ thread mode transition
commit 19ec80bd41

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:03:23 +10:00
Jim Warner
23776571a7 top: respond to that preceding fix in the 'select' i/f
This patch makes it possible to honor our '-H' threads
switch even when operating under that '-p' switch used
to select/filter only certain pids. Of course, what we
have done is just exploit the new library enhancement.

[ and we're pretending we don't know the enumerators ]
[ PIDS_FETCH_THREADS_TOO + PIDS_SELECT_PID are equal ]
[ to that PIDS_SELECT_PID_THREADS enumerator itself. ]

[ thankfully, those dependencies can be assured with ]
[ a 'make check-lib' which exploits ITEMTABLE_DEBUG! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:03:23 +10:00
Jim Warner
af34cc964a library: repair <pids> api boo-boo in the 'select' i/f
The patch referenced below corrected some flaws in the
procps_pids_select implementation. But, there remained
one flaw which this commit will now hopefully address.

Rather than assume callers wished to select only tasks
and not threads meant a command like 'top -H -p 10329'
works differently under newlib than release 3.3.17. It
fails to honor the '-H' (threads) switch under newlib.

So, to fix that oops, we'll allow that select function
to get threads or tasks depending on its 'which' parm.

Reference(s):
. Oct 2015, some flaws corrected
commit bc616b3615

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:03:23 +10:00
Jim Warner
a375262609 library: ensure thread group leader retrieval accuracy
With that recent addition of the autogroup provisions,
it became apparent that '/proc/<pid>/task' information
is sometimes incomplete. In fact, there's no autogroup
file at all in any '/proc/<pid>/task/<pid>' directory!

As a result, when the top -H mode was invoked, all the
processes showed a -1 for AGID, even the group leader.

So, this commit will ensure that for every TGID leader
its basic '/proc/<pid>' directory will always be used.
The 'task' subdirectory is now only used for siblings.

[ and it's time that readeither prologue was updated ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:03:23 +10:00
Jim Warner
949382afe5 NEWS: announce changes for displaying autogroup values
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
6c426c995e ps: document new items for displaying autogroup values
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
c5d1bdd083 ps: added 2 new fields for displaying autogroup values
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
70566acf34 top: document 2 fields for displaying autogroup values
[ we'll also eliminate a couple of references to the ]
[ repurposed 'F' command which should have gone away ]
[ with the introduction of that new 'focus' feature! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
b789b46f84 top: exploit new items for displaying autogroup values
Ordinarily, whenever a new field is added to top, that
RCF_VERSION_ID should be bumped which then prevents an
older version of top from reading the expanded rcfile.

With this change, however, we'll keep the existing 'k'
version since we've yet to release the newlib version.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
631e5d91f3 library: add provision for displaying autogroup values
In the link referenced below there's an explanation of
the linux autogroup feature which has been around ever
since linux-2.6.38. With that explanation there's also
surprising (maybe shocking) revelations about the nice
and renice commands if CONFIG_SCHED_AUTOGROUP was set.

When autogroups are active, such programs are rendered
mostly useless because the nice value will only affect
scheduling priority relative to other processes in the
same autogroup. In order to accomplish what we thought
of as renice, that nice value in /proc/<pid>/autogroup
must be changed. Altering any single member of a group
will also affect every other member of that autogroup.

So, this commit will set the stage for users of newlib
to display autogroup identifiers plus their associated
nice values (now that their importance is understood).

Reference(s):
https://github.com/nlburgin/reallynice

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
93c0a6cedf ps: eliminated an overlooked obsoleted <pids> API item
When several obsolete linux-2.6 fields were eliminated
and ps responded in the commit referenced below, there
was one reference overlooked. So, with this commit the
reference to PIDS_ALARM has been whacked at long last.

Reference(s):
. Sep 2016, ps response to removal
commit 86992bb58f

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
ece2394be7 top: eliminate all field numbers from the man document
As far back as release 3.2.8 (maybe even farther) this
man page used a number with each field's descriptions.

That practice caused no real harm when top was limited
to a grand total of 26 fields (thru lower case letters
of the alphabet). However, now we've reached 70 fields
and, depending on the name that's chosen, adding a new
field could result in a massive renumbering of fields.

Thus, this patch eliminates such potential by removing
all numbers from section '3a. DESCRIPTIONS of Fields'.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
0360f64027 library: refactor all the readproc.h 'PROC_FILL' flags
The 'PROC_FILL' flags, found in readproc.h, had become
almost unmanageable. The hex values were scattered all
over the map as new flags had been introduced. So this
commit resets all of them and will help ensure any new
flags don't duplicate some already existing hex value.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-09 22:00:23 +10:00
Jim Warner
6318010dab top: document summary area changes involving 2 abreast
Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-06 20:17:51 +10:00
Jim Warner
178acafb8a top: allow the summary memory lines to print 2 abreast
The commit shown below introduced two abreast printing
for %Cpu statistics. But, the Memory & Swap lines have
always been printed on separate lines when '4' was on.

With this commit, those lines will now be treated just
like %Cpu stats, either separate lines or two abreast.

Reference(s):
. May 2020, added '4' toggle for 2 abreast
commit 59f5a37a24

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-06 20:17:51 +10:00
Jim Warner
6eb9d4b586 top: add a visual separator to lines printed 2 abreast
When the '4' toggle is on, and Summary Area %Cpu lines
are shown two abreast, it's not always clear where the
separation is between the two processors. So with this
patch we'll now print a vertical bar separator between
them (looking similar to the existing field headings).

We will also reduce that 'GRAPH_suffix' constant to 1,
reflecting the the trailing ']' bracket only, so as to
maximize top's exploitation of available screen width.

[ plus a superfluous double '+ +' has now been fixed ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-06 20:17:51 +10:00
Jim Warner
2e11351b6f top: TREE_FOCUS_X works properly with multiple windows
As promised, this commit will ensure that TREE_FOCUS_X
impacts only the window under which an 'F' was issued.

Previously, when 'F' was invoked it would impact every
window that was displaying forest view. Now, only that
window where 'F' was applied will show the indentation
losses whenever multiple windows were being displayed.

[ each of the 4 windows can now have different focus ]
[ pids active and not impact any other forest views! ]

Reference(s):
. 'TREE_FOCUS_X' introduction
commit c23d2708d4

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-01 20:36:55 +10:00
Jim Warner
38eeba364a top: trade most pids_stack pointers for a WIN_t ppt ix
For every function whose parameters consist of a WIN_t
pointer and a pointer to a pids_stack, we will instead
pass the WIN_t pointer and index to that window's ppt.

This change will save six instructions per invocation,
and converts the index once in those called functions.

[ you can consider this an efficiency change, but it ]
[ is really being implemented so that 'TREE_FOCUS_X' ]
[ #define can be made to behave as one should expect ]
[ when running under top's 'alternate display' mode! ]

[ stay tuned for the very next commit to be pushed!! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-01 20:36:55 +10:00
Jim Warner
335ec18774 top: restored a function prologue lost a long time ago
For some reason, when top was modified to exploit that
<pids> api, one function prologue was removed. So this
commit just restores it to the 'window_show' function.

Reference(s):
. Aug, 2015 - adapted to new <pids> api
commit 77dc22b910

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-08-01 20:36:55 +10:00
Jim Warner
c23d2708d4 top: new #define added for focus ('F') toggle tweaking
Just to see if I could, the new #define will force the
indentation of a parent task to be reset to zero while
maintaining the proper child indentation relationship.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-07-27 21:17:18 +10:00
Jim Warner
37a4601882 top: introduced new focus toggle ('F'), man doc & NEWS
[ i also corrected an unrelated typographical error! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-07-27 21:17:18 +10:00
Jim Warner
3e922e671d top: introduced new focus toggle ('F'), program source
Sometimes, it might be useful to isolate a parent task
and its forked children from other system activity. As
an example, a user might want to examine workload in a
specific lxc container. Or maybe there is some need to
question what's happening under the same tmux session.

Since forest view mode tends to be a relatively stable
display, it will sometimes satisfy the above need. But
that mode can't currently guarantee the target process
always remains as the topmost task or even is visible.

So, this patch will enable focusing on any parent task
and keeping it as the topmost process while displaying
it and its forked children only. It then appears as if
there is no other activity in that system by virtue of
the blank lines which follow that final child process.

To implement this new feature, top's redundant 'F' key
has been repurposed. It made little sense devoting two
keys to the Fields Management screen (especially since
the key we've taken required two separate keystrokes).

[ and while we're at it, i also added punctuation to ]
[ the prologue for that renamed 'forest_display' guy ]
[ since all other forest functions used punctuation. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-07-27 21:17:18 +10:00