Merge pull request #9792 from poettering/hashmap-mempool

minor hashmap fixes
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-08-06 14:13:30 +02:00 committed by GitHub
commit 7735d9397b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 127 additions and 139 deletions

7
TODO
View File

@ -23,6 +23,13 @@ Janitorial Clean-ups:
Features:
* optionally, if a per-partition GPT flag is set for the root/home/… partitions
format the partition on next boot and unset the flag, in order to implement
factory reset. also, add a second flag that simply indicates whether such a
scheme is supported. then, add a tool (or maybe beef up systemd-dissect) to
show state of these flags, and optionally trigger such a factory reset on
next boot by setting the flag.
* logind: maybe watch utmp asynchronously using inotify, and populate our own
tracked session metadata from the fields available therein. Why bother? Right
now, all "ssh" sessions will be tracked without their TTY by logind (which is

View File

@ -42,6 +42,9 @@ All tools:
are understood, too (us, ms, s, min, h, d, w, month, y). If it is not set or set
to 0, then the built-in default is used.
* `$SYSTEMD_MEMPOOL=0` — if set the internal memory caching logic employed by
hash tables is turned off, and libc malloc() is used for all allocations.
systemctl:
* `$SYSTEMCTL_FORCE_BUS=1` — if set, do not connect to PID1's private D-Bus

View File

@ -48,11 +48,9 @@
<citerefentry><refentrytitle>udev_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
It is used to track library state and link objects together. No
global state is used by libudev, everything is always linked to
a udev context. Furthermore, multiple different udev contexts can
be used in parallel by multiple threads. However, a single context
must not be accessed by multiple threads in parallel. The caller
is responsible for providing suitable locking if they intend to use
it from multiple threads.</para>
a udev context.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<para>To introspect a local device on a system, a udev device
object can be created via

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd-bus-errors">
<refentry id="sd-bus-errors"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd-bus-errors</title>
@ -259,15 +260,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>The various error definitions described here are available
as a shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -77,16 +77,15 @@
<refsect1>
<title>Thread safety</title>
<para>Functions that operate on the <structname>sd_journal</structname> object are thread
agnostic — given <structname>sd_journal</structname> pointer may only be used from one thread at
a time, but multiple threads may use multiple such objects safely. Other functions —
those that are used to send entries to the journal, like
<citerefentry><refentrytitle>sd_journal_print</refentrytitle><manvolnum>3</manvolnum></citerefentry>
and similar, or those that are used to retrieve global information like
<citerefentry><refentrytitle>sd_journal_stream_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry>
and
<para>Functions that operate on <structname>sd_journal</structname> objects are thread agnostic — given
<structname>sd_journal</structname> pointer may only be used from one specific thread at all times (and it has to
be the very same one during the entire lifetime of the object), but multiple, independent threads may use multiple,
independent objects safely. Other functions — those that are used to send entries to the journal, like
<citerefentry><refentrytitle>sd_journal_print</refentrytitle><manvolnum>3</manvolnum></citerefentry> and similar,
or those that are used to retrieve global information like
<citerefentry><refentrytitle>sd_journal_stream_fd</refentrytitle><manvolnum>3</manvolnum></citerefentry> and
<citerefentry><refentrytitle>sd_journal_get_catalog_for_message_id</refentrytitle><manvolnum>3</manvolnum></citerefentry>
— are thread-safe and may be called from multiple threads in parallel.</para>
— are fully thread-safe and may be called from multiple threads in parallel.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />

View File

@ -8,7 +8,7 @@
Copyright © 2016 Julian Orth
-->
<refentry id="sd_bus_add_match">
<refentry id="sd_bus_add_match" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_add_match</title>
@ -154,9 +154,7 @@
<refsect1>
<title>Notes</title>
<para><function>sd_bus_add_match()</function> and the other functions described here are available as a shared
library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>
<refsect1>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_error_add_map">
<refentry id="sd_bus_error_add_map"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_error_add_map</title>
@ -123,15 +124,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>The various error definitions described here are available
as a shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_is_open">
<refentry id="sd_bus_is_open"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_is_open</title>
@ -88,13 +89,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_is_open()</function> and <function>sd_bus_is_ready()</function> are available as
a shared library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,7 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_path_encode">
<refentry id="sd_bus_path_encode" xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_path_encode</title>
@ -141,15 +141,7 @@
by the caller.</para>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_path_encode()</function> and
<function>sd_bus_path_decode()</function> are available as a
shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_request_name">
<refentry id="sd_bus_request_name"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_request_name</title>
@ -193,13 +194,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>The <function>sd_bus_acquire_name()</function> and the other interfaces described here are available as a
shared library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_set_connected_signal">
<refentry id="sd_bus_set_connected_signal"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_set_connected_signal</title>
@ -94,13 +95,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_set_connected_signal()</function> and <function>sd_bus_get_connected_signal()</function> are available as
a shared library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_set_sender">
<refentry id="sd_bus_set_sender"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_set_sender</title>
@ -89,13 +90,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_set_sender()</function> and <function>sd_bus_get_sender()</function> are available as
a shared library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_bus_set_watch_bind">
<refentry id="sd_bus_set_watch_bind"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_bus_set_watch_bind</title>
@ -100,13 +101,7 @@
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para><function>sd_bus_set_watch_bind()</function> and <function>sd_bus_get_watch_bind()</function> are available as
a shared library, which can be compiled and linked to with the <constant>libsystemd</constant> <citerefentry
project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry> file.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>See Also</title>

View File

@ -86,8 +86,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict" />
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -87,9 +87,14 @@
<refsect1>
<title>Notes</title>
<para>Function <function>sd_journal_get_catalog()</function> is thread-agnostic and only a
single thread may operate on a given <structname>sd_journal</structname> object. Function
<function>sd_journal_get_catalog_for_message_id()</function> is thread-safe.</para>
<para>Function <function>sd_journal_get_catalog()</function> is thread-agnostic and only
a single specific thread may operate on a given object during its entire lifetime. It's safe to allocate multiple
independent objects and use each from a specific thread in parallel. However, it's not safe to allocate such an
object in one thread, and operate or free it from any other, even if locking is used to ensure these threads don't
operate on it at the very same time.</para>
<para>Function <function>sd_journal_get_catalog_for_message_id()</function> is are thread-safe and may be called in
parallel from multiple threads.</para>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -98,8 +98,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict" />
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -96,8 +96,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict" />
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -156,7 +156,13 @@
success or a negative errno-style error code.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>Notes</title>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>
<refsect1>
<title>Examples</title>

View File

@ -226,14 +226,9 @@ else {
<refsect1>
<title>Notes</title>
<para>The <function>sd_journal_get_fd()</function>,
<function>sd_journal_get_events()</function>,
<function>sd_journal_reliable_fd()</function>,
<function>sd_journal_process()</function> and
<function>sd_journal_wait()</function> interfaces are available as
a shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>
<refsect1>

View File

@ -89,7 +89,13 @@
<function>sd_journal_get_monotonic_usec()</function>.</para>
</refsect1>
<xi:include href="libsystemd-pkgconfig.xml" />
<refsect1>
<title>Notes</title>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>
<refsect1>
<title>See Also</title>

View File

@ -56,8 +56,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -66,8 +66,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -122,8 +122,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -6,7 +6,8 @@
SPDX-License-Identifier: LGPL-2.1+
-->
<refentry id="sd_journal_open">
<refentry id="sd_journal_open"
xmlns:xi="http://www.w3.org/2001/XInclude">
<refentryinfo>
<title>sd_journal_open</title>
@ -184,15 +185,9 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<para>The <function>sd_journal_open()</function>,
<function>sd_journal_open_directory()</function> and
<function>sd_journal_close()</function> interfaces are available
as a shared library, which can be compiled and linked to with the
<constant>libsystemd</constant> <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>
file.</para>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>
<refsect1>

View File

@ -177,7 +177,8 @@ sd_journal_send("MESSAGE=Hello World, this is PID %lu!", (unsigned long) getpid(
<refsect1>
<title>Thread safety</title>
<para>All functions listed here are thread-safe and may be called in parallel from multiple threads.</para>
<xi:include href="threads-aware.xml" xpointer="safe"/>
<para><function>sd_journal_sendv()</function> is "async signal safe" in the meaning of <citerefentry
project='man-pages'><refentrytitle>signal-safety</refentrytitle><manvolnum>7</manvolnum></citerefentry>.

View File

@ -126,8 +126,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -120,8 +120,7 @@
<refsect1>
<title>Notes</title>
<para>All functions listed here are thread-agnostic and only a single thread may operate
on a given <structname>sd_journal</structname> object.</para>
<xi:include href="threads-aware.xml" xpointer="strict"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

View File

@ -92,8 +92,7 @@
<refsect1>
<title>Notes</title>
<para>Function <function>sd_journal_stream_fd()</function> is thread-safe and may be called
from multiple threads.</para>
<xi:include href="threads-aware.xml" xpointer="safe"/>
<xi:include href="libsystemd-pkgconfig.xml" xpointer="pkgconfig-text"/>
</refsect1>

17
man/threads-aware.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<!--
SPDX-License-Identifier: LGPL-2.1+
-->
<refsect1>
<para id="strict">All functions listed here are thread-agnostic and only a single specific thread may operate on a
given object during its entire lifetime. It's safe to allocate multiple independent objects and use each from a
specific thread in parallel. However, it's not safe to allocate such an object in one thread, and operate or free it
from any other, even if locking is used to ensure these threads don't operate on it at the very same time.</para>
<para id="safe">All functions listed here are thread-safe and may be called in parallel from multiple threads.</para>
</refsect1>

View File

@ -6,8 +6,9 @@
#include <string.h>
#include "alloc-util.h"
#include "hashmap.h"
#include "env-util.h"
#include "fileio.h"
#include "hashmap.h"
#include "macro.h"
#include "mempool.h"
#include "process-util.h"
@ -766,20 +767,31 @@ static void reset_direct_storage(HashmapBase *h) {
memset(p, DIB_RAW_INIT, sizeof(dib_raw_t) * hi->n_direct_buckets);
}
static bool use_pool(void) {
static int b = -1;
if (!is_main_thread())
return false;
if (b < 0)
b = getenv_bool("SYSTEMD_MEMPOOL") != 0;
return b;
}
static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
HashmapBase *h;
const struct hashmap_type_info *hi = &hashmap_type_info[type];
bool use_pool;
bool up;
use_pool = is_main_thread();
h = use_pool ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
up = use_pool();
h = up ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
if (!h)
return NULL;
h->type = type;
h->from_pool = use_pool;
h->from_pool = up;
h->hash_ops = hash_ops ? hash_ops : &trivial_hash_ops;
if (type == HASHMAP_TYPE_ORDERED) {
@ -857,9 +869,11 @@ static void hashmap_free_no_clear(HashmapBase *h) {
assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
#endif
if (h->from_pool)
if (h->from_pool) {
/* Ensure that the object didn't get migrated between threads. */
assert_se(is_main_thread());
mempool_free_tile(hashmap_type_info[h->type].mempool, h);
else
} else
free(h);
}

View File

@ -9,7 +9,7 @@
static void test_parse_etc_hosts_system(void) {
_cleanup_fclose_ FILE *f = NULL;
f = fopen("/etc/hosts", "r");
f = fopen("/etc/hosts", "re");
if (!f) {
assert_se(errno == -ENOENT);
return;
@ -27,7 +27,7 @@ static void test_parse_etc_hosts(const char *fname) {
_cleanup_fclose_ FILE *f;
if (fname) {
f = fopen(fname, "r");
f = fopen(fname, "re");
assert_se(f);
} else {
fd = mkostemp_safe(t);