-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_memorycontext.c
executable file
·266 lines (194 loc) · 6.18 KB
/
pg_memorycontext.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "postgres.h"
#include "executor/executor.h"
#include "funcapi.h"
#include "utils/memutils.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#define MXT_NAME_LENGTH 128
/* 1024 different memory context at most */
#define MXT_NAME_NUMBER 1024
#define ALLOCSET_NUM_FREELISTS 11
typedef struct AllocBlockData *AllocBlock;
typedef struct AllocChunkData *AllocChunk;
typedef struct AllocSetContext *AllocSet;
typedef void *AllocPointer;
typedef struct MxtStat
{
char name[MXT_NAME_LENGTH];
unsigned int cnt;
long total_size;
}MxtStat;
typedef struct AllocBlockData
{
AllocSet aset;
AllocBlock next;
char *freeptr;
char *endptr;
}AllocBlockData;
typedef struct AllocChunkData
{
void *aset;
Size size;
}AllocChunkData;
typedef struct AllocSetContext
{
MemoryContextData header;
AllocBlock blocks;
AllocChunk freelist[ALLOCSET_NUM_FREELISTS];
Size initBlockSize;
Size maxBlockSize;
Size nextBlockSize;
Size allocChunkLimit;
AllocBlock keeper;
} AllocSetContext;
void _PG_init(void);
void _PG_fini(void);
Datum pg_memorycontext(PG_FUNCTION_ARGS);
void MxtCacheInitialize(void);
void MxtCacheInsert(char *name, long totalspace);
long MxtAllocSetStats(MemoryContext context);
void MxtMemoryContextStats(MemoryContext context);
void MxtMemoryContextStatsInternal(MemoryContext context);
static HTAB *MxtCache;
void _PG_init(void)
{
}
void _PG_fini(void)
{
}
PG_FUNCTION_INFO_V1(pg_memorycontext);
Datum pg_memorycontext(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
int call_cntr;
int max_calls;
TupleDesc tupdesc;
AttInMetadata *attinmeta;
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
long mxt_number;
HASH_SEQ_STATUS *mxt_status;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
MxtCacheInitialize();
MxtMemoryContextStats(TopMemoryContext);
mxt_number = hash_get_num_entries(MxtCache);
funcctx->max_calls = mxt_number;
mxt_status = (HASH_SEQ_STATUS*)palloc(sizeof(HASH_SEQ_STATUS));
hash_seq_init(mxt_status, MxtCache);
funcctx->user_fctx = (void*)mxt_status;
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context"
"that cannot accept type record")));
}
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
call_cntr = funcctx->call_cntr;
max_calls = funcctx->max_calls;
attinmeta = funcctx->attinmeta;
if (call_cntr < max_calls)
{
char **values;
HeapTuple tuple;
Datum result = 0;
MxtStat *mxt_stat;
HASH_SEQ_STATUS *user_mxt_status;
user_mxt_status = (HASH_SEQ_STATUS*)funcctx->user_fctx;
mxt_stat = (MxtStat*)hash_seq_search(user_mxt_status);
if (mxt_stat)
{
/* int take up 12 char at most */
values = (char**)palloc(3 * sizeof(char*));
values[0] = (char*)palloc(MXT_NAME_LENGTH * sizeof(char));
values[1] = (char*)palloc(16 * sizeof(char));
values[2] = (char*)palloc(16 * sizeof(char));
snprintf(values[0], MXT_NAME_LENGTH, "%s", mxt_stat->name);
snprintf(values[1], 16, "%d", mxt_stat->cnt);
snprintf(values[2], 16, "%ld", mxt_stat->total_size);
tuple = BuildTupleFromCStrings(attinmeta, values);
result = HeapTupleGetDatum(tuple);
pfree(values[0]);
pfree(values[1]);
pfree(values[2]);
pfree(values);
}
SRF_RETURN_NEXT(funcctx, result);
}
else
{
/* finish the whole seq_search */
if (hash_seq_search((HASH_SEQ_STATUS*)funcctx->user_fctx))
{
elog(ERROR,
"pg_memorycontext: leaked scan hash table");
}
SRF_RETURN_DONE(funcctx);
}
}
void MxtCacheInitialize(void)
{
HASHCTL mxt_ctl;
MemSet(&mxt_ctl, 0, sizeof(mxt_ctl));
/*
* create memorycontext hashtable under SRF multi-call context
*
*/
mxt_ctl.keysize = MXT_NAME_LENGTH;
mxt_ctl.entrysize = sizeof(MxtStat);
mxt_ctl.hash = string_hash;
mxt_ctl.hcxt = CurrentMemoryContext;
MxtCache = hash_create("pg_memorycontext hash table", MXT_NAME_NUMBER,
&mxt_ctl, HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
if (NULL == MxtCache)
{
elog(ERROR,
"pg_memorycontext: can not create pg_memorycontext hash table");
}
}
void MxtCacheInsert(char * name,long totalspace)
{
MxtStat *mxt_stat;
bool found;
mxt_stat = (MxtStat*)hash_search(MxtCache, (void*)name, HASH_ENTER, &found);
if(!found)
{
mxt_stat->cnt = 0;
mxt_stat->total_size = 0 ;
}
mxt_stat->cnt += 1;
mxt_stat->total_size += totalspace;
}
long MxtAllocSetStats(MemoryContext context)
{
AllocSet set = (AllocSet)context;
long totalspace = 0;
AllocBlock block;
for (block = set->blocks; block != NULL; block = block->next)
{
totalspace += block->endptr - ((char*)block);
}
MxtCacheInsert(set->header.name, totalspace);
return totalspace;
}
void MxtMemoryContextStats(MemoryContext context)
{
MxtMemoryContextStatsInternal(context);
}
void MxtMemoryContextStatsInternal(MemoryContext context)
{
MemoryContext child;
AssertArg(MemoryContextIsValid(context));
MxtAllocSetStats(context);
for (child = context->firstchild; child != NULL; child = child->nextchild)
{
MxtMemoryContextStatsInternal(child);
}
}