class AmdgpuFan::Service

AmdgpuService

A service class for reading and interacting with AMD radeon graphics cards through the amdgpu Linux kernel driver.

Constants

BASE_FOLDER
FAN_MODES

Attributes

card_num[R]

Public Class Methods

new(card_num: 0) click to toggle source
# File lib/amdgpu_fan/service.rb, line 25
def initialize(card_num: 0)
  @card_num = card_num
end

Public Instance Methods

busy_percent() click to toggle source
# File lib/amdgpu_fan/service.rb, line 29
def busy_percent
  File.read("#{base_card_dir}/gpu_busy_percent").strip
end
connectors() click to toggle source
# File lib/amdgpu_fan/service.rb, line 33
def connectors
  @connectors ||= Connector.where card_num: card_num
end
core_clock() click to toggle source
# File lib/amdgpu_fan/service.rb, line 37
def core_clock
  clock_from_pp_file "#{base_card_dir}/pp_dpm_sclk"
end
display_names() click to toggle source
# File lib/amdgpu_fan/service.rb, line 41
def display_names
  connectors.map(&:display_name).compact
end
fan_mode() click to toggle source
# File lib/amdgpu_fan/service.rb, line 45
def fan_mode
  FAN_MODES[File.read(fan_mode_file).strip] || 'unknown'
end
fan_mode=(mode) click to toggle source

Set the fan mode to auto or manual.

# File lib/amdgpu_fan/service.rb, line 52
def fan_mode=(mode)
  sudo_write fan_mode_file, FAN_MODES.key(mode.to_s)
end
fan_speed=(value) click to toggle source

Set the fan speed to a percentage if <= 100 or a raw value

# File lib/amdgpu_fan/service.rb, line 59
def fan_speed=(value)
  if valid_fan_percent_speed?(value)
    new_raw = (value.to_f / 100 * fan_raw_speeds(:max).to_i).round
  elsif valid_fan_raw_speed?(value)
    new_raw = value
  end

  raise(self.class::Error, 'Invalid fan speed provided') if new_raw.to_s.empty?

  self.fan_mode = :manual unless fan_mode == 'manual'

  sudo_write fan_power_file, new_raw
end
fan_speed_percent() click to toggle source
# File lib/amdgpu_fan/service.rb, line 73
def fan_speed_percent
  (fan_speed_raw.to_f / fan_raw_speeds(:max).to_i * 100).round
end
fan_speed_rpm() click to toggle source

Return the fan speed in revolutions per minute

# File lib/amdgpu_fan/service.rb, line 80
def fan_speed_rpm
  File.read(fan_file(:input)).strip
end
memory_clock() click to toggle source

Return the current memory clock speed.

# File lib/amdgpu_fan/service.rb, line 87
def memory_clock
  clock_from_pp_file "#{base_card_dir}/pp_dpm_mclk"
end
memory_total() click to toggle source

Return the total memory on the card in bytes

# File lib/amdgpu_fan/service.rb, line 94
def memory_total
  File.read("#{base_card_dir}/mem_info_vram_total").to_i
end
model_id() click to toggle source
# File lib/amdgpu_fan/service.rb, line 98
def model_id
  @model_id ||= File.read(File.join(base_card_dir, '/subsystem_device')).gsub(/\A0x/, '').upcase
end
name() click to toggle source
# File lib/amdgpu_fan/service.rb, line 102
def name
  return 'Unknown' unless PCI.vendor_name(vendor_id)

  [PCI.vendor_name(vendor_id),
   PCI.device_name(vendor_id, device_id, subdevice_id) || 'unknown']
    .join(' ')
end
power_dpm_state() click to toggle source
# File lib/amdgpu_fan/service.rb, line 110
def power_dpm_state
  File.read("#{base_card_dir}/power_dpm_state").strip
end
power_draw() click to toggle source
# File lib/amdgpu_fan/service.rb, line 114
def power_draw
  power_raw_to_watts File.read(power_avg_file)
end
power_draw_percent() click to toggle source
# File lib/amdgpu_fan/service.rb, line 118
def power_draw_percent
  (power_draw.to_f / power_max.to_i * 100).round
end
power_max() click to toggle source
# File lib/amdgpu_fan/service.rb, line 122
def power_max
  @power_max ||= power_raw_to_watts File.read("#{base_hwmon_dir}/power1_cap")
end
profile_auto() click to toggle source
# File lib/amdgpu_fan/service.rb, line 126
def profile_auto
  sudo_write "#{base_card_dir}/power_dpm_force_performance_level", 'auto'
end
profile_force=(state) click to toggle source
# File lib/amdgpu_fan/service.rb, line 130
def profile_force=(state)
  sudo_write "#{base_card_dir}/power_dpm_force_performance_level", 'manual'
  sudo_write "#{base_card_dir}/pp_power_profile_mode", state
end
profile_mode() click to toggle source
# File lib/amdgpu_fan/service.rb, line 135
def profile_mode
  File.read("#{base_card_dir}/pp_power_profile_mode").slice(/\w+\s*+\*/).delete('*').strip
end
profile_summary() click to toggle source
# File lib/amdgpu_fan/service.rb, line 139
def profile_summary
  File.read("#{base_card_dir}/pp_power_profile_mode")
end
temperature() click to toggle source
# File lib/amdgpu_fan/service.rb, line 143
def temperature
  (File.read(temperature_file).to_f / 1000).round(1)
end
vbios_version() click to toggle source
# File lib/amdgpu_fan/service.rb, line 147
def vbios_version
  @vbios_version ||= File.read("#{base_card_dir}/vbios_version").strip
end

Private Instance Methods

base_card_dir() click to toggle source
# File lib/amdgpu_fan/service.rb, line 153
def base_card_dir
  @base_card_dir ||= "#{BASE_FOLDER}/card#{card_num}/device"
end
base_hwmon_dir() click to toggle source
# File lib/amdgpu_fan/service.rb, line 157
def base_hwmon_dir
  @base_hwmon_dir ||= Dir.glob("#{base_card_dir}/hwmon/hwmon*").first
end
clock_from_pp_file(file) click to toggle source
# File lib/amdgpu_fan/service.rb, line 161
def clock_from_pp_file(file)
  File.read(file).slice(/\w+(?= \*)/)
end
device_id() click to toggle source
# File lib/amdgpu_fan/service.rb, line 165
def device_id
  @device_id ||= File.read(File.join(base_card_dir, 'device')).to_i(16)
end
power_avg_file() click to toggle source
# File lib/amdgpu_fan/service.rb, line 169
def power_avg_file
  @power_avg_file ||= Dir.glob("#{base_card_dir}/**/power1_average").first
end
power_raw_to_watts(raw_string) click to toggle source
# File lib/amdgpu_fan/service.rb, line 173
def power_raw_to_watts(raw_string)
  (raw_string.strip.to_f / 1_000_000).round(2)
end
subdevice_id() click to toggle source
# File lib/amdgpu_fan/service.rb, line 177
def subdevice_id
  @subdevice_id ||= File.read(File.join(base_card_dir, 'subsystem_device')).to_i(16)
end
temperature_file() click to toggle source
# File lib/amdgpu_fan/service.rb, line 181
def temperature_file
  @temperature_file ||= Dir.glob("#{base_card_dir}/**/temp1_input").first
end
vendor_id() click to toggle source
# File lib/amdgpu_fan/service.rb, line 185
def vendor_id
  @vendor_id ||= File.read(File.join(base_card_dir, 'vendor')).to_i(16)
end