Class: AmdgpuFan::Cli

Inherits:
Thor
  • Object
show all
Includes:
CliOutputFormat
Defined in:
lib/amdgpu_fan/cli.rb

Overview

The command-line interface class

Constant Summary collapse

ICONS =
YAML.safe_load_file(File.join(__dir__, '../../config/icons.yml'))
.transform_keys(&:to_sym).freeze
WATCH_FIELD_SEPARATOR =
' | '

Constants included from CliOutputFormat

AmdgpuFan::CliOutputFormat::METER_CHARS, AmdgpuFan::CliOutputFormat::TIME_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliOutputFormat

#current_time, #percent_meter, #radeon_logo

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/amdgpu_fan/cli.rb', line 16

def self.exit_on_failure?
  true
end

Instance Method Details

#connectorsObject



21
22
23
24
25
26
# File 'lib/amdgpu_fan/cli.rb', line 21

def connectors
  amdgpu_service.connectors.each do |connector|
    puts "#{connector.type} #{connector.index}:\t" +
         (connector.connected? ? connector.display_name : connector.status)
  end
end

#fanObject



61
62
63
# File 'lib/amdgpu_fan/cli.rb', line 61

def fan
  puts fan_status
end

#fan_set(value) ⇒ Object



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

def fan_set(value)
  if value.strip.casecmp('auto').zero?
    amdgpu_service.fan_mode = :auto
  else
    return puts 'Invalid percentage' unless (0..100).cover?(value.to_i)

    amdgpu_service.fan_speed = value
  end
  puts fan_status
end

#power_mode_autoObject



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

def power_mode_auto
  amdgpu_service.set_performance_level('auto')
  puts oneline_power_mode
end

#power_mode_highObject



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

def power_mode_high
  amdgpu_service.set_performance_level('high')
  puts oneline_power_mode
end

#power_mode_lowObject



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

def power_mode_low
  amdgpu_service.set_performance_level('low')
  puts oneline_power_mode
end

#profileObject



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

def profile
  puts amdgpu_service.profile_summary
end

#profile_force(state) ⇒ Object



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

def profile_force(state)
  amdgpu_service.profile_force = state
  puts amdgpu_service.profile_summary
end

#status(option = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amdgpu_fan/cli.rb', line 78

def status(option = nil)
  puts  if option == '--logo'
  puts ICONS[:gpu] + ' GPU:'.ljust(11) + amdgpu_service.name
  puts ICONS[:vbios] + ' vBIOS:'.ljust(11) + amdgpu_service.vbios_version
  puts "#{ICONS[:display]} Displays: #{amdgpu_service.display_names.join(', ')}"
  puts ICONS[:clock] + ' Clocks:'.ljust(11) + clock_status
  puts ICONS[:memory] + ' Memory:'.ljust(11) + mem_total_mibibyes
  puts ICONS[:fan] + ' Fan:'.ljust(11) + fan_status
  puts ICONS[:temp] + ' Temp:'.ljust(11) + "#{amdgpu_service.temperature}°C"
  puts ICONS[:power] + ' Power:'.ljust(11) +
       "#{amdgpu_service.profile_mode} profile in " \
       "#{amdgpu_service.performance_level} mode using " \
       "#{amdgpu_service.power_draw} / #{amdgpu_service.power_max} Watts " \
       "(#{amdgpu_service.power_draw_percent}%)"
  puts ICONS[:load] + ' Load:'.ljust(11) + percent_meter(amdgpu_service.busy_percent, 12)
end

#versionObject



96
97
98
# File 'lib/amdgpu_fan/cli.rb', line 96

def version
  puts AmdgpuFan::VERSION
end

#watch(seconds = 1) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/amdgpu_fan/cli.rb', line 102

def watch(seconds = 1)
  return puts 'Seconds must be from 1 to 600' unless (1..600).cover?(seconds.to_i)

  puts "Watching #{amdgpu_service.name} every #{seconds} second(s)...",
       '  <Press Ctrl-C to exit>'

  trap 'SIGINT' do
    puts "\nAnd now the watch is ended."
    exit 0
  end

  loop do
    time = Time.now
    puts [time.strftime('%F %T'), summary_clock, summary_fan, summary_load, summary_power,
          summary_temp].join(WATCH_FIELD_SEPARATOR)

    # It can take a second or two to run the above so we remove them from the wait
    # here to get a more consistant watch interval.
    sec_left_to_wait = time.to_i + seconds.to_i - Time.now.to_i
    sleep sec_left_to_wait if sec_left_to_wait.positive?
  end
end

#watch_avgObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/amdgpu_fan/cli.rb', line 129

def watch_avg
  puts "Watching #{amdgpu_service.name} min, max and averges since #{Time.now}...",
       '  <Press Ctrl-C to exit>',
       "\n\n\n\n\n"

  trap 'SIGINT' do
    puts "\nAnd now the watch is ended."
    exit 0
  end

  watcher = Watcher.new amdgpu_service

  loop do
    watcher.measure
    6.times { print "\033[K\033[A" } # move up a line and clear to end of line

    puts "#{ICONS[:load]} Load        #{watcher.busy_percent}",
         "#{ICONS[:clock]} Core clock  #{watcher.core_clock}",
         "#{ICONS[:memory]} Memory clk  #{watcher.memory_clock}",
         "#{ICONS[:fan]} Fan speed   #{watcher.fan_speed_rpm}",
         "#{ICONS[:power]} Power usage #{watcher.power_draw}",
         "#{ICONS[:temp]} Temperature #{watcher.temperature}"
    sleep 1
  end
end

#watch_csv(seconds = 1) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/amdgpu_fan/cli.rb', line 157

def watch_csv(seconds = 1)
  return puts 'Seconds must be from 1 to 600' unless (1..600).cover?(seconds.to_i)

  puts 'Timestamp,Core Clock (Mhz),Memory Clock (Mhz),Fan speed (rpm),' \
       'Load (%),Power (Watts),Temp (°C)'

  trap 'SIGINT' do
    exit 0
  end

  loop do
    list = []
    threads = [
      Thread.new { list[0] = amdgpu_service.core_clock },
      Thread.new { list[1] = amdgpu_service.memory_clock },
      Thread.new { list[2] = amdgpu_service.fan_speed_rpm },
      Thread.new { list[3] = amdgpu_service.busy_percent },
      Thread.new { list[4] = amdgpu_service.power_draw },
      Thread.new { list[5] = amdgpu_service.temperature }
    ]
    threads.each(&:join)

    puts [Time.now.strftime('%F %T'), *list].join(',')

    sleep seconds.to_i
  end
end