compute_unit 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95a51bd70b85087d18049b7fdcab55801859e1a6f240b58b3b26262431160b3d
4
- data.tar.gz: 6c6da1daca1a6f46b3e9f651d0ca6c1ea9063cd78dc8ca0311cb68b99c6a6bb8
3
+ metadata.gz: e7454de36f403e2803ee5b87ee989781de8b663d32cfe290b3c22ab0c18f598d
4
+ data.tar.gz: 6b6afa144e05011bec545ddbda688ae663650dce4d09070be9a3922f84bc9606
5
5
  SHA512:
6
- metadata.gz: 7d7b0e432a47d65cae85ae48d86412702e037202404ece13c333495f805ed9eb75c4982e3d297d9f185ae24252b0bc559edb44a4b4168ceb5c70fbfb0c15ae3e
7
- data.tar.gz: 4349a92b6b913d7e777ede290028bffcd6496392b2c8a7f63b36c9cec7e0d19dd7824013481a5d792d963e9138721b7ff9bec4ecf72a7f5efc4dab1bbe745c29
6
+ metadata.gz: 3821595267038d8424a7ae3558437339f87b871ce4e294ffe80cce1c928924a9ae1ebd7fa9b293c08a2b40217a71f7a4358e8a943456dd0c978cf7e4bfbb5a0a
7
+ data.tar.gz: 4fa97e267f9fd8c60999d8af1dd96e15e9e77435c98ece688e53386147e0e7470b1f9a7e149f702fbcb237fbf6c41085f051db9ad441e1388dbde6ce9d2469a5
@@ -1,6 +1,13 @@
1
1
  # Compute Unit Changelog
2
2
 
3
- ## Unrleased
3
+ ## 0.5.0
4
+ Released 10/5/2020
5
+
6
+ * Add more method docs
7
+ * Allow overriding of cpu power
8
+ * Update cpu name using model
9
+ * Add new search methods
10
+ * Add voltage and other cpu metrics
4
11
  ## 0.4.0
5
12
  * Adds ability to list attached processes to compute_unit sorted by field
6
13
  * Adds ability to list top_x attached processes sorted by field
@@ -20,16 +20,36 @@ module ComputeUnit
20
20
  require 'compute_unit/cpu'
21
21
  require 'compute_unit/asic'
22
22
 
23
- copy_default_database unless File.exist?(PCI_DATABASE_PATH)
23
+ # if this file doesn't exist we need to fetch it or copy it
24
+ refresh_pci_database unless File.exist?(PCI_DATABASE_PATH)
24
25
 
25
- ComputeUnit::Gpu.find_all(use_opencl) +
26
- ComputeUnit::Cpu.find_all +
27
- ComputeUnit::Asic.find_all
26
+ Gpu.find_all(use_opencl) + Cpu.find_all + Asic.find_all
27
+ end
28
+
29
+ # @param pid [Integer] the pid to search with
30
+ # @param field [Symbol] - the field to sort by
31
+ # @return [Array] - a array of device paths
32
+ # @example usage
33
+ # device_paths_by_process(3748) => ["/sys/bus/pci/devices/0000:00:00.0"]
34
+ def self.device_paths_by_process(pid)
35
+ # processes = ComputeUnit::Cpu.attached_processes(field).last(1) + ComputeUnit::Gpu.attached_processes(field)
36
+ # processes.find_all { |process| process.pid == pid }
37
+ # We can get the utilized devices but it appears cubersome to convert to a device path
38
+ # This method uses more resources
39
+ find_by_process(pid).map(&:device_path)
40
+ end
41
+
42
+ # @param pid [Integer] the pid to search with
43
+ # @param use_opencl [Boolean] use opencl on gpu devices
44
+ def self.find_by_process(pid, use_opencl = false)
45
+ find_all(use_opencl).find_all do |unit|
46
+ unit.top_processes.first.pid.to_i == pid.to_i
47
+ end
28
48
  end
29
49
 
30
50
  # copies the default pci database from linux filesystem over to the gem path
31
51
  def self.copy_default_database
32
- FileUtils.cp(DEFAULT_PCIDB_PATH, PCI_DATABASE_PATH)
52
+ FileUtils.cp(DEFAULT_PCIDB_PATH, PCI_DATABASE_PATH) if File.exist?(DEFAULT_PCIDB_PATH)
33
53
  end
