Class: OsCtl::ExportFS::CGroup

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CGroup

Returns a new instance of CGroup.

Parameters:

  • path (String) —

    cgroup name



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

def initialize(path)
  @path = path
end

Instance Attribute Details

#path ⇒ String (readonly)

Returns:

  • (String)


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

def path
  @path
end

Class Method Details

.fs ⇒ String

Returns:

  • (String)


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

def self.fs
  OsCtl::Lib::CGroup.fs
end

Instance Method Details

#abs_cgroup_path(*names) ⇒ Object (protected)



70
71
72
73
74
75
76
# File 'lib/osctl/exportfs/cgroup.rb', line 70

def abs_cgroup_path(*names)
  args = [self.class.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



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

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

#destroy(name) ⇒ Object

Parameters:

  • name (String) —

    cgroup name



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

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

#enter(name) ⇒ Object

Parameters:

  • name (String) —

    cgroup name



30
31
32
# File 'lib/osctl/exportfs/cgroup.rb', line 30

def enter(name)
  File.write(proc_list(name), Process.pid)
end

#kill_all(name) ⇒ Integer

Returns number of killed processes.

Parameters:

  • name (String) —

    cgroup name

Returns:

  • (Integer) —

    number of killed processes



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

def kill_all(name)
  killed = 0

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

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

  killed
end

#kill_all_until_empty(name) ⇒ Object

Parameters:

  • name (String) —

    cgroup name



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

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)
  abs_cgroup_path(name, 'cgroup.procs')
end