Git Product home page Git Product logo

jcgroup's Introduction

jcgroup

Build Status Coverage Status

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

Subsystems

☑ blkio
☑ common
☑ cpu
☑ cpuacct
☑ cpuset
☑ devices
☑ freezer
☑ memory
☑ net_cls
☑ net_prio

Example

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

jcgroup_example_cpu

public class ExampleTest {

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

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

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

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

The wrapper requires a configuration file that defines the user and the password used to create the tasks. That user must be able to run sudo. You must add a user_conf file to the classpath, e.g. in the /src/main/resources or the /src/test/resources project folder, using the following format:

{
	"name" : "james",
	"password" : "bond"  
}

Requirements

  • Linux version (>= 2.6.18)
  • cgroups management tools. In Ubuntu or Debian, you may install the tools using: sudo apt-get install cgroup-bin

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

(http://www.apache.org/licenses/LICENSE-2.0)

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. See accompanying LICENSE file.

jcgroup's People

Contributors

haosdent avatar jaimechavarriaga avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.