├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── org │ └── lealone │ └── cgroup │ ├── Group.java │ ├── Threads.java │ └── subsystem │ ├── Blkio.java │ ├── Cpu.java │ ├── Cpuacct.java │ ├── Cpuset.java │ ├── Devices.java │ ├── Freezer.java │ ├── Memory.java │ ├── NetCls.java │ ├── NetPrio.java │ ├── SubSystem.java │ └── SubSystemType.java └── test ├── java └── org │ └── lealone │ └── cgroup │ ├── Example.java │ └── subsystem │ ├── BlkioTest.java │ ├── CpuTest.java │ ├── CpuacctTest.java │ ├── CpusetTest.java │ ├── DevicesTest.java │ ├── FreezerTest.java │ ├── MemoryTest.java │ ├── NetClsTest.java │ ├── NetPrioTest.java │ └── SubSystemTest.java └── resources ├── cgroup.properties └── logback-test.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | 14 | 15 | .settings 16 | .classpath 17 | .project 18 | target 19 | 20 | 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CGroup4j 2 | 3 | [Linux CGroups](https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt) For Java,改编自[jcgroup](https://github.com/haosdent/jcgroup) 4 | 5 | ## SubSystems 6 | 7 | ☑ blkio
8 | ☑ cpu
9 | ☑ cpuacct
10 | ☑ cpuset
11 | ☑ devices
12 | ☑ freezer
13 | ☑ memory
14 | ☑ net_cls
15 | ☑ net_prio
16 | 17 | ## Example 18 | 19 | [Example.java](https://github.com/codefollower/CGroup4j/blob/master/src/test/java/org/lealone/cgroup/Example.java) 20 | 21 | ## Requirements 22 | 23 | * Linux kernel 2.6.18+ 24 | * Java 1.7+ 25 | 26 | ## License 27 | 28 | [Apache License, Version 2.0](https://github.com/codefollower/CGroup4j/blob/master/LICENSE) 29 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 18 | 20 | 4.0.0 21 | 22 | org.lealone 23 | cgroup4j 24 | jar 25 | 1.0.0-SNAPSHOT 26 | 27 | 28 | 29 | 30 | org.apache.maven.plugins 31 | maven-compiler-plugin 32 | 33 | 34 | org.apache.maven.plugins 35 | maven-source-plugin 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-compiler-plugin 43 | 2.3.2 44 | 45 | 1.7 46 | 1.7 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-source-plugin 53 | 2.4 54 | 55 | 56 | attach-sources 57 | 58 | jar 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-resources-plugin 66 | 2.5 67 | 68 | UTF-8 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-jar-plugin 74 | 2.4 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | junit 83 | junit 84 | 4.8 85 | test 86 | 87 | 88 | org.slf4j 89 | slf4j-api 90 | 1.6.1 91 | 92 | 93 | ch.qos.logback 94 | logback-classic 95 | 1.1.2 96 | 97 | 98 | net.java.dev.jna 99 | jna 100 | 4.1.0 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/Group.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup; 19 | 20 | import java.io.BufferedReader; 21 | import java.io.Console; 22 | import java.io.IOException; 23 | import java.io.InputStream; 24 | import java.io.InputStreamReader; 25 | import java.util.LinkedList; 26 | import java.util.List; 27 | import java.util.Properties; 28 | 29 | import org.lealone.cgroup.subsystem.Blkio; 30 | import org.lealone.cgroup.subsystem.Cpu; 31 | import org.lealone.cgroup.subsystem.Cpuacct; 32 | import org.lealone.cgroup.subsystem.Cpuset; 33 | import org.lealone.cgroup.subsystem.Devices; 34 | import org.lealone.cgroup.subsystem.Freezer; 35 | import org.lealone.cgroup.subsystem.Memory; 36 | import org.lealone.cgroup.subsystem.NetCls; 37 | import org.lealone.cgroup.subsystem.NetPrio; 38 | import org.lealone.cgroup.subsystem.SubSystem; 39 | import org.lealone.cgroup.subsystem.SubSystemType; 40 | import org.slf4j.Logger; 41 | import org.slf4j.LoggerFactory; 42 | 43 | public class Group { 44 | 45 | private final Group parent; 46 | private final String name; 47 | private final Shell shell; 48 | private final int subSystems; 49 | 50 | private final List subSystemsList = new LinkedList<>(); 51 | private final List subGroups = new LinkedList<>(); 52 | 53 | private Blkio blkio; 54 | private Cpu cpu; 55 | private Cpuacct cpuacct; 56 | private Cpuset cpuset; 57 | private Devices devices; 58 | private Freezer freezer; 59 | private Memory memory; 60 | private NetCls netCls; 61 | private NetPrio netPrio; 62 | 63 | private volatile boolean umounted; 64 | 65 | protected Group(Group parent, String name, Shell shell, int subSystems) { 66 | this.parent = parent; 67 | this.name = name; 68 | this.shell = shell; 69 | this.subSystems = subSystems; 70 | if (parent != null) { 71 | shell.mkdir(getFullName()); 72 | parent.subGroups.add(this); 73 | } 74 | } 75 | 76 | public Group createSubGroup(String name, SubSystemType... subSystemTypes) { 77 | int subSystems = 0; 78 | for (SubSystemType type : subSystemTypes) 79 | subSystems |= type.value; 80 | 81 | subSystems |= this.subSystems; 82 | return new Group(this, name, shell, subSystems); 83 | } 84 | 85 | public void umount() { 86 | Group[] subGroups = new Group[this.subGroups.size()]; 87 | this.subGroups.toArray(subGroups); 88 | 89 | for (Group group : subGroups) { 90 | group.umount(); 91 | } 92 | if (parent != null) { 93 | synchronized (parent.subGroups) { 94 | parent.subGroups.remove(this); 95 | } 96 | 97 | shell.rmdir(getFullName()); 98 | } else { 99 | shell.umount(name); 100 | shell.rmdir(name); 101 | } 102 | umounted = true; 103 | } 104 | 105 | public boolean isUmounted() { 106 | return umounted; 107 | } 108 | 109 | public String getName() { 110 | return name; 111 | } 112 | 113 | public String getFullName() { 114 | if (parent == null) 115 | return name; 116 | else 117 | return parent.getFullName() + "/" + name; 118 | } 119 | 120 | @Override 121 | public String toString() { 122 | return getFullName(); 123 | } 124 | 125 | public List getSubSystemList() { 126 | return subSystemsList; 127 | } 128 | 129 | public Blkio getBlkio() { 130 | if (blkio == null) { 131 | check(SubSystemType.blkio); 132 | blkio = new Blkio(this); 133 | } 134 | return blkio; 135 | } 136 | 137 | public Cpu getCpu() { 138 | if (cpu == null) { 139 | check(SubSystemType.cpu); 140 | cpu = new Cpu(this); 141 | } 142 | return cpu; 143 | } 144 | 145 | public Cpuacct getCpuacct() { 146 | if (cpuacct == null) { 147 | check(SubSystemType.cpuacct); 148 | cpuacct = new Cpuacct(this); 149 | } 150 | 151 | return cpuacct; 152 | } 153 | 154 | public Cpuset getCpuset() { 155 | if (cpuset == null) { 156 | check(SubSystemType.cpuset); 157 | cpuset = new Cpuset(this); 158 | } 159 | 160 | return cpuset; 161 | } 162 | 163 | public Devices getDevices() { 164 | if (devices == null) { 165 | check(SubSystemType.devices); 166 | devices = new Devices(this); 167 | } 168 | 169 | return devices; 170 | } 171 | 172 | public Freezer getFreezer() { 173 | if (freezer == null) { 174 | check(SubSystemType.freezer); 175 | freezer = new Freezer(this); 176 | } 177 | 178 | return freezer; 179 | } 180 | 181 | public Memory getMemory() { 182 | if (memory == null) { 183 | check(SubSystemType.memory); 184 | memory = new Memory(this); 185 | } 186 | 187 | return memory; 188 | } 189 | 190 | public NetCls getNetCls() { 191 | if (netCls == null) { 192 | check(SubSystemType.net_cls); 193 | netCls = new NetCls(this); 194 | } 195 | 196 | return netCls; 197 | } 198 | 199 | public NetPrio getNetPrio() { 200 | if (netPrio == null) { 201 | check(SubSystemType.net_prio); 202 | netPrio = new NetPrio(this); 203 | } 204 | 205 | return netPrio; 206 | } 207 | 208 | private void check(SubSystemType type) { 209 | if ((subSystems & type.value) == 0) { 210 | String msg = "SubSystem '" + type.name() + "' not attach to group '" + getFullName() + "'"; 211 | throw new IllegalStateException(msg); 212 | } 213 | } 214 | 215 | public void setParameter(String parameterName, Object parameterValue) { 216 | shell.set(getFullName(), parameterName, parameterValue.toString()); 217 | } 218 | 219 | public String getParameter(String parameterName) { 220 | return shell.get(getFullName(), parameterName); 221 | } 222 | 223 | private static final String CGROUP_CONFIG_FILE = "cgroup.properties"; 224 | private static final String PASSWORD = "password"; 225 | private static final String SUDO = "sudo"; 226 | 227 | public static Group createRootGroup(String name, SubSystemType... subSystemTypes) { 228 | boolean sudo = false; 229 | String password = null; 230 | try { 231 | Properties prop = new Properties(); 232 | prop.load(Group.class.getClassLoader().getResourceAsStream(CGROUP_CONFIG_FILE)); 233 | sudo = Boolean.parseBoolean(prop.getProperty(SUDO)); 234 | if (sudo) { 235 | password = prop.getProperty(PASSWORD); 236 | } 237 | } catch (Exception e) { 238 | // ignore 239 | } 240 | if (sudo && password == null) { 241 | password = readPassword(); 242 | } 243 | 244 | int subSystems = 0; 245 | for (SubSystemType type : subSystemTypes) 246 | subSystems |= type.value; 247 | 248 | Shell shell = new Shell(password); 249 | shell.mkdir(name); 250 | shell.mount(name, subSystems); 251 | return new Group(null, name, shell, subSystems); 252 | } 253 | 254 | private static String readPassword() { 255 | Console console = System.console(); 256 | if (console == null) { // In Eclipse 257 | System.out.print("Password: "); 258 | BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 259 | try { 260 | return stdin.readLine(); 261 | } catch (IOException e) { 262 | throw new RuntimeException("Failed to read password", e); 263 | } 264 | } else { // Outside Eclipse 265 | char[] pwd = console.readPassword("Password: "); 266 | return new String(pwd); 267 | } 268 | } 269 | 270 | private static class Shell { 271 | 272 | private static final Logger LOG = LoggerFactory.getLogger(Shell.class); 273 | 274 | private static final String CGROUP_DIR_PREFIX = "/cgroup/%s"; 275 | 276 | private static final String SHELL_MKDIR = "mkdir -p %s"; 277 | private static final String SHELL_RMDIR = "rmdir %s"; 278 | private static final String SHELL_MOUNT = "mount -t cgroup -o %s %s %s"; 279 | private static final String SHELL_UMOUNT = "umount %s"; 280 | 281 | private static final String SHELL_CAT = "cat " + CGROUP_DIR_PREFIX + "/%s"; 282 | private static final String SHELL_ECHO = "echo \"%s\" > " + CGROUP_DIR_PREFIX + "/%s"; 283 | 284 | private final String cmdPrefix; 285 | 286 | public Shell(String password) { 287 | if (password != null) 288 | cmdPrefix = "echo " + password + " | sudo -S "; 289 | else 290 | cmdPrefix = ""; 291 | } 292 | 293 | public String exec(String cmd) { 294 | return exec(cmd, false); 295 | } 296 | 297 | public String exec(String cmd, boolean isPrivilege) { 298 | if (isPrivilege) { 299 | cmd = cmdPrefix + cmd; 300 | } 301 | LOG.info("Shell cmd: " + cmd); 302 | try { 303 | Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", cmd }); 304 | 305 | process.waitFor(); 306 | if (process.exitValue() != 0) { 307 | String errorOutput = toString(process.getErrorStream()); 308 | LOG.error("Shell Error Output: " + errorOutput); 309 | throw new IOException(errorOutput); 310 | } 311 | String output = toString(process.getInputStream()); 312 | return output; 313 | } catch (Exception e) { 314 | throw new RuntimeException("Failed to exec cmd: " + cmd, e); 315 | } 316 | } 317 | 318 | private static String toString(InputStream input) throws IOException { 319 | byte[] buffer = new byte[input.available()]; 320 | input.read(buffer); 321 | return new String(buffer); 322 | 323 | } 324 | 325 | public void mount(String name, int subSystems) { 326 | if (subSystems > 0) { 327 | String path = String.format(CGROUP_DIR_PREFIX, name); 328 | String flag = SubSystemType.getTypeNames(subSystems); 329 | String cmd = String.format(SHELL_MOUNT, flag, name, path); 330 | exec(cmd, true); 331 | } 332 | } 333 | 334 | public void umount(String name) { 335 | String path = String.format(CGROUP_DIR_PREFIX, name); 336 | String cmd = String.format(SHELL_UMOUNT, path); 337 | exec(cmd, true); 338 | } 339 | 340 | public void mkdir(String path) { 341 | path = String.format(CGROUP_DIR_PREFIX, path); 342 | String cmd = String.format(SHELL_MKDIR, path); 343 | exec(cmd, true); 344 | } 345 | 346 | public void rmdir(String path) { 347 | path = String.format(CGROUP_DIR_PREFIX, path); 348 | String cmd = String.format(SHELL_RMDIR, path); 349 | exec(cmd, true); 350 | } 351 | 352 | public void set(String group, String p, String value) { 353 | String cmd = String.format(SHELL_ECHO, value, group, p); 354 | exec(cmd, true); 355 | } 356 | 357 | public String get(String group, String p) { 358 | String cmd = String.format(SHELL_CAT, group, p); 359 | String result = exec(cmd); 360 | result = result.trim(); 361 | return result; 362 | } 363 | 364 | } 365 | 366 | } 367 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/Threads.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup; 19 | 20 | import com.sun.jna.Library; 21 | import com.sun.jna.Native; 22 | import com.sun.jna.Platform; 23 | 24 | public class Threads { 25 | 26 | private interface CLibrary extends Library { 27 | int syscall(int api); 28 | } 29 | 30 | // 64位是186,32位是224 31 | private static final int __NR_gettid = Platform.is64Bit() ? 186 : 224; 32 | 33 | private static final CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), 34 | CLibrary.class); 35 | 36 | public static int getThreadId() { 37 | return INSTANCE.syscall(__NR_gettid); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Blkio.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | // https://www.kernel.org/doc/Documentation/cgroups/blkio-controller.txt 23 | public class Blkio extends SubSystem { 24 | 25 | // 总共22个 26 | private static final String BLKIO_THROTTLE_IO_SERIVICE_BYTES = "blkio.throttle.io_service_bytes"; 27 | private static final String BLKIO_THROTTLE_IO_SERIVICED = "blkio.throttle.io_serviced"; 28 | private static final String BLKIO_THROTTLE_READ_BPS_DEVICE = "blkio.throttle.read_bps_device"; 29 | private static final String BLKIO_THROTTLE_READ_IOPS_DEVICE = "blkio.throttle.read_iops_device"; 30 | private static final String BLKIO_THROTTLE_WRITE_BPS_DEVICE = "blkio.throttle.write_bps_device"; 31 | private static final String BLKIO_THROTTLE_WRITE_IOPS_DEVICE = "blkio.throttle.write_iops_device"; 32 | 33 | private static final String BLKIO_WEIGHT = "blkio.weight"; 34 | private static final String BLKIO_WEIGHT_DEVICE = "blkio.weight_device"; 35 | 36 | private static final String BLKIO_IO_MERGED = "blkio.io_merged"; 37 | private static final String BLKIO_IO_QUEUED = "blkio.io_queued"; 38 | private static final String BLKIO_IO_SERIVICE_BYTES = "blkio.io_service_bytes"; 39 | private static final String BLKIO_IO_SERIVICED = "blkio.io_serviced"; 40 | private static final String BLKIO_IO_SERIVICE_TIME = "blkio.io_service_time"; 41 | private static final String BLKIO_IO_WAIT_TIME = "blkio.io_wait_time"; 42 | 43 | private static final String BLKIO_TIME = "blkio.time"; 44 | private static final String BLKIO_RESET_STATS = "blkio.reset_stats"; 45 | private static final String BLKIO_SECTORS = "blkio.sectors"; 46 | 47 | // 下面5个设置CONFIG_DEBUG_BLK_CGROUP=y时才能见到 48 | private static final String BLKIO_AVG_QUEUE_SIZE = "blkio.avg_queue_size"; 49 | private static final String BLKIO_GROUP_WAIT_TIME = "blkio.group_wait_time"; 50 | private static final String BLKIO_EMPTY_TIME = "blkio.empty_time"; 51 | private static final String BLKIO_IDLE_TIME = "blkio.idle_time"; 52 | private static final String BLKIO_DEQUEUE = "blkio.dequeue"; 53 | 54 | public Blkio(Group group) { 55 | super(group); 56 | } 57 | 58 | @Override 59 | public SubSystemType getType() { 60 | return SubSystemType.blkio; 61 | } 62 | 63 | public static class Record { 64 | public final int major; 65 | public final int minor; 66 | public final String operation; 67 | public final long value; 68 | 69 | public Record(int major, int minor, String operation, long value) { 70 | this.major = major; 71 | this.minor = minor; 72 | this.operation = operation; 73 | this.value = value; 74 | } 75 | 76 | public Record(String output) { 77 | String[] splits = output.split("[:\\s]"); 78 | if (splits.length == 2) { // blkio.io_merged or blkio.io_queued 79 | major = -1; 80 | minor = -1; 81 | operation = splits[1]; 82 | value = Long.parseLong(splits[0]); 83 | } else { 84 | major = Integer.parseInt(splits[0]); 85 | minor = Integer.parseInt(splits[1]); 86 | if (splits.length > 3) { 87 | operation = splits[2]; 88 | value = Long.parseLong(splits[3]); 89 | } else { 90 | operation = null; 91 | value = Long.parseLong(splits[2]); 92 | } 93 | } 94 | } 95 | 96 | private static Record[] parseRecordList(String output) { 97 | output = output.trim(); 98 | if (output.isEmpty()) 99 | return new Record[0]; 100 | String[] splits = output.split("\n"); 101 | // Skip Last Total Item. 102 | int len = splits.length; 103 | if (splits[splits.length - 1].trim().indexOf("Total") == 0) { 104 | len--; 105 | } 106 | Record[] records = new Record[len]; 107 | for (int i = 0, l = len; i < l; i++) { 108 | records[i] = new Record(splits[i].trim()); 109 | } 110 | 111 | return records; 112 | } 113 | 114 | @Override 115 | public String toString() { 116 | StringBuilder sb = new StringBuilder(); 117 | sb.append(major); 118 | sb.append(':'); 119 | sb.append(minor); 120 | if (operation != null) { 121 | sb.append(' '); 122 | sb.append(operation); 123 | } 124 | sb.append(' '); 125 | sb.append(value); 126 | 127 | return sb.toString(); 128 | } 129 | 130 | @Override 131 | public int hashCode() { 132 | final int prime = 31; 133 | int result = 1; 134 | result = prime * result + major; 135 | result = prime * result + minor; 136 | result = prime * result + ((operation == null) ? 0 : operation.hashCode()); 137 | result = prime * result + (int) (value ^ (value >>> 32)); 138 | return result; 139 | } 140 | 141 | @Override 142 | public boolean equals(Object obj) { 143 | if (this == obj) 144 | return true; 145 | if (obj == null) 146 | return false; 147 | if (getClass() != obj.getClass()) 148 | return false; 149 | Record other = (Record) obj; 150 | if (major != other.major) 151 | return false; 152 | if (minor != other.minor) 153 | return false; 154 | if (operation == null) { 155 | if (other.operation != null) 156 | return false; 157 | } else if (!operation.equals(other.operation)) 158 | return false; 159 | if (value != other.value) 160 | return false; 161 | return true; 162 | } 163 | 164 | } 165 | 166 | private void setThrottle(String p, int major, int minor, long v) { 167 | Record record = new Record(major, minor, null, v); 168 | setParameter(p, record); 169 | } 170 | 171 | private Record[] getThrottle(String p) { 172 | return parseRecordList(p); 173 | } 174 | 175 | private Record[] parseRecordList(String p) { 176 | String output = getStringParameter(p); 177 | return Record.parseRecordList(output); 178 | } 179 | 180 | public void setThrottleReadBpsDevice(int major, int minor, long bps) { 181 | setThrottle(BLKIO_THROTTLE_READ_BPS_DEVICE, major, minor, bps); 182 | } 183 | 184 | public Record[] getThrottleReadBpsDevice() { 185 | return getThrottle(BLKIO_THROTTLE_READ_BPS_DEVICE); 186 | } 187 | 188 | public void setThrottleReadIopsDevice(int major, int minor, long iops) { 189 | setThrottle(BLKIO_THROTTLE_READ_IOPS_DEVICE, major, minor, iops); 190 | } 191 | 192 | public Record[] getThrottleReadIopsDevice() { 193 | return getThrottle(BLKIO_THROTTLE_READ_IOPS_DEVICE); 194 | } 195 | 196 | public void setThrottleWriteBpsDevice(int major, int minor, long bps) { 197 | setThrottle(BLKIO_THROTTLE_WRITE_BPS_DEVICE, major, minor, bps); 198 | } 199 | 200 | public Record[] getThrottleWriteBpsDevice() { 201 | return getThrottle(BLKIO_THROTTLE_WRITE_BPS_DEVICE); 202 | } 203 | 204 | public void setThrottleWriteIopsDevice(int major, int minor, long iops) { 205 | setThrottle(BLKIO_THROTTLE_WRITE_IOPS_DEVICE, major, minor, iops); 206 | } 207 | 208 | public Record[] getThrottleWriteIopsDevice() { 209 | return getThrottle(BLKIO_THROTTLE_WRITE_IOPS_DEVICE); 210 | } 211 | 212 | public Record[] getThrottleIoServiced() { 213 | return getThrottle(BLKIO_THROTTLE_IO_SERIVICED); 214 | } 215 | 216 | public Record[] getThrottleIoServiceBytes() { 217 | return getThrottle(BLKIO_THROTTLE_IO_SERIVICE_BYTES); 218 | } 219 | 220 | public void setWeight(int v) { 221 | setParameter(BLKIO_WEIGHT, v); 222 | } 223 | 224 | public int getWeight() { 225 | return getIntParameter(BLKIO_WEIGHT); 226 | } 227 | 228 | public void setWeightDevice(int major, int minor, int weight) { 229 | StringBuilder s = new StringBuilder(); 230 | s.append(major).append(':').append(minor).append(' ').append(weight); 231 | setParameter(BLKIO_WEIGHT_DEVICE, s); 232 | } 233 | 234 | public String getWeightDevice() { 235 | return getStringParameter(BLKIO_WEIGHT_DEVICE); 236 | } 237 | 238 | public Record[] getIoServiced() { 239 | return parseRecordList(BLKIO_IO_SERIVICED); 240 | } 241 | 242 | public Record[] getIoServiceBytes() { 243 | return parseRecordList(BLKIO_IO_SERIVICE_BYTES); 244 | } 245 | 246 | public Record[] getIoServiceTime() { 247 | return parseRecordList(BLKIO_IO_SERIVICE_TIME); 248 | } 249 | 250 | public Record[] getIoWaitTime() { 251 | return parseRecordList(BLKIO_IO_WAIT_TIME); 252 | } 253 | 254 | public Record[] getIoMerged() { 255 | return parseRecordList(BLKIO_IO_MERGED); 256 | } 257 | 258 | public Record[] getIoQueued() { 259 | return parseRecordList(BLKIO_IO_QUEUED); 260 | } 261 | 262 | public void resetStats(int v) { 263 | setParameter(BLKIO_RESET_STATS, v); 264 | } 265 | 266 | public Record[] getIoTime() { 267 | return parseRecordList(BLKIO_TIME); 268 | } 269 | 270 | public Record[] getSectors() { 271 | return parseRecordList(BLKIO_SECTORS); 272 | } 273 | 274 | public int getAvgQueueSize() { 275 | return getIntParameter(BLKIO_AVG_QUEUE_SIZE); 276 | } 277 | 278 | public long getGroupWaitTime() { 279 | return getLongParameter(BLKIO_GROUP_WAIT_TIME); 280 | } 281 | 282 | public long getEmptyTime() { 283 | return getLongParameter(BLKIO_EMPTY_TIME); 284 | } 285 | 286 | public long getIdleTime() { 287 | return getLongParameter(BLKIO_IDLE_TIME); 288 | } 289 | 290 | public Record[] getDequeue() { 291 | return parseRecordList(BLKIO_DEQUEUE); 292 | } 293 | 294 | } 295 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Cpu.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | public class Cpu extends SubSystem { 23 | 24 | private static final String CPU_STAT = "cpu.stat"; 25 | private static final String CPU_SHARES = "cpu.shares"; 26 | 27 | private static final String CPU_CFS_PERIOD_US = "cpu.cfs_period_us"; 28 | private static final String CPU_CFS_QUOTA_US = "cpu.cfs_quota_us"; 29 | private static final String CPU_RT_PERIOD_US = "cpu.rt_period_us"; 30 | private static final String CPU_RT_RUNTIME_US = "cpu.rt_runtime_us"; 31 | 32 | public Cpu(Group group) { 33 | super(group); 34 | } 35 | 36 | @Override 37 | public SubSystemType getType() { 38 | return SubSystemType.cpu; 39 | } 40 | 41 | public static class Stat { 42 | public final int nrPeriods; 43 | public final int nrThrottled; 44 | public final int throttledTime; 45 | 46 | public Stat(String statStr) { 47 | String[] splits = statStr.split("\n"); 48 | this.nrPeriods = Integer.parseInt(splits[0].split(" ")[1]); 49 | this.nrThrottled = Integer.parseInt(splits[1].split(" ")[1]); 50 | this.throttledTime = Integer.parseInt(splits[2].split(" ")[1]); 51 | } 52 | } 53 | 54 | public Stat getStat() { 55 | String output = getStringParameter(CPU_STAT); 56 | Stat stat = new Stat(output); 57 | return stat; 58 | } 59 | 60 | public void setShares(int shares) { 61 | setParameter(CPU_SHARES, shares); 62 | } 63 | 64 | public int getShares() { 65 | return getIntParameter(CPU_SHARES); 66 | } 67 | 68 | public void setCfsPeriodUs(long v) { 69 | setParameter(CPU_CFS_PERIOD_US, v); 70 | } 71 | 72 | public long getCfsPeriodUs() { 73 | return getLongParameter(CPU_CFS_PERIOD_US); 74 | } 75 | 76 | public void setCfsQuotaUs(long v) { 77 | setParameter(CPU_CFS_QUOTA_US, v); 78 | } 79 | 80 | public long getCfsQuotaUs() { 81 | return getLongParameter(CPU_CFS_QUOTA_US); 82 | } 83 | 84 | public void setRtPeriodUs(long v) { 85 | setParameter(CPU_RT_PERIOD_US, v); 86 | } 87 | 88 | public long getRtPeriodUs() { 89 | return getLongParameter(CPU_RT_PERIOD_US); 90 | } 91 | 92 | public void setRtRuntimeUs(long v) { 93 | setParameter(CPU_RT_RUNTIME_US, v); 94 | } 95 | 96 | public long getRtRuntimeUs() { 97 | return getLongParameter(CPU_RT_RUNTIME_US); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Cpuacct.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | public class Cpuacct extends SubSystem { 23 | 24 | private static final String CPUACCT_STAT = "cpuacct.stat"; 25 | private static final String CPUACCT_USAGE = "cpuacct.usage"; 26 | private static final String CPUACCT_USAGE_PERCPU = "cpuacct.usage_percpu"; 27 | 28 | public Cpuacct(Group group) { 29 | super(group); 30 | } 31 | 32 | @Override 33 | public SubSystemType getType() { 34 | return SubSystemType.cpuacct; 35 | } 36 | 37 | public static class Stat { 38 | public final long userTime; 39 | public final long systemTime; 40 | 41 | public Stat(String statStr) { 42 | String[] splits = statStr.split("\n"); 43 | this.userTime = Integer.parseInt(splits[0].split(" ")[1]); 44 | this.systemTime = Integer.parseInt(splits[1].split(" ")[1]); 45 | } 46 | } 47 | 48 | public Stat getStat() { 49 | String output = getStringParameter(CPUACCT_STAT); 50 | Stat stat = new Stat(output); 51 | return stat; 52 | } 53 | 54 | public long getUsage() { 55 | return getLongParameter(CPUACCT_USAGE); 56 | } 57 | 58 | public void resetUsage() { 59 | setParameter(CPUACCT_USAGE, 0); 60 | } 61 | 62 | public long[] getUsagePerCpu() { 63 | String[] outputs = getStringParameter(CPUACCT_USAGE_PERCPU).split(" "); 64 | long[] usages = new long[outputs.length]; 65 | for (int i = 0, l = outputs.length; i < l; i++) { 66 | usages[i] = Long.parseLong(outputs[i]); 67 | } 68 | return usages; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Cpuset.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import java.util.LinkedList; 21 | 22 | import org.lealone.cgroup.Group; 23 | 24 | public class Cpuset extends SubSystem { 25 | 26 | private static final String CPUSET_CPUS = "cpuset.cpus"; 27 | private static final String CPUSET_MEMS = "cpuset.mems"; 28 | private static final String CPUSET_MEMORY_MIGRATE = "cpuset.memory_migrate"; 29 | private static final String CPUSET_CPU_EXCLUSIVE = "cpuset.cpu_exclusive"; 30 | private static final String CPUSET_MEM_EXCLUSIVE = "cpuset.mem_exclusive"; 31 | private static final String CPUSET_MEM_HARDWALL = "cpuset.mem_hardwall"; 32 | private static final String CPUSET_MEMORY_PRESSURE = "cpuset.memory_pressure"; 33 | private static final String CPUSET_MEMORY_PRESSURE_ENABLED = "cpuset.memory_pressure_enabled"; 34 | private static final String CPUSET_MEMORY_SPREAD_PAGE = "cpuset.memory_spread_page"; 35 | private static final String CPUSET_MEMORY_SPREAD_SLAB = "cpuset.memory_spread_slab"; 36 | private static final String CPUSET_SCHED_LOAD_BALANCE = "cpuset.sched_load_balance"; 37 | private static final String CPUSET_SCHED_RELAX_DOMAIN_LEVEL = "cpuset.sched_relax_domain_level"; 38 | 39 | public Cpuset(Group group) { 40 | super(group); 41 | } 42 | 43 | @Override 44 | public SubSystemType getType() { 45 | return SubSystemType.cpuset; 46 | } 47 | 48 | public void setCpus(int... nums) { 49 | StringBuilder sb = new StringBuilder(); 50 | for (int num : nums) { 51 | sb.append(num); 52 | sb.append(','); 53 | } 54 | sb.deleteCharAt(sb.length() - 1); 55 | setParameter(CPUSET_CPUS, sb); 56 | } 57 | 58 | public int[] getCpus() { 59 | String output = getStringParameter(CPUSET_CPUS); 60 | return parseNums(output); 61 | } 62 | 63 | public void setMems(int... nums) { 64 | StringBuilder sb = new StringBuilder(); 65 | for (int num : nums) { 66 | sb.append(num); 67 | sb.append(','); 68 | } 69 | sb.deleteCharAt(sb.length() - 1); 70 | setParameter(CPUSET_MEMS, sb); 71 | } 72 | 73 | public int[] getMems() { 74 | String output = getStringParameter(CPUSET_MEMS); 75 | return parseNums(output); 76 | } 77 | 78 | public void setMemMigrate(boolean flag) { 79 | int v = flag ? 1 : 0; 80 | setParameter(CPUSET_MEMORY_MIGRATE, v); 81 | } 82 | 83 | public boolean isMemMigrate() { 84 | return getIntParameter(CPUSET_MEMORY_MIGRATE) > 0; 85 | } 86 | 87 | public void setCpuExclusive(boolean flag) { 88 | int v = flag ? 1 : 0; 89 | setParameter(CPUSET_CPU_EXCLUSIVE, v); 90 | } 91 | 92 | public boolean isCpuExclusive() { 93 | return getIntParameter(CPUSET_CPU_EXCLUSIVE) > 0; 94 | } 95 | 96 | public void setMemExclusive(boolean flag) { 97 | int v = flag ? 1 : 0; 98 | setParameter(CPUSET_MEM_EXCLUSIVE, v); 99 | } 100 | 101 | public boolean isMemExclusive() { 102 | return getIntParameter(CPUSET_MEM_EXCLUSIVE) > 0; 103 | } 104 | 105 | public void setMemHardwall(boolean flag) { 106 | int v = flag ? 1 : 0; 107 | setParameter(CPUSET_MEM_HARDWALL, v); 108 | } 109 | 110 | public boolean isMemHardwall() { 111 | return getIntParameter(CPUSET_MEM_HARDWALL) > 0; 112 | } 113 | 114 | public int getMemPressure() { 115 | return getIntParameter(CPUSET_MEMORY_PRESSURE); 116 | } 117 | 118 | public void setMemPressureEnabled(boolean flag) { 119 | int v = flag ? 1 : 0; 120 | setParameter(CPUSET_MEMORY_PRESSURE_ENABLED, v); 121 | } 122 | 123 | public boolean isMemPressureEnabled() { 124 | return getIntParameter(CPUSET_MEMORY_PRESSURE_ENABLED) > 0; 125 | } 126 | 127 | public void setMemSpreadPage(boolean flag) { 128 | int v = flag ? 1 : 0; 129 | setParameter(CPUSET_MEMORY_SPREAD_PAGE, v); 130 | } 131 | 132 | public boolean isMemSpreadPage() { 133 | return getIntParameter(CPUSET_MEMORY_SPREAD_PAGE) > 0; 134 | } 135 | 136 | public void setMemSpreadSlab(boolean flag) { 137 | int v = flag ? 1 : 0; 138 | setParameter(CPUSET_MEMORY_SPREAD_SLAB, v); 139 | } 140 | 141 | public boolean isMemSpreadSlab() { 142 | return getIntParameter(CPUSET_MEMORY_SPREAD_SLAB) > 0; 143 | } 144 | 145 | public void setSchedLoadBlance(boolean flag) { 146 | int v = flag ? 1 : 0; 147 | setParameter(CPUSET_SCHED_LOAD_BALANCE, v); 148 | } 149 | 150 | public boolean isSchedLoadBlance() { 151 | return getIntParameter(CPUSET_SCHED_LOAD_BALANCE) > 0; 152 | } 153 | 154 | public void setSchedRelaxDomainLevel(int v) { 155 | setParameter(CPUSET_SCHED_RELAX_DOMAIN_LEVEL, v); 156 | } 157 | 158 | public int getSchedRelaxDomainLevel() { 159 | return getIntParameter(CPUSET_SCHED_RELAX_DOMAIN_LEVEL); 160 | } 161 | 162 | public static int[] parseNums(String outputStr) { 163 | char[] output = outputStr.toCharArray(); 164 | LinkedList numList = new LinkedList(); 165 | int value = 0; 166 | int start = 0; 167 | boolean isHyphen = false; 168 | for (char ch : output) { 169 | if (ch == ',') { 170 | if (isHyphen) { 171 | for (; start <= value; start++) { 172 | numList.add(start); 173 | } 174 | isHyphen = false; 175 | } else { 176 | numList.add(value); 177 | } 178 | value = 0; 179 | } else if (ch == '-') { 180 | isHyphen = true; 181 | start = value; 182 | value = 0; 183 | } else { 184 | value = value * 10 + (ch - '0'); 185 | } 186 | } 187 | if (output[output.length - 1] != ',') { 188 | if (isHyphen) { 189 | for (; start <= value; start++) { 190 | numList.add(start); 191 | } 192 | } else { 193 | numList.add(value); 194 | } 195 | } 196 | 197 | int[] nums = new int[numList.size()]; 198 | int index = 0; 199 | for (int num : numList) { 200 | nums[index] = num; 201 | index++; 202 | } 203 | 204 | return nums; 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Devices.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | import org.slf4j.Logger; 22 | import org.slf4j.LoggerFactory; 23 | 24 | public class Devices extends SubSystem { 25 | 26 | private static final Logger LOG = LoggerFactory.getLogger(Devices.class); 27 | 28 | private static final String DEVICES_ALLOW = "devices.allow"; 29 | private static final String DEVICES_DENY = "devices.deny"; 30 | private static final String DEVICES_LIST = "devices.list"; 31 | 32 | public static final char TYPE_ALL = 'a'; 33 | public static final char TYPE_BLOCK = 'b'; 34 | public static final char TYPE_CHAR = 'c'; 35 | 36 | public static final int ACCESS_READ = 1; 37 | public static final int ACCESS_WRITE = 2; 38 | public static final int ACCESS_CREATE = 4; 39 | 40 | public static final char ACCESS_READ_CH = 'r'; 41 | public static final char ACCESS_WRITE_CH = 'w'; 42 | public static final char ACCESS_CREATE_CH = 'm'; 43 | 44 | public Devices(Group group) { 45 | super(group); 46 | } 47 | 48 | @Override 49 | public SubSystemType getType() { 50 | return SubSystemType.devices; 51 | } 52 | 53 | public static class Record { 54 | 55 | char type; 56 | int major; 57 | int minor; 58 | int accesses; 59 | 60 | public Record(char type, int major, int minor, int accesses) { 61 | this.type = type; 62 | this.major = major; 63 | this.minor = minor; 64 | this.accesses = accesses; 65 | } 66 | 67 | public Record(String output) { 68 | if (output.contains("*")) { 69 | LOG.debug("Pre:" + output); 70 | output = output.replaceAll("\\*", "-1"); 71 | LOG.debug("After:" + output); 72 | } 73 | String[] splits = output.split("[: ]"); 74 | type = splits[0].charAt(0); 75 | major = Integer.parseInt(splits[1]); 76 | minor = Integer.parseInt(splits[2]); 77 | accesses = 0; 78 | for (char c : splits[3].toCharArray()) { 79 | if (c == ACCESS_READ_CH) { 80 | accesses |= ACCESS_READ; 81 | } 82 | if (c == ACCESS_CREATE_CH) { 83 | accesses |= ACCESS_CREATE; 84 | } 85 | if (c == ACCESS_WRITE_CH) { 86 | accesses |= ACCESS_WRITE; 87 | } 88 | } 89 | } 90 | 91 | @Override 92 | public String toString() { 93 | StringBuilder sb = new StringBuilder(); 94 | sb.append(type); 95 | sb.append(' '); 96 | sb.append(major); 97 | sb.append(':'); 98 | sb.append(minor); 99 | sb.append(' '); 100 | sb.append(getAccessesFlag(accesses)); 101 | 102 | return sb.toString(); 103 | } 104 | 105 | public static Record[] parseRecordList(String output) { 106 | String[] splits = output.split("/n"); 107 | Record[] records = new Record[splits.length]; 108 | for (int i = 0, l = splits.length; i < l; i++) { 109 | records[i] = new Record(splits[i]); 110 | } 111 | 112 | return records; 113 | } 114 | 115 | public static StringBuilder getAccessesFlag(int accesses) { 116 | StringBuilder sb = new StringBuilder(); 117 | if ((accesses & ACCESS_READ) != 0) { 118 | sb.append(ACCESS_READ_CH); 119 | } 120 | if ((accesses & ACCESS_WRITE) != 0) { 121 | sb.append(ACCESS_WRITE_CH); 122 | } 123 | if ((accesses & ACCESS_CREATE) != 0) { 124 | sb.append(ACCESS_CREATE_CH); 125 | } 126 | return sb; 127 | } 128 | } 129 | 130 | private void setPermission(String p, char type, int major, int minor, int accesses) { 131 | Record record = new Record(type, major, minor, accesses); 132 | setParameter(p, record); 133 | } 134 | 135 | public void setAllow(char type, int major, int minor, int accesses) { 136 | setPermission(DEVICES_ALLOW, type, major, minor, accesses); 137 | } 138 | 139 | public void setDeny(char type, int major, int minor, int accesses) { 140 | setPermission(DEVICES_DENY, type, major, minor, accesses); 141 | } 142 | 143 | public Record[] getList() { 144 | String output = getStringParameter(DEVICES_LIST); 145 | return Record.parseRecordList(output); 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Freezer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | public class Freezer extends SubSystem { 23 | 24 | private static final String FREEZER_STATE = "freezer.state"; 25 | 26 | public static final String STATE_FROZEN = "FROZEN"; 27 | public static final String STATE_FREEZING = "FREEZING"; 28 | public static final String STATE_THAWED = "THAWED"; 29 | 30 | public Freezer(Group group) { 31 | super(group); 32 | } 33 | 34 | @Override 35 | public SubSystemType getType() { 36 | return SubSystemType.freezer; 37 | } 38 | 39 | public void setState(String state) { 40 | setParameter(FREEZER_STATE, state); 41 | } 42 | 43 | public String getState() { 44 | return getStringParameter(FREEZER_STATE); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/Memory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | // https://www.kernel.org/doc/Documentation/cgroups/memory.txt 23 | public class Memory extends SubSystem { 24 | 25 | private static final String MEMORY_STAT = "memory.stat"; 26 | private static final String MEMORY_USAGE_IN_BYTES = "memory.usage_in_bytes"; 27 | private static final String MEMORY_MEMSW_USAGE_IN_BYTES = "memory.memsw.usage_in_bytes"; 28 | private static final String MEMORY_MAX_USAGE_IN_BYTES = "memory.max_usage_in_bytes"; 29 | private static final String MEMORY_MEMSW_MAX_USAGE_IN_BYTES = "memory.memsw.max_usage_in_bytes"; 30 | private static final String MEMORY_LIMIT_IN_BYTES = "memory.limit_in_bytes"; 31 | private static final String MEMORY_MEMSW_LIMIT_IN_BYTES = "memory.memsw.limit_in_bytes"; 32 | private static final String MEMORY_FAILCNT = "memory.failcnt"; 33 | private static final String MEMORY_MEMSW_FAILCNT = "memory.memsw.failcnt"; 34 | private static final String MEMORY_SOFT_LIMIT_IN_BYTES = "memory.soft_limit_in_bytes"; 35 | private static final String MEMORY_FORCE_EMPTY = "memory.force_empty"; 36 | private static final String MEMORY_SWAPPINESS = "memory.swappiness"; 37 | private static final String MEMORY_MOVE_CHARGE_AT_IMMIGRATE = "memory.move_charge_at_immigrate"; 38 | private static final String MEMORY_USE_HIERARCHY = "memory.use_hierarchy"; 39 | private static final String MEMORY_OOM_CONTROL = "memory.oom_control"; 40 | 41 | public Memory(Group group) { 42 | super(group); 43 | } 44 | 45 | @Override 46 | public SubSystemType getType() { 47 | return SubSystemType.memory; 48 | } 49 | 50 | public static class Stat { 51 | public final long cacheSize; 52 | public final long rssSize; 53 | public final long mappedFileSize; 54 | public final long pgpginNum; 55 | public final long pgpgoutNum; 56 | public final long swapSize; 57 | public final long activeAnonSize; 58 | public final long inactiveAnonSize; 59 | public final long activeFileSize; 60 | public final long inactiveFileSize; 61 | public final long unevictableSize; 62 | public final long hierarchicalMemoryLimitSize; 63 | public final long hierarchicalMemswLimitSize; 64 | public final long totalCacheSize; 65 | public final long totalRssSize; 66 | public final long totalMappedFileSize; 67 | public final long totalPgpginNum; 68 | public final long totalPgpgoutNum; 69 | public final long totalSwapSize; 70 | public final long totalActiveAnonSize; 71 | public final long totalInactiveAnonSize; 72 | public final long totalActiveFileSize; 73 | public final long totalInactiveFileSize; 74 | public final long totalUnevictableSize; 75 | public final long totalHierarchicalMemoryLimitSize; 76 | public final long totalHierarchicalMemswLimitSize; 77 | 78 | public Stat(String output) { 79 | String[] splits = output.split("\n"); 80 | this.cacheSize = Long.parseLong(splits[0]); 81 | this.rssSize = Long.parseLong(splits[1]); 82 | this.mappedFileSize = Long.parseLong(splits[2]); 83 | this.pgpginNum = Long.parseLong(splits[3]); 84 | this.pgpgoutNum = Long.parseLong(splits[4]); 85 | this.swapSize = Long.parseLong(splits[5]); 86 | this.inactiveAnonSize = Long.parseLong(splits[6]); 87 | this.activeAnonSize = Long.parseLong(splits[7]); 88 | this.inactiveFileSize = Long.parseLong(splits[8]); 89 | this.activeFileSize = Long.parseLong(splits[9]); 90 | this.unevictableSize = Long.parseLong(splits[10]); 91 | this.hierarchicalMemoryLimitSize = Long.parseLong(splits[11]); 92 | this.hierarchicalMemswLimitSize = Long.parseLong(splits[12]); 93 | this.totalCacheSize = Long.parseLong(splits[13]); 94 | this.totalRssSize = Long.parseLong(splits[14]); 95 | this.totalMappedFileSize = Long.parseLong(splits[15]); 96 | this.totalPgpginNum = Long.parseLong(splits[16]); 97 | this.totalPgpgoutNum = Long.parseLong(splits[17]); 98 | this.totalSwapSize = Long.parseLong(splits[18]); 99 | this.totalInactiveAnonSize = Long.parseLong(splits[19]); 100 | this.totalActiveAnonSize = Long.parseLong(splits[20]); 101 | this.totalInactiveFileSize = Long.parseLong(splits[21]); 102 | this.totalActiveFileSize = Long.parseLong(splits[22]); 103 | this.totalUnevictableSize = Long.parseLong(splits[23]); 104 | this.totalHierarchicalMemoryLimitSize = Long.parseLong(splits[24]); 105 | this.totalHierarchicalMemswLimitSize = Long.parseLong(splits[25]); 106 | } 107 | } 108 | 109 | public Stat getStat() { 110 | String output = getStringParameter(MEMORY_STAT); 111 | Stat stat = new Stat(output); 112 | return stat; 113 | } 114 | 115 | public long getUsageInBytes() { 116 | return getLongParameter(MEMORY_USAGE_IN_BYTES); 117 | } 118 | 119 | public long getMemswUsageInBytes() { 120 | return getLongParameter(MEMORY_MEMSW_USAGE_IN_BYTES); 121 | } 122 | 123 | public long getMaxUsageInBytes() { 124 | return getLongParameter(MEMORY_MAX_USAGE_IN_BYTES); 125 | } 126 | 127 | public long getMemswMaxUsageInBytes() { 128 | return getLongParameter(MEMORY_MEMSW_MAX_USAGE_IN_BYTES); 129 | } 130 | 131 | public void setLimitInBytes(long v) { 132 | setParameter(MEMORY_LIMIT_IN_BYTES, v); 133 | } 134 | 135 | public long getLimitInBytes() { 136 | return getLongParameter(MEMORY_LIMIT_IN_BYTES); 137 | } 138 | 139 | public void setMemswLimitInBytes(long v) { 140 | setParameter(MEMORY_MEMSW_LIMIT_IN_BYTES, v); 141 | } 142 | 143 | public long getMemswLimitInBytes() { 144 | return getLongParameter(MEMORY_MEMSW_LIMIT_IN_BYTES); 145 | } 146 | 147 | public int getFailcnt() { 148 | return getIntParameter(MEMORY_FAILCNT); 149 | } 150 | 151 | public int getMemswFailcnt() { 152 | return getIntParameter(MEMORY_MEMSW_FAILCNT); 153 | } 154 | 155 | public void setSoftLimitInBytes(long v) { 156 | setParameter(MEMORY_SOFT_LIMIT_IN_BYTES, v); 157 | } 158 | 159 | public long getSoftLimitInBytes() { 160 | return getLongParameter(MEMORY_SOFT_LIMIT_IN_BYTES); 161 | } 162 | 163 | public void forceEmpty() { 164 | setParameter(MEMORY_FORCE_EMPTY, 0); 165 | } 166 | 167 | public void setSwappiness(int v) { 168 | setParameter(MEMORY_SWAPPINESS, v); 169 | } 170 | 171 | public int getSwappiness() { 172 | return getIntParameter(MEMORY_SWAPPINESS); 173 | } 174 | 175 | public void setMoveChargeAtImmigrate(int v) { 176 | setParameter(MEMORY_MOVE_CHARGE_AT_IMMIGRATE, v); 177 | } 178 | 179 | public boolean isMoveChargeAtImmigrate() { 180 | return getIntParameter(MEMORY_MOVE_CHARGE_AT_IMMIGRATE) > 0; 181 | } 182 | 183 | public void setUseHierarchy(boolean flag) { 184 | int v = flag ? 1 : 0; 185 | setParameter(MEMORY_USE_HIERARCHY, v); 186 | } 187 | 188 | public boolean isUseHierarchy() { 189 | return getIntParameter(MEMORY_USE_HIERARCHY) > 0; 190 | } 191 | 192 | public void setOomControl(boolean flag) { 193 | int v = flag ? 1 : 0; 194 | setParameter(MEMORY_OOM_CONTROL, v); 195 | } 196 | 197 | public boolean isOomControl() { 198 | String output = getStringParameter(MEMORY_OOM_CONTROL); 199 | output = output.split("\n")[0].split("[\\s]")[1]; 200 | int value = Integer.parseInt(output); 201 | return value > 0; 202 | } 203 | 204 | } 205 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/NetCls.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import org.lealone.cgroup.Group; 21 | 22 | public class NetCls extends SubSystem { 23 | 24 | private static final String NET_CLS_CLASSID = "net_cls.classid"; 25 | 26 | public NetCls(Group group) { 27 | super(group); 28 | } 29 | 30 | @Override 31 | public SubSystemType getType() { 32 | return SubSystemType.net_cls; 33 | } 34 | 35 | private StringBuilder toHex(int num) { 36 | String hex = num + ""; 37 | StringBuilder sb = new StringBuilder(); 38 | int l = hex.length(); 39 | if (l > 4) { 40 | hex = hex.substring(l - 4 - 1, l); 41 | } 42 | for (; l < 4; l++) { 43 | sb.append('0'); 44 | } 45 | sb.append(hex); 46 | return sb; 47 | } 48 | 49 | public void setClassId(int major, int minor) { 50 | StringBuilder sb = new StringBuilder("0x"); 51 | sb.append(toHex(major)); 52 | sb.append(toHex(minor)); 53 | setParameter(NET_CLS_CLASSID, sb); 54 | } 55 | 56 | public int[] getClassId() { 57 | String output = getStringParameter(NET_CLS_CLASSID); 58 | output = Integer.toHexString(Integer.parseInt(output)); 59 | int major = Integer.parseInt(output.substring(0, output.length() - 4)); 60 | int minor = Integer.parseInt(output.substring(output.length() - 4)); 61 | int[] classId = { major, minor }; 62 | return classId; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/NetPrio.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | import org.lealone.cgroup.Group; 24 | 25 | public class NetPrio extends SubSystem { 26 | 27 | private static final String NET_PRIO_PRIOIDX = "net_prio.prioidx"; 28 | private static final String NET_PRIO_IFPRIOMAP = "net_prio.ifpriomap"; 29 | 30 | public NetPrio(Group group) { 31 | super(group); 32 | } 33 | 34 | @Override 35 | public SubSystemType getType() { 36 | return SubSystemType.net_prio; 37 | } 38 | 39 | public int getPrioId() { 40 | return getIntParameter(NET_PRIO_PRIOIDX); 41 | } 42 | 43 | public Map getIfPrioMap() { 44 | String output = getStringParameter(NET_PRIO_IFPRIOMAP); 45 | String[] splits = output.split("\n"); 46 | Map ifPrioMap = new HashMap(); 47 | for (String split : splits) { 48 | String[] tmpSplits = split.split(" "); 49 | ifPrioMap.put(tmpSplits[0].trim(), Integer.parseInt(tmpSplits[1])); 50 | } 51 | return ifPrioMap; 52 | } 53 | 54 | public void addIfPrioMap(String iface, int priority) { 55 | StringBuilder sb = new StringBuilder(); 56 | sb.append(iface); 57 | sb.append(' '); 58 | sb.append(priority); 59 | setParameter(NET_PRIO_IFPRIOMAP, sb); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/SubSystem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import java.util.LinkedList; 21 | import java.util.List; 22 | 23 | import org.lealone.cgroup.Group; 24 | import org.lealone.cgroup.Threads; 25 | 26 | public abstract class SubSystem { 27 | 28 | private static final String CGROUP_EVENT_CONTROL = "cgroup.event_control"; 29 | private static final String NOTIFY_ON_RELEASE = "notify_on_release"; 30 | private static final String RELEASE_AGENT = "release_agent"; 31 | private static final String TASKS = "tasks"; 32 | 33 | private final List taskList = new LinkedList<>(); 34 | private final Group group; 35 | 36 | public SubSystem(Group group) { 37 | this.group = group; 38 | List subSystemList = group.getSubSystemList(); 39 | synchronized (subSystemList) { 40 | subSystemList.add(this); 41 | } 42 | } 43 | 44 | public abstract SubSystemType getType(); 45 | 46 | public void addCurrentTask() { 47 | addTask(Threads.getThreadId()); 48 | } 49 | 50 | public void addTask(int task) { 51 | setParameter(TASKS, task); 52 | taskList.add(task); 53 | } 54 | 55 | public void setEventControl(String eventFd, String controlFd, String... args) { 56 | StringBuilder sb = new StringBuilder(); 57 | sb.append(eventFd); 58 | sb.append(' '); 59 | sb.append(controlFd); 60 | for (String arg : args) { 61 | sb.append(' '); 62 | sb.append(arg); 63 | } 64 | 65 | setParameter(CGROUP_EVENT_CONTROL, sb); 66 | } 67 | 68 | public void setNotifyOnRelease(boolean flag) { 69 | int v = flag ? 1 : 0; 70 | setParameter(NOTIFY_ON_RELEASE, v); 71 | } 72 | 73 | public boolean isNotifyOnRelease() { 74 | return getIntParameter(NOTIFY_ON_RELEASE) > 0; 75 | } 76 | 77 | public void setReleaseAgent(String cmd) { 78 | setParameter(RELEASE_AGENT, cmd); 79 | } 80 | 81 | public String getReleaseAgent() { 82 | return getStringParameter(RELEASE_AGENT); 83 | } 84 | 85 | protected void setParameter(String parameterName, Object parameterValue) { 86 | group.setParameter(parameterName, parameterValue); 87 | } 88 | 89 | protected String getStringParameter(String parameterName) { 90 | return group.getParameter(parameterName); 91 | } 92 | 93 | protected long getLongParameter(String parameterName) { 94 | return Long.parseLong(getStringParameter(parameterName)); 95 | } 96 | 97 | protected int getIntParameter(String parameterName) { 98 | return Integer.parseInt(getStringParameter(parameterName)); 99 | } 100 | 101 | @Override 102 | public String toString() { 103 | return getType().name(); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/lealone/cgroup/subsystem/SubSystemType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | public enum SubSystemType { 21 | 22 | blkio(1), 23 | cpu(2), 24 | cpuacct(4), 25 | cpuset(8), 26 | devices(16), 27 | freezer(32), 28 | memory(64), 29 | net_cls(128), 30 | net_prio(256); 31 | 32 | public final int value; 33 | 34 | private SubSystemType(int value) { 35 | this.value = value; 36 | } 37 | 38 | public static String getTypeNames(int subSystems) { 39 | StringBuilder buff = new StringBuilder(); 40 | for (SubSystemType type : SubSystemType.values()) { 41 | if ((subSystems & type.value) != 0) { 42 | if (buff.length() != 0) 43 | buff.append(','); 44 | buff.append(type.name()); 45 | } 46 | } 47 | 48 | return buff.toString(); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/Example.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup; 19 | 20 | import org.lealone.cgroup.subsystem.SubSystemType; 21 | 22 | public class Example { 23 | 24 | private static Group root; 25 | private static Group g1; 26 | private static Group g2; 27 | 28 | public static void main(String[] args) { 29 | try { 30 | // 把cpu和cpuset两个子系统挂载到/cgroup/example 31 | root = Group.createRootGroup("example", SubSystemType.cpu, SubSystemType.cpuset); 32 | 33 | g1 = root.createSubGroup("g1"); // 创建子分组/cgroup/example/g1 34 | g2 = root.createSubGroup("g2"); // 创建子分组/cgroup/example/g2 35 | 36 | run(); 37 | 38 | } finally { 39 | if (root != null) 40 | root.umount(); 41 | } 42 | } 43 | 44 | private static void run() { 45 | g1.getCpuset().setCpus(0); 46 | g2.getCpuset().setCpus(0); 47 | 48 | g1.getCpuset().setMems(0); 49 | g2.getCpuset().setMems(0); 50 | 51 | g1.getCpu().setShares(512); 52 | g2.getCpu().setShares(2048); 53 | 54 | MyThread t1 = new MyThread(g1); 55 | MyThread t2 = new MyThread(g2); 56 | 57 | t1.start(); 58 | t2.start(); 59 | 60 | try { 61 | Thread.sleep(3000); 62 | t1.end(); 63 | t2.end(); 64 | t1.join(); 65 | t2.join(); 66 | } catch (InterruptedException e) { 67 | e.printStackTrace(); 68 | } 69 | } 70 | 71 | private static class MyThread extends Thread { 72 | private final Group g; 73 | private volatile boolean end; 74 | 75 | public MyThread(Group g) { 76 | super(g.getFullName()); 77 | this.g = g; 78 | setDaemon(true); 79 | } 80 | 81 | public void end() { 82 | end = true; 83 | } 84 | 85 | @Override 86 | public void run() { 87 | int id = Threads.getThreadId(); 88 | System.out.println("Thread id:" + id); 89 | g.getCpu().addTask(id); 90 | 91 | while (!end) 92 | ; 93 | } 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/BlkioTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | public class BlkioTest extends SubSystemTest { 26 | 27 | public BlkioTest() { 28 | super(SubSystemType.blkio); 29 | } 30 | 31 | @Test 32 | public void run() { 33 | testThrottle(); 34 | testWeight(); 35 | testCommonParameters(); 36 | } 37 | 38 | private void testThrottle() { 39 | Blkio blkio = g1.getBlkio(); 40 | 41 | Blkio.Record excepted = new Blkio.Record(8, 0, null, 200); 42 | blkio.setThrottleReadBpsDevice(excepted.major, excepted.minor, excepted.value); 43 | Blkio.Record actual = blkio.getThrottleReadBpsDevice()[0]; 44 | assertEquals(excepted, actual); 45 | 46 | blkio.setThrottleReadIopsDevice(excepted.major, excepted.minor, excepted.value); 47 | actual = blkio.getThrottleReadIopsDevice()[0]; 48 | assertEquals(excepted, actual); 49 | 50 | blkio.setThrottleWriteBpsDevice(excepted.major, excepted.minor, excepted.value); 51 | actual = blkio.getThrottleWriteBpsDevice()[0]; 52 | assertEquals(excepted, actual); 53 | 54 | blkio.setThrottleWriteIopsDevice(excepted.major, excepted.minor, excepted.value); 55 | actual = blkio.getThrottleWriteIopsDevice()[0]; 56 | assertEquals(excepted, actual); 57 | 58 | Blkio.Record[] records = root.getBlkio().getThrottleIoServiced(); 59 | assertTrue(records.length > 0); 60 | 61 | records = root.getBlkio().getThrottleIoServiceBytes(); 62 | assertTrue(records.length > 0); 63 | } 64 | 65 | private void testWeight() { 66 | Blkio blkio = g1.getBlkio(); 67 | blkio.setWeight(500); 68 | assertEquals(500, blkio.getWeight()); 69 | 70 | // TODO 71 | // blkio.setWeightDevice(8, 0, 500); 72 | // assertEquals("8:0 500", blkio.getWeightDevice()); 73 | } 74 | 75 | private void testCommonParameters() { 76 | Blkio blkio = root.getBlkio(); 77 | 78 | Blkio.Record[] records = blkio.getIoServiced(); 79 | assertTrue(records.length == 0); 80 | 81 | records = blkio.getIoServiceBytes(); 82 | assertTrue(records.length == 0); 83 | 84 | records = blkio.getIoServiceTime(); 85 | assertTrue(records.length == 0); 86 | 87 | records = blkio.getIoWaitTime(); 88 | assertTrue(records.length == 0); 89 | 90 | records = blkio.getIoMerged(); 91 | assertTrue(records.length == 0); 92 | 93 | records = blkio.getIoQueued(); 94 | assertTrue(records.length == 0); 95 | 96 | blkio.resetStats(0); 97 | 98 | records = blkio.getIoTime(); 99 | assertTrue(records.length == 0); 100 | 101 | records = blkio.getSectors(); 102 | assertTrue(records.length == 0); 103 | 104 | } 105 | 106 | // TODO 107 | // @Test 108 | public void testDebugParameters() { 109 | Blkio blkio = g1.getBlkio(); 110 | blkio.getAvgQueueSize(); 111 | blkio.getGroupWaitTime(); 112 | blkio.getEmptyTime(); 113 | blkio.getIdleTime(); 114 | blkio.getDequeue(); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/CpuTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import org.junit.Test; 23 | 24 | public class CpuTest extends SubSystemTest { 25 | 26 | public CpuTest() { 27 | super(SubSystemType.cpu); 28 | } 29 | 30 | @Test 31 | public void run() { 32 | Cpu cpu = g1.getCpu(); 33 | cpu.setShares(100); 34 | assertEquals(100, cpu.getShares()); 35 | 36 | cpu.setCfsPeriodUs(1000); 37 | assertEquals(1000, cpu.getCfsPeriodUs()); 38 | 39 | cpu.setCfsQuotaUs(1000); 40 | assertEquals(1000, cpu.getCfsQuotaUs()); 41 | 42 | cpu.setRtPeriodUs(100000); 43 | assertEquals(100000, cpu.getRtPeriodUs()); 44 | 45 | cpu.setRtRuntimeUs(1000); 46 | assertEquals(1000, cpu.getRtRuntimeUs()); 47 | 48 | Cpu.Stat stat = new Cpu.Stat("nr_periods 0\nnr_throttled 0\nthrottled_time 0"); 49 | assertEquals(0, stat.nrPeriods); 50 | assertEquals(0, stat.nrThrottled); 51 | assertEquals(0, stat.throttledTime); 52 | 53 | stat = cpu.getStat(); 54 | assertEquals(0, stat.nrPeriods); 55 | assertEquals(0, stat.nrThrottled); 56 | assertEquals(0, stat.throttledTime); 57 | 58 | // cpu.addTask(Threads.getThreadId()); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/CpuacctTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | public class CpuacctTest extends SubSystemTest { 26 | 27 | public CpuacctTest() { 28 | super(SubSystemType.cpuacct); 29 | } 30 | 31 | @Test 32 | public void run() { 33 | Cpuacct cpuacct = g1.getCpuacct(); 34 | assertEquals(0, cpuacct.getUsage()); 35 | 36 | cpuacct.resetUsage(); 37 | assertEquals(0, cpuacct.getUsage()); 38 | 39 | Cpuacct.Stat stat = cpuacct.getStat(); 40 | assertEquals(0, stat.userTime); 41 | assertEquals(0, stat.systemTime); 42 | 43 | long[] usages = cpuacct.getUsagePerCpu(); 44 | assertTrue(usages.length > 0); 45 | for (long usage : usages) { 46 | assertEquals(0, usage); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/CpusetTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertArrayEquals; 21 | import static org.junit.Assert.assertEquals; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import org.junit.Test; 25 | 26 | public class CpusetTest extends SubSystemTest { 27 | 28 | public CpusetTest() { 29 | super(SubSystemType.cpuset); 30 | } 31 | 32 | @Test 33 | public void run() { 34 | Cpuset cpuset = g1.getCpuset(); 35 | 36 | int[] excepted = { 0, 1 }; 37 | cpuset.setCpus(excepted); 38 | int[] actual = cpuset.getCpus(); 39 | assertArrayEquals(excepted, actual); 40 | 41 | int[] excepted2 = { 0 }; 42 | cpuset.setMems(excepted2); 43 | actual = cpuset.getMems(); 44 | assertArrayEquals(excepted2, actual); 45 | 46 | cpuset.setMemMigrate(true); 47 | boolean flag = cpuset.isMemMigrate(); 48 | assertTrue(flag); 49 | 50 | cpuset.setCpuExclusive(true); 51 | flag = cpuset.isCpuExclusive(); 52 | assertTrue(flag); 53 | 54 | cpuset.setMemExclusive(true); 55 | flag = cpuset.isMemExclusive(); 56 | assertTrue(flag); 57 | 58 | cpuset.setMemHardwall(true); 59 | flag = cpuset.isMemHardwall(); 60 | assertTrue(flag); 61 | 62 | int pressure = cpuset.getMemPressure(); 63 | assertTrue(pressure >= 0); 64 | 65 | root.getCpuset().setMemPressureEnabled(true); 66 | flag = root.getCpuset().isMemPressureEnabled(); 67 | assertTrue(flag); 68 | 69 | cpuset.setMemSpreadPage(true); 70 | flag = cpuset.isMemSpreadPage(); 71 | assertTrue(flag); 72 | 73 | cpuset.setMemSpreadSlab(true); 74 | flag = cpuset.isMemSpreadSlab(); 75 | assertTrue(flag); 76 | 77 | cpuset.setSchedLoadBlance(true); 78 | flag = cpuset.isSchedLoadBlance(); 79 | assertTrue(flag); 80 | 81 | cpuset.setSchedRelaxDomainLevel(0); 82 | int level = cpuset.getSchedRelaxDomainLevel(); 83 | assertEquals(0, level); 84 | 85 | String output = "0-1,3"; 86 | actual = Cpuset.parseNums(output); 87 | int[] excepted3 = { 0, 1, 3 }; 88 | assertArrayEquals(excepted3, actual); 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/DevicesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertTrue; 21 | 22 | import org.junit.Test; 23 | 24 | public class DevicesTest extends SubSystemTest { 25 | 26 | public DevicesTest() { 27 | super(SubSystemType.devices); 28 | } 29 | 30 | @Test 31 | public void run() { 32 | Devices devices = g1.getDevices(); 33 | 34 | Devices.Record record = new Devices.Record(Devices.TYPE_ALL, 8, 0, 2); 35 | devices.setAllow(record.type, record.major, record.minor, record.accesses); 36 | 37 | record = new Devices.Record(Devices.TYPE_ALL, 8, 0, 2); 38 | devices.setDeny(record.type, record.major, record.minor, record.accesses); 39 | 40 | Devices.Record[] records = root.getDevices().getList(); 41 | assertTrue(records.length > 0); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/FreezerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | 22 | import org.junit.Test; 23 | 24 | public class FreezerTest extends SubSystemTest { 25 | 26 | public FreezerTest() { 27 | super(SubSystemType.freezer); 28 | } 29 | 30 | @Test 31 | public void run() { 32 | g1.getFreezer().setState(Freezer.STATE_FROZEN); 33 | assertEquals(Freezer.STATE_FROZEN, g1.getFreezer().getState()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/MemoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.Test; 24 | 25 | public class MemoryTest extends SubSystemTest { 26 | 27 | public MemoryTest() { 28 | super(SubSystemType.memory); 29 | } 30 | 31 | @Test 32 | public void run() { 33 | Memory memory = g1.getMemory(); 34 | 35 | long excepted = 200704l; 36 | memory.setLimitInBytes(excepted); 37 | long actual = memory.getLimitInBytes(); 38 | assertEquals(excepted, actual); 39 | 40 | // excepted = 2 * 1024; 41 | // memory.setMemswLimitInBytes(excepted); 42 | // actual = memory.getMemswLimitInBytes(); 43 | // assertEquals(excepted, actual); 44 | 45 | excepted = 60; 46 | memory.setSwappiness((int) excepted); 47 | actual = memory.getSwappiness(); 48 | assertEquals(excepted, actual); 49 | 50 | memory.setUseHierarchy(true); 51 | assertTrue(memory.isUseHierarchy()); 52 | 53 | memory.setOomControl(true); 54 | assertTrue(memory.isOomControl()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/NetClsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertArrayEquals; 21 | 22 | import org.junit.Test; 23 | 24 | public class NetClsTest extends SubSystemTest { 25 | 26 | public NetClsTest() { 27 | super(SubSystemType.net_cls); 28 | } 29 | 30 | @Test 31 | public void run() { 32 | NetCls netCls = g1.getNetCls(); 33 | int[] excepted = { 10, 1 }; 34 | netCls.setClassId(excepted[0], excepted[1]); 35 | int[] actual = netCls.getClassId(); 36 | assertArrayEquals(excepted, actual); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/NetPrioTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertNotNull; 22 | 23 | import java.util.Map; 24 | 25 | import org.junit.Test; 26 | 27 | public class NetPrioTest extends SubSystemTest { 28 | 29 | public NetPrioTest() { 30 | super(SubSystemType.net_prio); 31 | } 32 | 33 | @Test 34 | public void run() { 35 | NetPrio netPrio = g1.getNetPrio(); 36 | assertNotNull(netPrio.getPrioId()); 37 | netPrio.addIfPrioMap("eth0", 2); 38 | Map ifPrioMap = netPrio.getIfPrioMap(); 39 | log.error(ifPrioMap + ""); 40 | assertEquals(ifPrioMap.get("eth0"), 2); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/lealone/cgroup/subsystem/SubSystemTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | package org.lealone.cgroup.subsystem; 19 | 20 | import static org.junit.Assert.assertEquals; 21 | import static org.junit.Assert.assertTrue; 22 | 23 | import org.junit.After; 24 | import org.junit.AfterClass; 25 | import org.junit.Before; 26 | import org.junit.BeforeClass; 27 | import org.lealone.cgroup.Group; 28 | import org.lealone.cgroup.Threads; 29 | import org.slf4j.Logger; 30 | import org.slf4j.LoggerFactory; 31 | 32 | public class SubSystemTest { 33 | 34 | protected Logger log; 35 | protected Group root; 36 | protected Group g1; 37 | 38 | private static ThreadLocal instance = new ThreadLocal<>(); 39 | 40 | public SubSystemTest() { 41 | } 42 | 43 | protected SubSystemTest(SubSystemType type) { 44 | log = LoggerFactory.getLogger(this.getClass()); 45 | 46 | root = Group.createRootGroup(type.name(), type); 47 | g1 = root.createSubGroup("g1"); 48 | 49 | instance.set(this); 50 | } 51 | 52 | @BeforeClass 53 | public static void setUpClass() { 54 | } 55 | 56 | @AfterClass 57 | public static void tearDownClass() { 58 | SubSystemTest test = instance.get(); 59 | instance.remove(); 60 | if (test != null) { 61 | test.root.umount(); 62 | } 63 | } 64 | 65 | @Before 66 | public void setUp() { 67 | } 68 | 69 | @After 70 | public void tearDown() { 71 | } 72 | 73 | // @Test 74 | public void testAddTask() { 75 | int tid = Threads.getThreadId(); 76 | g1.getCpu().addTask(tid); 77 | } 78 | 79 | // FIXME 80 | // @Test 81 | public void testSetEventControl() { 82 | } 83 | 84 | // FIXME 85 | // @Test 86 | public void testSetNotifyOnRelease() { 87 | g1.getCpu().setNotifyOnRelease(true); 88 | assertTrue(g1.getCpu().isNotifyOnRelease()); 89 | } 90 | 91 | // FIXME 92 | // @Test 93 | public void testSetReleaseAgent() { 94 | String excepted = "echo 0"; 95 | g1.getCpu().setReleaseAgent(excepted); 96 | String actual = g1.getCpu().getReleaseAgent(); 97 | assertEquals(actual, excepted); 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/test/resources/cgroup.properties: -------------------------------------------------------------------------------- 1 | sudo=false -------------------------------------------------------------------------------- /src/test/resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 19 | 20 | 21 | 22 | 23 | %-5level %date{HH:mm:ss.SSS} %msg%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | --------------------------------------------------------------------------------