Message ID | 20220207144148.27033-2-andrea.cervesato@suse.de |
---|---|
State | Superseded |
Headers | show |
Series | Add futex_wait testing suite | expand |
Hi! > The TST_THREAD_STATE_WAIT macro can be used to wait and check for > pthread state changes. > > Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de> > --- > include/tst_test.h | 1 + > include/tst_thread_state.h | 33 +++++++++++++++++++++++++++++++++ > lib/tst_thread_state.c | 37 +++++++++++++++++++++++++++++++++++++ > 3 files changed, 71 insertions(+) > create mode 100644 include/tst_thread_state.h > create mode 100644 lib/tst_thread_state.c > > diff --git a/include/tst_test.h b/include/tst_test.h > index 450ddf086..79067f3bf 100644 > --- a/include/tst_test.h > +++ b/include/tst_test.h > @@ -27,6 +27,7 @@ > #include "tst_cmd.h" > #include "tst_cpu.h" > #include "tst_process_state.h" > +#include "tst_thread_state.h" > #include "tst_atomic.h" > #include "tst_kvercmp.h" > #include "tst_kernel.h" > diff --git a/include/tst_thread_state.h b/include/tst_thread_state.h > new file mode 100644 > index 000000000..4d6a345b8 > --- /dev/null > +++ b/include/tst_thread_state.h > @@ -0,0 +1,33 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> > + */ > + > +/* > + * These functions helps you wait till a thread with given tpid changes state. > + */ > + > +#ifndef TST_THREAD_STATE__ > +#define TST_THREAD_STATE__ > + > +#include <unistd.h> > + > +/* > + * Waits for thread state change. > + * > + * The state is one of the following: > + * > + * R - running > + * S - sleeping > + * D - disk sleep > + * T - stopped > + * t - tracing stopped > + * Z - zombie > + * X - dead > + */ > +#define TST_THREAD_STATE_WAIT(tid, state, msec_timeout) \ > + tst_thread_state_wait((tid), (state), (msec_timeout)) > + > +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout); > + > +#endif /* TST_THREAD_STATE__ */ > diff --git a/lib/tst_thread_state.c b/lib/tst_thread_state.c > new file mode 100644 > index 000000000..f5580c39e > --- /dev/null > +++ b/lib/tst_thread_state.c > @@ -0,0 +1,37 @@ > +// SPDX-License-Identifier: GPL-2.0-or-later > +/* > + * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> > + */ > + > +#include <stdio.h> > +#include <unistd.h> > +#include <string.h> > +#include <errno.h> > + > +#include "tst_safe_file_ops.h" > +#include "tst_thread_state.h" > + > +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout) > +{ > + char proc_path[128], cur_state; > + unsigned int msecs = 0; > + > + snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid); > + > + for (;;) { > + SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state); > + > + if (state == cur_state) > + break; > + > + usleep(1000); > + msecs += 1; > + > + if (msec_timeout && msecs >= msec_timeout) { > + errno = ETIMEDOUT; > + return -1; > + } > + } > + > + return 0; > +} Maybe we can just put this code into the existing tst_process_state.h header and C source. And we should add some documentaion about the function into the doc/c-test-api.txt as well. Other than that it looks good.
Hi! On 2/8/22 12:09, Cyril Hrubis wrote: > Hi! >> The TST_THREAD_STATE_WAIT macro can be used to wait and check for >> pthread state changes. >> >> Signed-off-by: Andrea Cervesato<andrea.cervesato@suse.de> >> --- >> include/tst_test.h | 1 + >> include/tst_thread_state.h | 33 +++++++++++++++++++++++++++++++++ >> lib/tst_thread_state.c | 37 +++++++++++++++++++++++++++++++++++++ >> 3 files changed, 71 insertions(+) >> create mode 100644 include/tst_thread_state.h >> create mode 100644 lib/tst_thread_state.c >> >> diff --git a/include/tst_test.h b/include/tst_test.h >> index 450ddf086..79067f3bf 100644 >> --- a/include/tst_test.h >> +++ b/include/tst_test.h >> @@ -27,6 +27,7 @@ >> #include "tst_cmd.h" >> #include "tst_cpu.h" >> #include "tst_process_state.h" >> +#include "tst_thread_state.h" >> #include "tst_atomic.h" >> #include "tst_kvercmp.h" >> #include "tst_kernel.h" >> diff --git a/include/tst_thread_state.h b/include/tst_thread_state.h >> new file mode 100644 >> index 000000000..4d6a345b8 >> --- /dev/null >> +++ b/include/tst_thread_state.h >> @@ -0,0 +1,33 @@ >> +// SPDX-License-Identifier: GPL-2.0-or-later >> +/* >> + * Copyright (C) 2022 SUSE LLC Andrea Cervesato<andrea.cervesato@suse.com> >> + */ >> + >> +/* >> + * These functions helps you wait till a thread with given tpid changes state. >> + */ >> + >> +#ifndef TST_THREAD_STATE__ >> +#define TST_THREAD_STATE__ >> + >> +#include <unistd.h> >> + >> +/* >> + * Waits for thread state change. >> + * >> + * The state is one of the following: >> + * >> + * R - running >> + * S - sleeping >> + * D - disk sleep >> + * T - stopped >> + * t - tracing stopped >> + * Z - zombie >> + * X - dead >> + */ >> +#define TST_THREAD_STATE_WAIT(tid, state, msec_timeout) \ >> + tst_thread_state_wait((tid), (state), (msec_timeout)) >> + >> +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout); >> + >> +#endif /* TST_THREAD_STATE__ */ >> diff --git a/lib/tst_thread_state.c b/lib/tst_thread_state.c >> new file mode 100644 >> index 000000000..f5580c39e >> --- /dev/null >> +++ b/lib/tst_thread_state.c >> @@ -0,0 +1,37 @@ >> +// SPDX-License-Identifier: GPL-2.0-or-later >> +/* >> + * Copyright (C) 2022 SUSE LLC Andrea Cervesato<andrea.cervesato@suse.com> >> + */ >> + >> +#include <stdio.h> >> +#include <unistd.h> >> +#include <string.h> >> +#include <errno.h> >> + >> +#include "tst_safe_file_ops.h" >> +#include "tst_thread_state.h" >> + >> +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout) >> +{ >> + char proc_path[128], cur_state; >> + unsigned int msecs = 0; >> + >> + snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid); >> + >> + for (;;) { >> + SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state); >> + >> + if (state == cur_state) >> + break; >> + >> + usleep(1000); >> + msecs += 1; >> + >> + if (msec_timeout && msecs >= msec_timeout) { >> + errno = ETIMEDOUT; >> + return -1; >> + } >> + } >> + >> + return 0; >> +} > Maybe we can just put this code into the existing tst_process_state.h > header and C source. The tst_safe_process_state.c implementation is currently using old API and it's importing "test.h" . If we want to add TST_THREAD_STATE_WAIT into tst_process_state.h then we also need to rewrite process state headers/implementation files in order to support both old and new API. > > And we should add some documentaion about the function into the > doc/c-test-api.txt as well. > > Other than that it looks good.
Hi! > > Maybe we can just put this code into the existing tst_process_state.h > > header and C source. > The tst_safe_process_state.c implementation is currently using old API > and it's importing "test.h" . If we want to add TST_THREAD_STATE_WAIT > into tst_process_state.h then we also need to rewrite process state > headers/implementation files in order to support both old and new API. Good point, I wouldn't bother. Maybe just use single header then and keep the implementation split?
diff --git a/include/tst_test.h b/include/tst_test.h index 450ddf086..79067f3bf 100644 --- a/include/tst_test.h +++ b/include/tst_test.h @@ -27,6 +27,7 @@ #include "tst_cmd.h" #include "tst_cpu.h" #include "tst_process_state.h" +#include "tst_thread_state.h" #include "tst_atomic.h" #include "tst_kvercmp.h" #include "tst_kernel.h" diff --git a/include/tst_thread_state.h b/include/tst_thread_state.h new file mode 100644 index 000000000..4d6a345b8 --- /dev/null +++ b/include/tst_thread_state.h @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> + */ + +/* + * These functions helps you wait till a thread with given tpid changes state. + */ + +#ifndef TST_THREAD_STATE__ +#define TST_THREAD_STATE__ + +#include <unistd.h> + +/* + * Waits for thread state change. + * + * The state is one of the following: + * + * R - running + * S - sleeping + * D - disk sleep + * T - stopped + * t - tracing stopped + * Z - zombie + * X - dead + */ +#define TST_THREAD_STATE_WAIT(tid, state, msec_timeout) \ + tst_thread_state_wait((tid), (state), (msec_timeout)) + +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout); + +#endif /* TST_THREAD_STATE__ */ diff --git a/lib/tst_thread_state.c b/lib/tst_thread_state.c new file mode 100644 index 000000000..f5580c39e --- /dev/null +++ b/lib/tst_thread_state.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> + */ + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#include "tst_safe_file_ops.h" +#include "tst_thread_state.h" + +int tst_thread_state_wait(pid_t tid, const char state, unsigned int msec_timeout) +{ + char proc_path[128], cur_state; + unsigned int msecs = 0; + + snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid); + + for (;;) { + SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state); + + if (state == cur_state) + break; + + usleep(1000); + msecs += 1; + + if (msec_timeout && msecs >= msec_timeout) { + errno = ETIMEDOUT; + return -1; + } + } + + return 0; +}
The TST_THREAD_STATE_WAIT macro can be used to wait and check for pthread state changes. Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.de> --- include/tst_test.h | 1 + include/tst_thread_state.h | 33 +++++++++++++++++++++++++++++++++ lib/tst_thread_state.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 include/tst_thread_state.h create mode 100644 lib/tst_thread_state.c