* [LTP] [v2 1/2] lib/tst_pid.c: Count used pid by traversing /proc
@ 2023-02-14 12:25 Leo Yu-Chi Liang
2023-02-14 12:25 ` [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure Leo Yu-Chi Liang
0 siblings, 1 reply; 12+ messages in thread
From: Leo Yu-Chi Liang @ 2023-02-14 12:25 UTC (permalink / raw)
To: ltp
Use "ps -eT | wc -l" to calculate the number of used pid
could have an incorrectly larger result because "ps -eT"
may list the same pid multiple times (with different SPID).
Instead, we could count used pid by traversing /proc
directory and countng the subdirectories that have name
composed of digits.
Suggested-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
Changes v1 -> v2
* Split into two patches
---
lib/tst_pid.c | 24 ++++++++++--------------
1 file changed, 10 insertions(+), 14 deletions(-)
diff --git a/lib/tst_pid.c b/lib/tst_pid.c
index 5595e79bd..a282f8cc9 100644
--- a/lib/tst_pid.c
+++ b/lib/tst_pid.c
@@ -18,6 +18,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -113,21 +114,16 @@ static int get_session_pids_limit(void (*cleanup_fn) (void))
int tst_get_free_pids_(void (*cleanup_fn) (void))
{
- FILE *f;
- int rc, used_pids, max_pids, max_session_pids, max_threads;
-
- f = popen("ps -eT | wc -l", "r");
- if (!f) {
- tst_brkm(TBROK, cleanup_fn, "Could not run 'ps' to calculate used pids");
- return -1;
- }
- rc = fscanf(f, "%i", &used_pids);
- pclose(f);
-
- if (rc != 1 || used_pids < 0) {
- tst_brkm(TBROK, cleanup_fn, "Could not read output of 'ps' to calculate used pids");
- return -1;
+ DIR *f;
+ struct dirent *ent;
+ int max_pids, max_session_pids, max_threads, used_pids = 0;
+
+ f = SAFE_OPENDIR("/proc");
+ while ((ent = SAFE_READDIR(f))) {
+ if (isdigit(ent->d_name[0]))
+ ++used_pids;
}
+ SAFE_CLOSEDIR(f);
SAFE_FILE_SCANF(cleanup_fn, PID_MAX_PATH, "%d", &max_pids);
SAFE_FILE_SCANF(cleanup_fn, THREADS_MAX_PATH, "%d", &max_threads);
--
2.34.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 12+ messages in thread* [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 12:25 [LTP] [v2 1/2] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
@ 2023-02-14 12:25 ` Leo Yu-Chi Liang
2023-02-14 13:37 ` Petr Vorel
0 siblings, 1 reply; 12+ messages in thread
From: Leo Yu-Chi Liang @ 2023-02-14 12:25 UTC (permalink / raw)
To: ltp
After Adjusting how we count used pid, we increase
the number of PIDS_RESERVED to void fork failure.
Suggested-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
---
Changes v1 -> v2
* Split into two patches
---
lib/tst_pid.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/tst_pid.c b/lib/tst_pid.c
index a282f8cc9..7582e4828 100644
--- a/lib/tst_pid.c
+++ b/lib/tst_pid.c
@@ -36,7 +36,7 @@
#define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
#define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
/* Leave some available processes for the OS */
-#define PIDS_RESERVE 50
+#define PIDS_RESERVE 200
pid_t tst_get_unused_pid_(void (*cleanup_fn) (void))
{
--
2.34.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 12+ messages in thread* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 12:25 ` [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure Leo Yu-Chi Liang
@ 2023-02-14 13:37 ` Petr Vorel
2023-02-14 13:43 ` Cyril Hrubis
2023-02-14 14:14 ` Leo Liang
0 siblings, 2 replies; 12+ messages in thread
From: Petr Vorel @ 2023-02-14 13:37 UTC (permalink / raw)
To: Leo Yu-Chi Liang; +Cc: ltp
Hi Leo,
> After Adjusting how we count used pid, we increase
> the number of PIDS_RESERVED to void fork failure.
nit: in this case I'd actually keep changes in single commit
(otherwise first commit alone would break tests),
Kind regards,
Petr
> Suggested-by: Petr Vorel <pvorel@suse.cz>
> Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
> Reviewed-by: Petr Vorel <pvorel@suse.cz>
> ---
> Changes v1 -> v2
> * Split into two patches
> ---
> lib/tst_pid.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
> diff --git a/lib/tst_pid.c b/lib/tst_pid.c
> index a282f8cc9..7582e4828 100644
> --- a/lib/tst_pid.c
> +++ b/lib/tst_pid.c
> @@ -36,7 +36,7 @@
> #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
> #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
> /* Leave some available processes for the OS */
> -#define PIDS_RESERVE 50
> +#define PIDS_RESERVE 200
> pid_t tst_get_unused_pid_(void (*cleanup_fn) (void))
> {
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 13:37 ` Petr Vorel
@ 2023-02-14 13:43 ` Cyril Hrubis
2023-02-14 14:17 ` Leo Liang
2023-02-14 14:14 ` Leo Liang
1 sibling, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2023-02-14 13:43 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi!
> > After Adjusting how we count used pid, we increase
> > the number of PIDS_RESERVED to void fork failure.
> nit: in this case I'd actually keep changes in single commit
> (otherwise first commit alone would break tests),
Do we get a different result from ps and parsing /proc? That sounds
strange...
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 13:43 ` Cyril Hrubis
@ 2023-02-14 14:17 ` Leo Liang
2023-02-14 14:26 ` Cyril Hrubis
0 siblings, 1 reply; 12+ messages in thread
From: Leo Liang @ 2023-02-14 14:17 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hi Cyril,
On Tue, Feb 14, 2023 at 02:43:35PM +0100, Cyril Hrubis wrote:
> Hi!
> > > After Adjusting how we count used pid, we increase
> > > the number of PIDS_RESERVED to void fork failure.
> > nit: in this case I'd actually keep changes in single commit
> > (otherwise first commit alone would break tests),
>
> Do we get a different result from ps and parsing /proc? That sounds
> strange...
I think that's because "ps -eT" would list threads with the same PID
but with different SPID.
I get the following output on my VM.
ycliang@ubuntu01:~$ ps -eT | wc -l
170
ycliang@ubuntu01:~$ ls -d /proc/[0-9]* | xargs -n1 | wc -l
127
Best regards,
Leo
>
> --
> Cyril Hrubis
> chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 14:17 ` Leo Liang
@ 2023-02-14 14:26 ` Cyril Hrubis
2023-02-14 15:09 ` Cyril Hrubis
0 siblings, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2023-02-14 14:26 UTC (permalink / raw)
To: Leo Liang; +Cc: ltp
Hi!
> > > > After Adjusting how we count used pid, we increase
> > > > the number of PIDS_RESERVED to void fork failure.
> > > nit: in this case I'd actually keep changes in single commit
> > > (otherwise first commit alone would break tests),
> >
> > Do we get a different result from ps and parsing /proc? That sounds
> > strange...
>
> I think that's because "ps -eT" would list threads with the same PID
> but with different SPID.
>
> I get the following output on my VM.
>
> ycliang@ubuntu01:~$ ps -eT | wc -l
> 170
> ycliang@ubuntu01:~$ ls -d /proc/[0-9]* | xargs -n1 | wc -l
> 127
Adjusting the RESERVED constant is then a lousy workaround that wouldn't
work for systems with many threads per process.
One alternative would be to open /proc/$PID/status and read the number
of threads from there. Should be as easy as one call to
SAFE_FILE_LINES_SCANF().
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 14:26 ` Cyril Hrubis
@ 2023-02-14 15:09 ` Cyril Hrubis
2023-02-16 5:40 ` Leo Liang
0 siblings, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2023-02-14 15:09 UTC (permalink / raw)
To: Leo Liang; +Cc: ltp
Hi!
> > > > > After Adjusting how we count used pid, we increase
> > > > > the number of PIDS_RESERVED to void fork failure.
> > > > nit: in this case I'd actually keep changes in single commit
> > > > (otherwise first commit alone would break tests),
> > >
> > > Do we get a different result from ps and parsing /proc? That sounds
> > > strange...
> >
> > I think that's because "ps -eT" would list threads with the same PID
> > but with different SPID.
> >
> > I get the following output on my VM.
> >
> > ycliang@ubuntu01:~$ ps -eT | wc -l
> > 170
> > ycliang@ubuntu01:~$ ls -d /proc/[0-9]* | xargs -n1 | wc -l
> > 127
>
> Adjusting the RESERVED constant is then a lousy workaround that wouldn't
> work for systems with many threads per process.
>
> One alternative would be to open /proc/$PID/status and read the number
> of threads from there. Should be as easy as one call to
> SAFE_FILE_LINES_SCANF().
Thinking of it again using SAFE_FILE_LINES_SCANF() may be prone to a
race where the process exits and the file disappears between the call to
the readdir() and the open in the SAFE_FILE_LINES_SCANF() so I suppose
that we should use just the FILE_LINES_SCANF() instead and add the
threads value only if the call succeeded.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 15:09 ` Cyril Hrubis
@ 2023-02-16 5:40 ` Leo Liang
2023-02-16 8:46 ` Cyril Hrubis
0 siblings, 1 reply; 12+ messages in thread
From: Leo Liang @ 2023-02-16 5:40 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hi Cyril,
On Tue, Feb 14, 2023 at 04:09:52PM +0100, Cyril Hrubis wrote:
> Hi!
> > > > > > After Adjusting how we count used pid, we increase
> > > > > > the number of PIDS_RESERVED to void fork failure.
> > > > > nit: in this case I'd actually keep changes in single commit
> > > > > (otherwise first commit alone would break tests),
> > > >
> > > > Do we get a different result from ps and parsing /proc? That sounds
> > > > strange...
> > >
> > > I think that's because "ps -eT" would list threads with the same PID
> > > but with different SPID.
> > >
> > > I get the following output on my VM.
> > >
> > > ycliang@ubuntu01:~$ ps -eT | wc -l
> > > 170
> > > ycliang@ubuntu01:~$ ls -d /proc/[0-9]* | xargs -n1 | wc -l
> > > 127
> >
> > Adjusting the RESERVED constant is then a lousy workaround that wouldn't
> > work for systems with many threads per process.
> >
> > One alternative would be to open /proc/$PID/status and read the number
> > of threads from there. Should be as easy as one call to
> > SAFE_FILE_LINES_SCANF().
>
> Thinking of it again using SAFE_FILE_LINES_SCANF() may be prone to a
> race where the process exits and the file disappears between the call to
> the readdir() and the open in the SAFE_FILE_LINES_SCANF() so I suppose
> that we should use just the FILE_LINES_SCANF() instead and add the
> threads value only if the call succeeded.
Thanks for the advice!
Will send a v3.
Just out of curiosity, is there any reason that we should do this in plain C ?
(Otherwise, we could drop this patchset and stay with the current implementation)
Best regards,
Leo
>
> --
> Cyril Hrubis
> chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-16 5:40 ` Leo Liang
@ 2023-02-16 8:46 ` Cyril Hrubis
2023-02-16 14:52 ` Leo Liang
0 siblings, 1 reply; 12+ messages in thread
From: Cyril Hrubis @ 2023-02-16 8:46 UTC (permalink / raw)
To: Leo Liang; +Cc: ltp
Hi!
> Just out of curiosity, is there any reason that we should do this in plain C ?
> (Otherwise, we could drop this patchset and stay with the current implementation)
There are a few, calling random scripts from C is a bad practice
overall.
Portabilitity may be one of the problems, there are several
iimplementations of the basic UNIX utilities for Linux eg. coreutils,
busybox, toybox, etc. These implemtations are subtly incompatible, not
all commandline options are supported and so on. And for the busybox and
toybox some options can be disabled at a compile time. We leaned that
sometimes you have to double check if the functionality available and
most of the time the end result is that it's just easier to rewrite the
code in C.
We also have rule to make tests as self contained as possible, which
simplifies debugging. One of the problems is that we do not have the
environment the shell code runs in under control, we had a few test
failing for non-standard settings of the LANG variables.
In this case the code is reasonably simple, so it will be less likely to
be problematic, however I would stil lean towards replacing it with C
code.
tl;dr Calling shell code from C programs makes things less predictable
and possibly unstable.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-16 8:46 ` Cyril Hrubis
@ 2023-02-16 14:52 ` Leo Liang
2023-02-16 22:59 ` Petr Vorel
0 siblings, 1 reply; 12+ messages in thread
From: Leo Liang @ 2023-02-16 14:52 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: ltp
Hi Cyril,
On Thu, Feb 16, 2023 at 09:46:59AM +0100, Cyril Hrubis wrote:
> Hi!
> > Just out of curiosity, is there any reason that we should do this in plain C ?
> > (Otherwise, we could drop this patchset and stay with the current implementation)
>
> There are a few, calling random scripts from C is a bad practice
> overall.
>
> Portabilitity may be one of the problems, there are several
> iimplementations of the basic UNIX utilities for Linux eg. coreutils,
> busybox, toybox, etc. These implemtations are subtly incompatible, not
> all commandline options are supported and so on. And for the busybox and
> toybox some options can be disabled at a compile time. We leaned that
> sometimes you have to double check if the functionality available and
> most of the time the end result is that it's just easier to rewrite the
> code in C.
>
> We also have rule to make tests as self contained as possible, which
> simplifies debugging. One of the problems is that we do not have the
> environment the shell code runs in under control, we had a few test
> failing for non-standard settings of the LANG variables.
>
> In this case the code is reasonably simple, so it will be less likely to
> be problematic, however I would stil lean towards replacing it with C
> code.
>
> tl;dr Calling shell code from C programs makes things less predictable
> and possibly unstable.
>
Understood! Thank you for the detailed explanation!!
Will send a v3 patch ASAP in accordance with your advice!
Best regards,
Leo
> --
> Cyril Hrubis
> chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure.
2023-02-14 13:37 ` Petr Vorel
2023-02-14 13:43 ` Cyril Hrubis
@ 2023-02-14 14:14 ` Leo Liang
1 sibling, 0 replies; 12+ messages in thread
From: Leo Liang @ 2023-02-14 14:14 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi Petr,
On Tue, Feb 14, 2023 at 02:37:14PM +0100, Petr Vorel wrote:
> Hi Leo,
>
> > After Adjusting how we count used pid, we increase
> > the number of PIDS_RESERVED to void fork failure.
> nit: in this case I'd actually keep changes in single commit
> (otherwise first commit alone would break tests),
>
That makes sense!
Then we could probably drop this v2 patch
and stay with the v1?
Best regards,
Leo
> Kind regards,
> Petr
>
> > Suggested-by: Petr Vorel <pvorel@suse.cz>
> > Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
> > Reviewed-by: Petr Vorel <pvorel@suse.cz>
> > ---
> > Changes v1 -> v2
> > * Split into two patches
> > ---
> > lib/tst_pid.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
>
> > diff --git a/lib/tst_pid.c b/lib/tst_pid.c
> > index a282f8cc9..7582e4828 100644
> > --- a/lib/tst_pid.c
> > +++ b/lib/tst_pid.c
> > @@ -36,7 +36,7 @@
> > #define CGROUPS_V1_SLICE_FMT "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max"
> > #define CGROUPS_V2_SLICE_FMT "/sys/fs/cgroup/user.slice/user-%d.slice/pids.max"
> > /* Leave some available processes for the OS */
> > -#define PIDS_RESERVE 50
> > +#define PIDS_RESERVE 200
>
> > pid_t tst_get_unused_pid_(void (*cleanup_fn) (void))
> > {
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2023-02-16 22:59 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-14 12:25 [LTP] [v2 1/2] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
2023-02-14 12:25 ` [LTP] [v2 2/2] lib/tst_pid.c: Increase PIDS_RESERVED to avoid fork failure Leo Yu-Chi Liang
2023-02-14 13:37 ` Petr Vorel
2023-02-14 13:43 ` Cyril Hrubis
2023-02-14 14:17 ` Leo Liang
2023-02-14 14:26 ` Cyril Hrubis
2023-02-14 15:09 ` Cyril Hrubis
2023-02-16 5:40 ` Leo Liang
2023-02-16 8:46 ` Cyril Hrubis
2023-02-16 14:52 ` Leo Liang
2023-02-16 22:59 ` Petr Vorel
2023-02-14 14:14 ` Leo Liang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox