Skip to content

Commit

Permalink
Fix test cases error in CpuTest,CpuacctTest,CpusetTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
haosdent committed Jan 21, 2014
1 parent 6032144 commit 135b485
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 52 deletions.
18 changes: 13 additions & 5 deletions src/main/java/me/haosdent/cgroup/manage/Admin.java
Expand Up @@ -18,20 +18,18 @@ public class Admin {
public static final String userConfName = "user_conf";
public static final String USER_CONF_KEY_NAME = "name";
public static final String USER_CONF_KEY_PASSWORD = "password";
public static final String PATH_ROOT = ".";
private Shell shell;
private String name;
private int subsystems;
private String password;
private Group rootGroup;
private List<Group> groupList = new LinkedList<Group>();

private static final Logger LOG = LoggerFactory.getLogger(Admin.class);

public Admin(String name, String password, int subsystems) throws IOException {
this.name = name;
this.password = password;
this.subsystems = subsystems;
this.shell = new Shell(this);
shell.mount(name, subsystems);
init(name, password, subsystems);
}

public Admin(int subsystems) throws IOException {
Expand All @@ -42,10 +40,16 @@ public Admin(int subsystems) throws IOException {
String name = json.getString(USER_CONF_KEY_NAME);
String password = json.getString(USER_CONF_KEY_PASSWORD);

init(name, password, subsystems);
}

private void init(String name, String password, int subsystems) throws IOException {
this.name = name;
this.password = password;
this.subsystems = subsystems;
this.shell = new Shell(this);
shell.mount(name, subsystems);
this.rootGroup = new Group(this, PATH_ROOT, subsystems, true);
}

public void umount() throws IOException {
Expand All @@ -64,6 +68,10 @@ public String getPassword() {
return password;
}

public Group getRootGroup() {
return rootGroup;
}

public List<Group> getGroupList() {
return groupList;
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/me/haosdent/cgroup/manage/Group.java
Expand Up @@ -13,6 +13,7 @@ public class Group {
private Admin admin;
private String name;
private int subsystems;
private boolean isRoot;

private Blkio blkio;
private Cpu cpu;
Expand All @@ -27,12 +28,19 @@ public class Group {
private List<Common> subSystemList = new LinkedList<Common>();

protected Group(Admin admin, String name, int subsystems) throws IOException {
this(admin, name, subsystems, false);
}

protected Group(Admin admin, String name, int subsystems, boolean isRoot) throws IOException {
this.admin = admin;
this.shell = admin.getShell();
this.name = name;
this.subsystems = subsystems;
shell.cgcreate(name, subsystems);
admin.getGroupList().add(this);
this.isRoot = isRoot;
if (!isRoot) {
shell.cgcreate(name, subsystems);
admin.getGroupList().add(this);
}
}

public void delete() throws IOException {
Expand All @@ -59,6 +67,10 @@ public List<Common> getSubSystemList() {
return subSystemList;
}

public boolean isRoot() {
return isRoot;
}

public Blkio getBlkio() {
if (blkio == null) {
blkio = new Blkio(this);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/me/haosdent/cgroup/subsystem/Cpuacct.java
Expand Up @@ -2,11 +2,15 @@

import me.haosdent.cgroup.manage.Group;
import me.haosdent.cgroup.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class Cpuacct extends Common {

private static final Logger LOG = LoggerFactory.getLogger(Cpuacct.class);

public static final int SUBSYS = Constants.SUBSYS_CPUACCT;
public static final String PROP_CPUACCT_USAGE = "cpuacct.usage";
public static final String PROP_CPUACCT_STAT = "cpuacct.stat";
Expand Down
92 changes: 66 additions & 26 deletions src/main/java/me/haosdent/cgroup/subsystem/Cpuset.java
Expand Up @@ -4,6 +4,7 @@
import me.haosdent.cgroup.util.Constants;

import java.io.IOException;
import java.util.LinkedList;

public class Cpuset extends Common {

Expand Down Expand Up @@ -31,14 +32,13 @@ public void setCpus(int[] nums) throws IOException {
sb.append(num);
sb.append(',');
}
sb.deleteCharAt(sb.length() - 1);
shell.cgset(group.getName(), PROP_CPUSET_CPUS, sb.toString());
}

public int[] getCpus() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_CPUS);
int[] nums = {};
//TODO
return nums;
return parseNums(output);
}

public void setMems(int[] nums) throws IOException {
Expand All @@ -47,14 +47,13 @@ public void setMems(int[] nums) throws IOException {
sb.append(num);
sb.append(',');
}
sb.deleteCharAt(sb.length() - 1);
shell.cgset(group.getName(), PROP_CPUSET_MEMS, sb.toString());
}

public int[] getMems() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEMS);
int[] nums = {};
//TODO
return nums;
return parseNums(output);
}

public void setMemMigrate(boolean flag) throws IOException {
Expand All @@ -63,9 +62,8 @@ public void setMemMigrate(boolean flag) throws IOException {
}

public boolean isMemMigrate() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEMORY_MIGRATE);
boolean flag = Boolean.parseBoolean(output);
return flag;
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEMORY_MIGRATE));
return output > 0;
}

public void setCpuExclusive(boolean flag) throws IOException {
Expand All @@ -74,9 +72,8 @@ public void setCpuExclusive(boolean flag) throws IOException {
}

public boolean isCpuExclusive() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_CPU_EXCLUSIVE);
boolean flag = Boolean.parseBoolean(output);
return flag;
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_CPU_EXCLUSIVE));
return output > 0;
}

public void setMemExclusive(boolean flag) throws IOException {
Expand All @@ -85,9 +82,8 @@ public void setMemExclusive(boolean flag) throws IOException {
}

public boolean isMemExclusive() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEM_EXCLUSIVE);
boolean flag = Boolean.parseBoolean(output);
return flag;
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEM_EXCLUSIVE));
return output > 0;
}

public void setMemHardwall(boolean flag) throws IOException {
Expand All @@ -96,9 +92,8 @@ public void setMemHardwall(boolean flag) throws IOException {
}

public boolean isMemHardwall() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEM_HARDWALL);
boolean flag = Boolean.parseBoolean(output);
return flag;
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEM_HARDWALL));
return output > 0;
}

public int getMemPressure() throws IOException {
Expand All @@ -112,8 +107,8 @@ public void setMemPressureEnabled(boolean flag) throws IOException {
}

public boolean isMemPressureEnabled() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEMORY_PRESSURE_ENABLED);
return Boolean.parseBoolean(output);
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEMORY_PRESSURE_ENABLED));
return output > 0;
}

public void setMemSpreadPage(boolean flag) throws IOException {
Expand All @@ -122,8 +117,8 @@ public void setMemSpreadPage(boolean flag) throws IOException {
}

public boolean isMemSpreadPage() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEMORY_SPREAD_PAGE);
return Boolean.parseBoolean(output);
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEMORY_SPREAD_PAGE));
return output > 0;
}

public void setMemSpreadSlab(boolean flag) throws IOException {
Expand All @@ -132,8 +127,8 @@ public void setMemSpreadSlab(boolean flag) throws IOException {
}

public boolean isMemSpreadSlab() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_MEMORY_SPREAD_SLAB);
return Boolean.parseBoolean(output);
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_MEMORY_SPREAD_SLAB));
return output > 0;
}

public void setSchedLoadBlance(boolean flag) throws IOException {
Expand All @@ -142,8 +137,8 @@ public void setSchedLoadBlance(boolean flag) throws IOException {
}

public boolean isSchedLoadBlance() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_SCHED_LOAD_BALANCE);
return Boolean.parseBoolean(output);
int output = Integer.parseInt(shell.cgget(group.getName(), PROP_CPUSET_SCHED_LOAD_BALANCE));
return output > 0;
}

