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)


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

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.



143
144
145
# File 'lib/libosctl/zfs/iostat.rb', line 143

def accumulated_stats
  @accumulated_stats
end

#current_statsObject (readonly, protected)

Returns the value of attribute current_stats.



143
144
145
# File 'lib/libosctl/zfs/iostat.rb', line 143

def current_stats
  @current_stats
end

#intervalInteger (readonly)

Returns:

  • (Integer)


28
29
30
# File 'lib/libosctl/zfs/iostat.rb', line 28

def interval
  @interval
end

#iostat_pidObject (readonly, protected)

Returns the value of attribute iostat_pid.



143
144
145
# File 'lib/libosctl/zfs/iostat.rb', line 143

def iostat_pid
  @iostat_pid
end

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



143
144
145
# File 'lib/libosctl/zfs/iostat.rb', line 143

def mutex
  @mutex
end

#poolsArray<String>

Returns:

  • (Array<String>)


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

def pools
  @pools
end

#readerObject (readonly, protected)

Returns the value of attribute reader.



143
144
145
# File 'lib/libosctl/zfs/iostat.rb', line 143

def reader
  @reader
end

Instance Method Details

#accumulated_allPoolStats

Returns:



138
139
140
# File 'lib/libosctl/zfs/iostat.rb', line 138

def accumulated_all
  sync { @accumulated_all }
end

#accumulated_pool(pool) ⇒ PoolStats

Parameters:

  • pool (String)

Returns:



128
129
130
# File 'lib/libosctl/zfs/iostat.rb', line 128

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

#add_pool(pool) ⇒ Object

Parameters:

  • pool (String)


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

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

  pools << pool
  stop if started?
  start
  nil
end

#current_allPoolStats

Returns:



133
134
135
# File 'lib/libosctl/zfs/iostat.rb', line 133

def current_all
  sync { @current_all }
end

#current_pool(pool) ⇒ PoolStats

Parameters:

  • pool (String)

Returns:



122
123
124
# File 'lib/libosctl/zfs/iostat.rb', line 122

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

#parser(r) ⇒ Object (protected)



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
173
174
# File 'lib/libosctl/zfs/iostat.rb', line 146

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
end

#remove_pool(pool) ⇒ Object

Parameters:

  • pool (String)


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

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



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

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)


55
56
57
# File 'lib/libosctl/zfs/iostat.rb', line 55

def started?
  !reader.nil?
end

#stopObject



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

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

  if reader
    reader.join
    @reader = nil
  end
end

#syncObject (protected)



176
177
178
# File 'lib/libosctl/zfs/iostat.rb', line 176

def sync
  mutex.synchronize { yield }
end