-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuprobe_collector.c
224 lines (175 loc) · 5.2 KB
/
uprobe_collector.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
#include "postgres.h"
#include <signal.h>
#include "utils/elog.h"
#include "utils/memutils.h"
#include "postmaster/bgworker.h"
#include "storage/ipc.h"
#include "list.h"
#include "uprobe_shared_config.h"
#include "uprobe_factory.h"
#include "uprobe_message_buffer.h"
static UprobeList* storageList = NULL;
static char currentSymbol[MAX_SYMBOL_SIZE + 1] = {'\0'};
static UprobeStorage* currentStorage = NULL;
static MemoryContext collectorContext = NULL;
PGDLLEXPORT void StatCollectorMain(void);
static int StorageListComparator(UprobeStorage* entry, char* symbol);
static void ProcessMessageNewSharedUprobe(MessageNewSharedUprobe* mes);
static void ProcessMessageWriteStat(MessageWriteStat* mes);
static void ProcessMessageDeleteSharedUprobe(MessageDeleteSharedUprobe* mes);
static void ProcessMessageSymbol(MessageSymbol* mes);
static void ProcessMessageCustom(Message* mes);
static void ProcessOneMessage(Message* mes);
static void TermSignalHandler(SIGNAL_ARGS);
static void StatCollectorAtExitCallback(void);
static void StatCollectorSharedConfigApplyFunc(const char* func, const char* type);
static int
StorageListComparator(UprobeStorage* entry, char* symbol)
{
return strcmp(entry->symbol, symbol);
}
static void
ProcessMessageNewSharedUprobe(MessageNewSharedUprobe* mes)
{
UprobeStorage* newEntry;
if (ListContains(storageList, (void*) currentSymbol))
return;
newEntry = GetUprobeStorageForType(mes->uprobeType, currentSymbol);
if (!newEntry)
return;
ListAdd(storageList, newEntry);
}
static void
ProcessMessageWriteStat(MessageWriteStat* mes)
{
if (currentStorage)
currentStorage->writeStat(currentStorage, mes->shouldEmptyData);
if (mes->latch)
SetLatch(mes->latch);
}
static void
ProcessMessageDeleteSharedUprobe(MessageDeleteSharedUprobe* mes)
{
UprobeStorage* entryToDelete = ListPop(storageList, currentSymbol);
if (entryToDelete == NULL)
{
if (mes->latch && mes->shouldWriteStat)
SetLatch(mes->latch);
return;
}
entryToDelete->delete(entryToDelete, mes->shouldWriteStat);
if (mes->latch && mes->shouldWriteStat)
SetLatch(mes->latch);
}
static void
ProcessMessageSymbol(MessageSymbol* mes)
{
uint16 symbolLen = mes->base.size - sizeof(MessageSymbol);
memcpy(currentSymbol, mes->symbol, symbolLen);
currentSymbol[symbolLen] = '\0';
currentStorage = NULL;
LIST_FOREACH(storageList, it)
{
UprobeStorage* storage = (UprobeStorage*) it->value;
if (strcmp(storage->symbol, currentSymbol) == 0)
{
currentStorage = storage;
break;
}
}
}
static void
ProcessMessageCustom(Message* mes)
{
if (currentStorage)
currentStorage->putData(currentStorage, mes);
}
static void
ProcessOneMessage(Message* mes)
{
switch (mes->type)
{
case MESSAGE_NEW_SHARED_UPROBE:
{
ProcessMessageNewSharedUprobe((MessageNewSharedUprobe*) mes);
break;
}
case MESSAGE_WRITE_STAT:
{
ProcessMessageWriteStat((MessageWriteStat*) mes);
break;
}
case MESSAGE_DELETE_SHARED_UPROBE:
{
ProcessMessageDeleteSharedUprobe((MessageDeleteSharedUprobe*) mes);
break;
}
case MESSAGE_SYMBOL:
{
ProcessMessageSymbol((MessageSymbol*) mes);
break;
}
case MESSAGE_CUSTOM:
{
ProcessMessageCustom(mes);
break;
}
default:
elog(LOG, "invalid message type %d for symbol %128s", mes->type, currentSymbol);
}
}
static void
TermSignalHandler(SIGNAL_ARGS)
{
MessageBufferDelete();
proc_exit(0);
}
static void
StatCollectorAtExitCallback(void)
{
LIST_FOREACH(storageList, it)
{
UprobeStorage* storage = (UprobeStorage*) it->value;
storage->writeStat(storage, false);
}
}
static void
StatCollectorSharedConfigApplyFunc(const char* func, const char* type)
{
MessageNewSharedUprobe mes;
mes.uprobeType = GetTypeByCharName(type);
if (mes.uprobeType == INVALID_TYPE)
{
elog(WARNING, "invalid attach type %128s", type);
return;
}
strncpy(currentSymbol, func, 255);
ProcessMessageNewSharedUprobe(&mes);
}
void
StatCollectorMain(void)
{
Message* messages;
uint32 currentNumberOfMessages;
pqsignal(SIGTERM, TermSignalHandler);
pqsignal(SIGHUP, SIG_IGN);
pqsignal(SIGUSR1, SIG_IGN);
BackgroundWorkerUnblockSignals();
collectorContext = AllocSetContextCreate(NULL, "pg_uprobe collector", ALLOCSET_DEFAULT_SIZES);
MemoryContextSwitchTo(collectorContext);
ListInit(&storageList, (CompareFunction) &StorageListComparator, collectorContext);
PGUprobeLoadFromSharedConfig(&StatCollectorSharedConfigApplyFunc);
before_shmem_exit((pg_on_exit_callback) StatCollectorAtExitCallback, (Datum) 0);
messages = palloc(MESSAGEBUFFER_SIZE);
while (true)
{
Message* currentMessage;
currentNumberOfMessages = MessageBufferGet(messages, MESSAGEBUFFER_SIZE);
currentMessage = messages;
for (uint32 i = 0; i < currentNumberOfMessages; i++)
{
ProcessOneMessage(currentMessage);
currentMessage = (Message*) (((char*) currentMessage) + currentMessage->size);
}
}
}