chiark / gitweb /
journald: cache cgroup root path, instead of querying it on every incoming log message
authorLennart Poettering <lennart@poettering.net>
Wed, 11 Dec 2013 22:31:07 +0000 (23:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 11 Dec 2013 22:31:07 +0000 (23:31 +0100)
src/journal/journald-server.c
src/journal/journald-server.h
src/journal/sd-journal.c
src/shared/cgroup-util.c
src/shared/cgroup-util.h
src/test/test-cgroup-util.c

index a4fa394bc70629ccac30c34db543f782e994ecf4..0f67fb8d5701947895abfd25a43f582eb7015c66 100644 (file)
@@ -622,7 +622,7 @@ static void dispatch_message_real(
                 }
 #endif
 
                 }
 #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;
 
                 if (r >= 0) {
                         char *session = NULL;
 
@@ -743,7 +743,7 @@ static void dispatch_message_real(
                 }
 #endif
 
                 }
 #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);
                 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;
 
         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;
 
         if (r < 0)
                 goto finish;
 
@@ -1563,6 +1563,10 @@ int server_init(Server *s) {
         if (!s->rate_limit)
                 return -ENOMEM;
 
         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);
         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->buffer);
         free(s->tty_path);
+        free(s->cgroup_root);
 
         if (s->mmap)
                 mmap_cache_unref(s->mmap);
 
         if (s->mmap)
                 mmap_cache_unref(s->mmap);
index 86c4be4ba260a960d281e42d6f40315e7a6f3e23..2a81061f262afb1197db2873a29e1ff288fecc3c 100644 (file)
@@ -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;
         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
 } Server;
 
 #define N_IOVEC_META_FIELDS 20
index dda9351daf31e734bdfb6fd08d0969952c99e8f6..bb116df0474a568163303010ee06c63a4b2192e8 100644 (file)
@@ -1616,8 +1616,8 @@ static int add_current_paths(sd_journal *j) {
          * treat them as fatal. */
 
         HASHMAP_FOREACH(f, j->files, i) {
          * treat them as fatal. */
 
         HASHMAP_FOREACH(f, j->files, i) {
-                int r;
                 _cleanup_free_ char *dir;
                 _cleanup_free_ char *dir;
+                int r;
 
                 dir = dirname_malloc(f->path);
                 if (!dir)
 
                 dir = dirname_malloc(f->path);
                 if (!dir)
index 88bc33e3d55c21e6da797a1ec30c77476ecc852e..2c2ffc589879ead8b7f04c2cf31bcff833d428cb 100644 (file)
@@ -1082,42 +1082,42 @@ int cg_get_root_path(char **path) {
         return 0;
 }
 
         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;
 
         _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;
 
 
         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;
                         return -ENOMEM;
-                }
 
                 *cgroup = c;
 
                 *cgroup = c;
-        }
-
-        if (root) {
-                cg_process[p-cg_process] = 0;
-                *root = cg_process;
         } else
         } else
-                free(cg_process);
+                *cgroup = cg_process;
 
         return 0;
 }
 
         return 0;
 }
index b64457300a078e720976894a9f02120c66a7b040..3e6a3f6abed4f2aa437dc4f948efe808be47346a 100644 (file)
@@ -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_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);
 
 int cg_pid_get_session(pid_t pid, char **session);
 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
index 4d802d483a912fb317a4edc7261b2780918a554d..eae67395bcef03d06e5b6561cd901505c8be8382 100644 (file)
@@ -140,7 +140,7 @@ static void test_proc(void) {
         assert_se(d);
 
         FOREACH_DIRENT(de, d, break) {
         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;
 
                 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);
                         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);
                 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);
 
                 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,
                        (unsigned long) pid,
                        path,
-                       prefix,
                        path_shifted,
                        (unsigned long) uid,
                        session,
                        path_shifted,
                        (unsigned long) uid,
                        session,