-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_session.c
599 lines (502 loc) · 16.6 KB
/
trace_session.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#include "postgres.h"
#include <time.h>
#include "utils/memutils.h"
#include "utils/portal.h"
#include "utils/jsonb.h"
#include "utils/guc.h"
#include "commands/explain.h"
#include "storage/ipc.h"
#include "trace_parsing.h"
#include "uprobe_internal.h"
#include "trace_lock_on_buffers.h"
#include "trace_execute_nodes.h"
#include "trace_planning.h"
#include "list.h"
#include "trace_wait_events.h"
#include "json_to_jsonbvalue_parser.h"
#include "trace_file.h"
#include "trace_session.h"
#define CLOCKTYPE CLOCK_MONOTONIC
#define MAX_PLAN_NEST_LEVEL 100
static UprobeList* uprobesList = NULL;
static MemoryContext traceMemoryContext = NULL;
static uint64 executionStarted;
static volatile Uprobe* lastSetUprobe = NULL; //we need it in case that ListAdd fails and we need to delete this uprobe
static ExecutorRun_hook_type prev_ExecutorRun_hook = NULL; // hook to log plan before execution
bool isExecuteTime = false;
//GUC parametrs
int writeMode = TEXT_WRITE_MODE;
bool traceLWLocksForEachNode;
bool writeOnlySleepLWLocksStat;
static const struct config_enum_entry writeModeOptions[] = {
{"text", TEXT_WRITE_MODE, false},
{"json", JSON_WRITE_MODE, false},
{NULL, 0, false}
};
static void TraceSessionExecutorRun(QueryDesc *queryDesc,
ScanDirection direction,
uint64 count,
bool execute_once);
static char* ProcessDescBeforeExec(QueryDesc *queryDesc);
static UprobeList* MakeNodesPlanStringsText(char* startPlanExplain, char* endPlanExplain);
static JsonbValue* FindField(JsonbValue* json, char* field, size_t len);
static void JsonbDeleteField(JsonbValue* json, JsonbValue* value);
static void AddExplainJsonbToNode(JsonbValue* planNode, UprobeList* strNodes);
static void MakeNodesRecursive(JsonbValue* planNode, UprobeList* strNodes);
static UprobeList* MakeNodesPlanStringsJson(char* explainString, size_t len);
static void SetUprobes2Ptr(UprobeAttachInterface** uprobes, size_t size);
static void SetUprobes1Ptr(UprobeAttachInterface* uprobes, size_t size);
static char* BeforeExecution(QueryDesc *queryDesc);
static void AfterExecution(QueryDesc* queryDesc, char* planString);
static void ClearAfterEreportInFunc(void* data);
static void ClearAfterEreportRetFunc(void* data);
static void ClearAfterEreportCleanFunc(UprobeAttachInterface* uprobe);
static UprobeAttachInterface* UprobeOnClearAfterEreportGet(void);
static void
SetUprobes2Ptr(UprobeAttachInterface** uprobes, size_t size)
{
Uprobe* uprobe;
UPROBE_INIT_RES res;
for (size_t i = 0; i < size; i++)
{
res = UprobeInit(uprobes[i], &uprobe);
if (res != SUCCESS)
{
elog(ERROR, "can't set uprobe for %s symbol", uprobes[i]->targetSymbol);
}
lastSetUprobe = uprobe; //save it in case ListAdd faild
ListAdd(uprobesList, uprobe);
lastSetUprobe = NULL;
}
}
static void
SetUprobes1Ptr(UprobeAttachInterface* uprobes, size_t size)
{
Uprobe* uprobe;
UPROBE_INIT_RES res;
for (size_t i = 0; i < size; i++)
{
res = UprobeInit(&uprobes[i], &uprobe);
if (res != SUCCESS)
{
elog(ERROR, "can't set uprobe for %s symbol", uprobes[i].targetSymbol);
}
lastSetUprobe = uprobe; //save it in case ListAdd faild
ListAdd(uprobesList, uprobe);
lastSetUprobe = NULL;
}
}
static char*
BeforeExecution(QueryDesc *queryDesc)
{
struct timespec time;
char* planCopy;
clock_gettime(CLOCKTYPE, &time);
executionStarted = time.tv_sec * 1000000000L + time.tv_nsec;
LockOnBuffersTraceStatPush();
TraceWaitEventsClearStat();
planCopy = ProcessDescBeforeExec(queryDesc);
isExecuteTime = true;
if (writeMode == JSON_WRITE_MODE)
TracePrintf("\"executionEvents\": [\n");
return planCopy;
}
static void
AfterExecution(QueryDesc* queryDesc, char* planString)
{
struct timespec time;
uint64 timeDiff;
StringInfoData str;
MemoryContext old;
bool hasStat;
clock_gettime(CLOCKTYPE, &time);
timeDiff = time.tv_sec * 1000000000L + time.tv_nsec - executionStarted;
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("TRACE. Execution finished for %lu nanosec\n", timeDiff);
else
TracePrintf("\n],\n\"executionTime\": \"%lu nanosec\"\n", timeDiff);
isExecuteTime = false;
if (!traceMemoryContext)
{
if (writeMode == JSON_WRITE_MODE)
{
TracePrintf("\n}]");
}
CloseTraceSessionFile();
return;
}
ExecutorTraceDumpAndClearStat(queryDesc, planString);
old = MemoryContextSwitchTo(traceMemoryContext);
initStringInfo(&str);
hasStat = LockOnBuffersTraceWriteStat(&str, false);
if (hasStat)
{
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("TRACE LWLOCK. Buffer locks stat inside PortalRun %s\n", str.data);
else
TracePrintf(",\n\"locksInsidePortalRun\": %s", str.data);
}
LockOnBuffersTraceStatPop();
resetStringInfo(&str);
hasStat = LockOnBuffersTraceWriteStat(&str, true);
if (hasStat)
{
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("TRACE LWLOCK. Buffer locks stat inside PortalRun %s\n", str.data);
else
TracePrintf(",\n\"locksOutsidePortalRun\": %s", str.data);
}
resetStringInfo(&str);
hasStat = TraceWaitEventDumpStat(&str);
if (hasStat)
{
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("TRACE WAIT EVENTS.\n %s\n", str.data);
else
TracePrintf(",\n\"waitEventStat\": %s\n", str.data);
}
pfree(str.data);
MemoryContextSwitchTo(old);
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("\n\n---------query end------------\n\n");
else
TracePrintf("},\n");
}
static void
ClearAfterEreportInFunc(void* data)
{
ExecutorTraceClearStat();
LockOnBuffersTraceClearStat();
}
static void
ClearAfterEreportRetFunc(void* data)
{
}
static void
ClearAfterEreportCleanFunc(UprobeAttachInterface* uprobe)
{
pfree(uprobe);
}
static UprobeAttachInterface*
UprobeOnClearAfterEreportGet(void)
{
UprobeAttachInterface* res = palloc0(sizeof(UprobeAttachInterface));
res->cleanFunc = ClearAfterEreportCleanFunc;
res->inFunc = ClearAfterEreportInFunc;
res->retFunc = ClearAfterEreportRetFunc;
res->targetSymbol = "FlushErrorState";
return res;
}
static void
TraceNotNormalShutdown(void)
{
if (!traceFile)
return;
if (writeMode == JSON_WRITE_MODE)
TracePrintf("{}\n]");
CloseTraceSessionFile();
}
void
SessionTraceStart(void)
{
UprobeAttachInterface* fixedUprobes[2];
UprobeAttachInterface* uprobes;
MemoryContext old;
size_t size;
if (uprobesList)
elog(ERROR, "session trace is already ON");
PG_TRY();
{
traceMemoryContext = AllocSetContextCreate(TopMemoryContext, "uprobe trace context", ALLOCSET_DEFAULT_SIZES);
OpenTraceSessionFile(true);
ListInit(&uprobesList, (CompareFunction) UprobeCompare, traceMemoryContext);
old = MemoryContextSwitchTo(traceMemoryContext);
LockOnBuffersUprobesGet(traceMemoryContext, fixedUprobes, writeOnlySleepLWLocksStat);
SetUprobes2Ptr(fixedUprobes, 2);
uprobes = ParsingUprobeGet();
SetUprobes1Ptr(uprobes, 1);
PlanningUprobesGet(fixedUprobes);
SetUprobes2Ptr(fixedUprobes, 2);
ExecutorTraceUprobesGet(fixedUprobes, traceMemoryContext, traceLWLocksForEachNode);
SetUprobes2Ptr(fixedUprobes, 1);
uprobes = UprobeOnClearAfterEreportGet();
SetUprobes1Ptr(uprobes, 1);
uprobes = TraceWaitEventsUprobesGet(&size);
SetUprobes1Ptr(uprobes, size);
on_proc_exit((pg_on_exit_callback) TraceNotNormalShutdown, 0);
}
PG_CATCH();
{
CloseTraceSessionFile();
if (!uprobesList)
PG_RE_THROW();
LIST_FOREACH(uprobesList, it)
{
UprobeDelete(it->value);
}
ListFree(uprobesList);
uprobesList = NULL;
traceMemoryContext = NULL;
if (lastSetUprobe)
UprobeDelete((Uprobe*) lastSetUprobe);
lastSetUprobe = NULL;
TraceWaitEventsUprobesClean();
PG_RE_THROW();
}
PG_END_TRY();
if (writeMode == JSON_WRITE_MODE)
TracePrintf("[\n");
MemoryContextSwitchTo(old);
}
void
SessionTraceStop(void)
{
if (uprobesList == NULL)
elog(ERROR, "session trace is not ON");
ExecutorTraceStop();
LIST_FOREACH(uprobesList, it)
{
UprobeDelete(it->value);
}
uprobesList = NULL;
MemoryContextDelete(traceMemoryContext);
traceMemoryContext = NULL;
//outputFile will be closed at TraceSessionExecutorRun
}
void
SessionTraceInit(void)
{
prev_ExecutorRun_hook = ExecutorRun_hook;
ExecutorRun_hook = TraceSessionExecutorRun;
DefineCustomEnumVariable("pg_uprobe.trace_write_mode",
"format for session trace output",
NULL,
&writeMode,
JSON_WRITE_MODE,
writeModeOptions,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_uprobe.trace_LWLocks_for_each_node",
"if set to true LWLock stat will be traced for each node execution otherwise it will be traced for whole execution",
NULL,
&traceLWLocksForEachNode,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
DefineCustomBoolVariable("pg_uprobe.write_only_sleep_LWLocks_stat",
"if set to true LWLock stat will be traced only for those locks that you had to fall asleep to take.",
NULL,
&writeOnlySleepLWLocksStat,
true,
PGC_SUSET,
0,
NULL,
NULL,
NULL);
TraceFileDeclareGucVariables();
}
static char*
ProcessDescBeforeExec(QueryDesc *queryDesc)
{
MemoryContext old;
ExplainState* es;
size_t explainPlanStartOffset;
size_t explainPlanEnd;
UprobeList* stringPlanParts;
char* planCopy;
old = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
es = NewExplainState();
es->analyze = queryDesc->instrument_options;
es->verbose = true;
es->buffers = es->analyze;
es->wal = es->analyze;
es->timing = es->analyze;
es->summary = es->analyze;
if (writeMode == TEXT_WRITE_MODE)
es->format = EXPLAIN_FORMAT_TEXT;
else
es->format = EXPLAIN_FORMAT_JSON;
es->settings = true;
ExplainBeginOutput(es);
ExplainQueryText(es, queryDesc);
#if PG_MAJORVERSION_NUM > 15
ExplainQueryParameters(es, queryDesc->params, -1);
#endif
explainPlanStartOffset = es->str->len;
ExplainPrintPlan(es, queryDesc);
explainPlanEnd = es->str->len;
if (es->analyze)
ExplainPrintTriggers(es, queryDesc);
if (es->costs)
ExplainPrintJITSummary(es, queryDesc);
ExplainEndOutput(es);
/* Remove last line break */
if (es->str->len > 0 && es->str->data[es->str->len - 1] == '\n')
es->str->data[--es->str->len] = '\0';
/* Fix JSON to output an object */
if (es->format == EXPLAIN_FORMAT_JSON)
{
es->str->data[0] = '{';
es->str->data[es->str->len - 1] = '}';
}
if (writeMode == TEXT_WRITE_MODE)
TracePrintf("TRACE EXECUTE. plan: \n%s\n", es->str->data);
else
TracePrintf("\"explain\": %s,\n", es->str->data);
if (writeMode == TEXT_WRITE_MODE)
{
planCopy = pnstrdup(es->str->data + explainPlanStartOffset, explainPlanEnd - explainPlanStartOffset);
stringPlanParts = MakeNodesPlanStringsText(es->str->data + explainPlanStartOffset, es->str->data + explainPlanEnd);
}
else
{
planCopy = MemoryContextStrdup(traceMemoryContext, es->str->data);
stringPlanParts = MakeNodesPlanStringsJson(es->str->data, es->str->len);
}
if (stringPlanParts)
{
InitExecutorNodes(queryDesc->planstate, stringPlanParts);
ListFree(stringPlanParts);
}
MemoryContextSwitchTo(old);
return planCopy;
}
static void
TraceSessionExecutorRun(QueryDesc* queryDesc, ScanDirection direction, uint64 count, bool execute_once)
{
bool traceWasOn = false;
char* planCopy;
if (traceMemoryContext)
{
planCopy = BeforeExecution(queryDesc);
traceWasOn = true;
}
if (prev_ExecutorRun_hook)
(*prev_ExecutorRun_hook) (queryDesc, direction, count, execute_once);
else
standard_ExecutorRun(queryDesc, direction, count, execute_once);
//if it is the end of Session Trace, we should close trace file
if (traceWasOn)
{
AfterExecution(queryDesc, planCopy);
}
}
//find the plan field in explain jsonb
static JsonbValue*
FindField(JsonbValue* json, char* field, size_t len)
{
JsonbPair* pairs = json->val.object.pairs;
Assert(json->type == jbvObject);
for (int i = 0; i < json->val.object.nPairs; i++)
{
if (len != (size_t) pairs[i].key.val.string.len)
continue;
if (!strncmp(pairs[i].key.val.string.val, field, len))
{
return &pairs[i].value;
}
}
return NULL;
}
static void
AddExplainJsonbToNode(JsonbValue* planNode, UprobeList* strNodes)
{
Jsonb* jsonb = JsonbValueToJsonb(planNode);
char* stringJsonb = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
ListAdd(strNodes, stringJsonb);
pfree(jsonb);
}
static void
JsonbDeleteField(JsonbValue* json, JsonbValue* value)
{
JsonbPair* pairs = json->val.object.pairs;
int nPairs = json->val.object.nPairs;
Assert(json->type == jbvObject);
for (int i = 0; i < nPairs; i++)
{
if (&pairs[i].value == value)
{
json->val.object.nPairs--;
if (i == nPairs - 1)
break;
memmove(&pairs[i], &pairs[i + 1], sizeof(JsonbPair) * (nPairs - i - 1));
break;
}
}
}
static void
MakeNodesRecursive(JsonbValue* planNode, UprobeList* strNodes)
{
JsonbValue* subPlanNodes = FindField(planNode, "Plans", sizeof("Plans") - 1);
JsonbValue subPlans;
if (subPlanNodes != NULL)
{
subPlans = *subPlanNodes;
JsonbDeleteField(planNode, subPlanNodes);
}
else
{
subPlans.val.array.nElems = 0;
}
AddExplainJsonbToNode(planNode, strNodes);
if (subPlanNodes == NULL)
return;
for (int i = 0; i < subPlans.val.array.nElems; i++)
{
MakeNodesRecursive(&subPlans.val.array.elems[i], strNodes);
}
}
static UprobeList*
MakeNodesPlanStringsJson(char* explainString, size_t len)
{
JsonbValue* explain = jsonToJsonbValue(explainString, len);
JsonbValue* planNode;
UprobeList* result;
if (explain == NULL)
return NULL;
planNode = FindField(explain, "Plan", sizeof("Plan") - 1);
if (!planNode)
return NULL;
ListInit(&result, (CompareFunction) strcmp, CurrentMemoryContext);
MakeNodesRecursive(planNode, result);
return result;
}
static UprobeList*
MakeNodesPlanStringsText(char* startPlanExplain, char* endPlanExplain)
{
char* currentNodeExplain = startPlanExplain;
UprobeList* result;
ListInit(&result, (CompareFunction) strcmp, CurrentMemoryContext);
while (true)
{
char* nextNodeSymbol = strstr(currentNodeExplain, "->");
char* CTESymbol = strstr(currentNodeExplain, "CTE");
// +4 for skipping "CTE "
if (CTESymbol != NULL && CTESymbol < nextNodeSymbol && strncmp(CTESymbol + 4, "Scan on", sizeof("Scan on") - 1))
{
CTESymbol[-1] = '\0';
ListAdd(result, currentNodeExplain);
//we skip next node symbol
nextNodeSymbol[0] = ' ';
nextNodeSymbol[1] = ' ';
currentNodeExplain = CTESymbol + 4; //+4 for skipping "CTE "
continue;
}
if (!nextNodeSymbol || nextNodeSymbol >= endPlanExplain)
{
break;
}
nextNodeSymbol[0] = '\0';
ListAdd(result, currentNodeExplain);
currentNodeExplain = nextNodeSymbol + 4;
}
endPlanExplain[0] = '\0';
ListAdd(result, currentNodeExplain);
return result;
}