blob: b65b1fb8d7e6a08e834223eab1c6675d7e6a1df3 [file] [log] [blame]
Jens Axboea696fa22009-12-04 10:05:02 +01001/*
2 * Code related to setting up a blkio cgroup
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include "fio.h"
Jens Axboe39f22022009-12-04 11:29:12 +01007#include "flist.h"
Jens Axboea696fa22009-12-04 10:05:02 +01008#include "cgroup.h"
Jens Axboe39f22022009-12-04 11:29:12 +01009#include "smalloc.h"
10
Jens Axboe39f22022009-12-04 11:29:12 +010011static struct fio_mutex *lock;
12
13struct cgroup_member {
14 struct flist_head list;
15 char *root;
16};
17
Jens Axboedae53412009-12-04 19:54:18 +010018static void add_cgroup(const char *name, struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010019{
20 struct cgroup_member *cm;
21
22 cm = smalloc(sizeof(*cm));
23 INIT_FLIST_HEAD(&cm->list);
24 cm->root = smalloc_strdup(name);
25
26 fio_mutex_down(lock);
Jens Axboedae53412009-12-04 19:54:18 +010027 flist_add_tail(&cm->list, clist);
Jens Axboe39f22022009-12-04 11:29:12 +010028 fio_mutex_up(lock);
29}
30
Jens Axboedae53412009-12-04 19:54:18 +010031void cgroup_kill(struct flist_head *clist)
Jens Axboe39f22022009-12-04 11:29:12 +010032{
33 struct flist_head *n, *tmp;
34 struct cgroup_member *cm;
35
36 fio_mutex_down(lock);
Jens Axboe39f22022009-12-04 11:29:12 +010037
Jens Axboedae53412009-12-04 19:54:18 +010038 flist_for_each_safe(n, tmp, clist) {
Jens Axboe39f22022009-12-04 11:29:12 +010039 cm = flist_entry(n, struct cgroup_member, list);
40 rmdir(cm->root);
41 flist_del(&cm->list);
42 sfree(cm->root);
43 sfree(cm);
44 }
45
Jens Axboe39f22022009-12-04 11:29:12 +010046 fio_mutex_up(lock);
47}
Jens Axboea696fa22009-12-04 10:05:02 +010048
Jens Axboeddf16fe2009-12-04 10:54:45 +010049/*
50 * Check if the given root appears valid
51 */
52static int cgroup_check_fs(struct thread_data *td)
53{
54 struct stat sb;
55 char tmp[256];
56
57 sprintf(tmp, "%s/tasks", td->o.cgroup_root);
58 return stat(tmp, &sb);
59}
60
Jens Axboea696fa22009-12-04 10:05:02 +010061static char *get_cgroup_root(struct thread_data *td)
62{
63 char *str = malloc(64);
64
65 if (td->o.cgroup)
66 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.cgroup);
67 else
68 sprintf(str, "%s/%s", td->o.cgroup_root, td->o.name);
69
70 return str;
71}
72
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010073static int write_int_to_file(struct thread_data *td, const char *path,
74 const char *filename, unsigned int val,
75 const char *onerr)
Jens Axboea696fa22009-12-04 10:05:02 +010076{
Jens Axboe3858bf92009-12-04 19:26:22 +010077 char tmp[256];
Jens Axboea696fa22009-12-04 10:05:02 +010078 FILE *f;
Jens Axboe3858bf92009-12-04 19:26:22 +010079
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010080 sprintf(tmp, "%s/%s", path, filename);
Jens Axboea696fa22009-12-04 10:05:02 +010081 f = fopen(tmp, "w");
82 if (!f) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010083 td_verror(td, errno, onerr);
Jens Axboea696fa22009-12-04 10:05:02 +010084 return 1;
85 }
86
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010087 fprintf(f, "%u", val);
Jens Axboea696fa22009-12-04 10:05:02 +010088 fclose(f);
Jens Axboea696fa22009-12-04 10:05:02 +010089 return 0;
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010090
Jens Axboe3858bf92009-12-04 19:26:22 +010091}
92
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010093static int cgroup_write_pid(struct thread_data *td, const char *root)
Jens Axboe3858bf92009-12-04 19:26:22 +010094{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010095 unsigned int val = td->pid;
Jens Axboe3858bf92009-12-04 19:26:22 +010096
Jens Axboe8a4d0ff2009-12-04 22:13:43 +010097 return write_int_to_file(td, root, "tasks", val, "cgroup write pid");
Jens Axboea696fa22009-12-04 10:05:02 +010098}
99
100/*
101 * Move pid to root class
102 */
103static int cgroup_del_pid(struct thread_data *td)
104{
Jens Axboe3858bf92009-12-04 19:26:22 +0100105 return cgroup_write_pid(td, td->o.cgroup_root);
Jens Axboea696fa22009-12-04 10:05:02 +0100106}
107
Jens Axboedae53412009-12-04 19:54:18 +0100108int cgroup_setup(struct thread_data *td, struct flist_head *clist)
Jens Axboea696fa22009-12-04 10:05:02 +0100109{
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100110 char *root;
Jens Axboea696fa22009-12-04 10:05:02 +0100111
Jens Axboeddf16fe2009-12-04 10:54:45 +0100112 if (cgroup_check_fs(td)) {
113 log_err("fio: blkio cgroup mount point %s not valid\n",
114 td->o.cgroup_root);
115 return 1;
116 }
117
Jens Axboea696fa22009-12-04 10:05:02 +0100118 /*
119 * Create container, if it doesn't exist
120 */
121 root = get_cgroup_root(td);
122 if (mkdir(root, 0755) < 0) {
123 int __e = errno;
124
125 if (__e != EEXIST) {
126 td_verror(td, __e, "cgroup mkdir");
Jens Axboe3858bf92009-12-04 19:26:22 +0100127 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100128 }
129 } else
Jens Axboedae53412009-12-04 19:54:18 +0100130 add_cgroup(root, clist);
Jens Axboea696fa22009-12-04 10:05:02 +0100131
Jens Axboe39f22022009-12-04 11:29:12 +0100132 if (td->o.cgroup_weight) {
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100133 if (write_int_to_file(td, root, "blkio.weight",
134 td->o.cgroup_weight,
135 "cgroup open weight"))
Jens Axboe3858bf92009-12-04 19:26:22 +0100136 goto err;
Jens Axboea696fa22009-12-04 10:05:02 +0100137 }
138
Jens Axboe8a4d0ff2009-12-04 22:13:43 +0100139 if (!cgroup_write_pid(td, root)) {
140 free(root);
141 return 0;
142 }
Jens Axboea696fa22009-12-04 10:05:02 +0100143
Jens Axboe3858bf92009-12-04 19:26:22 +0100144err:
145 free(root);
146 return 1;
Jens Axboea696fa22009-12-04 10:05:02 +0100147}
148
149void cgroup_shutdown(struct thread_data *td)
150{
Jens Axboeddf16fe2009-12-04 10:54:45 +0100151 if (cgroup_check_fs(td))
152 return;
Jens Axboe39f22022009-12-04 11:29:12 +0100153 if (!td->o.cgroup_weight && td->o.cgroup)
Jens Axboea696fa22009-12-04 10:05:02 +0100154 return;
155
156 cgroup_del_pid(td);
Jens Axboe39f22022009-12-04 11:29:12 +0100157}
Jens Axboea696fa22009-12-04 10:05:02 +0100158
Jens Axboe39f22022009-12-04 11:29:12 +0100159static void fio_init cgroup_init(void)
160{
161 lock = fio_mutex_init(1);
162}
163
164static void fio_exit cgroup_exit(void)
165{
166 fio_mutex_remove(lock);
Jens Axboea696fa22009-12-04 10:05:02 +0100167}