Class: HKeyPerfDataReader::Reader

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/fluent/plugin/hkey_perf_data_reader.rb

Defined Under Namespace

Classes: NullLogger

Constant Summary

Constants included from Constants

Constants::ERROR_MORE_DATA, Constants::ERROR_SUCCESS, Constants::HKEY_PERFORMANCE_DATA, Constants::HKEY_PERFORMANCE_TEXT, Constants::PERF_NO_INSTANCES

Instance Method Summary collapse

Constructor Details

#initialize(object_name_whitelist: [], logger: nil) ⇒ Reader

Returns a new instance of Reader.



65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/hkey_perf_data_reader.rb', line 65

def initialize(object_name_whitelist: [], logger: nil)
  @raw_data = nil
  @is_little_endian = true
  @binary_parser = nil
  @counter_name_reader = CounterNameTableReader.new
  @object_name_whitelist = Set.new(object_name_whitelist)
  @logger = logger.nil? ? NullLogger.new : logger
end

Instance Method Details

#readObject



74
75
76
77
78
79
80
81
82
83
84
85
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
111
112
113
114
115
116
117
# File 'lib/fluent/plugin/hkey_perf_data_reader.rb', line 74

def read
  @raw_data = RawReader.read(@logger)
  # `littleEndian` flag in PerfDataBlock: https://docs.microsoft.com/en-us/windows/win32/api/winperf/ns-winperf-perf_data_block
  # Although we use this flag value, it will probably never be BigEndian, and we probably don't need to use this value.
  @is_little_endian = @raw_data[8..11].unpack("L")[0] == 1
  if @binary_parser.nil?
    @logger.trace("HKeyPerfData LittlEndian: #{@is_little_endian}")
    @binary_parser = BinaryParser.new(is_little_endian: @is_little_endian)
  end

  header = read_header
  @logger.trace("HKeyPerfData numObjectTypes: #{header.numObjectTypes}")

  unless header.signature == "PERF"
    @logger.error("Could not read HKeyPerfData. The header is invalid.")
    return {}
  end

  perf_objects = {}

  offset = header.headerLength
  header.numObjectTypes.times do
    perf_object, total_byte_length, success = read_perf_object(offset)
    unless success
      if total_byte_length.nil?
        @logger.trace("Can not continue. Stop reading.")
        break
      else
        @logger.trace("Skip this object and continue reading.")
        offset += total_byte_length
        next
      end
    end

    @logger.trace("Duplicate object name: #{perf_object.name}") if perf_objects.key?(perf_object.name)
    perf_objects[perf_object.name] = perf_object
    offset += total_byte_length
  end

  perf_objects
rescue => e
  @logger.error("Could not read HKeyPerfData. Message: #{e.message}")
  {}
end