Skip to content

Commit 7993bb3

Browse files
cpu: add instance CPU usage metric
This patch adds new `tnt_cpu_instance` metric. Closes #TNTP-4361
1 parent edef7a9 commit 7993bb3

File tree

9 files changed

+57
-0
lines changed

9 files changed

+57
-0
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939
cartridge: ""
4040
- tarantool: "3.3"
4141
cartridge: ""
42+
- tarantool: "3.4"
43+
cartridge: ""
4244
runs-on: ubuntu-24.04
4345
steps:
4446
- uses: actions/checkout@v4

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- `tnt_cpu_instance` metric.
12+
1113
### Changed
1214

1315
### Fixed

doc/monitoring/metrics_reference.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,13 @@ They are only available on Linux.
527527
* ``kind`` can be either ``user`` or ``system``
528528
* ``thread_name`` is ``tarantool``, ``wal``, ``iproto``, or ``coio``
529529
* ``file_name`` is the entrypoint file name, for example, ``init.lua``.
530+
* - ``tnt_cpu_instance``
531+
- Tarantool instance CPU time.
532+
This metric always has the labels
533+
``{kind="user"}``,
534+
where:
535+
536+
* ``kind`` can be either ``user`` or ``system``.
530537

531538
There are also two cross-platform metrics, which can be obtained with a ``getrusage()`` call.
532539

metrics/psutils/cpu.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ local function update_cpu_metrics()
4949
collectors_list.cpu_thread:remove(thread_info)
5050
end
5151
threads = new_threads
52+
53+
local instance_info = psutils.get_instance_cpu_time()
54+
if instance_info ~= nil then
55+
local instance_stime_labels = {kind = 'system'}
56+
collectors_list.instance_cpu_usage = utils.set_gauge('cpu_instance',
57+
'Tarantool instance cpu time', instance_info.stime,
58+
instance_stime_labels, nil, {default = true})
59+
60+
local instance_utime_labels = {kind = 'user'}
61+
collectors_list.instance_cpu_usage = utils.set_gauge('cpu_instance',
62+
'Tarantool instance cpu time', instance_info.utime,
63+
instance_utime_labels, nil, {default = true})
64+
65+
end
5266
end
5367

5468
local function clear_cpu_metrics()

metrics/psutils/psutils_linux.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ local function get_process_cpu_time()
6666
return thread_time
6767
end
6868

69+
local function get_instance_cpu_time()
70+
return parse_process_stat('/proc/self/stat')
71+
end
72+
6973
return {
7074
get_cpu_time = get_cpu_time,
7175
get_process_cpu_time = get_process_cpu_time,
76+
get_instance_cpu_time = get_instance_cpu_time,
7277
get_cpu_count = get_nprocs_conf,
7378
}

test/psutils_linux_test.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ g.test_get_cpu_count = function(cg)
5252
t.assert_gt(psutils_linux.get_cpu_count(), 0)
5353
end)
5454
end
55+
56+
g.test_get_instance_cpu_time = function(cg)
57+
cg.server:exec(function()
58+
local psutils_linux = require('metrics.psutils.psutils_linux')
59+
local expected = {pid = 280908, comm = 'tarantool', utime = 222, stime = 111}
60+
t.assert_items_equals(psutils_linux.get_instance_cpu_time(), expected)
61+
end)
62+
end

test/psutils_linux_test_payload/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
local payload_dir = debug.sourcedir()
22
local stat_file_path = payload_dir .. '/proc_stat'
3+
local self_stat_file_path = payload_dir .. '/proc_self_stat'
34
local task_dir_path = payload_dir .. '/proc_self_task'
45

56
return {
67
files = {
78
['/proc/stat'] = stat_file_path,
9+
['/proc/self/stat'] = self_stat_file_path,
810
['/proc/self/task/1/stat'] = task_dir_path .. '/1/stat',
911
['/proc/self/task/12/stat'] = task_dir_path .. '/12/stat',
1012
['/proc/self/task/13/stat'] = task_dir_path .. '/13/stat',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
280908 (tarantool) R 12484 280908 12484 34819 280908 4194304 101 0 0 0 222 111 0 0 20 0 1 0 50085946 8679424 448 18446744073709551615 110672370106368 110672370123953 140731302033040 0 0 0 0 0 0 0 0 0 17 9 0 0 0 0 0 110672370137744 110672370139240 110673314615296 140731302035060 140731302035080 140731302035080 140731302039531 0

test/psutils_linux_thread_clean_test.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ g.test_cpu_count = function(cg)
5656
end)
5757
end
5858

59+
g.test_instance_cpu = function(cg)
60+
cg.server:exec(function()
61+
local metrics = require('metrics')
62+
local utils = require('test.utils') -- luacheck: ignore 431
63+
64+
require('metrics.psutils.cpu').update()
65+
66+
local observations = metrics.collect()
67+
local metric = utils.find_metric('tnt_cpu_instance', observations)
68+
t.assert_not_equals(metric, nil)
69+
t.assert_equals(#metric, 2)
70+
t.assert_gt(metric[1].value, 0)
71+
t.assert_gt(metric[2].value, 0)
72+
end)
73+
end
5974

6075
g.test_clear = function(cg)
6176
cg.server:exec(function()
@@ -71,6 +86,7 @@ g.test_clear = function(cg)
7186
'tnt_cpu_number',
7287
'tnt_cpu_time',
7388
'tnt_cpu_thread',
89+
'tnt_cpu_instance',
7490
}
7591

7692
for _, metric_name in ipairs(expected) do

0 commit comments

Comments
 (0)