class CacheXML::Builder

Attributes

block_size[RW]
dirty_percentage[R]
hint_width[R]
mapping_policy[R]
nr_cache_blocks[RW]
nr_mappings[R]
policy_name[RW]
uuid[RW]

Public Class Methods

new() click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 10
def initialize
  @uuid = ''
  @block_size = 128
  @nr_cache_blocks = 0
  @policy_name = 'mq'
  @mapping_policy = :random
  @nr_mappings = 0
  @dirty_percentage = 0
  @hint_width = 4
end

Public Instance Methods

dirty_percentage=(n) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 79
def dirty_percentage=(n)
  raise "invalid percentage (#{n})" if n < 0 || n > 100
  @dirty_percentage = n
end
generate() click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 21
def generate
  superblock = Superblock.new(@uuid, @block_size.to_i,
                              @nr_cache_blocks.to_i,
                              @policy_name, @hint_width)
  mappings = []

  case @mapping_policy
  when :linear
    cb = 0
    ob = safe_rand(@nr_cache_blocks - @nr_mappings)

    @nr_mappings.times do
      dirty = safe_rand(100) < @dirty_percentage
      mappings << Mapping.new(cb, ob, dirty)
      cb += 1
      ob += 1
    end

  when :random
    origin_blocks = []

    ob = 0
    @nr_mappings.times do
      origin_blocks << ob
      ob += 1
    end

    0.upto(@nr_mappings - 1) do |n|
      tmp = origin_blocks[n]

      index = n + rand(@nr_mappings - n)
      origin_blocks[n] = origin_blocks[index]
      origin_blocks[index] = tmp

      dirty = safe_rand(100) < @dirty_percentage
      mappings << Mapping.new(n, origin_blocks[n], dirty)
    end

  else
    raise "unknown mapping policy"
  end

  hints = []

  Metadata.new(superblock, mappings, hints)
end
hint_width=(n) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 84
def hint_width=(n)
  raise "invalid hint width (#{n})" if n > 128 || ((n % 4) != 0)
  @hint_width = n
end
mapping_policy=(sym) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 68
def mapping_policy=(sym)
  raise "mapping policy must be :random or :linear" unless valid_policy(sym)
  @mapping_policy = sym
end
nr_mappings=(n) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 73
def nr_mappings=(n)
  n = n.to_i
  raise "nr_mappings must not exceed nr_cache_blocks" if n > @nr_cache_blocks
  @nr_mappings = n
end

Private Instance Methods

safe_rand(n) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 94
def safe_rand(n)
  n == 0 ? 0 : rand(n)
end
valid_policy(sym) click to toggle source
# File lib/thinp_xml/cache/builder.rb, line 90
def valid_policy(sym)
  [:random, :linear].member?(sym)
end