Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging/unix thread names #1238

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Staging/unix thread names #1238

wants to merge 7 commits into from

Conversation

nunojsa
Copy link
Contributor

@nunojsa nunojsa commented Jan 31, 2025

PR Description

This adds support for naming threads in Unix (marked as TODO). The first commits are about fixing the names as they cannot be bigger than 16 chars. And one of the patches (on the iiod side) is actually a fix since the name of the main network thread was not being set because of the above.

This is kind of a RFC, since it depends on pthread_setname_np() which is non portable (and needs _GNU_SOURCE to be defined) . Still, it should have no effect on windows and was tested under linux. We also check for the API availability in cmake but it would still be a good thing if someone could test this under MAC.

If it is yet not clear, this only adds support for Unix and does not touches on Windows builds.

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have commented new code, particulary complex or unclear areas
  • I have checked that I did not intoduced new warnings or errors (CI output)
  • I have checked that components that use libiio did not get broken
  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)

@nunojsa nunojsa marked this pull request as draft January 31, 2025 09:01
@nunojsa
Copy link
Contributor Author

nunojsa commented Jan 31, 2025

OK, this looks to be not reliable... Not sure if it will be doable at all because it looks like the API just has a different footprint in MAC and that is already discouraging.

err = iio_err(priv->write_task);
if (err)
goto err_free_io;

if (!NO_THREADS) {
priv->read_thrd = iio_thrd_create(iiod_responder_reader_thrd, priv,
"iiod-responder-reader-thd");
"reader-thd");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the length constraint, I suggest removing "-thd" since it's already clear that we're naming threads. Instead, those four characters could be used to better describe the thread or distinguish it from others in relevant situations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it does make sense. I'll see if i can thing on something better... I'm reworking things a bit as I just realized (stupid me) that we do need a "pure" define in here and not something that evaluates to 0 or 1. If the API is not available and place inside an if(0) it will still fail to compile. Dead code removal happens in a later point in time...

Copy link
Contributor Author

@nunojsa nunojsa Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm thinking a bit more I can replace it for something like "job" (we do sabe 1 char). Or "client-reader" but here your argument also applies (we already now it's the client). One thing that could be beneficial would be to differentiate between the clients created on behave of buffer and the main client. But the trouble for that seemed not worth it for me.

@nunojsa nunojsa force-pushed the staging/unix-thread-names branch 4 times, most recently from 108b6ad to ea3eb7b Compare January 31, 2025 14:59
If the iio-config header is not properly included,
HAS_PTHREAD_SETNAME_NP will never be defined and we we'll never set the
thread names.

Signed-off-by: Nuno Sá <[email protected]>
pthread_setname_np(3) does not support names bigger
than 16 characters, including the terminating null byte, which means
it was not properly setting the name of the thread.

Signed-off-by: Nuno Sá <[email protected]>
This is in preparation of adding support for naming threads (in unix).
This is done since pthread_setname_np(3) does not support names bigger
than 16 characters, including the terminating null byte.

Signed-off-by: Nuno Sá <[email protected]>
This is in preparation of adding support for naming threads (in unix).
This is done since pthread_setname_np(3) does not support names bigger
than 16 characters, including the terminating null byte.

Signed-off-by: Nuno Sá <[email protected]>
This is in preparation of adding support for naming threads (in unix).
This is done since pthread_setname_np(3) does not support names bigger
than 16 characters, including the terminating null byte.

Signed-off-by: Nuno Sá <[email protected]>
Create a cmake utilities library for functions or macros that will be
shared across CMake files. For now, just add a function to check the
availability of pthread_setname_np(3). This is in preparation of adding
support for thread names where we'll also check this from the top level
CMake file.

Signed-off-by: Nuno Sá <[email protected]>
Add support for naming threads under Unix. It's done with
pthread_setname_np(3) so we need to make sure it's available. On top of
that, on linux, we also need to compile lock.c with _GNU_SOURCE.

Signed-off-by: Nuno Sá <[email protected]>
@nunojsa nunojsa force-pushed the staging/unix-thread-names branch from ea3eb7b to a4437a1 Compare January 31, 2025 15:06
@nunojsa nunojsa marked this pull request as ready for review January 31, 2025 15:14
@nunojsa
Copy link
Contributor Author

nunojsa commented Jan 31, 2025

Ok, this should be now ready for review. The fact that pthreas_set_name_np() is non portable is far from great but I'm not sure how it can be done in way that tools like ps or top see the names (I mean, I know how I could do it on linux but not on other platforms). I tried anyways to keep things as self contained as possible but it would still be nice to see if this works on MAC (freebsd would also be nice but I guess more difficult)

@dNechita
Copy link
Contributor

dNechita commented Feb 3, 2025

I will try to do a test on MAC this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants