Bug 268532 - [libthr] pthread_mutex_destroy/pshared_gc slow when destroying many process shared locks
Summary: [libthr] pthread_mutex_destroy/pshared_gc slow when destroying many process s...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-12-24 05:42 UTC by Atle Solbakken
Modified: 2026-10-05 01:05 UTC (History)
0 users

See Also:
linimon: mfc-stable14?


Attachments
Patch (1.67 KB, patch)
2022-12-24 05:42 UTC, Atle Solbakken
no flags Details | Diff
Patch with saving last timestamp after iteration instead of before (1.62 KB, patch)
2023-09-24 06:05 UTC, Atle Solbakken
no flags Details | Diff
Patch with saving last timestamp after iteration instead of before (1.37 KB, patch)
2026-05-27 06:30 UTC, Mark Linimon
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Atle Solbakken 2022-12-24 05:42:07 UTC
Created attachment 238992 [details]
Patch

The garbage collection function of process shared locks, pshared_gc, is called whenever a process shared lock is destroyed. The GC function has a loop which checks entries in a hashmap of linked lists, looping through all registered locks.

If a process has a lot of process shared locks, like more than a few hundred, the GC function causes destruction of these locks to take a long time (exponential). This is noticeable especially if many locks are to be destroyed at the same time.

There are probably multiple solutions for this, like calling the GC function only every X lock destruction, by having a time limit or by calling it upon some other event.

The provided patch limits how often the GC function can run to at most every 25 milliseconds per process. If i understand it correctly, the only potential GC required after lock destruction should be in other processes, which have their own timers.

Best regards
Atle
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2022-12-27 20:19:06 UTC
Assign.

Delete the [patch] convention as it has been obsoleted by Bugzilla understanding
which attachments are patches.
Comment 2 Konstantin Belousov freebsd_committer freebsd_triage 2023-01-01 09:39:33 UTC
In principle, I agree with this tweak.
I have a suggestion, please record the time of the finish of the last gc,
instead of start. Otherwise, I suspect it still would allow for too much gcing
in some situations.
Comment 3 Atle Solbakken 2023-09-24 06:05:40 UTC
Created attachment 245177 [details]
Patch with saving last timestamp after iteration instead of before

Thanks for the suggestion.

I've moved the store of the last iteration time to after the GC-ing. Now, in the situation of GC-ing being slow, we will still wait a consistent amount of time before doing it again.
Comment 4 Mark Linimon freebsd_committer freebsd_triage 2026-05-27 06:30:24 UTC
Created attachment 271223 [details]
Patch with saving last timestamp after iteration instead of before

^Triage: rebase patch.
Comment 5 commit-hook freebsd_committer freebsd_triage 2026-09-27 23:51:59 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=4472a048cf95c9c1593a8b04655a59096b44b0c9

commit 4472a048cf95c9c1593a8b04655a59096b44b0c9
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-27 22:16:33 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-09-27 22:30:00 +0000

    libthr: GC pshared locks not more than each 25msecs by default

    Provide a tunable env variable to request immediate GC on destroy
    to allow to return to the previous behavior.

    Based on the report and patch by Atle Solbakken <atle.solbakken@gmail.com>.
    PR:     268532
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week

 lib/libthr/thread/thr_init.c    |  3 +++
 lib/libthr/thread/thr_private.h |  1 +
 lib/libthr/thread/thr_pshared.c | 25 ++++++++++++++++++++++---
 3 files changed, 26 insertions(+), 3 deletions(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-10-04 00:34:57 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=e831067d51b422e7350a0962ba77ffc68b10a045

commit e831067d51b422e7350a0962ba77ffc68b10a045
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-09-27 22:16:33 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-10-04 00:33:40 +0000

    libthr: GC pshared locks not more than each 25msecs by default

    PR:     268532

    (cherry picked from commit 4472a048cf95c9c1593a8b04655a59096b44b0c9)

 lib/libthr/thread/thr_init.c    |  3 +++
 lib/libthr/thread/thr_private.h |  1 +
 lib/libthr/thread/thr_pshared.c | 25 ++++++++++++++++++++++---
 3 files changed, 26 insertions(+), 3 deletions(-)
Comment 7 Mark Linimon freebsd_committer freebsd_triage 2026-10-05 01:03:09 UTC
^Triage: assign.  If mfc-14 not planned, please just close.