34
54
 
35
55
  # get a fresh copy of the database and then use find_all
@@ -30,10 +30,13 @@ module ComputeUnit
30
30
  end
31
31
 
32
32
  # @summary Finds all cpu attached processes and sorts by pctcpu
33
+ # param filter [Regex] - if supplied filter out devices from fd list
33
34
  # @param field [Symbol] - the field to sort by
34
35
  # @return [Array] - an array of attached processes
35
- def attached_processes(_field = :pctcpu)
36
- raise NotImplementedError
36
+ def attached_processes(field = :pctcpu, filter = nil)
37
+ raise NotImplementedError unless self.class.respond_to?(:attached_processes)
38
+
39
+ self.class.attached_processes(field, filter)
37
40
  end
38
41
 
39
42
  # @summary Find the processes consuming the most cpu
@@ -6,8 +6,8 @@ module ComputeUnit
6
6
  class Cpu < ComputeBase
7
7
  DEVICE_CLASS = '060000'
8
8
  DEVICE_CLASS_NAME = 'CPU'
9
+ VOLTAGE_MSR = 0x198
9
10
  # @return [Array] - returns a list of device paths of all devices considered for display
10
- alias name model
11
11
 
12
12
  def self.devices
13
13
  ComputeUnit::ComputeBase.devices.find_all do |device|
@@ -15,45 +15,83 @@ module ComputeUnit
15
15
  end
16
16
  end
17
17
 
18
+ def utilization
19
+ 1 # until we can calculate this. Is loadavg a good metric? or load / cpus
20
+ end
21
+
18
22
  # @summary Finds all cpu attached processes and sorts by pctcpu
19
23
  # @param field [Symbol] - the field to sort by
24
+ # @param filter [Regex] - if supplied filter out devices from fd list
20
25
  # @return [Array] - an array of attached processes
21
- def attached_processes(field = :pctcpu)
26
+ def self.attached_processes(field = :pctcpu, _filter = nil)
22
27
  Sys::ProcTable.ps(smaps: false).sort_by(&field)
23
28
  end
24
29
 
30
+ # @summary [Float] - returns the voltage of the cpu
31
+ # @param processor_id [Integer] - the id of the cpu
32
+ def voltage(processor_id = 0)
33
+ file = "/dev/cpu/#{processor_id}/msr"
34
+ return 0 unless File.exist?(file)
35
+
36
+ # read 8 bytes, then unpack it by turning it into an integer
37
+ # make it a binary string, pluck out some specific bits, then
38
+ # convert it back to an integer
39
+ # divide by 8192 to give the voltage
40
+ # lowbit = 32
41
+ # highbit = 47
42
+ # bits = 47 - 32 + 1 # we want to read bits 32-47
43
+ msr = IO.new IO.sysopen(file, 'rb')
44
+ msr.sysseek(VOLTAGE_MSR)
45
+ data, = msr.sysread(8).unpack('q')
46
+ format('%.2f', ((data >> 32) / 8192.to_f))
47
+ end
48
+
49
+ # @return [String] - the model / name of the cpu
25
50
  def model
26
51
  raw_cpu_data[:model_name]
27
52
  end
28
53
 
54
+ # @return [String] - the model / name of the cpu
55
+ def name
56
+ model
57
+ end
58
+
59
+ # @return [String] - the maker of the cpu
29
60
  def make
30
61
  raw_cpu_data[:vendor_id]
31
62
  end
32
63
 
64
+ # @return [Integer] - the number of cores
33
65
  def num_cores
34
66
  raw_cpu_data[:cores_per_socket].to_i
35
67
  end
36
68
 
69
+ # @return [Integer] - the number of threads
37
70
  def num_threads
38
71
  raw_cpu_data[:threads_per_core].to_i
39
72
  end
40
73
 
74
+ # @return [Integer] - the number of cpus
41
75
  def nproc
42
76
  raw_cpu_data[:cpus].to_i
43
77
  end
44
78
 
79
+ # @return [Float] - current mhz of cpu
45
80
  def current_freq_mhz
