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 a4fa394..0f67fb8 100644 (file)
@@ -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);
index 86c4be4..2a81061 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;
+
+        /* 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
index dda9351..bb116df 100644 (file)
@@ -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)
index 88bc33e..2c2ffc5 100644 (file)
@@ -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;
 }
index b644573..3e6a3f6 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_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);
index 4d802d4..eae6739 100644 (file)
@@ -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,