Skip to content

Commit

Permalink
Add libproc2-test, and explain why we don't use libproc2
Browse files Browse the repository at this point in the history
  • Loading branch information
rfjakob committed Mar 29, 2024
1 parent 1b532cc commit f0dd004
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------

Expand Down
1 change: 1 addition & 0 deletions contrib/libproc2-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/out
5 changes: 5 additions & 0 deletions contrib/libproc2-test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
out: Makefile *.c
gcc *.c -Wall -Wextra -g -o out -lproc2

format:
clang-format --style=file -i *.c
30 changes: 30 additions & 0 deletions contrib/libproc2-test/libproc2-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <libproc2/pids.h>
#include <stdio.h>
#include <stdlib.h>

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);
}

0 comments on commit f0dd004

Please sign in to comment.