Skip to content

Commit 2543258

Browse files
committed
Fix the proc cpu percent got by gpsmon
The proc cpu percent got by gpsmon sometimes is 0 although the proc cpu cost is not 0. The bug is caused by the pidtab hash table's key. The key is not pid, but defined as queryid + pid. In the case a session runs a bunch of queries, there will be multiple entries in the pidtable which have the same pid, and the cpu percent will be computed multiple times for a proc. The cpu percent is computed as nowcpucost-lastcpucost/timemow-lasttime, then the nowcpucost-lastcpucost is 0 if it is collected within a minimal timeframe. Fix that by using pid as the hash table's key. By the way, did a small refactor to make the code more clean.
1 parent 2cdf4f0 commit 2543258

4 files changed

Lines changed: 194 additions & 192 deletions

File tree

contrib/perfmon/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
NAME = perfmon
22
EXTVERSION = 1.1.0
33

4-
REGRESS = pre_run_check guc_config extension_test pg_qs post_run
4+
REGRESS = pre_run_check guc_config query extension_test pg_qs post_run
55

66
ifdef USE_PGXS
77
PG_CONFIG = pg_config

contrib/perfmon/expected/query.out

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,20 @@ select sess_id from pg_stat_activity where pg_backend_pid()=pid;
3434
(1 row)
3535

3636
\gset
37-
-- end_ignore
38-
CREATE TABLE foo(a int);
37+
CREATE TABLE foo(a int, b int);
3938
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Cloudberry Database data distribution key for this table.
4039
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
4140
CREATE TABLE test(a int);
4241
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Cloudberry Database data distribution key for this table.
4342
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
44-
INSERT INTO foo SELECT generate_series(0,40000000);
45-
INSERT INTO test SELECT generate_series(0,40000000);
43+
\timing
44+
INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i;
45+
Time: 149935.380 ms (02:29.935)
46+
\timing
47+
-- end_ignore
4648
-- test query text in multiple lines
4749
INSERT INTO test
4850
SELECT generate_series(0,10);
49-
select count(*) from foo,test where foo.a=test.a;
50-
count
51-
----------
52-
40000012
53-
(1 row)
54-
5551
-- test nested query
5652
create or replace function n_join_foo_test() returns integer as $$
5753
begin
@@ -61,7 +57,7 @@ $$ language plpgsql;
6157
select * from n_join_foo_test();
6258
n_join_foo_test
6359
-----------------
64-
40000012
60+
10
6561
(1 row)
6662

6763
DROP TABLE foo;
@@ -117,29 +113,27 @@ select count(*) > 0 from diskspace_history;
117113

118114
select ccnt, status, query_text, length(query_plan) > 0 from queries_history
119115
where ssid = :sess_id order by ccnt;
120-
ccnt | status | query_text | ?column?
121-
------+--------+------------------------------------------------------------------+----------
122-
2 | done | select sess_id from pg_stat_activity where pg_backend_pid()=pid; | t
123-
4 | done | select sess_id from pg_stat_activity where pg_backend_pid()=pid; | t
124-
8 | done | INSERT INTO foo SELECT generate_series(0,40000000); | t
125-
10 | done | INSERT INTO test SELECT generate_series(0,40000000); | t
126-
12 | done | INSERT INTO test +| t
127-
| | SELECT generate_series(0,10); |
128-
14 | done | select count(*) from foo,test where foo.a=test.a; | t
129-
17 | done | select * from n_join_foo_test(); | t
130-
(7 rows)
116+
ccnt | status | query_text | ?column?
117+
------+--------+---------------------------------------------------------------------+----------
118+
2 | done | select sess_id from pg_stat_activity where pg_backend_pid()=pid; | t
119+
4 | done | select sess_id from pg_stat_activity where pg_backend_pid()=pid; | t
120+
8 | done | INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i; | t
121+
10 | done | INSERT INTO test +| t
122+
| | SELECT generate_series(0,10); |
123+
13 | done | select * from n_join_foo_test(); | t
124+
(5 rows)
131125

132126
SELECT COUNT(*) FROM (SELECT DISTINCT ccnt FROM queries_history
133127
where ssid = :sess_id) as temp;
134128
count
135129
-------
136-
7
130+
5
137131
(1 row)
138132

139133
select mem_peak>0, cpu_currpct>0, spill_file_size>0, skew_cpu>0, status, query_text, length(query_plan) > 0 from queries_history
140-
where ssid = :sess_id and query_text = 'select count(*) from foo,test where foo.a=test.a;'
141-
?column? | ?column? | ?column? | ?column? | status | query_text | ?column?
142-
----------+----------+----------+----------+--------+---------------------------------------------------+----------
143-
t | t | t | t | done | select count(*) from foo,test where foo.a=test.a; | t
134+
where ssid = :sess_id and query_text = 'INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i;'
135+
?column? | ?column? | ?column? | ?column? | status | query_text | ?column?
136+
----------+----------+----------+----------+--------+---------------------------------------------------------------------+----------
137+
t | t | t | t | done | INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i; | t
144138
(1 row)
145139

contrib/perfmon/sql/query.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ select wait_for_gpmmon_work();
2424
\c contrib_regression
2525
select sess_id from pg_stat_activity where pg_backend_pid()=pid;
2626
\gset
27+
CREATE TABLE foo(a int, b int);
28+
CREATE TABLE test(a int);
29+
\timing
30+
INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i;
31+
\timing
2732
-- end_ignore
2833

29-
CREATE TABLE foo(a int);
30-
CREATE TABLE test(a int);
31-
INSERT INTO foo SELECT generate_series(0,40000000);
32-
INSERT INTO test SELECT generate_series(0,40000000);
3334
-- test query text in multiple lines
3435
INSERT INTO test
3536
SELECT generate_series(0,10);
36-
select count(*) from foo,test where foo.a=test.a;
3737
-- test nested query
3838
create or replace function n_join_foo_test() returns integer as $$
3939
begin
@@ -67,4 +67,4 @@ SELECT COUNT(*) FROM (SELECT DISTINCT ccnt FROM queries_history
6767
where ssid = :sess_id) as temp;
6868

6969
select mem_peak>0, cpu_currpct>0, spill_file_size>0, skew_cpu>0, status, query_text, length(query_plan) > 0 from queries_history
70-
where ssid = :sess_id and query_text = 'select count(*) from foo,test where foo.a=test.a;'
70+
where ssid = :sess_id and query_text = 'INSERT INTO foo SELECT i + 1 from generate_series(0,80000000) as i;'

0 commit comments

Comments
 (0)