2 * collectd - src/cgroups_cpuacct.c
3 * Copyright (C) 2011 Michael Stapelberg
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the license is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Michael Stapelberg <michael at stapelberg.de>
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
29 static const char *config_keys[] =
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
36 static ignorelist_t *il_cgroup = NULL;
38 __attribute__ ((nonnull(1)))
39 __attribute__ ((nonnull(2)))
40 static void cgroups_submit_one (const char *plugin_instance,
41 const char *type, const char *type_instance,
45 value_list_t vl = VALUE_LIST_INIT;
47 values[0].derive = value;
51 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
52 sstrncpy (vl.plugin, "cgroups_cpuacct", sizeof (vl.plugin));
53 sstrncpy (vl.plugin_instance, plugin_instance,
54 sizeof (vl.plugin_instance));
55 sstrncpy (vl.type, type, sizeof (vl.type));
56 if (type_instance != NULL)
57 sstrncpy (vl.type_instance, type_instance,
58 sizeof (vl.type_instance));
60 plugin_dispatch_values (&vl);
61 } /* void cgroups_submit_one */
65 * This callback reads the user/system CPU time for each cgroup.
68 static int read_cpuacct_procs (const char *dirname, const char *filename,
71 char abs_path[PATH_MAX];
76 if (ignorelist_match (il_cgroup, filename))
79 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
81 status = lstat (abs_path, &statbuf);
84 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
88 /* We are only interested in directories, so skip everything else. */
89 if (!S_ISDIR (statbuf.st_mode))
94 ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat", dirname, filename);
96 if ((bytes_read = read_file_contents (abs_path, buf, sizeof(buf))) <= 0)
99 ERROR ("cgroups_cpuacct plugin: read_file_contents(%s): %s",
100 abs_path, sstrerror (errno, errbuf, sizeof (errbuf)));
103 buf[bytes_read] = '\0';
107 if ((numfields = strsplit (buf, fields, 4)) != 4)
109 ERROR ("cgroups_cpuacct plugin: unexpected content in file %s", abs_path);
112 uint64_t usertime, systemtime;
113 usertime = atoll (fields[1]);
114 systemtime = atoll (fields[3]);
116 cgroups_submit_one(filename, "cpuacct", "user", (derive_t)usertime);
117 cgroups_submit_one(filename, "cpuacct", "system", (derive_t)systemtime);
123 * Gets called for every file/folder in /sys/fs/cgroup/cpu,cpuacct (or
124 * whereever cpuacct is mounted on the system). Calls walk_directory with the
125 * read_cpuacct_procs callback on every folder it finds, such as "system".
128 static int read_cpuacct_root (const char *dirname, const char *filename,
131 char abs_path[PATH_MAX];
135 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
137 status = lstat (abs_path, &statbuf);
140 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
144 if (S_ISDIR (statbuf.st_mode))
146 status = walk_directory (abs_path, read_cpuacct_procs, NULL, 0);
153 static int cgroups_init (void)
155 if (il_cgroup == NULL)
156 il_cgroup = ignorelist_create (1);
161 static int cgroups_config (const char *key, const char *value)
165 if (strcasecmp (key, "CGroup") == 0)
167 if (ignorelist_add (il_cgroup, value))
171 else if (strcasecmp (key, "IgnoreSelected") == 0)
175 ignorelist_set_invert (il_cgroup, 0);
179 ignorelist_set_invert (il_cgroup, 1);
187 static int cgroups_read (void)
189 cu_mount_t *mnt_list;
191 int cgroup_found = 0;
194 if (cu_mount_getlist (&mnt_list) == NULL)
196 ERROR ("cgroups_cpuacct plugin: cu_mount_getlist failed.");
200 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
202 /* Find the cgroup mountpoint which contains the cpuacct
204 if (strcmp(mnt_ptr->type, "cgroup") != 0 ||
205 !cu_mount_getoptionvalue(mnt_ptr->options, "cpuacct"))
208 walk_directory (mnt_ptr->dir, read_cpuacct_root, NULL, 0);
210 /* It doesn't make sense to check other cpuacct mount-points
211 * (if any), they contain the same data. */
216 WARNING ("cpuacct mountpoint not found. Cannot collect any data.");
218 cu_mount_freelist (mnt_list);
221 } /* int cgroup_read */
223 void module_register (void)
225 plugin_register_config ("cgroups_cpuacct", cgroups_config,
226 config_keys, config_keys_num);
227 plugin_register_init ("cgroups_cpuacct", cgroups_init);
228 plugin_register_read ("cgroups_cpuacct", cgroups_read);
229 } /* void module_register */