Class: OsCtl::Lib::Zfs::IOStat

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/zfs/iostat.rb

Defined Under Namespace

Classes: PoolStats

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pools: nil, interval: 1) ⇒ IOStat

Returns a new instance of IOStat.

Parameters:

  • pools (Array<String>, nil) (defaults to: nil)
  • interval (Integer) (defaults to: 1)


30
31
32
33
34
35
36
37
38
# File 'lib/libosctl/zfs/iostat.rb', line 30

def initialize(pools: nil, interval: 1)
  @pools = pools
  @interval = interval
  @current_stats = {}
  @accumulated_stats = {}
  @current_all = nil
  @accumulated_all = PoolStats.new(nil, nil, nil, 0, 0, 0, 0)
  @mutex = Mutex.new
end

Instance Attribute Details

#accumulated_statsObject (readonly, protected)

Returns the value of attribute accumulated_stats.



141
142
143
# File 'lib/libosctl/zfs/iostat.rb', line 141

def accumulated_stats
  @accumulated_stats
end

#current_statsObject (readonly, protected)

Returns the value of attribute current_stats.



141
142
143
# File 'lib/libosctl/zfs/iostat.rb', line 141

def current_stats
  @current_stats
end

#intervalInteger (readonly)

Returns:

  • (Integer)


26
27
28
# File 'lib/libosctl/zfs/iostat.rb', line 26

def interval
  @interval
end

#iostat_pidObject (readonly, protected)

Returns the value of attribute iostat_pid.



141
142
143
# File 'lib/libosctl/zfs/iostat.rb', line 141

def iostat_pid
  @iostat_pid
end

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



141
142
143
# File 'lib/libosctl/zfs/iostat.rb', line 141

def mutex
  @mutex
end

#poolsArray<String>

Returns:

  • (Array<String>)


23
24
25
# File 'lib/libosctl/zfs/iostat.rb', line 23

def pools
  @pools
end

#readerObject (readonly, protected)

Returns the value of attribute reader.



141
142
143
# File 'lib/libosctl/zfs/iostat.rb', line 141

def reader
  @reader
end

Instance Method Details

#accumulated_allPoolStats

Returns:



135
136
137
# File 'lib/libosctl/zfs/iostat.rb', line 135

def accumulated_all
  sync { @accumulated_all }
end

#accumulated_pool(pool) ⇒ PoolStats

Parameters:

  • pool (String)

Returns:



125
126
127
# File 'lib/libosctl/zfs/iostat.rb', line 125

def accumulated_pool(pool)
  sync { accumulated_stats[pool] }
end

#add_pool(pool) ⇒ Object

Parameters:

  • pool (String)


71
72
73
74
75
76
77
78
79
# File 'lib/libosctl/zfs/iostat.rb', line 71

def add_pool(pool)
  @pools ||= []
  return if pools.include?(pool)

  pools << pool
  stop if started?
  start
  nil
end

#current_allPoolStats

Returns:



130
131
132
# File 'lib/libosctl/zfs/iostat.rb', line 130

def current_all
  sync { @current_all }
end

#current_pool(pool) ⇒ PoolStats

Parameters:

  • pool (String)

Returns:



119
120
121
# File 'lib/libosctl/zfs/iostat.rb', line 119

def current_pool(pool)
  sync { current_stats[pool] }
end

#parser(r) ⇒ Object (protected)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/libosctl/zfs/iostat.rb', line 144

def parser(r)
  r.each_line do |line|
    pool, alloc, free, rio, wio, rbytes, wbytes = line.strip.split("\t")
    st = PoolStats.new(
      pool,
      alloc.to_i,
      free.to_i,
      rio.to_i,
      wio.to_i,
      rbytes.to_i,
      wbytes.to_i
    )

    sync do
      current_stats[pool] = st

      if accumulated_stats.has_key?(pool)
        accumulated_stats[pool] << st
      else
        accumulated_stats[pool] = st
      end

      @current_all = st
      @accumulated_all << st
    end
  end
rescue IOError
  # ignore
end

#remove_pool(pool) ⇒ Object

Parameters:

  • pool (String)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/libosctl/zfs/iostat.rb', line 82

def remove_pool(pool)
  return if pools.nil? || !pools.include?(pool)

  pools.delete(pool)
  return unless started?

  stop

  sync do
    current_stats.delete(pool)
    accumulated_stats.delete(pool)
  end

  start
  nil
end

#startObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libosctl/zfs/iostat.rb', line 40

def start
  return if pools.is_a?(Array) && pools.empty?

  args = ['zpool', 'iostat', '-Hpy']
  args.concat(pools) if pools
  args << interval.to_s

  r, w = IO.pipe
  @reader = Thread.new { parser(r) }
  @iostat_pid = Process.spawn(*args, out: w)
  w.close
end

#started?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/libosctl/zfs/iostat.rb', line 53

def started?
  !reader.nil?
end

#stopObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/libosctl/zfs/iostat.rb', line 57

def stop
  if iostat_pid
    Process.kill('TERM', iostat_pid)
    Process.wait(iostat_pid)
    @iostat_pid = nil
  end

  return unless reader

  reader.join
  @reader = nil
end

#syncObject (protected)



174
175
176
# File 'lib/libosctl/zfs/iostat.rb', line 174

def sync(&)
  mutex.synchronize(&)
end