Class: ComputeUnit::ComputeBase

Inherits:
Device
  • Object
show all
Includes:
Formatters, Logger, Sys
Defined in:
lib/compute_unit/compute_base.rb

Direct Known Subclasses

Asic, Cpu, Gpu

Constant Summary collapse

CACHE_TIMEOUT =

timeout value

30

Constants inherited from Device

Device::PROC_PATH, Device::SYSFS_DEVICES_PATH

Instance Attribute Summary collapse

Attributes inherited from Device

#device_class_id, #device_id, #device_path, #device_vendor_id, #make, #model, #subsystem_device_id, #subsystem_vendor_id, #vendor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

color, log_file, log_level, logger, #logger

Methods included from Formatters

#value_micro_formatter

Methods inherited from Device

#base_hwmon_path, create_from_path, device, device_class, device_lookup, device_vendor, find_all, #generic_model, #hwmon_path, #lock_rom, logger, manual_device_database, manual_device_lookup, manual_vendor_lookup, manual_vendors, name_map, name_translation, pci_database, #read_file, #read_hwmon_data, read_kernel_setting, #read_kernel_setting, #rom_data, #rom_path, subsystem_device, subsystem_device_lookup, subsystem_vendor, subsystem_vendor_lookup, #sysfs_model_name, system_checksum, #to_h, #to_json, #unlock_rom, vendor_lookup, #write_hwmon_data, #write_kernel_setting, write_kernel_setting

Methods included from Utils

check_for_root, #root?, root?

Constructor Details

#initialize(device_path, opts = {}) ⇒ ComputeBase

Returns a new instance of ComputeBase.



58
59
60
61
# File 'lib/compute_unit/compute_base.rb', line 58

def initialize(device_path, opts = {})
  super
  @timestamp = Time.now.to_i
end

Instance Attribute Details

#compute_typeObject (readonly)

Returns the value of attribute compute_type.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def compute_type
  @compute_type
end

#indexObject (readonly)

Returns the value of attribute index.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def index
  @index
end

#metaObject (readonly)

Returns the value of attribute meta.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def meta
  @meta
end

#power_offsetObject

Returns the value of attribute power_offset.



14
15
16
# File 'lib/compute_unit/compute_base.rb', line 14

def power_offset
  @power_offset
end

#serialObject (readonly)

Returns the value of attribute serial.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def serial
  @serial
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def type
  @type
end

#uuidObject (readonly)

Returns the value of attribute uuid.



13
14
15
# File 'lib/compute_unit/compute_base.rb', line 13

def uuid
  @uuid
end

Class Method Details

.compute_classesArray

Returns - find all the decendants of thyself.

Returns:

  • (Array)
    • find all the decendants of thyself



23
24
25
26
27
28
29
30
# File 'lib/compute_unit/compute_base.rb', line 23

def self.compute_classes
  ObjectSpace.each_object(Class).select do |klass|
    # <Class:#<Crossbelt::ComputeUnit::NvidiaGpu:0x00007fddc5c02a10>>
    #  We have to filter out these kinds of Ojbects as they don't respond to :new
    #  without a singleton error.
    klass < self && !klass.to_s.include?('Class')
  end
end

.devicesArray

Note:

there is not a filter applied

Returns - an array of pci bus device locations (every device on the pci bus).

Returns:

  • (Array)
    • an array of pci bus device locations (every device on the pci bus)



69
70
71
# File 'lib/compute_unit/compute_base.rb', line 69

def self.devices
  Dir.glob(File.join(ComputeUnit::Device::SYSFS_DEVICES_PATH, '*'))
end

Instance Method Details

#attached_processes(field = :pctcpu, filter = nil) ⇒ Array

param filter [Regex] - if supplied filter out devices from fd list

Parameters:

  • field (Symbol) (defaults to: :pctcpu)
    • the field to sort by

Returns:

  • (Array)
    • an array of attached processes

Raises:

  • (NotImplementedError)


36
37
38
39
40
# File 'lib/compute_unit/compute_base.rb', line 36

def attached_processes(field = :pctcpu, filter = nil)
  raise NotImplementedError unless self.class.respond_to?(:attached_processes)

  self.class.attached_processes(field, filter)
end

#device_class_nameObject



63
64
65
# File 'lib/compute_unit/compute_base.rb', line 63

def device_class_name
  self.class.const_get('DEVICE_CLASS_NAME')
end

#experimental_on?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/compute_unit/compute_base.rb', line 77

def experimental_on?
  unless ENV['XB_EXPERIMENTAL'].to_s == '1'
    logger.warn('You must set environment variable XB_EXPERIMENTAL=1 to use this feature')
    return false
  end
  true
end

#expired_metadata?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/compute_unit/compute_base.rb', line 73

def expired_metadata?
  (timestamp + CACHE_TIMEOUT) < Time.now.to_i
end

#micro_formatter(item, add_unit = false) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/compute_unit/compute_base.rb', line 85

def micro_formatter(item, add_unit = false)
  data = {}
  item.each do |key, value|
    if %i[hourly_cost hourly_earnings kwh_cost].include?(key)
      v = (value * 1_000_000).round(4)
      data[key] = add_unit ? "#{v} \u00B5BTC" : v
    end
  end
  item.merge(data)
end

#top_processes(x = 1, field = :pctcpu) ⇒ Array

Returns - an array of attached processes.

Parameters:

  • x (Integer) (defaults to: 1)

    the number of processes to return, defaults to 1

  • field (Symbol) (defaults to: :pctcpu)
    • the field to sort by

Returns:

  • (Array)
    • an array of attached processes



46
47
48
# File 'lib/compute_unit/compute_base.rb', line 46

def top_processes(x = 1, field = :pctcpu)
  attached_processes(field).last(x)
end