Module: LinuxStat::Kernel

Defined in:
lib/linux_stat/kernel.rb

Class Method Summary collapse

Class Method Details

.build_dateObject

Returns the time when the kernel was compiled.

The return value is a Time object.

If the information isn’t available, it will return nil

The time will be searched in specific order.

  • It will match any date matching any of these formats:

  1. %b %d %H:%M:%S %z %Y

  2. %d %b %Y %H:%M:%S %z

  3. %Y-%m-%d

Most kernels have date in them in this format.

Do note that Ruby sometimes fails to work with timezones like BST for example.

In such case, the timezone is unrealiable and often returns the local timezone.

You have to use regexp yourself to get the proper zone.

Use LinuxStat::Kernel.build_date_string to get the original string if you need that.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/linux_stat/kernel.rb', line 86

def build_date
	return nil if splitted.empty?

	@@time ||= begin
		require 'time' unless Time.respond_to?(:strptime)

		splitted.each_cons(5).map do |x|
			joined = x.each(&:strip!).join(?\s.freeze)

			# Match 21 Oct 2020 01:11:20 +0000
			if joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/]
				Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil

			# Match Aug 25 17:23:54 UTC 2020
			elsif joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{2}\s\w+\s\d*$/]
				Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil

			# Match 2017-09-19
			elsif joined[/\d{4}-\d{1,2}-\d{1,2}/]
				Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil

			else
				nil
			end
		end.tap(&:compact!)[0]
	end
end

.build_date_stringObject

Returns the time when the kernel was compiled.

The return value is a String.

If the information isn’t available, it will return nil

The time will be searched in specific order.

  • It will match any date matching any of these formats:

  1. %b %d %H:%M:%S %z %Y

  2. %d %b %Y %H:%M:%S %z

  3. %Y-%m-%d

Most kernels have date in them in this format.

Do note that Ruby sometimes fails to work with timezones like BST for example.

In such case, the timezone is unrealiable and often returns the local timezone.

You have to use regexp yourself to get the proper zone.

Use LinuxStat::Kernel.build_date_string to get the original string if you need that.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/linux_stat/kernel.rb', line 140

def build_date_string
	return nil if splitted.empty?

	@@time2 ||= begin
		require 'time' unless Time.respond_to?(:strptime)

		splitted.each_cons(5).map do |x|
			joined = x.each(&:strip!).join(?\s.freeze)

			# Match 21 Oct 2020 01:11:20 +0000
			if (joined[/^\d{1,2}\s\w{3}\s\d{4}\s\d{1,2}:\d{1,2}:\d{1,2}\s\+\d*$/] && (Time.strptime(joined, '%d %b %Y %H:%M:%S %Z') rescue nil)) ||

			# Match Aug 25 17:23:54 UTC 2020
			(joined[/^\w{3}\s\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}\s\w+\s\d*$/] && (Time.strptime(joined, '%b %d %H:%M:%S %z %Y') rescue nil)) ||

			# Match 2017-09-19
			(joined[/\d{4}-\d{1,2}-\d{1,2}/] && (Time.strptime(joined[/\d{4}-\d{2}-\d{2}/] + " +00:00", '%Y-%m-%d %z') rescue nil))
				joined
			else
				nil
			end
		end.tap(&:compact!)[0]
	end
end

.build_userObject

Returns the name of the user who built the kernel using KBUILD_FLAGS.

If the information isn’t available, it will return a frozen empty string.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



21
22
23
24
25
# File 'lib/linux_stat/kernel.rb', line 21

def build_user
	@@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
		.reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
		.split[0].to_s[1..-2].to_s.freeze
end

.compilerObject

Returns the compiler used to compile the Linux Kernel.

If the information isn’t available, it will return a frozen empty string.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/linux_stat/kernel.rb', line 33

def compiler
	return ''.freeze if string.empty?

	@@compiler ||= string.split(/(\(.+\))/).each(&:strip!)
		.reject(&:empty?)
		.find { |x| x[/^\(.+\)$/] }.to_s
		.split.find { |x| !x[/^(.+@.+)$/] }.to_s[/\w+/].to_s.freeze

	@@compiler_val ||= case @@compiler
		when /gcc/i then [:gcc ]
		when /clang/i then [:clang]
		when /icc/i then [:icc]
		else [@@compiler &.to_sym]
	end << compiler_version
end

.compiler_versionObject

Returns the compiler version used to compile the Linux Kernel.

If the information isn’t available, it will return a frozen empty string.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



55
56
57
58
59
# File 'lib/linux_stat/kernel.rb', line 55

def compiler_version
	@@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
		.reject(&:empty?)[2..4].to_a
		.find { |x| x[/[\d.]+/] }.to_s[/[\d.]+/].to_s.freeze
end

.stringObject

Reads maximum 1024 bytes from /proc/version and returns the string.

The output is also cached ; as changing the value in runtime is unexpected.



169
170
171
# File 'lib/linux_stat/kernel.rb', line 169

def string
	@@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
end

.ticksObject Also known as: clk_tck

Returns the sc_clk_tck or the same output from command ‘getconf CLK_TCK`.

Also, clk_tck is an alias of this method.

The output is also cached ; as changing the value in runtime is unexpected.



179
180
181
# File 'lib/linux_stat/kernel.rb', line 179

def ticks
	@@tick ||= LinuxStat::Sysconf.sc_clk_tck
end

.versionObject Also known as: release

Returns the Linux Kernel version.

If the information isn’t available, it will return a frozen empty string.

The output is also cached (memoized) ; as changing the value in runtime is unexpected.



10
11
12
13
# File 'lib/linux_stat/kernel.rb', line 10

def version
	return ''.freeze if string.empty?
	@@version ||= splitted[2]
end