Class: Win32::Pdh::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/win32/pdh/counter.rb

Overview

Class representing an individual counter.

This won’t usually be created directly, but built using Query#add_counter.

Defined Under Namespace

Classes: PDH_COUNTER_INFO, PDH_COUNTER_PATH_ELEMENTS, PDH_FMT_COUNTERVALUE, PDH_FMT_COUNTERVALUE_VALUE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query:, path:) ⇒ Counter

Initializes the counter from a query and a path. This usually won’t be called directly, but generated from Query#add_counter



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/win32/pdh/counter.rb', line 66

def initialize(query:, path:)
  path = (path + "\0").encode('UTF-16LE')
  handle_pointer = FFI::MemoryPointer.new(:pointer)
  status = PdhFFI.PdhAddCounterW(
    query,
    path,
    FFI::Pointer::NULL,
    handle_pointer,
  )
  Pdh.check_status status
  @handle = handle_pointer.read_pointer

  # Make sure we don't leak when we throw an exception
  begin
    load_info
  rescue
    remove
    raise
  end
end

Instance Attribute Details

#counter_nameObject

Returns the value of attribute counter_name.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def counter_name
  @counter_name
end

#default_scaleObject

Returns the value of attribute default_scale.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def default_scale
  @default_scale
end

#explain_textObject

Returns the value of attribute explain_text.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def explain_text
  @explain_text
end

#full_pathObject

Returns the value of attribute full_path.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def full_path
  @full_path
end

#instance_indexObject

Returns the value of attribute instance_index.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def instance_index
  @instance_index
end

#instance_nameObject

Returns the value of attribute instance_name.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def instance_name
  @instance_name
end

#machine_nameObject

Returns the value of attribute machine_name.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def machine_name
  @machine_name
end

#object_nameObject

Returns the value of attribute object_name.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def object_name
  @object_name
end

#parent_instanceObject

Returns the value of attribute parent_instance.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def parent_instance
  @parent_instance
end

#scaleObject

Returns the value of attribute scale.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def scale
  @scale
end

#statusObject

Returns the value of attribute status.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def status
  @status
end

#typeObject

Returns the value of attribute type.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def type
  @type
end

#versionObject

Returns the value of attribute version.



61
62
63
# File 'lib/win32/pdh/counter.rb', line 61

def version
  @version
end

Instance Method Details

#get(format) ⇒ Object

Get the PDH_FMT_COUNTERVALUE_VALUE given the format, checking status and raising an exception if necessary.

Usually, you won’t use this directly; you’d use #get_double, #get_long, or #get_large to get the formatted value without any hassle.

Will raise a PdhError if the value is bad (if you’ve only run Query#collect_query_data once, but this counter requires a history to calculate a rate, this will raise an exception. Be careful, and probably wrap all calls to this in an exception block that takes into account the possibility of a counter failing to get a value)



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/win32/pdh/counter.rb', line 151

def get(format)
  value = PDH_FMT_COUNTERVALUE.new
  status = PdhFFI.PdhGetFormattedCounterValue(
    @handle,
    format,
    FFI::Pointer::NULL,
    value,
  )
  Pdh.check_status status
  Pdh.check_status value[:CStatus]
  value[:value]
end

#get_doubleObject

Get value as a double.

This uses #get, so read the notes in there about exception raising.



168
169
170
# File 'lib/win32/pdh/counter.rb', line 168

def get_double
  get(Constants::PDH_FMT_DOUBLE)[:doubleValue]
end

#get_largeObject

Get value as a 64-bit integer.

This uses #get, so read the notes in there about exception raising.



176
177
178
# File 'lib/win32/pdh/counter.rb', line 176

def get_large
  get(Constants::PDH_FMT_LARGE)[:largeValue]
end

#get_longObject

Get value as a 32-bit integer

This uses #get, so read the notes in there about exception raising.



184
185
186
# File 'lib/win32/pdh/counter.rb', line 184

def get_long
  get(Constants::PDH_FMT_LONG)[:longValue]
end

#removeObject Also known as: close

Remove the counter from its Query.

This isn’t necessary as a part of general cleanup, as closing a Query will also clean up all counters. This is necessary if you are doing long-term management that may need counters added and removed while running. So if you’re using a Query as a one-off and it is being closed after use, don’t bother with this. If you are keeping a single query open long-term, make sure you use this when necessary, otherwise you will leak memory.



97
98
99
100
101
102
103
104
# File 'lib/win32/pdh/counter.rb', line 97

def remove
  # Only allow removing once
  unless @handle.nil?
    status = PdhFFI.PdhRemoveCounter(@handle) unless @handle.nil?
    Pdh.check_status status
    @handle = nil
  end
end