From f0dd004dcfbffabd99520dbe59d236f665e43529 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Fri, 29 Mar 2024 17:31:01 +0100 Subject: [PATCH] Add libproc2-test, and explain why we don't use libproc2 --- README.md | 7 +++++++ contrib/libproc2-test/.gitignore | 1 + contrib/libproc2-test/Makefile | 5 +++++ contrib/libproc2-test/libproc2-test.c | 30 +++++++++++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 contrib/libproc2-test/.gitignore create mode 100644 contrib/libproc2-test/Makefile create mode 100644 contrib/libproc2-test/libproc2-test.c diff --git a/README.md b/README.md index 7091a87..13c3024 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,13 @@ accept * Use case reports and feedback +Implementation Notes +-------------------- + +* We don't use [procps/libproc2](https://man7.org/linux/man-pages/man3/procps_pids.3.html) because + procps_pids_select(), for some reason, always parses /proc/$pid/status. + This is relatively expensive, and we don't need it. + Changelog --------- diff --git a/contrib/libproc2-test/.gitignore b/contrib/libproc2-test/.gitignore new file mode 100644 index 0000000..e2e7327 --- /dev/null +++ b/contrib/libproc2-test/.gitignore @@ -0,0 +1 @@ +/out diff --git a/contrib/libproc2-test/Makefile b/contrib/libproc2-test/Makefile new file mode 100644 index 0000000..daca5d6 --- /dev/null +++ b/contrib/libproc2-test/Makefile @@ -0,0 +1,5 @@ +out: Makefile *.c + gcc *.c -Wall -Wextra -g -o out -lproc2 + +format: + clang-format --style=file -i *.c \ No newline at end of file diff --git a/contrib/libproc2-test/libproc2-test.c b/contrib/libproc2-test/libproc2-test.c new file mode 100644 index 0000000..84dd166 --- /dev/null +++ b/contrib/libproc2-test/libproc2-test.c @@ -0,0 +1,30 @@ +#include +#include +#include + +int main(int argc, char* argv[]) +{ + enum pids_item items[] = { PIDS_STATE }; + struct pids_info* info; + struct pids_stack* stack; + struct pids_fetch* fetched; + + int ret = procps_pids_new(&info, items, 1); + if (ret != 0) { + printf("new failured, ret=%d\n", ret); + exit(4); + } + + unsigned pidlist[] = { 1 }; + fetched = procps_pids_select(info, pidlist, 1, PIDS_SELECT_PID_THREADS); + if (!fetched) { + printf("select error\n"); + exit(3); + } + if (fetched->counts->total != 1) { + exit(2); + } + stack = fetched->stacks[0]; + char state = PIDS_VAL(0, s_ch, stack, info); + printf("%c\n", state); +} \ No newline at end of file