[systemd-devel] [PATCH 1/2] RFC: journald: Allow to cache the cg_get_root_path

Holger Hans Peter Freyther holger at freyther.de
Thu Jun 27 09:30:10 PDT 2013


From: Holger Hans Peter Freyther <holger at moiji-mobile.com>

Allow to cache the cg_get_root_path and introduce a new method
cg_pid_get_path_shifted_with_root that can use the cached version
instead of allocating a new string.
---
 src/journal/journald-server.c |   20 +++++++++++++++++---
 src/shared/cgroup-util.c      |   19 ++++++++++++++-----
 src/shared/cgroup-util.h      |    1 +
 3 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 44ba916..b08aa1d 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -89,6 +89,20 @@ static const char* const split_mode_table[] = {
 DEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode);
 DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting");
 
+static const char *cached_cg_root(void) {
+        static char *cached = NULL;
+
+        int r;
+
+        if (!cached) {
+                r = cg_get_root_path(&cached);
+                if (r < 0)
+                        cached = NULL;
+        }
+
+        return cached;
+}
+
 static uint64_t available_space(Server *s, bool verbose) {
         char ids[33];
         _cleanup_free_ char *p = NULL;
@@ -592,7 +606,7 @@ static void dispatch_message_real(
                 }
 #endif
 
-                r = cg_pid_get_path_shifted(ucred->pid, NULL, &c);
+                r = cg_pid_get_path_shifted_with_root(ucred->pid, NULL, &c, cached_cg_root());
                 if (r >= 0) {
                         char *session = NULL;
 
@@ -701,7 +715,7 @@ static void dispatch_message_real(
                 }
 #endif
 
-                r = cg_pid_get_path_shifted(object_pid, NULL, &c);
+                r = cg_pid_get_path_shifted_with_root(object_pid, NULL, &c, cached_cg_root());
                 if (r >= 0) {
                         x = strappenda("OBJECT_SYSTEMD_CGROUP=", c);
                         IOVEC_SET_STRING(iovec[n++], x);
@@ -841,7 +855,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_with_root(ucred->pid, NULL, &path, cached_cg_root());
         if (r < 0)
                 goto finish;
 
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index 5816b7d..1a76794 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1059,14 +1059,12 @@ char **cg_shorten_controllers(char **controllers) {
         return strv_uniq(controllers);
 }
 
-int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
-        _cleanup_free_ char *cg_root = NULL;
+int cg_pid_get_path_shifted_with_root(pid_t pid, char **root, char **cgroup, const char *cg_root) {
         char *cg_process, *p;
         int r;
 
-        r = cg_get_root_path(&cg_root);
-        if (r < 0)
-                return r;
+        if (!cg_root)
+                return -1;
 
         r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process);
         if (r < 0)
@@ -1099,6 +1097,17 @@ int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
         return 0;
 }
 
+int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
+        _cleanup_free_ char *cg_root = NULL;
+        int r;
+
+        r = cg_get_root_path(&cg_root);
+        if (r < 0)
+                return r;
+
+        return cg_pid_get_path_shifted_with_root(pid, root, cgroup, cg_root);
+}
+
 int cg_path_decode_unit(const char *cgroup, char **unit){
         char *p, *e, *c, *s, *k;
 
diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h
index 9883d94..7f6605c 100644
--- a/src/shared/cgroup-util.h
+++ b/src/shared/cgroup-util.h
@@ -104,6 +104,7 @@ 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_with_root(pid_t pid, char **root, char **cgroup, const char *cg_root);
 
 int cg_pid_get_session(pid_t pid, char **session);
 int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
-- 
1.7.10.4



More information about the systemd-devel mailing list