]> granicus.if.org Git - procps-ng/log
procps-ng
2 years agodoc: document 'scale_tics' function for Ctrl-E scaling
Jim Warner [Thu, 3 Mar 2022 06:00:00 +0000 (00:00 -0600)]
doc: document 'scale_tics' function for Ctrl-E scaling

[ i also tweaked that 'STARTED' narrative just a bit ]
[ since its original wording implied the value could ]
[ change, whereas it's fixed when a task is started. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: extended 'scale_tics' function for Ctrl-E scaling
Jim Warner [Thu, 3 Mar 2022 06:00:00 +0000 (00:00 -0600)]
top: extended 'scale_tics' function for Ctrl-E scaling

That normalization of the 'scale_tics' function in the
prior commit convinced me that I won't please everyone
with my arbitrary choices for the scaling transitions.

So, this patch will provide the users with a means for
setting their own scaling transition points with a new
toggle. Ctrl-E was chosen since the 'e/E' toggles were
already present as a means of scaling (albeit memory).

[ this toggle will also serve an educational purpose ]
[ by allowing one to see all the scaling conventions ]

The scaling a user establishes is saved in the rcfile.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: refactor 'scale_tics' function for better scaling
Jim Warner [Thu, 3 Mar 2022 06:00:00 +0000 (00:00 -0600)]
top: refactor 'scale_tics' function for better scaling

This patch refactors the 'scale_tics' function to more
closely parallel uptime shown on the first line of the
summary area. The old logic has been preserved through
the header file's new  #define SCALE_FORMER provision.

However, the former logic was actually a big disaster.
These are some potential problems with that old logic:

1. With respect to our time fields top no longer deals
solely with cpu time. So, the old limits of '68 weeks'
could possibly be insufficient to reflect those times.

2. Given the widths of top's new time fields, the code
never got beyond scaling to hours. For example, with a
ridiculously large span of 19 years, the scaled result
would then be shown as '167832h'. We never reached the
days ('6993d') or even the weeks ('999w') equivalents.

3. Similarly, with that 'TIME+' field and a large tics
value, results would then appear as 'MMMMMM:SS' rather
than the more meaningful 'HH:MM:SS' or days and hours.

So henceforth we will adopt these scaling conventions:

  MMM:SS.hh ... minutes:seconds.hundredths
  MMM:SS ...... minutes:seconds
  HH,MM ....... hours,minutes
  D+H ......... days+hours (with 'd' & 'h' suffixes)
  D ........... days (with 'd' suffix)
  W+D ......... weeks+days (with 'w' & 'd' suffixes)
  W ........... weeks (with 'w' suffix)

Note that, unlike our former scaling logic, that 'MMM'
portion won't be allowed to grow unconditionally. It's
limited (arbitrarily?) to 360 total minutes (6 hours).
Additionally, the 'HH' guy will be limited to 96 hours
(4 days) while that 'D' limit was set at 14 (2 weeks).

Whenever a limit is hit, scaling will advance a level.

Reference(s):
. Feb, 2022 - added 'ELAPSED'
commit 9348d3b0085bd389749cc5619fdde4ec3fae5f11
. Feb, 2022 - added 'STARTED'
commit 7647e96b0a35d473fa9bc644ea6107487b3b0527

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: added some elapsed running time 'ELAPSED' support
Jim Warner [Sun, 27 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: added some elapsed running time 'ELAPSED' support

When the 'STARTED' field was added, in the message for
the commit referenced below, I explained why 'ELAPSED'
shouldn't be implemented though it might be preferred.

Well, after climbing out of my box to do a little more
thinking, I came up with the way to add that 'ELAPSED'
field while avoiding the possible performance penalty.

Just do not show what would change with every refresh!

If we do not show the seconds portion of a scaled tics
amount then the problem goes away. And this comes with
an additional benefit. The HH,MM (hours,minutes) style
then is readily compared with that system uptime shown
as HH:MM. The only difference is just the comma/colon.

[ assuming the top uptime/load average toggle was on ]

Reference(s):
. introduced 'start time' field
commit 7647e96b0a35d473fa9bc644ea6107487b3b0527

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: tweaked the recently introduced 'STARTED' support
Jim Warner [Sun, 27 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: tweaked the recently introduced 'STARTED' support

When the 'STARTED' field was added, the width was made
the same as the 'TIME+' field. Thus, a full time could
be shown (which then included hundredths of a second).

That kind of granularity is totally unnecessary. After
all, this column is potentially confusing enough since
it is so counterintuitive. So, this commit will reduce
the width of the field with some help from scale_tics.

Henceforth it will not include those ol' centiseconds.

[ along the way let's expand the man document with a ]
[ a remainder about content representation & scaling ]

Reference(s):
. introduced 'start time' field
commit 7647e96b0a35d473fa9bc644ea6107487b3b0527

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: trade 'float' type for a 'double', <pids> api
Jim Warner [Sun, 27 Feb 2022 06:00:00 +0000 (00:00 -0600)]
library: trade 'float' type for a 'double', <pids> api

While we really do not need the extra precision, we'll
trade that recently introduced float type for a double
for efficiency on a 64 bit platform. Additionally, the
UTILIZATION algorithm was tweaked producing less code.

The net result is four fewer machine instructions with
a reduction of 14 total bytes in that function's size.

Reference(s):
. introduced 'real' type & 'utilization' item
commit c69104b2b835176f9801dfd1a1d893579879a5b6

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agow: responded to those library changes for 'TIME' stuff
Jim Warner [Sat, 26 Feb 2022 06:00:00 +0000 (00:00 -0600)]
w: responded to those library changes for 'TIME' stuff

The library refactored 'TIME' items for consistency so
we must adapt to some new data types and calculations.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agops: response to those library changes for 'TIME' stuff
Jim Warner [Sat, 26 Feb 2022 06:00:00 +0000 (00:00 -0600)]
ps: response to those library changes for 'TIME' stuff

The library refactored 'TIME' items for consistency so
we must adapt to some new data types and calculations.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agopgrep: adapted to the library changes for 'TIME' stuff
Jim Warner [Sat, 26 Feb 2022 06:00:00 +0000 (00:00 -0600)]
pgrep: adapted to the library changes for 'TIME' stuff

The library refactored 'TIME' items for consistency so
we must adapt to some new data types and calculations.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agodoc: updated with new 'start time' & 'cpu utilization'
Jim Warner [Fri, 25 Feb 2022 06:00:00 +0000 (00:00 -0600)]
doc: updated with new 'start time' & 'cpu utilization'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: added fields for 'start time' & 'cpu utilization'
Jim Warner [Fri, 25 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: added fields for 'start time' & 'cpu utilization'

This patch will exploit some new library capabilities.

[ one will raise eyebrows, the other likely will not ]

A new 'STARTED' field was added which shows the time a
process started after system boot. As such the largest
interval represents the most recently started process.

This is the field that will likely be questioned since
it's somewhat counterintuitive. But were we to instead
use TIME_ELAPSED, the value will change with every top
refresh. This will defeat any PUFF macro optimization.

The new '%CUU' field will probably be better received.
It represents the cpu usage over the life of the task.
When a process was showing high %CPU usage, this field
can be used to determine if it's an anomaly or normal.

[ and as with %CPU, %CUU shows a '?' when running in ]
[ a namespace when /proc was mounted with subset=pid ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: added important new functionality, <pids> api
Jim Warner [Fri, 25 Feb 2022 06:00:00 +0000 (00:00 -0600)]
library: added important new functionality, <pids> api

This commit introduces some new capabilities available
in libproc-2 under the <PIDS> interface. Along the way
errors impacting some item values have been corrected.

The following summarizes the major changes being made.

1. The PIDS_TIME_START item was represented as seconds
since system boot but really held tics since boot. And
some programs properly divided it by Hertz to actually
yield seconds while others acted as if it already was.

So, now we have a new PIDS_TICS_BEGAN field and all of
the 'TIME' fields properly reflect seconds. With those
'TIME' fields, the type was changed to 'float/real' so
one could convert it back to tics without loss of some
centiseconds reflected in the Hertz guy (usually 100).

2. The boot_seconds was established in procps_pids_new
meaning it was fixed/unchanging. As a result, one item
(PIDS_TIME_ELAPSED) was rendered useless. So now, each
of the three retrieval functions establishes a current
boot_seconds well before the set functions are called.

3. Added a PIDS_UTILIZATION item that will provide the
CPU usage over the life of a process, as a percentage.

4. Added PIDS_TIME_ALL_C for symmetry with the similar
item called PIDS_TICS_ALL_C (which reflects raw tics).

5. That 'derived from' notation has been added to some
additional header items to reflect their true origins.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agodoc: minor tweaks affecting three newlib man documents
Jim Warner [Thu, 24 Feb 2022 06:00:00 +0000 (00:00 -0600)]
doc: minor tweaks affecting three newlib man documents

. add the proper function names to the procps_pids man
page when discussing the 'LIBPROC_HIDE_KERNEL' feature
under recently added 'ENVIRONMENT VARIABLE(S)' section

. ensure the 'SEE ALSO' references are comma delimited

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: just trade an assignement for a manifest constant
Jim Warner [Wed, 23 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: just trade an assignement for a manifest constant

The assignment was originally created to reduce length
of one line of code (i.e. for readability). But we can
achieve this objective without adding executable code.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: trade that 'strncpy' for more efficient 'memccpy'
Jim Warner [Mon, 14 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: trade that 'strncpy' for more efficient 'memccpy'

This patch was prompted by the merge request for pgrep
referenced below. In top's case, any performance gains
will be minimal since the now defunct strncpy was only
employed for termcap rebuilds after interacting with a
user (+ 1 other non-termcap related user interaction).

[ golly, strncpy always calls at least two functions ]
[ but usually calls a total of 3. on the other hand, ]
[ memccpy will only call a maximum of two functions. ]

And thanks to Baruch Siach for these strncpy insights.

Reference(s):
https://gitlab.com/procps-ng/procps/-/merge_requests/148

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: tweak a couple task_show() macros for consistency
Jim Warner [Sun, 13 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: tweak a couple task_show() macros for consistency

There was a potential problem with these macros should
that '#define SCROLLVAR_NO' be active and they were to
appear in an 'if' statement (like is necessary for the
master branch version of top under that EU_CMD label).

[ now they're always usable without requiring an ';' ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: let's eliminate some additional compiler warnings
Jim Warner [Sat, 12 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: let's eliminate some additional compiler warnings

This patch eliminates those warnings referenced below.
They are sometimes associated with an obscure #define.

We'll also corrrect one header file function prototype
so it will aggree with the actual function definition.

Reference(s):
top.c: In function 'adj_geometry':
top.c:1874:20: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
 1874 |    if (Screen_cols < DOUBLE_limit) Curwin->rc.double_up = 0;
      |                    ^
top.c: In function 'zap_fieldstab':
top.c:2359:26: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
 2359 |    if (wtab[EU_CPN].wmin < digits) {
      |                          ^
top.c:2365:26: warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]
 2365 |    if (wtab[EU_NMA].wmin < digits) {
      |                          ^
top.c: In function 'keys_summary':
top.c:5128:45: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
 5128 |          if (w->rc.double_up && Screen_cols < DOUBLE_limit) {
      |                                             ^
top.c: In function 'sum_tics':
top.c:5605:22: warning: unused variable 'num_syst' [-Wunused-variable]
 5605 |    int ix, num_user, num_syst;
      |                      ^~~~~~~~
top.c:5605:12: warning: unused variable 'num_user' [-Wunused-variable]
 5605 |    int ix, num_user, num_syst;
      |            ^~~~~~~~

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: attempt to clarify the 'iokey' parameter meanings
Jim Warner [Fri, 11 Feb 2022 06:00:00 +0000 (00:00 -0600)]
top: attempt to clarify the 'iokey' parameter meanings

The 'iokey' function's parameter 'action' was utilized
with literal numbers in the calling functions. So this
change will replace those literal numbers with #define
constants which, one hopes, will clarify their impact.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agofree: Use wstr width and not length
Craig Small [Tue, 1 Feb 2022 05:46:09 +0000 (16:46 +1100)]
free: Use wstr width and not length

The previous commit used the value from mbstowcs() to work out
the spacing required, as printf() got it completely wrong.

However, for alignment of text you don't use the string length
but the string width.

As the referenced website says:
 Use wcslen when allocating memory for wide characters, and use wcswidth to
 align text.

Which is what free does now. Chinese is still off by one but I cannot
see why this is so. It's close enough for now. If someone can work
it out, I'd love to know what the fix is.

As a side effect, #213 is fixed because we are putting the correct
number of spaces in.

French is still an issue (see #24 ) but this is because the string is
too long!

References:
 procps-ng/procps#24
 procps-ng/procps#213
 procps-ng/procps#229
 commit 9f4db0fb5606e4872829bd44b29443d5707b1505
 https://www.linux.com/news/programming-wide-characters/

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2 years agofree: Fix first column justification
Craig Small [Mon, 31 Jan 2022 07:14:12 +0000 (18:14 +1100)]
free: Fix first column justification

free has for many years had a problem with translated header columns
or the first column. This is because printf("-9s", str) doesn't use
the wide length of the string but the char length meaning they are
mis-aligned.

Using the mbstowcs() function to get the wide length and then
a precision parameter to append the right number of spaces after the
number means we get what we need.

References:
 procps-ng/procps#229
 procps-ng/procps#204
 procps-ng/procps#206
 https://bugs.debian.org/1001689

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2 years agoNEWS: updated to acknowledge 'LIBPROC_HIDE_KERNEL' var
Jim Warner [Thu, 27 Jan 2022 06:00:00 +0000 (00:00 -0600)]
NEWS: updated to acknowledge 'LIBPROC_HIDE_KERNEL' var

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: tweak the heck out of some command line help text
Jim Warner [Wed, 26 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: tweak the heck out of some command line help text

This patch began as just an attempt to make any option
which also included an argument a little more readable
by adding one space before the '=ARGUMENT' convention.

[ by the way, i don't agree with most of those other ]
[ procps-ng programs that use an '<arg>' convention. ]
[ it's too easily misread as an 'optional' argument. ]

[ top uses a convention like that found in coreutils ]
[ albeit now with one extra space before the equals. ]

In adjusting those arguments it was apparent that many
explanations already lined up nicely at the right hand
margin. So, this commit will force right-justification
with all explanations (as we do with commit messages).

[ and as a final challenge, for those options taking ]
[ an argument, that argument was repeated within the ]
[ explanation and made the rightmost item on a line. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: eliminate the potential warnings '-Wsign-compare'
Jim Warner [Tue, 25 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: eliminate the potential warnings '-Wsign-compare'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: avoid a potential warning '-Wmaybe-uninitialized'
Jim Warner [Tue, 25 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: avoid a potential warning '-Wmaybe-uninitialized'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: avoid a possible warning '-Wimplicit-fallthrough'
Jim Warner [Tue, 25 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: avoid a possible warning '-Wimplicit-fallthrough'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: add visual clue when focus toggle ('F') is active
Jim Warner [Fri, 14 Jan 2022 20:14:14 +0000 (14:14 -0600)]
top: add visual clue when focus toggle ('F') is active

When in forest view mode, that focus toggle ('F') is a
useful tool occasionally. But, if a focused parent has
enough cloned siblings to exceed screen rows, it could
be hard to remember that such a toggle remains active.

So, this patch will provide a subtle visual clue added
to the leftmost position in the COMMAND column. Now if
the focus toggle was active, regardless of total tasks
affected, the users will always know when it's active.

Reference(s):
. -7/24/21, introduced new focus toggle
commit 3e922e671daf09ae60ee211b5c0ef94996ee573d
. 09/23/21, ensure focused tasks stay focused
commit 69978e365043f27305e487709474947bb377084d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: correct one source comment plus one man page oops
Jim Warner [Thu, 13 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: correct one source comment plus one man page oops

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: banish one more warning for '-Wformat-truncation'
Jim Warner [Thu, 13 Jan 2022 19:13:13 +0000 (13:13 -0600)]
top: banish one more warning for '-Wformat-truncation'

In the commit referenced below, I fixed what I thought
was all the top 'truncation' warnings. For that commit
I had been using CFLAGS='-ggdb -Wall'. However, if one
uses just a vanilla './configure', then a hidden extra
warning will surface. This patch will finally kill it.

[ thanks a bunch gcc - we love this kind of behavior ]

Reference(s):
. 01/08/22, original warning fix
commit 44ca06f1a05b31880ca0ec4e59923a3255f5816d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agosysctl: eliminate a warning '-Wmisleading-indentation'
Jim Warner [Sat, 8 Jan 2022 06:00:00 +0000 (00:00 -0600)]
sysctl: eliminate a warning '-Wmisleading-indentation'

[ and in the affected function, we'll also eliminate ]
[ all those f**king tab characters making a hot mess ]
[ of any attempt at a properly formatted C function! ]

[ plus i adjusted a little non-tab misalignment too! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: eliminate warnings for '-Wmisleading-indentation'
Jim Warner [Sat, 8 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: eliminate warnings for '-Wmisleading-indentation'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: eliminate every warning for '-Wformat-truncation'
Jim Warner [Sat, 8 Jan 2022 06:00:00 +0000 (00:00 -0600)]
top: eliminate every warning for '-Wformat-truncation'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: Better PID file checks (with one small tweak)
Jim Warner [Fri, 7 Jan 2022 06:00:00 +0000 (00:00 -0600)]
library: Better PID file checks (with one small tweak)

Since gcc no longer warns of 'unreachable code', we'll
just deal with any such possibility ourselves I guess.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: Better PID file checks
Craig Small [Fri, 7 Jan 2022 20:49:13 +0000 (07:49 +1100)]
library: Better PID file checks

This started off with fixing the compilier warning:

proc/readproc.c: In function ‘simple_nextpid’:
proc/readproc.c:1373:38: warning: ‘%s’ directive output may be truncated writing up to 255 bytes into a region of size 58 [-Wformat-truncation=]
 1373 |   snprintf(path, PROCPATHLEN, "/proc/%s", ent->d_name);
      |                                      ^~
proc/readproc.c:1373:3: note: ‘snprintf’ output between 7 and 262 bytes into a destination of size 64
 1373 |   snprintf(path, PROCPATHLEN, "/proc/%s", ent->d_name);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We know that ent->d_name will fit under 64 bytes because we check the
name starts with a digit. The first change was simple and changed the
printf to use tgid like the task function below it.

Is everything under /proc that starts with a digit a directory with a
PID only? Today, it is but there are no guarantees.

The entire function works ok if every non-pid directory doesn't start
with a number. We don't check for strtoul() having an issue nor
if the for loop just falls off the end. The moment the kernel guys
(or some module writer) think "/proc/12mykernelval" is a neat idea this
function is in trouble. We won't get buffer overflow as we are
using snprintf at least.

This change now:
 We check if strtoul() actually came across a number
 Process the pid directory as a conditional branch
 Treat falling off the for loop as a not-found

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2 years agopmap: Fix minor compiler warning
Craig Small [Fri, 7 Jan 2022 20:47:46 +0000 (07:47 +1100)]
pmap: Fix minor compiler warning

/usr/include/x86_64-linux-gnu/bits/error.h:40:5: warning: ‘%s’ directive argument is null [-Wformat-overflow=]
   40 |     __error_noreturn (__status, __errnum, __format, __va_arg_pack ());
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pmap.c: In function ‘main’:
pmap.c:760:35: note: format string is defined here
  760 |         xerrx(EXIT_FAILURE, "%s: '%s'", _("failed to parse argument"), optarg);

A simple check for optarg being null silenced this, not sure why only
this one was a problem.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2 years agomisc: fixed several inconsistencies in email addresses
Jim Warner [Fri, 7 Jan 2022 06:00:00 +0000 (00:00 -0600)]
misc: fixed several inconsistencies in email addresses

[ you wouldn't believe how many back-and-forths were ]
[ involved in Craig convincing me there were several ]
[ inconsistencies. i am so dense sometimes (often?). ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: Fix my email address
Craig Small [Fri, 7 Jan 2022 08:29:23 +0000 (19:29 +1100)]
top: Fix my email address

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2 years agodoc: updated 3 man pages for 'LIBPROC_HIDE_KERNEL' var
Jim Warner [Mon, 3 Jan 2022 23:00:00 +0000 (00:00 +0100)]
doc: updated 3 man pages for 'LIBPROC_HIDE_KERNEL' var

This patch was prompted by Björn Fischer's merge #147
request referenced below. And since the library change
may impact all users, multiple man pages were updated.

[ and thanks to Björn for initiating this extension ]

Reference(s):
https://gitlab.com/procps-ng/procps/-/merge_requests/147

Prototyped-by: Björn Fischer <bf@CeBiTec.Uni-Bielefeld.DE>
Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: add support for the 'LIBPROC_HIDE_KERNEL' var
Jim Warner [Mon, 3 Jan 2022 23:00:00 +0000 (00:00 +0100)]
library: add support for the 'LIBPROC_HIDE_KERNEL' var

This patch was prompted by Björn Fischer's merge #147
request referenced below. It has been generalized such
that it now embraces both of those 'pids_fetch' types.

[ and thanks to Björn for initiating this extension ]

Reference(s):
https://gitlab.com/procps-ng/procps/-/merge_requests/147

Prototyped-by: Björn Fischer <bf@CeBiTec.Uni-Bielefeld.DE>
Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: bust up that overly large 'summary_show' function
Jim Warner [Mon, 3 Jan 2022 05:00:00 +0000 (00:00 -0500)]
top: bust up that overly large 'summary_show' function

Over the years the summary_show function has increased
from around 77 lines of code & comments to its current
size of 243 lines. This is well beyond an ideal length
of available screen rows. So this patch will split it.

We'll take the cpu and memory duties and make separate
functions out of them. Of course, this will incur some
additional call overhead but, given current cpu/memory
logic, any such increase really becomes insignificant.

Now summary_show's a svelte 57 lines of code/comments.

[ this is like what was done to that do_key function ]
[ a decade ago except overhead of new function calls ]
[ plus table lookup was even less of a concern since ]
[ a human was involved, not normal iterative output. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agotop: provide for avoiding task focus ('F') distortions
Jim Warner [Mon, 3 Jan 2022 05:00:00 +0000 (00:00 -0500)]
top: provide for avoiding task focus ('F') distortions

In the patch referenced below the focus task logic was
improved to ensure that newly cloned tasks 'above' the
focused task did not force an effect like the up arrow
key. That commit also acknowledged that when some task
'above' ended, it *would* act like the down arrow key.

Well, with this commit a task ending 'above' a focused
task no longer distorts the focus. That's assuming the
new '#define FOCUS_HARD_Y' is specified plus the total
focused tasks does not exceed the current screen rows.

Thus, the manual scrolling with up and down arrow keys
is allowed when the total focused exceeds screen rows.

[ but keep in mind that when a focused task has been ]
[ hardened some otherwise useful toggles will not be ]
[ available. keystrokes like 'v' and even 'F' itself ]
[ can not be applied to another task with no scroll. ]

Reference(s):
. Sep, 2021 - 'focus' logic improved
commit 69978e365043f27305e487709474947bb377084d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agosysctl: eliminate a warning of '-Wmaybe-uninitialized'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
sysctl: eliminate a warning of '-Wmaybe-uninitialized'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agosysctl: eliminate some warnings of '-Wunused-function'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
sysctl: eliminate some warnings of '-Wunused-function'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agosysctl: eliminate some warnings of '-Wunused-variable'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
sysctl: eliminate some warnings of '-Wunused-variable'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: eliminate that warning of '-Wunused-variable'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
library: eliminate that warning of '-Wunused-variable'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: eliminate warning '-Wunused-but-set-variable'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
library: eliminate warning '-Wunused-but-set-variable'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agolibrary: eliminate those warnings for '-Wpointer-sign'
Jim Warner [Sun, 2 Jan 2022 06:00:09 +0000 (00:00 -0600)]
library: eliminate those warnings for '-Wpointer-sign'

Signed-off-by: Jim Warner <james.warner@comcast.net>
2 years agomisc: amend the man page & source file copyright dates
Jim Warner [Sat, 1 Jan 2022 06:00:00 +0000 (00:00 -0600)]
misc: amend the man page & source file copyright dates

This just updates the copyright dates in the documents
where I was already represented. Others are unchanged.

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agonls: Update man translations
Craig Small [Tue, 21 Dec 2021 22:02:45 +0000 (09:02 +1100)]
nls: Update man translations

Fixed comment in Makefile.am too, thanks to Göran Uddeborg

Signed-off-by: Craig Small <csmall@dropbear.xyz>
References:
 https://www.freelists.org/post/procps/A-comment-needing-an-update

3 years agotop: make the __LINE__ usage and whitespace consistent
Jim Warner [Sat, 18 Dec 2021 06:00:00 +0000 (00:00 -0600)]
top: make the __LINE__ usage and whitespace consistent

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: touch up that code for proc mounted as subset=pid
Jim Warner [Sat, 18 Dec 2021 06:00:00 +0000 (00:00 -0600)]
top: touch up that code for proc mounted as subset=pid

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: adapt for running with proc mounted as subset=pid
Jim Warner [Thu, 16 Dec 2021 06:00:00 +0000 (00:00 -0600)]
top: adapt for running with proc mounted as subset=pid

As the issue cited below illustrates, a pids namespace
with proc mounted as subset=pid denies our library any
access to non-task data. In top's case, the result was
a fatal error message which involved "cpu statistics".

With this patch top will now assume an error involving
global cpu (stat) or memory (meminfo) data means we're
running under a restricted pids namespace. As such, an
attempt will be made to still display task level data.

[ if our assumption is incorrect, it's of no matter. ]
[ instead of a fatal error, we'll still try to offer ]
[ a user some minimally useful bit of functionality. ]

Reference(s):
https://gitlab.com/procps-ng/procps/-/issues/227
https://www.freelists.org/post/procps/three-for-newlib,1
. 1st cut at subset=pid
commit bcb837b8c73f23536a2403b61deeb2b7b3c6be20

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: reposition those 'info' structures in headers
Jim Warner [Wed, 15 Dec 2021 06:00:00 +0000 (00:00 -0600)]
library: reposition those 'info' structures in headers

For some unknown reason all the 'info' structures were
declared between macros and function prototypes rather
than right after all the other structure declarations.

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agops,top: convert 'PIDS_PROCESSOR' into a signed integer
Jim Warner [Tue, 14 Dec 2021 06:00:00 +0000 (00:00 -0600)]
ps,top: convert 'PIDS_PROCESSOR' into a signed integer

Not only does that library tweak help to simplify some
top code, but now that ps snprintf fmtstr will finally
be accurate. That is two birdies with a single pebble!

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: change 'PIDS_PROCESSOR' into a signed integer
Jim Warner [Tue, 14 Dec 2021 06:00:00 +0000 (00:00 -0600)]
library: change 'PIDS_PROCESSOR' into a signed integer

This change is really being made on behalf of the ps &
top programs. Besides, over 2 billion CPUs are plenty!

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agoFirst cut at subset=pid proc mount handling
Craig Small [Thu, 16 Dec 2021 09:36:00 +0000 (20:36 +1100)]
First cut at subset=pid proc mount handling

The procfs mount option subset=pid only shows the processes, not things
such as /proc/stat etc.

For certain programs, this should mean they still work, but have reduced
functionality. This is the first cut at some of them.

pgrep - Removed always loading uptime which we never used anyway. The
program now works fine unless we use --older. Add note in man page
stating it will silently fail.

ps - Load boot time and memory total only when required instead of
always. Changed the error messages to something the user actually
cares about "can't get system boot time" vs "create a structure".
Works for most fields except starts and percent memory.

uptime - Give more useful error messages if uptime not available.

vmstat - move header generation after testing for required proc
files, makes the default output more consistent with the rest
of the options.

References:
 procps-ng/procps#227
 https://www.kernel.org/doc/html/latest/filesystems/proc.html#chapter-4-configuring-procfs
 https://github.com/torvalds/linux/commit/6814ef2d992af09451bbeda4770daa204461329e

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agovmstat: Use KiB instead of pages for paged in/out
Craig Small [Sun, 14 Nov 2021 07:50:58 +0000 (18:50 +1100)]
vmstat: Use KiB instead of pages for paged in/out

While the kernel calls the fields pgpgin and pgpgout, the units
here are not pages, but KiB (or 2x 512 sectors).

The comments come from the referenced merged request, this commit fixes
the "vmstat -s lies" part:

https://elixir.bootlin.com/linux/v5.15-rc7/source/block/blk-core.c#L1057
has submit_bio() which includes the count_vm_events(PGPGIN, count) but what
is count? it is usually what bio_sectors() returns.

bio_sectors() is a macro in
https://elixir.bootlin.com/linux/v5.15-rc7/source/include/linux/bio.h#L49
that defines that as bio->bi_iter.bi_size >> 9. 2^9 is 512 or the sector
size. So our count is incremented by the number of 512-byte sectors.

As @dublio has already pointed out before this result is printed to vmstat,
it is /= 2 to give the number of kibibytes (as the sectors were 512 bytes,
we now made the block size 2*512 or 1024). The code even has
"sectors -> kbytes".

So unless there is something very strange going on, pgpgin and pgpgout in
/proc/vmstat return kibibytes.

What about pages (which is sort of implied in the name) or blocks (as
described on the man page)?

Pages can vary, but they are generally 4 KiB so they're out. That also means
vmstat -s lies :(

Blocks are harder to discount. While these too can vary, they can be 1 KiB;
they could also be something else (e.g dd its 512, filesystems 4096).
However, for memory management inside the kernel, there are sectors and
there are (near userland export) KiB, nothing else. It's probably more
accurate to say sectors are shifted in and out of block devices and the
kernel expresses these transfers to userland as KiB by halving the numbers.

What all this means is that using KiB for bi/bo aka pgpgin/pgpgout is more
accurate than saying blocks or pages.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
References:
 procps-ng/procps!64

3 years agovmstat: use KiB/s for bi/bo instead of blocks/s
Weiping Zhang [Wed, 16 May 2018 05:43:59 +0000 (13:43 +0800)]
vmstat: use KiB/s for bi/bo instead of blocks/s

/proc/vmstat provide kbytes to pgpgin and pgpgout instead of blocks,
correct unit for bi/bo.

References:
 procps-ng/procps!64

Signed-off-by: Weiping Zhang <zhangweiping@didichuxing.com>
Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agolibrary: refine support for multiple concurrent access
Jim Warner [Wed, 10 Nov 2021 05:00:00 +0000 (00:00 -0500)]
library: refine support for multiple concurrent access

Our new library's now well protected against potential
problems which arise when a multi-threaded application
opens more than one context within the same API at the
same time. However, with a single-threaded application
designed along those same lines, some problems remain.

So, to avoid potential corruption of some data (which
was classified as local 'static __thread') from those
single-threaded designs, we'll move several variables
to the info structure itself and remove the '__thread'
qualifier. Now they're protected against both designs.

[ we'll not be protected against some multi-threaded ]
[ application that shares a single context yet calls ]
[ that interface from separate threads. this is just ]
[ bad application design & no different than sharing ]
[ other modifiable global data between such threads! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: this is why we hate those darn tab characters
Jim Warner [Tue, 9 Nov 2021 06:00:00 +0000 (00:00 -0600)]
library: this is why we hate those darn tab characters

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolib: Initialise uptime variables
Craig Small [Tue, 2 Nov 2021 06:23:37 +0000 (17:23 +1100)]
lib: Initialise uptime variables

upminutes and uphours could both not get initialised in the
procps_uptime_sprint_short() function.

Error was probably introduced at the referenced commit.

References:
 Coverity 240787 Uninitialized scalar variable
 Coverity 240776 Uninitialized scalar variable
 commit 0496b39876d569fe1cecb76ad5ef212cd14c0374

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agopgrep: Fix format security
Craig Small [Tue, 2 Nov 2021 06:19:37 +0000 (17:19 +1100)]
pgrep: Fix format security

do_regcomp() error message didn't use a string literal for the format string.

pgrep.c:538:24: error: format not a string literal and no format arguments
[-Werror=format-security]

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agopmap: minor Coverity fix for -N option
Craig Small [Tue, 2 Nov 2021 06:14:29 +0000 (17:14 +1100)]
pmap: minor Coverity fix for -N option

99126 Explicit null dereferenced
Not 100% sure this is valid (the same branch that sets the variable
is the one that sets N_option) but not too hard to fix.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agotop: tweak some end-of-job logic when separate threads
Jim Warner [Thu, 28 Oct 2021 05:00:00 +0000 (00:00 -0500)]
top: tweak some end-of-job logic when separate threads

The separate threads for background updates were added
to top in the commit shown below. At that time cleanup
logic was added to end-of-job processing to cancel any
active threads and destroy any semaphores then in use.

That seemed like simple good stewardship with an added
benefit of avoiding potential valgrind 'possibly lost'
warnings for 320 byte blocks. Those blocks represented
an initial stack allocation for each of three threads.

All of that worked perfectly if such code was executed
under the main thread. In other words, if the keyboard
or a signal directed to any thread was used to trigger
program end. However, it might not always be the case.

Each of those 'refresh' routines supporting a separate
thread interacts with a newlib interface. As a result,
each is required to check the library's return result.
Should some error be detected, 'error_exit' is called.
Now we've got big problems with that eoj cleanup code.

One can't 'pthread_cancel' and 'pthread_join' a thread
from withing that same thread. Thus, when an error was
returned by the library with threads active, top would
hang with no possibility of removal short of a reboot.

So, this commit only executes that cancel/join cleanup
code when we are running under the main thread. Should
program end be triggered by a library error under some
sibling thread, all such cleanup will now be bypassed.
In the latter case, we will rely on documentation that
says any thread calling exit(3) will end every thread.

[ now, the only time we'll see any valgrind warnings ]
[ is with a library error, which is the least likely ]
[ scenario for running valgrind & top to begin with. ]

[ besides, if the valgrind warnings became a problem ]
[ one could easily add a 'warning-suppression' file. ]

Reference(s):
. Sep 2021, top introduced threads
commit 29f0a674a85bfb92443c56f070956a7dd60bb5f7

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: remedy several 'coverity scan' resource leaks
Jim Warner [Wed, 27 Oct 2021 05:00:00 +0000 (00:00 -0500)]
library: remedy several 'coverity scan' resource leaks

This commit will place the 'file2strvec' function on a
par with the 'file2str' function if ENOMEM was raised.

Plus, just to keep coverity happy, we'll also clean up
after potential failures with the 'openproc' function.

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: oh no, failed to right-justify 1 comment line
Jim Warner [Wed, 27 Oct 2021 05:00:00 +0000 (00:00 -0500)]
library: oh no, failed to right-justify 1 comment line

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agopgrep: Match on cgroup v2 paths
Craig Small [Tue, 26 Oct 2021 09:56:19 +0000 (20:56 +1100)]
pgrep: Match on cgroup v2 paths

You can match or filter on cgroup paths. Currently the match is only
done for version 2 cgroups because these are way simpler as they have
a unified name and always start with "0::".

cgroup v1 can have:
 named groups "1:name=myspecialname:"
 controllers "9:blkio:"
 multiple controllers! "4:cpu,cpuacct:"

So they are very much more complicated from a options parsing and
cgroup matching point of view.

In addition, both my Debian bookworm and bullseye systems use
v2 cgroups.

$ ./pgrep --cgroup /system.slice/cron.service
760

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agoNEWS: Add entry for free -h --si fix
Craig Small [Tue, 26 Oct 2021 07:25:37 +0000 (18:25 +1100)]
NEWS: Add entry for free -h --si fix

References:
 commit 91e58792282d676fbcb3781d2ed48d6b082c6c56
 Issues #133 #223
 merge !140

3 years agofix uid/gid > 2^31
Todd Lewis [Mon, 25 Oct 2021 23:38:10 +0000 (19:38 -0400)]
fix uid/gid > 2^31

This MR revisits a partial fix from 2018. The problem stems from incorrect
handling of unsigned 32-bit uid_ts and gid_ts as signed when values are
large - i.e. when the high bit is set. In that case, pgrep and pkill fail to
identify processes by uid. (They succeed when finding the same processes by
username.) The primary fix for this is to impliment the "FIXME" comment in
proc/readproc.h, the implementation of which allows the removal of the (int)
casts from the partial fix from 2018.

The other fixed code in this MR consists of tests in strict_atol() that
detects and errors out on overflows.

References:
 Merge !146

3 years agolibrary: expand warnings in 'warning-suppression' file
Jim Warner [Fri, 22 Oct 2021 05:00:00 +0000 (00:00 -0500)]
library: expand warnings in 'warning-suppression' file

With the addition of more '__thread' attributes in the
previous commit, additional valgrind warnings might be
encountered if developing multi-threaded applications.

So, this patch expands the libproc.supp file which was
originally introduced with the patch referenced below.

Reference(s):
commit be1ddc275623b6cf16060d8a0fa1fb36f677010a

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: extend thread safety to more static variables
Jim Warner [Fri, 22 Oct 2021 05:00:00 +0000 (00:00 -0500)]
library: extend thread safety to more static variables

In the commit referenced below, a '__thread' attribute
was added to numerous static variables to protect them
from concurrent access conflicts with multi-threading.

Unfortunately, that patch did not go quite far enough.

So, this commit adds a few more '__thread' qualifiers.

Reference(s):
commit 23cfb7136636f2d522b31417892de79b011ad3e4

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agodoc: added the new valgrind 'warning-suppression' file
Jim Warner [Thu, 14 Oct 2021 05:00:00 +0000 (00:00 -0500)]
doc: added the new valgrind 'warning-suppression' file

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: created a valgrind 'warning-suppression' file
Jim Warner [Thu, 14 Oct 2021 05:00:00 +0000 (00:00 -0500)]
library: created a valgrind 'warning-suppression' file

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: only use 'pthread_sigmask' under separate threads
Jim Warner [Thu, 14 Oct 2021 05:00:00 +0000 (00:00 -0500)]
top: only use 'pthread_sigmask' under separate threads

When multi-threading was introduced in the patch shown
below, the former calls to sigprocmask were traded for
a pthread_sigmask call. This was done unconditionally.

As a result, even when those threads weren't enabled a
need to link with libpthread was created. In hindsight
the need should only arise when top is multi-threaded.

This commit will make pthread_sigmask use conditional.

Reference(s):
. 09/2021, separate threads introduced
commit 29f0a674a85bfb92443c56f070956a7dd60bb5f7

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agouptime: Correctly print pretty/short format
ed neville [Thu, 14 Oct 2021 08:37:42 +0000 (19:37 +1100)]
uptime: Correctly print pretty/short format

uptime -p would show empty output after 52 weeks of uptime. This commit
is largely the work of Ed but reformatted for newlib branch.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
References:
 procps-ng/procps!141
 procps-ng/procps#217

3 years agovmstat: Add NEWS and man page for previous
Craig Small [Thu, 14 Oct 2021 07:52:34 +0000 (18:52 +1100)]
vmstat: Add NEWS and man page for previous

3 years agovmstat: Provides option to omit first report
Sanskriti Sharma [Wed, 8 Aug 2018 15:48:32 +0000 (11:48 -0400)]
vmstat: Provides option to omit first report

The first report from vmstat provides statistics since system boot. This is
often thrown out. Thus, this provides a command line option to omit it. The
program still provides <count> reports.

Signed-off-by: Sanskriti Sharma <sansharm@redhat.com>
3 years agomisc: Fix typo
林博仁(Buo-ren, Lin) [Tue, 23 Feb 2021 03:02:10 +0000 (03:02 +0000)]
misc: Fix typo

Signed-off-by: 林博仁(Buo-ren, Lin) <Buo.Ren.Lin@gmail.com>
3 years agomisc: Add NEWS for ps ignoring SIGURG
Craig Small [Wed, 13 Oct 2021 20:52:43 +0000 (07:52 +1100)]
misc: Add NEWS for ps ignoring SIGURG

References:
 commit b6ccf865f83f98478a814c1d79d17a0b5212cc6e

3 years agops: ignore SIGURG
Tommi Rantala [Mon, 11 Oct 2021 10:33:18 +0000 (13:33 +0300)]
ps: ignore SIGURG

Stop registering signal handler for SIGURG, to avoid ps failure if
someone sends such signal. Without the signal handler, SIGURG will
just be ignored.

  Signal 23 (URG) caught by ps (3.3.16).
  ps:ps/display.c:66: please report this bug

https://man7.org/linux/man-pages/man7/signal.7.html
https://www.freebsd.org/cgi/man.cgi?sektion=3&query=signal

3 years agoRemove autogen.sh libtool prog check #222.
Rafael Kitover [Mon, 11 Oct 2021 20:23:12 +0000 (20:23 +0000)]
Remove autogen.sh libtool prog check #222.

Remove the check for the libtool executable as many libtool distribution
packages do not have it and it is not necessary for building, as
libtoolize is also checked for to determine the presence of libtool.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
3 years agotestsuite: Make test program compile on Hurd
Craig Small [Tue, 12 Oct 2021 20:56:39 +0000 (07:56 +1100)]
testsuite: Make test program compile on Hurd

procps fails to build from source due to usage of field si_int of struct
siginfo_t in lib/test_process.c which does not exist on GNU/Hurd.

Thanks to Svante Signell for the patch.

References:
 https://bugs.debian.org/987557

Signed-off-by: Craig Small <csmall@dropbear.xyz>
3 years agotop: restore ending ']' when summary graphs are scaled
Jim Warner [Mon, 4 Oct 2021 05:00:00 +0000 (00:00 -0500)]
top: restore ending ']' when summary graphs are scaled

When a visual separator was added to 2 abreast summary
items in a recent commit, this bug was introduced. So,
from that earlier patch we'll revert one line of code.

The bug surfaced under an 80/even column terminal only
when that '4' toggle was off. With an an 81/odd column
screen, it existed in both single and 2 abreast modes.

[ this commit also goes the extra distance to ensure ]
[ two abreast mode maximizes available screen width. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: revert part of a patch regarding separate threads
Jim Warner [Thu, 30 Sep 2021 05:00:00 +0000 (00:00 -0500)]
top: revert part of a patch regarding separate threads

In the patch referenced below, some logic was moved in
that frame_make function in an effort to overlap those
background threads with additional main thread duties.

Unfortunately, the auto-size column feature was broken
in the process. So this patch will revert a portion of
the earlier patch so as to restore the broken feature.

Reference(s):
. Sep 2021, top overlap additional processing
commit f11f43b4f7118f284f9aec19f6885d163848ffe7

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: ensure thread safety via function substitutes
Jim Warner [Tue, 28 Sep 2021 05:00:00 +0000 (00:00 -0500)]
library: ensure thread safety via function substitutes

Even though we we had to abandon the master branch top
multi-thread effort and even though the newlib version
of a multi-threaded top provides no real benefit, that
whole exercise was not wasted. Rather, it has revealed
some deficiencies in our library which this addresses.

If two or more threads in the same address space tried
to use procps_loadavg or procps_uptime simultaneously,
there's a chance they would experience problems due to
thread-unsafe functions our library called internally.

So, this patch switches them for thread-safe versions.

[ along the way we will also make that procps_uptime ]
[ initialization of his 'up' & 'idle' variables mean ]
[ something by delaying the -ERANGE return a little. ]

Reference(s):
https://www.freelists.org/post/procps/a-few-more-patches,7

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agolibrary: ensure thread safety for all static variables
Jim Warner [Tue, 28 Sep 2021 05:00:00 +0000 (00:00 -0500)]
library: ensure thread safety for all static variables

Even though we we had to abandon the master branch top
multi-thread effort and even though the newlib version
of a multi-threaded top provides no real benefit, that
whole exercise was not wasted. Rather, it has revealed
some deficiencies in our library which this addresses.

If two or more threads in the same address space tried
to access the same api simultaneously, there is a good
chance some function-local static variables will yield
some of those renowned unpredictable results. So, this
patch protects them with the '__thread' storage class.

Reference(s):
https://www.freelists.org/post/procps/a-few-more-patches,7

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: ensure those potential focused tasks stay focused
Jim Warner [Thu, 23 Sep 2021 05:00:00 +0000 (00:00 -0500)]
top: ensure those potential focused tasks stay focused

When that 'F' focus command has been applied to a task
in forest view it should remain as the topmost process
in a particular window. But without this patch that is
not guaranteed. Newly forked/cloned tasks 'above' such
a process result in task(s) appearing which shouldn't.

The effect was as if that up arrow key scrolled beyond
the topmost parent task, which would never be allowed.

[ since scrolling is permitted within a focus range, ]
[ when any task 'above' our focus/topmost task ends, ]
[ we respond as if scrolled with the down arrow key. ]

[ that result is completely appropriate. if the user ]
[ wishes to return to a focused parent, the up arrow ]
[ or home key can be used to accomplish such a goal. ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
3 years agotop: overlap some more processing for separate threads
Jim Warner [Wed, 22 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: modify semaphore destruction for separate threads
Jim Warner [Tue, 21 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: reverse the #define defaults for separate threads
Jim Warner [Sun, 19 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: introduce background updates via separate threads
Jim Warner [Sat, 18 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: refactor 3 routines ahead of the separate threads
Jim Warner [Sat, 18 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: tweak end-of-job processing if invoked via signal
Jim Warner [Thu, 16 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agotop: justify those header #define description comments
Jim Warner [Tue, 14 Sep 2021 05:00:00 +0000 (00:00 -0500)]
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>
3 years agodocs: Add note in sysctl.conf.5 for line length
Craig Small [Wed, 15 Sep 2021 11:45:26 +0000 (21:45 +1000)]
docs: Add note in sysctl.conf.5 for line length

References:
 procps-ng/procps#77

3 years agodocs: Fix comment for priority
Craig Small [Wed, 15 Sep 2021 11:36:40 +0000 (21:36 +1000)]
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

3 years agosysctl: Return non-zero if param setting failed
Jan Rybar [Wed, 15 Sep 2021 11:07:43 +0000 (21:07 +1000)]
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>
3 years agomisc: Add NEWS for previous
Craig Small [Wed, 15 Sep 2021 10:54:30 +0000 (20:54 +1000)]
misc: Add NEWS for previous

References:
 procps-ng/procps!91

3 years agopgrep: Add a newline after regex error messages
Tom Levy [Fri, 1 Nov 2019 02:17:54 +0000 (02:17 +0000)]
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>
3 years agotestsuite: Add sysctl test for directory checks
Craig Small [Wed, 15 Sep 2021 10:16:06 +0000 (20:16 +1000)]
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 f25d462166f80b844d33dad3e4c06088c809a426

3 years agosysctl: Update previous patch for newlib
Craig Small [Wed, 15 Sep 2021 10:09:44 +0000 (20:09 +1000)]
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 6389deca5bf667f5fab5912acde78ba8e0febbc7