* [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc
@ 2023-02-13 13:41 Leo Yu-Chi Liang
2023-02-13 17:33 ` Petr Vorel
2023-02-14 10:58 ` Cyril Hrubis
0 siblings, 2 replies; 4+ messages in thread
From: Leo Yu-Chi Liang @ 2023-02-13 13:41 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 count the subdirectories that have name
composed of digits.
Increase PIDS_RESERVED to avoid fork failure.
Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
---
lib/tst_pid.c | 26 +++++++++++---------------
1 file changed, 11 insertions(+), 15 deletions(-)
diff --git a/lib/tst_pid.c b/lib/tst_pid.c
index 5595e79bd..7582e4828 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>
@@ -35,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))
{
@@ -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] 4+ messages in thread
* Re: [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc
2023-02-13 13:41 [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
@ 2023-02-13 17:33 ` Petr Vorel
2023-02-14 10:58 ` Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: Petr Vorel @ 2023-02-13 17:33 UTC (permalink / raw
To: Leo Yu-Chi Liang; +Cc: ltp
Hi Leo,
> 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 count the subdirectories that have name
> composed of digits.
> Increase PIDS_RESERVED to avoid fork failure.
Thank you!
Waiting little longer before merging to give others change to have look.
Suggested-by: Petr Vorel <pvorel@suse.cz>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc
2023-02-13 13:41 [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
2023-02-13 17:33 ` Petr Vorel
@ 2023-02-14 10:58 ` Cyril Hrubis
2023-02-14 11:59 ` Leo Liang
1 sibling, 1 reply; 4+ messages in thread
From: Cyril Hrubis @ 2023-02-14 10:58 UTC (permalink / raw
To: Leo Yu-Chi Liang; +Cc: ltp
Hi!
> 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 count the subdirectories that have name
> composed of digits.
>
> Increase PIDS_RESERVED to avoid fork failure.
The changes looks good, but can we please split the patch into two?
We should keep the change to the PIDS_RESERVED separate from the code
changes.
> Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
> ---
> lib/tst_pid.c | 26 +++++++++++---------------
> 1 file changed, 11 insertions(+), 15 deletions(-)
>
> diff --git a/lib/tst_pid.c b/lib/tst_pid.c
> index 5595e79bd..7582e4828 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>
> @@ -35,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))
> {
> @@ -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
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc
2023-02-14 10:58 ` Cyril Hrubis
@ 2023-02-14 11:59 ` Leo Liang
0 siblings, 0 replies; 4+ messages in thread
From: Leo Liang @ 2023-02-14 11:59 UTC (permalink / raw
To: Cyril Hrubis; +Cc: ltp
Hi Cyril,
On Tue, Feb 14, 2023 at 11:58:21AM +0100, Cyril Hrubis wrote:
> Hi!
> > 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 count the subdirectories that have name
> > composed of digits.
> >
> > Increase PIDS_RESERVED to avoid fork failure.
>
> The changes looks good, but can we please split the patch into two?
>
> We should keep the change to the PIDS_RESERVED separate from the code
> changes.
>
Got it!
Will send a v3 ASAP!
Best regards,
Leo
> > Signed-off-by: Leo Yu-Chi Liang <ycliang@andestech.com>
> > ---
> > lib/tst_pid.c | 26 +++++++++++---------------
> > 1 file changed, 11 insertions(+), 15 deletions(-)
> >
> > diff --git a/lib/tst_pid.c b/lib/tst_pid.c
> > index 5595e79bd..7582e4828 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>
> > @@ -35,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))
> > {
> > @@ -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
>
> --
> Cyril Hrubis
> chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-02-14 11:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-13 13:41 [LTP] [PATCH 1/1] lib/tst_pid.c: Count used pid by traversing /proc Leo Yu-Chi Liang
2023-02-13 17:33 ` Petr Vorel
2023-02-14 10:58 ` Cyril Hrubis
2023-02-14 11:59 ` Leo Liang
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.