Module: ComputeUnit

Defined in:
lib/compute_unit/device.rb,
lib/compute_unit.rb,
lib/compute_unit/cpu.rb,
lib/compute_unit/gpu.rb,
lib/compute_unit/asic.rb,
lib/compute_unit/utils.rb,
lib/compute_unit/logger.rb,
lib/compute_unit/version.rb,
lib/compute_unit/exceptions.rb,
lib/compute_unit/cache_store.rb,
lib/compute_unit/compute_base.rb,
lib/compute_unit/gpus/amd_gpu.rb,
lib/compute_unit/gpus/nvidia_gpu.rb

Overview

More information about sysfs can be found here - www.kernel.org/doc/Documentation/filesystems/sysfs-pci.txt

Defined Under Namespace

Modules: Exceptions, Logger, Utils Classes: AmdGpu, Asic, CacheStore, ComputeBase, Cpu, Device, Gpu, NvidiaGpu

Constant Summary collapse

CACHE_DIR =
File.join('/var', 'run', 'compute_unit_cache')
SYSFS_PATH =
ENV['SYSFS_PATH'] || '/sys'
SYS_DEVICE_PATH =
File.join(SYSFS_PATH, 'bus/pci/devices')
PCI_DATABASE_PATH =
File.join(File.dirname(__dir__), 'pci.ids')
PCI_DATABASE_URL =
'http://pci-ids.ucw.cz/v2.2/pci.ids'
DEFAULT_PCIDB_PATH =
'/usr/share/misc/pci.ids'
VERSION =
'0.5.1'

Class Method Summary collapse

Class Method Details

.copy_default_databaseObject

copies the default pci database from linux filesystem over to the gem path



51
52
53
# File 'lib/compute_unit.rb', line 51

def self.copy_default_database
  FileUtils.cp(DEFAULT_PCIDB_PATH, PCI_DATABASE_PATH) if File.exist?(DEFAULT_PCIDB_PATH)
end

.device_paths_by_process(pid) ⇒ Array

Returns - a array of device paths.

Examples:

usage

device_paths_by_process(3748) => ["/sys/bus/pci/devices/0000:00:00.0"]

Parameters:

  • pid (Integer)

    the pid to search with

  • field (Symbol)
    • the field to sort by

Returns:

  • (Array)
    • a array of device paths



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

def self.device_paths_by_process(pid)
  # processes = ComputeUnit::Cpu.attached_processes(field).last(1) + ComputeUnit::Gpu.attached_processes(field)
  # processes.find_all { |process| process.pid == pid }
  # We can get the utilized devices but it appears cubersome to convert to a device path
  # This method uses more resources
  find_by_process(pid).map(&:device_path)
end

.find_all(use_opencl = false) ⇒ Array

Returns - return a list of compute units.

Parameters:

  • use_opencl (Boolean) (defaults to: false)

Returns:

  • (Array)
    • return a list of compute units



18
19
20
21
22
23
24
25
26
27
# File 'lib/compute_unit.rb', line 18

def self.find_all(use_opencl = false)
  require 'compute_unit/gpu'
  require 'compute_unit/cpu'
  require 'compute_unit/asic'

  # if this file doesn't exist we need to fetch it or copy it
  refresh_pci_database unless File.exist?(PCI_DATABASE_PATH)

  Gpu.find_all(use_opencl) + Cpu.find_all + Asic.find_all
end

.find_all_with_database(use_opencl = false) ⇒ Array

get a fresh copy of the database and then use find_all

Parameters:

  • use_opencl (Boolean) (defaults to: false)

Returns:

  • (Array)
    • return a list of compute units



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

def self.find_all_with_database(use_opencl = false)
  refresh_pci_database
  find_all(use_opencl)
end

.find_by_process(pid, use_opencl = false) ⇒ Object

Parameters:

  • pid (Integer)

    the pid to search with

  • use_opencl (Boolean) (defaults to: false)

    use opencl on gpu devices



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

def self.find_by_process(pid, use_opencl = false)
  find_all(use_opencl).find_all do |unit|
    unit.top_processes.first.pid.to_i == pid.to_i
  end
end

.refresh_pci_databaseObject

downloads the pci database



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/compute_unit.rb', line 64

def self.refresh_pci_database
  ComputeUnit::Utils.check_for_root
  require 'net/http'
  require 'uri'
  uri = URI.parse(PCI_DATABASE_URL)
  response = Net::HTTP.get_response(uri)
  # I can't write to it unless it has correct permissions
  File.chmod(0o644, PCI_DATABASE_PATH) if File.exist?(PCI_DATABASE_PATH)
  File.write(PCI_DATABASE_PATH, response.body) if response.code == '200'
  File.chmod(0o644, PCI_DATABASE_PATH)
  PCI_DATABASE_PATH
end