Module: Sunburst

Defined in:
lib/sunburst/version.rb,
lib/sunburst/sunburst.rb,
ext/stats/stats.c

Constant Summary collapse

VERSION =
"0.4.1"
PAGESIZE =
UINT2NUM(PAGESIZE)
TICKS =
UINT2NUM(TICKS)

Class Method Summary collapse

Class Method Details

.calculate_cpu_usage(pid, sleep_time) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sunburst/sunburst.rb', line 13

def self.calculate_cpu_usage(pid, sleep_time)
	uptime_file = '/proc/uptime'.freeze

	unless File.readable?(uptime_file)
		sleep(sleep_time)
		return nil
	end

	ticks = Sunburst::TICKS
	stat = Sunburst.ps_stat(pid)
	uptime = IO.read('/proc/uptime').to_f

	if stat.empty?
		sleep(sleep_time)
		return nil
	end

	utime, stime, starttime = *stat.values_at(1, 2, 5).map(&:to_f)
	uptime *= ticks

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

	sleep(sleep_time)

	stat = Sunburst.ps_stat(pid)
	uptime = IO.read('/proc/uptime').to_f
	return nil if stat.empty?

	utime2, stime2, starttime2 = *stat.values_at(1, 2, 5).map(&:to_f)
	uptime *= ticks

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

	totald = idle2.+(total_time2).-(idle1 + total_time)
	cpu_u = totald.-(idle2 - idle1).fdiv(totald).abs.*(100)./(Sunburst.nprocessors)

	cpu_u > 100 ? 100.0 : cpu_u
end

.clock_monotonicObject



76
77
78
79
80
81
82
# File 'ext/stats/stats.c', line 76

VALUE clock_monotonic(volatile VALUE obj) {
	struct timespec tv ;
	clock_gettime(CLOCK_MONOTONIC, &tv) ;
	float time = tv.tv_sec + tv.tv_nsec / 1000000000.0 ;

	return rb_float_new(time) ;
}

.get_mem(pid) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'ext/stats/stats.c', line 10

VALUE statm_memory(volatile VALUE obj, volatile VALUE pid) {
	int _pid = FIX2INT(pid) ;
	if (_pid < 0) return Qnil ;

	char _path[22] ;
	sprintf(_path, "/proc/%d/statm", _pid) ;

	FILE *f = fopen(_path, "r") ;
	if (!f) return Qnil ;

	unsigned int resident, shared ;
	char status = fscanf(f, "%*u %u %u", &resident, &shared) ;
	fclose(f) ;

	if (status != 2) return Qnil ;

	unsigned int v = resident - shared ;
	return UINT2NUM(v) ;
}

.get_stats(pid) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/sunburst/sunburst.rb', line 2

def self.get_stats(pid)
	stats = Sunburst.ps_stat(pid)

	if stats.empty?
		Process.kill(9, pid)
		fail RuntimeError, 'Something horribly wrong happened! Exiting.'
	end

	stats
end

.measure(command:, time: nil, sleep_time: 0.001) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sunburst/sunburst.rb', line 54

