From e9174f29c7e3ee45137537b126458718913a3ec5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 11 Dec 2013 23:31:07 +0100 Subject: journald: cache cgroup root path, instead of querying it on every incoming log message --- src/journal/journald-server.c | 11 ++++++++--- src/journal/journald-server.h | 3 +++ src/journal/sd-journal.c | 2 +- src/shared/cgroup-util.c | 42 +++++++++++++++++++++--------------------- src/shared/cgroup-util.h | 2 +- src/test/test-cgroup-util.c | 7 +++---- 6 files changed, 37 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index a4fa394bc7..0f67fb8d57 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -622,7 +622,7 @@ static void dispatch_message_real( } #endif - r = cg_pid_get_path_shifted(ucred->pid, NULL, &c); + r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &c); if (r >= 0) { char *session = NULL; @@ -743,7 +743,7 @@ static void dispatch_message_real( } #endif - r = cg_pid_get_path_shifted(object_pid, NULL, &c); + r = cg_pid_get_path_shifted(object_pid, s->cgroup_root, &c); if (r >= 0) { x = strappenda("OBJECT_SYSTEMD_CGROUP=", c); IOVEC_SET_STRING(iovec[n++], x); @@ -878,7 +878,7 @@ void server_dispatch_message( if (!ucred) goto finish; - r = cg_pid_get_path_shifted(ucred->pid, NULL, &path); + r = cg_pid_get_path_shifted(ucred->pid, s->cgroup_root, &path); if (r < 0) goto finish; @@ -1563,6 +1563,10 @@ int server_init(Server *s) { if (!s->rate_limit) return -ENOMEM; + r = cg_get_root_path(&s->cgroup_root); + if (r < 0) + return r; + server_cache_hostname(s); server_cache_boot_id(s); server_cache_machine_id(s); @@ -1643,6 +1647,7 @@ void server_done(Server *s) { free(s->buffer); free(s->tty_path); + free(s->cgroup_root); if (s->mmap) mmap_cache_unref(s->mmap); diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h index 86c4be4ba2..2a81061f26 100644 --- a/src/journal/journald-server.h +++ b/src/journal/journald-server.h @@ -136,6 +136,9 @@ typedef struct Server { char machine_id_field[sizeof("_MACHINE_ID=") + 32]; char boot_id_field[sizeof("_BOOT_ID=") + 32]; char *hostname_field; + + /* Cached cgroup root, so that we don't have to query that all the time */ + char *cgroup_root; } Server; #define N_IOVEC_META_FIELDS 20 diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index dda9351daf..bb116df047 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -1616,8 +1616,8 @@ static int add_current_paths(sd_journal *j) { * treat them as fatal. */ HASHMAP_FOREACH(f, j->files, i) { - int r; _cleanup_free_ char *dir; + int r; dir = dirname_malloc(f->path); if (!dir) diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 88bc33e3d5..2c2ffc5898 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -1082,42 +1082,42 @@ int cg_get_root_path(char **path) { return 0; } -int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) { +int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { _cleanup_free_ char *cg_root = NULL; char *cg_process, *p; int r; - r = cg_get_root_path(&cg_root); - if (r < 0) - return r; + assert(pid >= 0); + assert(cgroup); + + if (!root) { + /* If the root was specified let's use that, otherwise + * let's determine it from PID 1 */ + + r = cg_get_root_path(&cg_root); + if (r < 0) + return r; + + root = cg_root; + } r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process); if (r < 0) return r; - p = path_startswith(cg_process, cg_root); - if (p) - p--; - else - p = cg_process; + p = path_startswith(cg_process, root); + if (p) { + char *c; - if (cgroup) { - char* c; + c = strdup(p - 1); + free(cg_process); - c = strdup(p); - if (!c) { - free(cg_process); + if (!c) return -ENOMEM; - } *cgroup = c; - } - - if (root) { - cg_process[p-cg_process] = 0; - *root = cg_process; } else - free(cg_process); + *cgroup = cg_process; return 0; } diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h index b64457300a..3e6a3f6abe 100644 --- a/src/shared/cgroup-util.h +++ b/src/shared/cgroup-util.h @@ -103,7 +103,7 @@ int cg_path_get_user_unit(const char *path, char **unit); int cg_path_get_machine_name(const char *path, char **machine); int cg_path_get_slice(const char *path, char **slice); -int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup); +int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup); int cg_pid_get_session(pid_t pid, char **session); int cg_pid_get_owner_uid(pid_t pid, uid_t *uid); diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 4d802d483a..eae67395bc 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -140,7 +140,7 @@ static void test_proc(void) { assert_se(d); FOREACH_DIRENT(de, d, break) { - _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *prefix = NULL, *slice = NULL; + _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL; pid_t pid; uid_t uid = (uid_t) -1; @@ -156,7 +156,7 @@ static void test_proc(void) { continue; cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); - cg_pid_get_path_shifted(pid, &prefix, &path_shifted); + cg_pid_get_path_shifted(pid, NULL, &path_shifted); cg_pid_get_owner_uid(pid, &uid); cg_pid_get_session(pid, &session); cg_pid_get_unit(pid, &unit); @@ -164,10 +164,9 @@ static void test_proc(void) { cg_pid_get_machine_name(pid, &machine); cg_pid_get_slice(pid, &slice); - printf("%lu\t%s\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n", + printf("%lu\t%s\t%s\t%lu\t%s\t%s\t%s\t%s\t%s\n", (unsigned long) pid, path, - prefix, path_shifted, (unsigned long) uid, session, -- cgit v1.2.3-54-g00ecf