Class: OsCtl::Lib::Zfs::ZpoolTransactionGroups

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

Overview

Read interface to /proc/spl/kstat/zfs/<pool>/txgs

Defined Under Namespace

Classes: TransactionGroup, TransactionGroupList

Constant Summary collapse

PROC_PATH =
'/proc/spl/kstat/zfs'.freeze
STATES =
{
  'B' => :birth,
  'O' => :open,
  'Q' => :quiesced,
  'W' => :wait_for_sync,
  'S' => :synced,
  'C' => :committed,
  '?' => :unknown
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pools: []) ⇒ ZpoolTransactionGroups

Returns a new instance of ZpoolTransactionGroups.

Parameters:

  • pools (Array<String>) (defaults to: [])


142
143
144
145
146
147
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 142

def initialize(pools: [])
  read_pools = pools.empty? ? list_pools : pools
  paths = read_pools.to_h { |pool| [pool, txgs_path(pool)] }

  @pools = read_paths(paths)
end

Instance Attribute Details

#poolsHash<String, TransactionGroupList> (readonly)

Returns:



139
140
141
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 139

def pools
  @pools
end

Instance Method Details

#each {|pool, transaction| ... } ⇒ Object

Yield Parameters:



151
152
153
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 151

def each(&)
  @pools.each(&)
end

#list_poolsObject (protected)



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 202

def list_pools
  ret = []

  Dir.entries(PROC_PATH).each do |f|
    next if %w[. ..].include?(f)
    next unless Dir.exist?(File.join(PROC_PATH, f))

    ret << f
  end

  ret
end

#read_file(pool, txgs_path, uptime) ⇒ Object (protected)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 168

def read_file(pool, txgs_path, uptime)
  ret = TransactionGroupList.new

  File.open(txgs_path) do |f|
    it = f.each_line
    it.next # skip the header
    it.each do |line|
      values = line.strip.split

      birth_ns = values[1].to_i
      birth_time = uptime.booted_at + (birth_ns / 1_000_000_000.to_f)

      ret << TransactionGroup.new(
        pool:,
        txg: values[0].to_i,
        birth_time:,
        birth_ns:,
        state: STATES[values[2]],
        ndirty: values[3].to_i,
        nread: values[4].to_i,
        nwritten: values[5].to_i,
        reads: values[6].to_i,
        writes: values[7].to_i,
        otime_ns: values[8].to_i,
        qtime_ns: values[9].to_i,
        wtime_ns: values[10].to_i,
        stime_ns: values[11].to_i
      )
    end
  end

  ret
end

#read_paths(txgs_paths) ⇒ Object (protected)



157
158
159
160
161
162
163
164
165
166
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 157

def read_paths(txgs_paths)
  ret = {}
  uptime = Uptime.new

  txgs_paths.each do |pool, txgs_path|
    ret[pool] = read_file(pool, txgs_path, uptime)
  end

  ret
end

#txgs_path(pool) ⇒ Object (protected)



215
216
217
# File 'lib/libosctl/zfs/zpool_transaction_groups.rb', line 215

def txgs_path(pool)
  File.join(PROC_PATH, pool, 'txgs')
end