public void setSchedRelaxDomainLevel(int value) throws IOException {
Expand All @@ -154,4 +149,49 @@ public int getSchedRelaxDomainLevel() throws IOException {
String output = shell.cgget(group.getName(), PROP_CPUSET_SCHED_RELAX_DOMAIN_LEVEL);
return Integer.parseInt(output);
}

public static int[] parseNums(String outputStr) {
char[] output = outputStr.toCharArray();
LinkedList<Integer> numList = new LinkedList<Integer>();
int value = 0;
int start = 0;
boolean isHyphen = false;
for (char ch : output) {
if (ch == ',') {
if (isHyphen) {
for (; start <= value; start++) {
numList.add(start);
}
isHyphen = false;
} else {
numList.add(value);
}
value = 0;
} else if (ch == '-') {
isHyphen = true;
start = value;
value = 0;
} else {
value = value * 10 + (ch - '0');
}
}
if (output[output.length - 1] != ',') {
if (isHyphen) {
for (; start <= value; start++) {
numList.add(start);
}
} else {
numList.add(value);
}
}

int[] nums = new int[numList.size()];
int index = 0;
for (int num : numList) {
nums[index] = num;
index++;
}

return nums;
}
}
12 changes: 2 additions & 10 deletions src/test/java/me/haosdent/cgroup/subsystem/CpuTest.java
Expand Up @@ -63,8 +63,6 @@ public void testSetCfsPeriodTime() {
try {
one.getCpu().setCfsPeriodTime(1000);
assertEquals(one.getCpu().getCfsPeriodTime(), 1000);
two.getCpu().setCfsPeriodTime(2000);
assertEquals(two.getCpu().getCfsPeriodTime(), 2000);
} catch (IOException e) {
LOG.error("Set cfs_period_us failed.", e);
assertTrue(false);
Expand All @@ -76,8 +74,6 @@ public void testSetCfsQuotaTime() {
try {
one.getCpu().setCfsQuotaTime(1000);
assertEquals(one.getCpu().getCfsQuotaTime(), 1000);
two.getCpu().setCfsQuotaTime(2000);
assertEquals(two.getCpu().getCfsQuotaTime(), 2000);
} catch (IOException e) {
LOG.error("Set cfs_quota_us failed.", e);
assertTrue(false);
Expand All @@ -87,10 +83,8 @@ public void testSetCfsQuotaTime() {
@Test
public void testSetRtPeriodTime() {
try {
one.getCpu().setRtPeriodTime(1000);
assertEquals(one.getCpu().getRtPeriodTime(), 1000);
two.getCpu().setRtPeriodTime(2000);
assertEquals(two.getCpu().getRtPeriodTime(), 2000);
one.getCpu().setRtPeriodTime(100000);
assertEquals(one.getCpu().getRtPeriodTime(), 100000);
} catch (IOException e) {
LOG.error("Set rt_period_us failed.", e);
assertTrue(false);
Expand All @@ -102,8 +96,6 @@ public void testSetRtRuntimeTime() {
try {
one.getCpu().setRtRuntimeTime(1000);
assertEquals(one.getCpu().getRtRuntimeTime(), 1000);
two.getCpu().setRtRuntimeTime(2000);
assertEquals(two.getCpu().getRtRuntimeTime(), 2000);
} catch (IOException e) {
LOG.error("Set rt_runtime_us failed.", e);
assertTrue(false);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/me/haosdent/cgroup/subsystem/CpuacctTest.java
Expand Up @@ -49,7 +49,7 @@ public void tearDown() {}
public void testGetUsage() {
try {
long usage = one.getCpuacct().getUsage();
assertTrue(usage >= 0);
assertEquals(usage, 0);
} catch (IOException e) {
LOG.error("Get usage failed.", e);
assertTrue(false);
Expand All @@ -72,8 +72,8 @@ public void testResetUsage() {
public void testGetStat() {
try {
Cpuacct.Stat stat = one.getCpuacct().getStat();
assertTrue(stat.userTime >= 0);
assertTrue(stat.systemTime >= 0);
assertEquals(stat.userTime, 0);
assertEquals(stat.systemTime, 0);
} catch (IOException e) {
LOG.error("Get stat failed.", e);
assertTrue(false);
Expand All @@ -86,7 +86,7 @@ public void testGetPerUsage() {
long[] usages = one.getCpuacct().getPerUsage();
assertTrue(usages.length > 0);
for (long usage: usages) {
assertTrue(usage > 0);
assertEquals(usage, 0);
}
} catch (IOException e) {
LOG.error("Get usage_percpu failed.", e);
Expand Down

0 comments on commit 135b485

Please sign in to comment.