1
2
3
4
5
6
7
8
9
10
11
12
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
53
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* -*-pgsql-c-*- */
/*
*
* $Header$
*
* pgpool: a language independent connection pool server for PostgreSQL
* written by Tatsuo Ishii
*
* Copyright (c) 2003-2020 PgPool Global Development Group
*
*/
/*--------------------------------------------------------------------
* statistics.c
*
* Various statistics related functions.
*--------------------------------------------------------------------
*/
#include <unistd.h>
#include <string.h>
#include "pool.h"
#include "utils/statistics.h"
#include "parser/nodes.h"
/*
* Per backend node stat area in shared memory
*/
typedef struct
{
uint64 select_cnt; /* number of read SELECT queries issued */
uint64 insert_cnt; /* number of INSERT queries issued */
uint64 update_cnt; /* number of UPDATE queries issued */
uint64 delete_cnt; /* number of DELETE queries issued */
uint64 ddl_cnt; /* number of DDL queries issued */
uint64 other_cnt; /* number of any other queries issued */
uint64 panic_cnt; /* number of PANIC messages */
uint64 fatal_cnt; /* number of FATAL messages */
uint64 error_cnt; /* number of ERROR messages */
} PER_NODE_STAT;
static volatile PER_NODE_STAT *per_node_stat;
/*
* Return shared memory size necessary for this module
*/
size_t
stat_shared_memory_size(void)
{
size_t size;
/* query counter area */
size = MAXALIGN(MAX_NUM_BACKENDS * sizeof(PER_NODE_STAT));
return size;
}
/*
* Set PER_NODE_STAT address in the shared memory area to global variable.
* This should be called from pgpool main process upon startup.
*/
void
stat_set_stat_area(void *address)
{
per_node_stat = (PER_NODE_STAT *) address;
}
/*
* Initialize shared memory stat area
*/
void
stat_init_stat_area(void)
{
memset((void *) per_node_stat, 0, stat_shared_memory_size());
}
/*
* Update stat counter
*/
void
stat_count_up(int backend_node_id, Node *parse_tree)
{
if (parse_tree == NULL)
{
/*
* No parse tree. does not worth to gather statistics (internal
* queries). If we want to gather statistics for queries without
* parse tree, we could call the parser but I don't think it's worth
* the trouble.
*/
return;
}
if (IsA(parse_tree, SelectStmt))
{
per_node_stat[backend_node_id].select_cnt++;
}
else if (IsA(parse_tree, InsertStmt))
{
per_node_stat[backend_node_id].insert_cnt++;
}
else if (IsA(parse_tree, UpdateStmt))
{
per_node_stat[backend_node_id].update_cnt++;
}
else if (IsA(parse_tree, DeleteStmt))
{
per_node_stat[backend_node_id].delete_cnt++;
}
else
{
switch (nodeTag(parse_tree))
{
case (T_CheckPointStmt):
case (T_DeallocateStmt):
case (T_DiscardStmt):
case (T_ExecuteStmt):
case (T_ExplainStmt):
case (T_ListenStmt):
case (T_LoadStmt):
case (T_LockStmt):
case (T_NotifyStmt):
case (T_PrepareStmt):
case (T_TransactionStmt):
case (T_UnlistenStmt):
case (T_VacuumStmt):
case (T_VariableSetStmt):
case (T_VariableShowStmt):
per_node_stat[backend_node_id].other_cnt++;
break;
default:
per_node_stat[backend_node_id].ddl_cnt++;
}
}
}
/*
* Update error stat counter
*/
void
error_stat_count_up(int backend_node_id, char *str)
{
if (strcasecmp(str, "PANIC") == 0)
per_node_stat[backend_node_id].panic_cnt++;
else if (strcasecmp(str, "FATAL") == 0)
per_node_stat[backend_node_id].fatal_cnt++;
else if (strcasecmp(str, "ERROR") == 0)
per_node_stat[backend_node_id].error_cnt++;
}
/*
* Stat counter read functions
*/
uint64
stat_get_select_count(int backend_node_id)
{
return per_node_stat[backend_node_id].select_cnt;
}
uint64
stat_get_insert_count(int backend_node_id)
{
return per_node_stat[backend_node_id].insert_cnt;
}
uint64
stat_get_update_count(int backend_node_id)
{
return per_node_stat[backend_node_id].update_cnt;
}
uint64
stat_get_delete_count(int backend_node_id)
{
return per_node_stat[backend_node_id].delete_cnt;
}
uint64
stat_get_ddl_count(int backend_node_id)
{
return per_node_stat[backend_node_id].ddl_cnt;
}
uint64
stat_get_other_count(int backend_node_id)
{
return per_node_stat[backend_node_id].other_cnt;
}
uint64
stat_get_panic_count(int backend_node_id)
{
return per_node_stat[backend_node_id].panic_cnt;
}
uint64
stat_get_fatal_count(int backend_node_id)
{
return per_node_stat[backend_node_id].fatal_cnt;
}
uint64
stat_get_error_count(int backend_node_id)
{
return per_node_stat[backend_node_id].error_cnt;
}
|