forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_mem.c
242 lines (195 loc) · 5.47 KB
/
profile_mem.c
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "aqo.h"
#include "profile_mem.h"
int aqo_profile_classes;
bool aqo_profile_enable;
bool out_of_memory = false;
static HTAB *profile_mem_queries = NULL;
typedef struct ProfileMemEntry
{
int key;
double time;
unsigned int counter;
} ProfileMemEntry;
PG_FUNCTION_INFO_V1(aqo_show_classes);
PG_FUNCTION_INFO_V1(aqo_clear_classes);
shmem_startup_hook_type prev_shmem_startup_hook = NULL;
/*
* Check a state of shared memory allcated for classes buffer.
* Warn user, if he want to enable profiling without shared memory at all.
*/
bool
check_aqo_profile_enable(bool *newval, void **extra, GucSource source)
{
if (*newval == true && aqo_profile_classes <= 0)
{
elog(WARNING, "Before enabling profiling, set the value of the aqo_profile_classes GUC to allocate a buffer storage.");
return false;
}
/* If it isn't startup process, we should check a shared memory existence. */
Assert(!IsUnderPostmaster || !(*newval == true && !profile_mem_queries));
return true;
}
/*
* Returns query classes.
*/
Datum
aqo_show_classes(PG_FUNCTION_ARGS)
{
HASH_SEQ_STATUS hash_seq;
ProfileMemEntry *entry;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
if (!(rsinfo->allowedModes & SFRM_Materialize))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("materialize mode required, but it is not allowed in this context")));
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);
tupstore = tuplestore_begin_heap(true, false, work_mem);
attinmeta = TupleDescGetAttInMetadata(tupdesc);
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
MemoryContextSwitchTo(oldcontext);
if (aqo_profile_classes <= 0)
{
ReleaseTupleDesc(tupdesc);
tuplestore_donestoring(tupstore);
elog(WARNING, "Hash table 'profile_mem_queries' doesn't exist");
PG_RETURN_VOID();
}
hash_seq_init(&hash_seq, profile_mem_queries);
while (((entry = (ProfileMemEntry *) hash_seq_search(&hash_seq)) != NULL))
{
Datum values[3];
bool nulls[3] = {0, 0, 0};
values[0] = Int32GetDatum(entry->key);
values[1] = Float8GetDatum(entry->time);
values[2] = UInt32GetDatum(entry->counter);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
ReleaseTupleDesc(tupdesc);
tuplestore_donestoring(tupstore);
PG_RETURN_VOID();
}
/*
* Clear the hash table. Return a number of deleted entries. Just for info.
*/
long
profile_clear_hash_table(void)
{
HASH_SEQ_STATUS status;
ProfileMemEntry *entry;
long deleted;
Assert(aqo_profile_classes > 0);
/* callback only gets registered after creating the hash */
Assert(profile_mem_queries != NULL);
deleted = hash_get_num_entries(profile_mem_queries);
if (deleted == 0)
/* Fast path. Table is empty yet. */
return 0;
hash_seq_init(&status, profile_mem_queries);
while ((entry = (ProfileMemEntry *) hash_seq_search(&status)) != NULL)
{
if (hash_search(profile_mem_queries,
(void *) &entry->key,
HASH_REMOVE,
NULL) == NULL)
elog(ERROR, "AQO: hash table corrupted");
}
Assert(hash_get_num_entries(profile_mem_queries) == 0);
return deleted;
}
Datum
aqo_clear_classes(PG_FUNCTION_ARGS)
{
int64 deleted = -1;
if (aqo_profile_classes > 0)
deleted = profile_clear_hash_table();
PG_RETURN_INT64(deleted);
}
/*
* Change entry or add new, if necessary.
* Access to this shared hash table must be processed under a custom AQO lock,
* related to a query hash value.
*/
void
update_profile_mem_table(double total_time)
{
bool found;
ProfileMemEntry *pentry;
if (aqo_profile_classes <= 0 || !aqo_profile_enable)
return;
Assert(profile_mem_queries);
pentry = (ProfileMemEntry *) hash_search(profile_mem_queries,
&query_context.query_hash,
HASH_ENTER_NULL, &found);
if (pentry == NULL)
{
/* Out of memory. */
elog(LOG, "AQO: profiling buffer is full.");
return;
}
if (!found)
{
pentry->time = 0;
pentry->counter = 0;
}
pentry->time += total_time;
pentry->counter++;
}
/*
* Estimate shared memory space needed.
*/
static Size
profile_memsize(void)
{
Size size = 0;
Assert(aqo_profile_classes > 0);
size = add_size(size,
hash_estimate_size(aqo_profile_classes,
sizeof(ProfileMemEntry)));
return size;
}
void
profile_init(void)
{
if (aqo_profile_classes <= 0)
return;
RequestAddinShmemSpace(profile_memsize());
}
/*
* shmem_startup hook: allocate or attach to shared memory.
* Allocate and initialize profiling-related shared memory, if not already
* done, and set up backend-local pointer to that state. Returns false if this
* operation was failed.
*/
void
profile_shmem_startup(void)
{
HASHCTL ctl;
if (prev_shmem_startup_hook)
prev_shmem_startup_hook();
if (aqo_profile_classes <= 0)
return;
ctl.keysize = sizeof(int);
ctl.entrysize = sizeof(ProfileMemEntry);
profile_mem_queries = ShmemInitHash("aqo_profile_mem_queries",
aqo_profile_classes,
aqo_profile_classes,
&ctl,
HASH_ELEM | HASH_BLOBS);
}