Class: OsCtl::ExportFS::CGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/exportfs/cgroup.rb

Constant Summary collapse

FS =
'/sys/fs/cgroup'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, path) ⇒ CGroup

Returns a new instance of CGroup.

Parameters:

  • controller (String)
  • path (String)

    cgroup name



15
16
17
18
# File 'lib/osctl/exportfs/cgroup.rb', line 15

def initialize(controller, path)
  @controller = controller
  @path = path
end

Instance Attribute Details

#controllerString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/osctl/exportfs/cgroup.rb', line 8

def controller
  @controller
end

#pathString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/osctl/exportfs/cgroup.rb', line 11

def path
  @path
end

Instance Method Details

#create(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



21
22
23
# File 'lib/osctl/exportfs/cgroup.rb', line 21

def create(name)
  FileUtils.mkdir_p(File.join(FS, controller, path, name))
end

#destroy(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



26
27
28
# File 'lib/osctl/exportfs/cgroup.rb', line 26

def destroy(name)
  Dir.rmdir(File.join(FS, controller, path, name))
end

#enter(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



31
32
33
34
35
# File 'lib/osctl/exportfs/cgroup.rb', line 31

def enter(name)
  File.open(proc_list(name), 'w') do |f|
    f.write(Process.pid)
  end
end

#kill_all(name) ⇒ Integer

Returns number of killed processes.

Parameters:

  • name (String)

    cgroup name

Returns:

  • (Integer)

    number of killed processes



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/osctl/exportfs/cgroup.rb', line 39

def kill_all(name)
  killed = 0

  File.open(proc_list(name)) do |f|
    f.each_line do |line|
      pid = line.strip.to_i

      begin
        killed += 1
        Process.kill('TERM', pid)
      rescue Errno::ESRCH
      end
    end
  end

  killed
end

#kill_all_until_empty(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



58
59
60
61
62
63
# File 'lib/osctl/exportfs/cgroup.rb', line 58

def kill_all_until_empty(name)
  loop do
    break if kill_all(name) == 0
    sleep(3)
  end
end

#proc_list(name) ⇒ Object (protected)



66
67
68
# File 'lib/osctl/exportfs/cgroup.rb', line 66

def proc_list(name)
  File.join(FS, controller, path, name, 'cgroup.procs')
end