journald: cache cgroup root path, instead of querying it on every incoming log message

This commit is contained in:
Lennart Poettering 2013-12-11 23:31:07 +01:00
parent 897395791f
commit e9174f29c7
6 changed files with 37 additions and 30 deletions

View 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);

View 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

View 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)

View 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;
}

View 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);

View 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,