Method: LinuxStat::ProcessInfo.thread_usage

Defined in:
lib/linux_stat/process_info.rb

.thread_usage(pid: $$, sleep: ticks_to_ms_t5) ⇒ Object

thread_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5)

Where pid is the process ID and sleep time is the interval between measurements.

By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck * 5

The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.

It retuns the per core CPU usage as Float.

For example:

LinuxStat::ProcessInfo.core_usage

=> 200.0

A value of 100.0 indicates it is using 100% processing power of a core.

The value could be 0 to (100 * the number of CPU threads (including hyperthreading) in the system)

But if the info isn’t available, it will return nil.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/linux_stat/process_info.rb', line 359

def thread_usage(pid: $$, sleep: ticks_to_ms_t5)
	file = "/proc/#{pid}/stat"
	ticks = get_ticks

	return nil unless File.readable?(file)
	utime, stime, starttime = IO.read(file)
		.split.values_at(13, 14, 21).map(&:to_f)

	uptime = IO.read('/proc/uptime'.freeze).to_f * ticks

	total_time = utime + stime
	idle1 = uptime - starttime - total_time

	sleep(sleep)

	return nil unless File.readable?(file)
	utime2, stime2, starttime2 = IO.read(file)
		.split.values_at(13, 14, 21).map(&:to_f)

	uptime = IO.read('/proc/uptime'.freeze).to_f * ticks

	total_time2 = utime2 + stime2
	idle2 = uptime - starttime2 - total_time2

	totald = idle2.+(total_time2).-(idle1 + total_time)

	u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)

	cpu_count_t100 = cpu_count * 100
	u > cpu_count_t100 ? cpu_count_t100 : u.round(2)
end