Class: Tracer::Var

Inherits:
Object show all
Defined in:
lib/redshift/util/tracer/var.rb

Overview

Represents the various options and controls associated with a variable being traced. The #trace attr holds the actual data. The Var object references the original object, and will therefore keep it from being GC-ed. If you want to retain the data without the object, keep only the #trace.

Constant Summary collapse

DEFAULT_TYPE =
"float"
DEFAULT_CHUNK_SIZE =
128
DEFAULT_PERIOD =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, name, opts = {}, &value_getter) ⇒ Var

The opts is a hash of the form:

{ :type => t, :chunk_size => s,:dims => nil or [d1,d2,...],
  :period => p }

Strings may be used instead of symbols as the keys.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/redshift/util/tracer/var.rb', line 70

def initialize key, name, opts={}, &value_getter
  @key, @name, @value_getter = key, name, value_getter

  @type = (opts[:type] || opts["type"] || DEFAULT_TYPE).to_s
  @chunk_size = opts[:chunk_size] || opts["chunk_size"] ||
    DEFAULT_CHUNK_SIZE
  @dims = opts[:dims] || opts["dims"] || []
  @period = opts[:period] || opts["period"] || DEFAULT_PERIOD

  @period = nil unless @period.kind_of?(Integer) and @period > 1
  @counter = 0
  @active = true
  @trace = Trace.new(
    :type => type, :chunk_size => chunk_size, :dims => dims)
end

Instance Attribute Details

#activeObject

Is this var currently recording? See Tracer#stop and Tracer#start.



31
32
33
# File 'lib/redshift/util/tracer/var.rb', line 31

def active
  @active
end

#chunk_sizeObject (readonly)

Chunk size is the size of the chunks (in values, not bytes) in which memory is allocated. Each chunk is a Vector; the list of chunks is a ruby array. This only needs to be adjusted for performance tuning.



23
24
25
# File 'lib/redshift/util/tracer/var.rb', line 23

def chunk_size
  @chunk_size
end

#counterObject

Counter to check for period expiration.



38
39
40
# File 'lib/redshift/util/tracer/var.rb', line 38

def counter
  @counter
end

#dimsObject (readonly)

Dimensions of each entry. A vlaue of nil means scalar entries, as does an empty array. A value of [d1, d2…] indicates multidimensional data.



28
29
30
# File 'lib/redshift/util/tracer/var.rb', line 28

def dims
  @dims
end

#keyObject (readonly)

The key can be either an object that has a named variable (accessed by the #name attr) or some arbitrary object (in which case a value_getter must be provided to compute the value).



49
50
51
# File 'lib/redshift/util/tracer/var.rb', line 49

def key
  @key
end

#nameObject (readonly)

The name of the variable to access in the object.



52
53
54
# File 'lib/redshift/util/tracer/var.rb', line 52

def name
  @name
end

#periodObject

How many calls to #run! before trace is updated? A period of 0, 1, or nil means trace is updated on each call.



35
36
37
# File 'lib/redshift/util/tracer/var.rb', line 35

def period
  @period
end

#traceObject (readonly)

Stores the trace as a list of Vectors



55
56
57
# File 'lib/redshift/util/tracer/var.rb', line 55

def trace
  @trace
end

#typeObject (readonly)

Type affects precision and storage size. Can be any of the following strings, which are the same as the NArray scalar types:

"byte"   ::   1 byte unsigned integer
"sint"   ::   2 byte signed integer
"int"    ::   4 byte signed integer
"sfloat" ::   single precision float
"float"  ::   double precision float


18
19
20
# File 'lib/redshift/util/tracer/var.rb', line 18

def type
  @type
end

#value_getterObject

Optional code to compute value, rather than use name. During #run!, the code will be called; if it accepts an arg, it will be passed the Var, from which #key and #name can be read. If code returns nil/false, no data is added to the trace.



44
45
46
# File 'lib/redshift/util/tracer/var.rb', line 44

def value_getter
  @value_getter
end

Instance Method Details

#run!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/redshift/util/tracer/var.rb', line 86

def run!
  return unless @active
  
  if @period
    @counter += 1
    return if @counter < @period
    @counter = 0
  end

  result =
    if (vg=@value_getter)
      case vg.arity
      when 0; vg.call
      when 1,-1; vg.call(self)
      else raise ArgumentError,
        "value_getter for #{@key}.#{@name} must take 0 or 1 arguments"
      end
    else
      @key.send @name
    end

  if result
    @trace << result
  end
end