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

Instance Method Summary collapse

Constructor Details

#initialize(card_num: 0) ⇒ Service

Returns a new instance of Service.



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

def initialize(card_num: 0)
  @card_num = card_num
end

Instance Attribute Details

#card_numObject (readonly)

Returns the value of attribute card_num.



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

def card_num
  @card_num
end

Instance Method Details

#busy_percentObject



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

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

#connectorsObject



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

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

#core_clockObject



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

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

#display_namesObject



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

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

#fan_modeObject



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

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

#fan_mode=(mode) ⇒ Object



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

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

#fan_speed=(value) ⇒ Object

Raises:

  • (self.class::Error)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/amdgpu_fan/service.rb', line 52

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



66
67
68
# File 'lib/amdgpu_fan/service.rb', line 66

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

#fan_speed_rpmObject



70
71
72
# File 'lib/amdgpu_fan/service.rb', line 70

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

#memory_clockObject



74
75
76
# File 'lib/amdgpu_fan/service.rb', line 74

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

#memory_totalObject



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

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

#nameObject



82
83
84
# File 'lib/amdgpu_fan/service.rb', line 82

def name
  lspci_subsystem.split(': ')[1].strip
end

#power_dpm_stateObject



86
87
88
# File 'lib/amdgpu_fan/service.rb', line 86

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

#power_drawObject



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

def power_draw
  power_raw_to_watts File.read(power_avg_file)
end

#power_draw_percentObject



94
95
96
# File 'lib/amdgpu_fan/service.rb', line 94

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

#power_maxObject



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

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

#profile_autoObject



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

def profile_auto
  sudo_write "#{base_card_dir}/power_dpm_force_performance_level", 'auto'
end

#profile_force=(state) ⇒ Object



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

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



111
112
113
# File 'lib/amdgpu_fan/service.rb', line 111

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

#profile_summaryObject



115
116
117
# File 'lib/amdgpu_fan/service.rb', line 115

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

#temperatureObject



119
120
121
# File 'lib/amdgpu_fan/service.rb', line 119

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

#vbios_versionObject



123
124
125
# File 'lib/amdgpu_fan/service.rb', line 123

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