Class: OsCtld::Container::Hints

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/osctld/container/hints.rb

Defined Under Namespace

Classes: CpuDaily

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(ct, cpu_daily: nil) ⇒ Hints

Returns a new instance of Hints.

Parameters:



67
68
69
70
71
# File 'lib/osctld/container/hints.rb', line 67

def initialize(ct, cpu_daily: nil)
  init_lock
  @ct = ct
  @cpu_daily = cpu_daily || CpuDaily.new(user_us: 0, system_us: 0)
end

Instance Attribute Details

#cpu_dailyCpuDaily (readonly)

Returns:



64
65
66
# File 'lib/osctld/container/hints.rb', line 64

def cpu_daily
  @cpu_daily
end

#ctContainer (readonly)

Returns:



61
62
63
# File 'lib/osctld/container/hints.rb', line 61

def ct
  @ct
end

Class Method Details

.load(ct, cfg) ⇒ Object

Parameters:



51
52
53
54
55
56
# File 'lib/osctld/container/hints.rb', line 51

def self.load(ct, cfg)
  new(
    ct,
    cpu_daily: CpuDaily.load(cfg.fetch('cpu_daily', {}))
  )
end

Instance Method Details

#account_cpu_useObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctld/container/hints.rb', line 73

def 
  cg_reader = OsCtl::Lib::CGroup::PathReader.new(
    CGroup.subsystem_paths,
    ct.base_cgroup_path
  )

  vals = cg_reader.read_stats(%i[cpu_us cpu_user_us cpu_system_us], true)
  return if !vals[:cpu_us] || !vals[:cpu_user_us] || !vals[:cpu_system_us]

  begin
    st = File.stat(CGroup.abs_cgroup_path('cpuacct', ct.base_cgroup_path))
  rescue SystemCallError
    return
  end

  elapsed_time = Time.now - st.mtime

  exclusively do
    cpu_daily.update(vals[:cpu_user_us].raw, vals[:cpu_system_us].raw, elapsed_time)
  end
end

#dumpObject



95
96
97
98
99
# File 'lib/osctld/container/hints.rb', line 95

def dump
  {
    'cpu_daily' => cpu_daily.dump
  }
end

#dup(new_ct) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/osctld/container/hints.rb', line 101

def dup(new_ct)
  ret = super()
  ret.init_lock
  ret.instance_variable_set('@ct', new_ct)
  ret.instance_variable_set('@cpu_daily', cpu_daily.dup)
  ret
end