-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlockmanager_trace.c
338 lines (275 loc) · 8.33 KB
/
lockmanager_trace.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
#include "postgres.h"
#include <time.h>
#include "utils/hsearch.h"
#include "storage/lwlock.h"
#include "uprobe_message_buffer.h"
#include "trace_file.h"
#include "lockmanager_trace.h"
#define INVALID_PARTITION -1
typedef struct LockManagerMapKey
{
int partition;
uint64 time;
} LockManagerMapKey;
typedef struct LockManagerMapEntry
{
LockManagerMapKey key;
uint64 count;
} LockManagerMapEntry;
typedef struct LockManagerStorage
{
UprobeStorage base;
HTAB* map;
} LockManagerStorage;
typedef struct LockManagerMessage
{
Message base;
LockManagerMapEntry entry;
} LockManagerMessage;
typedef struct LockManagerData
{
HTAB* map;
} LockManagerData;
//lock Acquire data
static int partition;
static uint64 acquireTime;
static void LWLockAcquireInFunc(void* data, LWLock* lock);
static void LWLockAcquireRetFunc(void* data);
static void LWLockAcquireClean(UprobeAttachInterface* this);
static HTAB* CreateLockManagerHashMap(void);
static void LWLockReleaseInFunc(LockManagerData* data);
static void LWLockReleaseRetFunc(LockManagerData* data);
static void LWLockReleaseTimedCallback(UprobeAttachInterface* this);
static void LWLockReleaseClean(UprobeAttachInterface* this);
static void LockManagerStoragePutData(LockManagerStorage* storage, LockManagerMessage* mes);
static void LockManagerStorageWriteStat(LockManagerStorage* storage, bool shouldClearStat);
static void LockManagerStorageDelete(LockManagerStorage* storage, bool shouldWriteStat);
static void
LWLockAcquireInFunc(void* data, LWLock* lock)
{
LWLockPadded* l;
partition = INVALID_PARTITION;
if (lock->tranche != LWTRANCHE_LOCK_MANAGER)
return;
l = MainLWLockArray + LOCK_MANAGER_LWLOCK_OFFSET;
for (int id = 0; id < NUM_LOCK_PARTITIONS; id++, l++)
{
if (&l->lock == lock)
partition = id;
}
}
static void
LWLockAcquireRetFunc(void* data)
{
struct timespec time;
if (partition == INVALID_PARTITION)
return;
clock_gettime(CLOCK_MONOTONIC, &time);
acquireTime = time.tv_nsec + time.tv_sec * 1000000000L;
}
static void
LWLockAcquireClean(UprobeAttachInterface* this)
{
if (this == NULL)
return;
pfree(this->targetSymbol);
pfree(this);
}
static HTAB*
CreateLockManagerHashMap(void)
{
HTAB* map;
HASHCTL map_info;
map_info.keysize = sizeof(LockManagerMapKey);
map_info.entrysize = sizeof(LockManagerMapEntry);
map = hash_create("map for Lock Manager trace", 1024, &map_info, HASH_ELEM | HASH_BLOBS);
return map;
}
static void
LWLockReleaseInFunc(LockManagerData* data)
{
struct timespec time;
uint64 timeDiff;
LockManagerMapKey key;
LockManagerMapEntry* entry;
bool isFound;
if (partition == INVALID_PARTITION)
return;
clock_gettime(CLOCK_MONOTONIC, &time);
timeDiff = time.tv_nsec + time.tv_sec * 1000000000L - acquireTime;
key.partition = partition;
key.time = timeDiff / 100 + ((timeDiff % 100 >= 50) ? 1: 0);
entry = (LockManagerMapEntry*) hash_search(data->map, &key, HASH_ENTER_NULL, &isFound);
if (likely(entry))
{
if (isFound)
entry->count++;
else
entry->count = 1;
}
}
static void
LWLockReleaseRetFunc(LockManagerData* data)
{
}
static void
LWLockReleaseTimedCallback(UprobeAttachInterface* this)
{
LockManagerData* data = ( LockManagerData*) this->data;
LockManagerMessage messages[1024];
uint32 currentIndex = 0;
uint32 numSend = 0;
HASH_SEQ_STATUS mapIterator;
LockManagerMapEntry* mapEntry;
hash_seq_init(&mapIterator, data->map);
mapEntry = (LockManagerMapEntry*) hash_seq_search(&mapIterator);
while (mapEntry)
{
messages[currentIndex].base.type = MESSAGE_CUSTOM;
messages[currentIndex].base.size = sizeof(LockManagerMessage);
messages[currentIndex].entry = *mapEntry;
currentIndex++;
if (currentIndex == 1024)
{
numSend = MessageBufferPut((Message*) messages, currentIndex, this->targetSymbol);
if (numSend != currentIndex)
{
memmove(messages, &messages[numSend], (currentIndex - numSend) * sizeof(LockManagerMessage));
}
currentIndex = currentIndex - numSend;
}
mapEntry = (LockManagerMapEntry*) hash_seq_search(&mapIterator);
}
while (currentIndex != 0)
{
numSend = MessageBufferPut((Message*) messages, currentIndex, this->targetSymbol);
if (numSend != currentIndex)
{
memmove(messages, &messages[numSend], (currentIndex - numSend) * sizeof(LockManagerMessage));
}
currentIndex = currentIndex - numSend;
}
hash_destroy(data->map);
data->map = CreateLockManagerHashMap();
}
static void
LWLockReleaseClean(UprobeAttachInterface* this)
{
LockManagerData* data;
if (!this)
return;
data = (LockManagerData*) this->data;
hash_destroy(data->map);
pfree(data);
pfree(this->targetSymbol);
pfree(this);
}
static void
LockManagerStoragePutData(LockManagerStorage* storage, LockManagerMessage* mes)
{
LockManagerMapEntry* entry;
bool isFound;
entry = (LockManagerMapEntry*) hash_search(storage->map, &mes->entry.key, HASH_ENTER_NULL, &isFound);
if (likely(entry))
{
if (isFound)
{
entry->count += mes->entry.count;
}
else
{
entry->count = mes->entry.count;
}
}
}
static void
LockManagerStorageWriteStat(LockManagerStorage* storage, bool shouldClearStat)
{
FILE* files[256];
char filePath[MAXPGPATH];
HASH_SEQ_STATUS mapIterator;
LockManagerMapEntry* mapEntry;
for (int i = 0; i < NUM_LOCK_PARTITIONS; i++)
{
sprintf(filePath,"%sLock_Manager_%d.txt", dataDir, i);
files[i] = fopen(filePath, "w");
if (files[i] == NULL)
{
elog(LOG, "can't open file %s for writting", filePath);
for (int j = 0; j < i; j++)
fclose(files[j]);
return;
}
fprintf(files[i], "time,count\n");
}
hash_seq_init(&mapIterator, storage->map);
mapEntry = (LockManagerMapEntry*) hash_seq_search(&mapIterator);
while (mapEntry)
{
double time = (double) mapEntry->key.time / 10.0;
fprintf(files[mapEntry->key.partition], "%.1lf,%lu\n", time, mapEntry->count);
mapEntry = (LockManagerMapEntry*) hash_seq_search(&mapIterator);
}
for (int i = 0; i < NUM_LOCK_PARTITIONS; i++)
fclose(files[i]);
if (shouldClearStat)
{
hash_destroy(storage->map);
storage->map = CreateLockManagerHashMap();
}
}
static void
LockManagerStorageDelete(LockManagerStorage* storage, bool shouldWriteStat)
{
if (shouldWriteStat)
LockManagerStorageWriteStat(storage, false);
hash_destroy(storage->map);
pfree(storage->base.symbol);
pfree(storage);
}
UprobeAttachInterface*
LWLockAcquireInit(const char* symbol)
{
UprobeAttachInterface* res = palloc(sizeof(UprobeAttachInterface));
res->inFunc = LWLockAcquireInFunc;
res->retFunc = LWLockAcquireRetFunc;
res->timedCallback = NULL;
res->cleanFunc = LWLockAcquireClean;
res->needRetVal = false;
res->numArgs = 1;
res->data = NULL;
res->targetSymbol = pstrdup(symbol);
return res;
}
UprobeAttachInterface*
LWLockReleaseInit(const char* symbol)
{
UprobeAttachInterface* res = palloc(sizeof(UprobeAttachInterface));
LockManagerData* storage = palloc(sizeof(LockManagerData));
res->inFunc = LWLockReleaseInFunc;
res->retFunc = LWLockReleaseRetFunc;
res->timedCallback = LWLockReleaseTimedCallback;
res->cleanFunc = LWLockReleaseClean;
res->needRetVal = false;
res->numArgs = 0;
res->data = storage;
res->targetSymbol = pstrdup(symbol);
storage->map = CreateLockManagerHashMap();
return res;
}
UprobeStorage*
LockManagerStorageInit(const char* symbol)
{
LockManagerStorage* storage = palloc(sizeof(LockManagerStorage));
storage->base.delete = (StorageDeleteFunc) LockManagerStorageDelete;
storage->base.putData = (StoragePutDataFunc) LockManagerStoragePutData;
storage->base.writeStat = (StorageWriteStat) LockManagerStorageWriteStat;
storage->base.symbol = pstrdup(symbol);
storage->map = CreateLockManagerHashMap();
return (UprobeStorage*) storage;
}
UprobeStorage*
NullStorageInit(const char* symbol)
{
return NULL;
}