Code

d20ff4b40996ddbbc7a9222f76252979569a182a
[collectd.git] / src / cgroups_cpuacct.c
1 /**
2  * collectd - src/cgroups_cpuacct.c
3  * Copyright (C) 2011  Michael Stapelberg
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Michael Stapelberg <michael at stapelberg.de>
20  **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
29 static const char *config_keys[] =
30 {
31         "CGroup",
32         "IgnoreSelected"
33 };
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,
42                 derive_t value)
43 {
44         value_t values[1];
45         value_list_t vl = VALUE_LIST_INIT;
47         values[0].derive = value;
49         vl.values = values;
50         vl.values_len = 1;
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 */
64 /*
65  * This callback reads the user/system CPU time for each cgroup.
66  *
67  */
68 static int read_cpuacct_procs (const char *dirname, const char *filename,
69     void *user_data)
70 {
71         char abs_path[PATH_MAX];
72         struct stat statbuf;
73         char buf[1024];
74         int status;
76         if (ignorelist_match (il_cgroup, filename))
77                 return (0);
79         ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
81         status = lstat (abs_path, &statbuf);
82         if (status != 0)
83         {
84                 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
85                 return (-1);
86         }
88         /* We are only interested in directories, so skip everything else. */
89         if (!S_ISDIR (statbuf.st_mode))
90         {
91                 return (0);
92         }
94         ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat", dirname, filename);
95         int bytes_read;
96         if ((bytes_read = read_file_contents (abs_path, buf, sizeof(buf))) <= 0)
97         {
98                 char errbuf[1024];
99                 ERROR ("cgroups_cpuacct plugin: read_file_contents(%s): %s",
100                                 abs_path, sstrerror (errno, errbuf, sizeof (errbuf)));
101                 return (-1);
102         }
103         buf[bytes_read] = '\0';
105         char *fields[4];
106         int numfields = 0;
107         if ((numfields = strsplit (buf, fields, 4)) != 4)
108         {
109                 ERROR ("cgroups_cpuacct plugin: unexpected content in file %s", abs_path);
110                 return (-1);
111         }
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);
119         return (0);
122 /*
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".
126  *
127  */
128 static int read_cpuacct_root (const char *dirname, const char *filename,
129     void *user_data)
131   char abs_path[PATH_MAX];
132   struct stat statbuf;
133   int status;
135   ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
137   status = lstat (abs_path, &statbuf);
138   if (status != 0)
139   {
140     ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
141     return (-1);
142   }
144   if (S_ISDIR (statbuf.st_mode))
145   {
146     status = walk_directory (abs_path, read_cpuacct_procs, NULL, 0);
147     return (status);
148   }
150   return (0);
153 static int cgroups_init (void)
155         if (il_cgroup == NULL)
156                 il_cgroup = ignorelist_create (1);
158         return (0);
161 static int cgroups_config (const char *key, const char *value)
163         cgroups_init ();
165         if (strcasecmp (key, "CGroup") == 0)
166         {
167                 if (ignorelist_add (il_cgroup, value))
168                         return (1);
169                 return (0);
170         }
171         else if (strcasecmp (key, "IgnoreSelected") == 0)
172         {
173                 if (IS_TRUE (value))
174                 {
175                         ignorelist_set_invert (il_cgroup, 0);
176                 }
177                 else
178                 {
179                         ignorelist_set_invert (il_cgroup, 1);
180                 }
181                 return (0);
182         }
184         return (-1);
187 static int cgroups_read (void)
189         cu_mount_t *mnt_list;
190         cu_mount_t *mnt_ptr;
191         int cgroup_found = 0;
193         mnt_list = NULL;
194         if (cu_mount_getlist (&mnt_list) == NULL)
195         {
196                 ERROR ("cgroups_cpuacct plugin: cu_mount_getlist failed.");
197                 return (-1);
198         }
200         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
201         {
202                 /* Find the cgroup mountpoint which contains the cpuacct
203                  * controller. */
204                 if (strcmp(mnt_ptr->type, "cgroup") != 0 ||
205                         !cu_mount_getoptionvalue(mnt_ptr->options, "cpuacct"))
206                         continue;
208                 walk_directory (mnt_ptr->dir, read_cpuacct_root, NULL, 0);
209                 cgroup_found = 1;
210                 /* It doesn't make sense to check other cpuacct mount-points
211                  * (if any), they contain the same data. */
212                 break;
213         }
215         if (!cgroup_found)
216                 WARNING ("cpuacct mountpoint not found. Cannot collect any data.");
218         cu_mount_freelist (mnt_list);
220         return (0);
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 */