Class: AmdgpuFan::Service

Inherits:
Object
  • Object
show all
Includes:
Fan, SysWrite
Defined in:
lib/amdgpu_fan/service.rb

Overview

AmdgpuService

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

BASE_FOLDER =
'/sys/class/drm'
FAN_MODES =
{ '1' => 'manual', '2' => 'auto' }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(card_num: nil) ⇒ Service



29
30
31
# File 'lib/amdgpu_fan/service.rb', line 29

def initialize(card_num: nil)
  @card_num = card_num || self.class.card_numbers.first
end

Instance Attribute Details

#card_numObject (readonly)

Returns the value of attribute card_num.



21
22
23
# File 'lib/amdgpu_fan/service.rb', line 21

def card_num
  @card_num
end

Class Method Details

.card_numbersObject



25
26
27
# File 'lib/amdgpu_fan/service.rb', line 25

def self.card_numbers
  Dir["#{BASE_FOLDER}/card*"].filter_map { |path| path.split('/').last.slice(/(?<=card)\d+\Z/) }
end

Instance Method Details

#busy_percentObject



33
34
35
# File 'lib/amdgpu_fan/service.rb', line 33

def busy_percent
  File.read("#{base_card_dir}/gpu_busy_percent").strip.to_i
end

#connectorsObject



37
38
39
# File 'lib/amdgpu_fan/service.rb', line 37

def connectors
  @connectors ||= Connector.where card_num:
end

#core_clockObject



41
42
43
# File 'lib/amdgpu_fan/service.rb', line 41

def core_clock
  clock_from_pp_file "#{base_card_dir}/pp_dpm_sclk"
end

#display_namesObject



45
46
47
# File 'lib/amdgpu_fan/service.rb', line 45

def display_names
  connectors.map(&:display_name).compact
end

#fan_modeObject



49
50
51
# File 'lib/amdgpu_fan/service.rb', line 49

def fan_mode
  FAN_MODES[File.read(fan_mode_file).strip] || 'unknown'
end

#fan_mode=(mode) ⇒ Object

Set the fan mode to auto or manual.



56
57
58
# File 'lib/amdgpu_fan/service.rb', line 56

def fan_mode=(mode)
  sudo_write fan_mode_file, FAN_MODES.key(mode.to_s)
end

#fan_speed=(value) ⇒ Object

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

Raises:

  • (self.class::Error)


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

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_percentObject



77
78
79
# File 'lib/amdgpu_fan/service.rb', line 77

def fan_speed_percent
  (fan_speed_raw.to_f / fan_raw_speeds(:max).to_i * 100).round
end

#fan_speed_rpmObject

Return the fan speed in revolutions per minute



84
85
86
# File 'lib/amdgpu_fan/service.rb', line 84

def fan_speed_rpm
  File.read(fan_file(:input)).strip.to_i
end

#memory_clockObject

Return the current memory clock speed.



91
92
93
# File 'lib/amdgpu_fan/service.rb', line 91

def memory_clock
  clock_from_pp_file "#{base_card_dir}/pp_dpm_mclk"
end

#memory_totalObject

Return the total memory on the card in bytes



98
99
100
# File 'lib/amdgpu_fan/service.rb', line 98

def memory_total
  File.read("#{base_card_dir}/mem_info_vram_total").to_i
end

#model_idObject



102
103
104
# File 'lib/amdgpu_fan/service.rb', line 102

def model_id
  @model_id ||= File.read(File.join(base_card_dir, '/subsystem_device')).gsub(/\A0x/, '').upcase
end

#nameObject



106
107
108
109
110
111
112
# File 'lib/amdgpu_fan/service.rb', line 106

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

#performance_levelObject



134
135
136
# File 'lib/amdgpu_fan/service.rb', line 134

def performance_level
  File.read("#{base_card_dir}/power_dpm_force_performance_level").strip
end

#power_dpm_stateObject



117
118
119
# File 'lib/amdgpu_fan/service.rb', line 117

def power_dpm_state
  File.read("#{base_card_dir}/power_dpm_state").strip
end

#power_drawObject



121
122
123
# File 'lib/amdgpu_fan/service.rb', line 121

def power_draw
  power_raw_to_watts File.read(power_avg_file)
end

#power_draw_percentObject



125
126
127
# File 'lib/amdgpu_fan/service.rb', line 125

def power_draw_percent
  (power_draw.to_f / power_max.to_i * 100).round
end

#power_maxObject



129
130
131
# File 'lib/amdgpu_fan/service.rb', line 129

def power_max
  @power_max ||= power_raw_to_watts File.read("#{base_hwmon_dir}/power1_cap")
end

#profile_force=(state) ⇒ Object



142
143
144
145
# File 'lib/amdgpu_fan/service.rb', line 142

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_modeObject



147
148
149
# File 'lib/amdgpu_fan/service.rb', line 147

def profile_mode
  File.read("#{base_card_dir}/pp_power_profile_mode").slice(/\w+\s*+\*/).delete('*').strip
end

#profile_summaryObject



151
152
153
# File 'lib/amdgpu_fan/service.rb', line 151

def profile_summary
  File.read("#{base_card_dir}/pp_power_profile_mode")
end

#set_performance_level(profile_name = 'auto') ⇒ Object



138
139
140
# File 'lib/amdgpu_fan/service.rb', line 138

def set_performance_level(profile_name = 'auto')
  sudo_write "#{base_card_dir}/power_dpm_force_performance_level", profile_name
end

#temperatureObject



155
156
157
# File 'lib/amdgpu_fan/service.rb', line 155

def temperature
  (File.read(temperature_file).to_f / 1000).round(1)
end

#vbios_versionObject



159
160
161
# File 'lib/amdgpu_fan/service.rb', line 159

def vbios_version
  @vbios_version ||= File.read("#{base_card_dir}/vbios_version").strip
end