def self.measure(command:, time: nil, sleep_time: 0.001)
	progress = block_given?

	r = {
		execution_time: nil, cpu_time: nil,
		memory: nil, max_threads: nil,
		avg_mem: nil, max_memory: nil, state: nil, last_state: nil,
		avg_cpu_usage: nil, max_cpu_usage: nil
	}

	IO.popen(command) { |x|
		time1 = Sunburst.clock_monotonic
		pid = x.pid

		t = if progress
			Thread.new { }
		else
			Thread.new { print x.readpartial(4096) until x.eof? }
		end

		cpu_usage = 0
		max_cpu_usage = 0
		cpu_usage_sum = 0
		cpu_usage_measure_count = 0

		Thread.new {
			while true
				_cpu_usage = calculate_cpu_usage(pid, 0.25)

				if _cpu_usage
					cpu_usage = "%05.2f%%".freeze % _cpu_usage
					cpu_usage_sum += _cpu_usage
					cpu_usage_measure_count += 1

					max_cpu_usage = _cpu_usage if _cpu_usage > max_cpu_usage
				else
					cpu_usage = ?X.freeze
				end

			end
		}

		last_mem = 0
		max_threads = 0
		max_mem = 0
		last_state = nil

		avg_mem = 0
		mem_measure_count = 0

		while true
			_last_mem = Sunburst.get_mem(pid)

			break if (time && Sunburst.clock_monotonic - time1 > time) || _last_mem == 0
			last_mem = _last_mem
			max_mem = last_mem if max_mem < _last_mem

			avg_mem += _last_mem
			mem_measure_count += 1

			# Get stats
			stats = get_stats(pid)
			_threads = stats[3]
			max_threads = _threads if max_threads < _threads
			last_state = stats[4]

			cpu_time = stats[1].+(stats[2]).fdiv(Sunburst::TICKS)

			if progress
				yield(
					Sunburst.clock_monotonic.-(time1),
					stats[1].+(stats[2]).fdiv(Sunburst::TICKS),
					_last_mem * Sunburst::PAGESIZE,
					_threads,
					last_state,
					cpu_usage
				)
			end

			sleep(sleep_time)
		end

		time2 = Sunburst.clock_monotonic

		# Get Stats
		stats = get_stats(pid)
		cpu_time = stats[1].+(stats[2]).fdiv(Sunburst::TICKS)

		_threads = stats[3]
		max_threads = _threads if max_threads < _threads

		_last_mem = Sunburst.get_mem(pid)
		max_mem = _last_mem if max_mem < _last_mem
		last_mem = _last_mem unless _last_mem == 0

		state = stats[4]

		t.kill
		Process.kill(9, pid)

		r[:cpu_time] = cpu_time
		r[:max_threads] = max_threads unless max_threads == 0
		r[:memory] = last_mem * Sunburst::PAGESIZE if last_mem > 0
		r[:max_memory] = max_mem * Sunburst::PAGESIZE if last_mem > 0
		r[:avg_mem] =  (avg_mem * Sunburst::PAGESIZE) / mem_measure_count if mem_measure_count > 0

		r[:execution_time] = time2.-(time1).truncate(5)
		r[:state] = state
		r[:last_state] = last_state

		r[:avg_cpu_usage] = sprintf("%05.2f%%", cpu_usage_sum / cpu_usage_measure_count) if cpu_usage_measure_count > 0
		r[:max_cpu_usage] = sprintf("%05.2f%%", max_cpu_usage) if max_cpu_usage > 0
	}

	r
end

.nprocessorsObject



71
72
73
74
# File 'ext/stats/stats.c', line 71

VALUE nProcessors(volatile VALUE obj) {
	int coreCount = sysconf(_SC_NPROCESSORS_CONF) ;
	return (coreCount == -1) ? Qnil : INT2NUM(coreCount) ;
}

.ps_stat(pid) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/stats/stats.c', line 30

VALUE ps_stat(volatile VALUE obj, volatile VALUE pid) {
	int _pid = FIX2INT(pid) ;
	if (_pid < 0) return rb_str_new_cstr("") ;

	char _path[22] ;
	sprintf(_path, "/proc/%d/stat", _pid) ;

	FILE *f = fopen(_path, "r") ;

	if (!f) return rb_ary_new() ;

	// For this info
	// follow https://man7.org/linux/man-pages/man5/proc.5.html
	char state[1] ;
	int ppid, processor ;
	long unsigned utime, stime ;
	long num_threads ;
	long long unsigned starttime ;

	char status = fscanf(
		f, "%*llu (%*[^)]%*[)] %1s "
		"%d %*d %*d %*d %*d %*u "
		"%*lu %*lu %*lu %*lu %lu %lu "
		"%*ld %*ld %*ld %*ld %ld %*ld %ld",
		&state, &ppid, &utime, &stime, &num_threads, &starttime
	) ;

	fclose(f) ;

	if (status != 6) return rb_ary_new() ;

	return rb_ary_new_from_args(6,
		INT2NUM(ppid),
		ULONG2NUM(utime),
		ULONG2NUM(stime),
		LONG2NUM(num_threads),
		rb_str_new(state, 1),
		ULL2NUM(starttime)
	) ;
}

.total_ramObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/stats/stats.c', line 90

VALUE totalRAM(volatile VALUE obj) {
	struct sysinfo buf ;
	char status = sysinfo(&buf) ;

	if (status != 0) return Qnil ;

	return rb_funcall(
		ULONG2NUM(buf.totalram),
		rb_intern("*"),
		1,
		ULONG2NUM(buf.mem_unit)
	) ;
}

.win_widthObject



84
85
86
87
88
# File 'ext/stats/stats.c', line 84

VALUE winWidth(volatile VALUE obj) {
	struct winsize w ;
	ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) ;
	return INT2NUM(w.ws_col) ;
}