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(path) ⇒ CGroup

Returns a new instance of CGroup.

Parameters:

  • path (String)

    cgroup name



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

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


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

def path
  @path
end

Instance Method Details

#abs_cgroup_path(*names) ⇒ Object (protected)



66
67
68
69
70
71
72
# File 'lib/osctl/exportfs/cgroup.rb', line 66

def abs_cgroup_path(*names)
  args = [FS]
  args << 'systemd' unless OsCtl::Lib::CGroup.v2?
  args << path
  args.concat(names)
  File.join(*args)
end

#create(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



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

def create(name)
  FileUtils.mkdir_p(abs_cgroup_path(name))
end

#destroy(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



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

def destroy(name)
  Dir.rmdir(abs_cgroup_path(name))
end

#enter(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



27
28
29
30
31
# File 'lib/osctl/exportfs/cgroup.rb', line 27

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/osctl/exportfs/cgroup.rb', line 35

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



54
55
56
57
58
59
# File 'lib/osctl/exportfs/cgroup.rb', line 54

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

#proc_list(name) ⇒ Object (protected)



62
63
64
# File 'lib/osctl/exportfs/cgroup.rb', line 62

def proc_list(name)
  abs_cgroup_path(name, 'cgroup.procs')
end