jcgroup

#Cgroup on JVM

Build Status Coverage Status

jcgroup is a cgroup wrapper on JVM. You could use this library to limit the CPU shares, Disk I/O speed, Network bandwidth and etc of a thread.

Subsystems

☑ blkio

☑ common

☑ cpu

☑ cpuacct

☑ cpuset

☑ devices

☑ freezer

☑ memory

☑ net_cls

☑ net_prio

Example

This code snippet create two threads and set different cpu shares of them. One is 512 while another is 2048.

jcgroup_example_cpu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class ExampleTest {

private static final Logger LOG = LoggerFactory.getLogger(ExampleTest.class);
private static Admin admin;
private static Group root;
private static Group one;
private static Group two;

@BeforeClass
public static void setUpClass() {
try {
admin = new Admin(Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);
root = admin.getRootGroup();
one = admin.createGroup("one", Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);
two = admin.createGroup("two", Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);
} catch (IOException e) {
LOG.error("Create cgroup Failed.", e);
assertTrue(false);
}
}

@AfterClass
public static void tearDownClass() {
try {
admin.umount();
} catch (IOException e) {
LOG.error("Umount cgroup failed.", e);
assertTrue(false);
}
}

@Test
public void testCpu() {
try {
one.getCpuset().setCpus(new int[]{0});
two.getCpuset().setCpus(new int[]{0});
one.getCpuset().setMems(new int[]{0});
two.getCpuset().setMems(new int[]{0});
one.getCpu().setShares(512);
two.getCpu().setShares(2048);
final Group oneTmp = one;
final Group twoTmp = two;
new Thread(){
@Override
public void run() {
int id = Threads.getThreadId();
LOG.info("Thread id:" + id);
try {
oneTmp.getCpu().addTask(id);
while (true);
} catch (IOException e) {
LOG.error("Test cpu failed.", e);
assertTrue(false);
}
}
}.start();
new Thread(){
@Override
public void run() {
int id = Threads.getThreadId();
LOG.info("Thread id:" + id);
try {
twoTmp.getCpu().addTask(id);
while (true);
} catch (IOException e) {
LOG.error("Test cpu failed.", e);
assertTrue(false);
}
}
}.start();
Thread.sleep(60000l);
} catch (Exception e) {
LOG.error("Test cpu failed.", e);
assertTrue(false);
}
}
}

Requirements

  • Linux version (>= 2.6.18)