46
81
  raw_cpu_data[:cpu_mhz].to_f.round(0)
47
82
  end
48
83
 
84
+ # @return [Float] - max mhz of cpu
49
85
  def max_freq_mhz
50
86
  raw_cpu_data[:cpu_max_mhz].to_f.round(0)
51
87
  end
52
88
 
89
+ # @return [Float] - current min of cpu
53
90
  def min_freq_mhz
54
91
  raw_cpu_data[:cpu_min_mhz].to_f.round(0)
55
92
  end
56
93
 
94
+ # @return [String] the path of the hwmon path for monitoring temps
57
95
  def base_hwmon_path
58
96
  File.join(ComputeUnit::SYSFS_PATH, 'devices/platform/coretemp.0/hwmon')
59
97
  end
@@ -75,6 +113,40 @@ module ComputeUnit
75
113
  read_hwmon_data('temp1_input', 0).to_f.round(0) / 1000
76
114
  end
77
115
 
116
+ def status
117
+ 0
118
+ end
119
+
120
+ def power
121
+ ENV.fetch('CPU_POWER', 60) # until we can calculate power we will just use 60
122
+ end
123
+
124
+ def fan
125
+ 2500 # until we can calculate fan speed
126
+ end
127
+
128
+ def mem_temp
129
+ 0 # until we can get the mem temp
130
+ end
131
+
132
+ def pci_loc
133
+ device_path
134
+ end
135
+
136
+ def status_info
137
+ { index: "CPU#{index}",
138
+ name: model,
139
+ bios: 'N/A',
140
+ core_clock: current_freq_mhz,
141
+ memory_clock: 'N/A',
142
+ power: power,
143
+ fan: fan,
144
+ core_volt: voltage,
145
+ temp: temp,
146
+ mem_temp: mem_temp,
147
+ status: status }
148
+ end
149
+
78
150
  def metrics
79
151
  {
80
152
  temp: temp,
@@ -94,6 +166,14 @@ module ComputeUnit
94
166
  super.merge(metrics)
95
167
  end
96
168
 
169
+ def initialize(_device_path, opts)
170
+ super
171
+ @type = :CPU
172
+ @pci_loc = device_path,
173
+ @index = opts[:index].to_i
174
+ @power_offset = 0
175
+ end
176
+
97
177
  def self.create_from_path(device_path, index)
98
178
  opts = {
99
179
  device_class_id: device_class(device_path),
@@ -106,7 +186,7 @@ module ComputeUnit
106
186
  new(device_path, opts)
107
187
  end
108
188
 
109
- def self.find_all
189
+ def self.find_all(_use_opencl = false)
110
190
  devices.sort.map.with_index do |device_path, index|
111
191
  create_from_path(device_path, index)
112
192
  end
@@ -14,12 +14,14 @@ module ComputeUnit
14
14
  end
15
15
 
16
16
  # @summary Finds all cpu attached processes and sorts by pctcpu
17
+ # @param filter [Regex] - if supplied filter out devices from fd list
17
18
  # @param field [Symbol] - the field to sort by
18
19
  # @return [Array] - an array of attached processes
19
- def attached_processes(field = :pctcpu)
20
+ def self.attached_processes(field = :pctcpu, filter = %r{/dev/dri|nvidia\d+})
21
+ filter ||= %r{/dev/dri|nvidia\d+}
20
22
  # looks for any fd device with dri or nvidia in the name
21
23
  p = Sys::ProcTable.ps(smaps: false).find_all do |p|
22
- p.fd.values.find { |f| f =~ %r{/dev/dri|nvidia\d+} }
24
+ p.fd.values.find { |f| f =~ filter }
23
25
  end
24
26
  p.sort_by(&field)
25
27
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ComputeUnit
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compute_unit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Osman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-29 00:00:00.000000000 Z
11
+ date: 2020-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sys-proctable
@@ -133,7 +133,6 @@ files:
133
133
  - lib/compute_unit/monkey_patches.rb
134
134
  - lib/compute_unit/utils.rb
135
135
  - lib/compute_unit/version.rb
136
- - miner@192.168.0.119
137
136
  homepage: https://gitlab.com/blockops/compute_unit
138
137
  licenses:
139
138
  - MIT
Binary file