Class: OsCtl::Cli::Top::Measurement
- Inherits:
-
Object
- Object
- OsCtl::Cli::Top::Measurement
- Includes:
- CGroupParams
- Defined in:
- lib/osctl/cli/top/measurement.rb
Constant Summary
Constants included from CGroupParams
CGroupParams::CGPARAM_DEFAULT_FIELDS, CGroupParams::CGPARAM_FIELDS, CGroupParams::CGPARAM_FILTERS, CGroupParams::CGPARAM_STATS
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#group_path ⇒ Object
readonly
protected
Returns the value of attribute group_path.
-
#netifs ⇒ Object
readonly
protected
Returns the value of attribute netifs.
-
#subsystems ⇒ Object
readonly
protected
Returns the value of attribute subsystems.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #diff_from(other, mode) ⇒ Object
- #do_diff_from(other, mine, mode, delta) ⇒ Object protected
-
#initialize(subsystems, group_path, netifs) ⇒ Measurement
constructor
A new instance of Measurement.
- #measure ⇒ Object
- #netif_stats ⇒ Object protected
- #read_netif_stats(netif, dir, type) ⇒ Object protected
Methods included from CGroupParams
#cg_add_stats, #cg_blkio_stats, #cg_read_stats, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_memory_stat, #parse_subsystem, #read_cgparam, #read_memory_usage
Constructor Details
#initialize(subsystems, group_path, netifs) ⇒ Measurement
Returns a new instance of Measurement
10 11 12 13 14 15 |
# File 'lib/osctl/cli/top/measurement.rb', line 10 def initialize(subsystems, group_path, netifs) @subsystems = subsystems @group_path = group_path @netifs = netifs @data = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data
8 9 10 |
# File 'lib/osctl/cli/top/measurement.rb', line 8 def data @data end |
#group_path ⇒ Object (readonly, protected)
Returns the value of attribute group_path
41 42 43 |
# File 'lib/osctl/cli/top/measurement.rb', line 41 def group_path @group_path end |
#netifs ⇒ Object (readonly, protected)
Returns the value of attribute netifs
41 42 43 |
# File 'lib/osctl/cli/top/measurement.rb', line 41 def netifs @netifs end |
#subsystems ⇒ Object (readonly, protected)
Returns the value of attribute subsystems
41 42 43 |
# File 'lib/osctl/cli/top/measurement.rb', line 41 def subsystems @subsystems end |
#time ⇒ Object (readonly)
Returns the value of attribute time
8 9 10 |
# File 'lib/osctl/cli/top/measurement.rb', line 8 def time @time end |
Instance Method Details
#diff_from(other, mode) ⇒ Object
36 37 38 |
# File 'lib/osctl/cli/top/measurement.rb', line 36 def diff_from(other, mode) do_diff_from(other.data, data, mode, time - other.time) end |
#do_diff_from(other, mine, mode, delta) ⇒ Object (protected)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/osctl/cli/top/measurement.rb', line 43 def do_diff_from(other, mine, mode, delta) ret = {} other.each do |k, v| if v.is_a?(Hash) ret[k] = do_diff_from(v, mine[k], mode, delta) elsif %i(memory nproc).include?(k) ret[k] = mine[k] else if mode == :realtime ret[k] = ((mine[k] - v) / delta.to_f).round else ret[k] = mine[k] - v end ret[k] = 0 if ret[k] < 0 end end ret end |
#measure ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/osctl/cli/top/measurement.rb', line 17 def measure @time = Time.now data.update(cg_read_stats( subsystems, group_path, %i(cpu_time cpu_stat memory nproc), true )) data[:blkio] = cg_blkio_stats( subsystems, group_path, %i(bytes iops) ) data.update(netif_stats) end |
#netif_stats ⇒ Object (protected)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/osctl/cli/top/measurement.rb', line 67 def netif_stats ret = {tx: {bytes: 0, packets: 0}, rx: {bytes: 0, packets: 0}} netifs.each do |netif| next unless netif.veth %i(bytes packets).each do |type| # rx/tx are reversed within the container {rx: :tx, tx: :rx}.each do |host_dir, ct_dir| ret[ct_dir][type] = read_netif_stats(netif, host_dir, type) end end end ret end |
#read_netif_stats(netif, dir, type) ⇒ Object (protected)
84 85 86 87 88 89 90 |
# File 'lib/osctl/cli/top/measurement.rb', line 84 def read_netif_stats(netif, dir, type) ret = File.read("/sys/class/net/#{netif.veth}/statistics/#{dir}_#{type}") ret.strip.to_i rescue Errno::ENOENT 0 end |