Class: OsCtl::ExportFS::CGroup

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

Constant Summary collapse

FS =
'/sys/fs/cgroup'.freeze

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)



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

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
# File 'lib/osctl/exportfs/cgroup.rb', line 27

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



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

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
        # ignore
      end
    end
  end

  killed
end

#kill_all_until_empty(name) ⇒ Object

Parameters:

  • name (String)

    cgroup name



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

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

    sleep(3)
  end
end

#proc_list(name) ⇒ Object (protected)



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

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