forked from heterodb/pg-strom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_utils.h
566 lines (506 loc) · 12.5 KB
/
pg_utils.h
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
/*
* pg_utils.h
*
* Inline routines of misc utility purposes
* --
* Copyright 2011-2023 (C) KaiGai Kohei <[email protected]>
* Copyright 2014-2023 (C) PG-Strom Developers Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the PostgreSQL License.
*/
#ifndef PG_UTILS_H
#define PG_UTILS_H
/* Max/Min macros that takes 3 or more arguments */
#define Max3(a,b,c) ((a) > (b) ? Max((a),(c)) : Max((b),(c)))
#define Max4(a,b,c,d) Max(Max((a),(b)), Max((c),(d)))
#define Min3(a,b,c) ((a) > (b) ? Min((a),(c)) : Min((b),(c)))
#define Min4(a,b,c,d) Min(Min((a),(b)), Min((c),(d)))
/*
* transformation from align character into width
*/
static inline int
typealign_get_width(char type_align)
{
switch (type_align)
{
case 'c':
return 1;
case 's':
return ALIGNOF_SHORT;
case 'i':
return ALIGNOF_INT;
case 'd':
return ALIGNOF_DOUBLE;
default:
elog(ERROR, "unexpected type alignment: %c", type_align);
}
return -1; /* be compiler quiet */
}
/*
* get_next_log2
*
* It returns N of the least 2^N value that is larger than or equal to
* the supplied value.
*/
static inline int
get_next_log2(Size size)
{
int shift = 0;
if (size == 0 || size == 1)
return 0;
size--;
#ifdef __GNUC__
shift = sizeof(Size) * BITS_PER_BYTE - __builtin_clzl(size);
#else
#if SIZEOF_VOID_P == 8
if ((size & 0xffffffff00000000UL) != 0)
{
size >>= 32;
shift += 32;
}
#endif
if ((size & 0xffff0000UL) != 0)
{
size >>= 16;
shift += 16;
}
if ((size & 0x0000ff00UL) != 0)
{
size >>= 8;
shift += 8;
}
if ((size & 0x000000f0UL) != 0)
{
size >>= 4;
shift += 4;
}
if ((size & 0x0000000cUL) != 0)
{
size >>= 2;
shift += 2;
}
if ((size & 0x00000002UL) != 0)
{
size >>= 1;
shift += 1;
}
if ((size & 0x00000001UL) != 0)
shift += 1;
#endif /* !__GNUC__ */
return shift;
}
/*
* get_next_log2
*
* It returns N of the least 2^N value that is larger than or equal to
* the supplied value.
*/
static inline int
get_bitcount(unsigned long val)
{
#ifdef __GNUC__
return __builtin_popcountl(val);
#else
static int __bitcount_4bit[16] = {
0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4
};
int count = 0;
while (val != 0)
{
count += __bitcount_4bit[(val & 0x0f)];
val >>= 4;
}
return count;
#endif /* !__GNUC__ */
}
/*
* __trim - remove whitespace at the head/tail of cstring
*/
static inline char *
__trim(char *token)
{
char *tail = token + strlen(token) - 1;
while (isspace(*token))
token++;
while (tail >= token && isspace(*tail))
*tail-- = '\0';
return token;
}
/*
* __strtol / __strtoul / __strtosz - set errno if token is not pure digits
*/
static inline long int
__strtol(const char *token)
{
long int ival;
char *end;
errno = 0; /* clear */
ival = strtol(token, &end, 10);
if (*end != '\0')
errno = EINVAL;
return ival;
}
static inline unsigned long int
__strtoul(const char *token)
{
unsigned long int ival;
char *end;
errno = 0; /* clear */
ival = strtoul(token, &end, 10);
if (*end != '\0')
errno = EINVAL;
return ival;
}
static inline size_t
__strtosz(const char *token)
{
size_t sz;
char *end;
errno = 0; /* clear */
sz = strtoul(token, &end, 10);
if (errno == 0)
{
if (strcasecmp(end, "t") == 0 || strcasecmp(end, "tb") == 0)
{
if (sz > 0x0000000000ffffffUL)
errno = ERANGE;
else
sz <<= 40;
}
else if (strcasecmp(end, "g") == 0 || strcasecmp(end, "gb") == 0)
{
if (sz > 0x00000003ffffffffUL)
errno = ERANGE;
else
sz <<= 30;
}
else if (strcasecmp(end, "m") == 0 || strcasecmp(end, "mb") == 0)
{
if (sz > 0x00000fffffffffffUL)
errno = ERANGE;
else
sz <<= 20;
}
else if (strcasecmp(end, "k") == 0 || strcasecmp(end, "kb") == 0)
{
if (sz > 0x003fffffffffffffUL)
errno = ERANGE;
else
sz <<= 10;
}
else if (*end != '\0')
errno = EINVAL;
}
return sz;
}
/* lappend on the specified memory-context */
static inline List *
lappend_cxt(MemoryContext memcxt, List *list, void *datum)
{
MemoryContext oldcxt = MemoryContextSwitchTo(memcxt);
List *r;
r = lappend(list, datum);
MemoryContextSwitchTo(oldcxt);
return r;
}
/* initStringInfo on the specified memory-context */
static inline void
initStringInfoCxt(MemoryContext memcxt, StringInfo buf)
{
MemoryContext oldcxt;
oldcxt = MemoryContextSwitchTo(memcxt);
initStringInfo(buf);
MemoryContextSwitchTo(oldcxt);
}
/*
* formater of bytesz/millisec
*/
static inline char *
__format_bytesz(char *buffer, size_t bufsz, size_t nbytes)
{
if (nbytes > (1UL<<43))
snprintf(buffer, bufsz, "%.2fTB", (double)nbytes / (double)(1UL<<40));
else if (nbytes > (1UL<<33))
snprintf(buffer, bufsz, "%.2fGB", (double)nbytes / (double)(1UL<<30));
else if (nbytes > (1UL<<23))
snprintf(buffer, bufsz, "%.2fMB", (double)nbytes / (double)(1UL<<20));
else if (nbytes > (1UL<<13))
snprintf(buffer, bufsz, "%.2fKB", (double)nbytes / (double)(1UL<<10));
else
snprintf(buffer, bufsz, "%uB", (unsigned int)nbytes);
return buffer;
}
#define format_bytesz(nbytes) \
__format_bytesz(alloca(40), 40, (nbytes))
static inline char *
__format_millisec(char *buffer, size_t bufsz, double msec)
{
if (msec > 300000.0) /* more than 5min */
snprintf(buffer, bufsz, "%.2fmin", msec / 60000.0);
else if (msec > 8000.0)
snprintf(buffer, bufsz, "%.2fsec", msec / 1000.0);
else
snprintf(buffer, bufsz, "%.0fmsec", msec);
return buffer;
}
#define format_millisec(msec) \
__format_millisec(alloca(40), 40, (msec))
/*
* pmemdup
*/
static inline void *
pmemdup(const void *src, Size sz)
{
void *dst = palloc(sz);
memcpy(dst, src, sz);
return dst;
}
/*
* buildoidvector
*/
static inline oidvector *
__buildoidvector1(Oid a)
{
return buildoidvector(&a, 1);
}
static inline oidvector *
__buildoidvector2(Oid a, Oid b)
{
Oid oids[2];
oids[0] = a;
oids[1] = b;
return buildoidvector(oids, 2);
}
static inline oidvector *
__buildoidvector3(Oid a, Oid b, Oid c)
{
Oid oids[3];
oids[0] = a;
oids[1] = b;
oids[2] = c;
return buildoidvector(oids, 3);
}
static inline oidvector *
__buildoidvector4(Oid a, Oid b, Oid c, Oid d)
{
Oid oids[4];
oids[0] = a;
oids[1] = b;
oids[2] = c;
oids[3] = d;
return buildoidvector(oids, 4);
}
/*
* Macros for worker threads
*/
#define __FATAL(fmt,...) \
do { \
fprintf(stderr, "(%s:%d) " fmt "\n", \
basename(__FILE__), __LINE__, \
##__VA_ARGS__); \
_exit(1); \
} while(0)
static inline void
pthreadMutexInit(pthread_mutex_t *mutex)
{
if ((errno = pthread_mutex_init(mutex, NULL)) != 0)
__FATAL("failed on pthread_mutex_init: %m");
}
static inline void
pthreadMutexInitShared(pthread_mutex_t *mutex)
{
pthread_mutexattr_t mattr;
if ((errno = pthread_mutexattr_init(&mattr)) != 0)
__FATAL("failed on pthread_mutexattr_init: %m");
if ((errno = pthread_mutexattr_setpshared(&mattr, 1)) != 0)
__FATAL("failed on pthread_mutexattr_setpshared: %m");
if ((errno = pthread_mutex_init(mutex, &mattr)) != 0)
__FATAL("failed on pthread_mutex_init: %m");
if ((errno = pthread_mutexattr_destroy(&mattr)) != 0)
__FATAL("failed on pthread_mutexattr_destroy: %m");
}
static inline void
pthreadMutexLock(pthread_mutex_t *mutex)
{
if ((errno = pthread_mutex_lock(mutex)) != 0)
__FATAL("failed on pthread_mutex_lock: %m");
}
static inline bool
pthreadMutexTryLock(pthread_mutex_t *mutex)
{
if ((errno = pthread_mutex_trylock(mutex)) == 0)
return true;
if (errno != EBUSY)
__FATAL("failed on pthread_mutex_trylock: %m");
return false;
}
static inline void
pthreadMutexUnlock(pthread_mutex_t *mutex)
{
if ((errno = pthread_mutex_unlock(mutex)) != 0)
__FATAL("failed on pthread_mutex_unlock: %m");
}
static inline void
pthreadRWLockInit(pthread_rwlock_t *rwlock)
{
if ((errno = pthread_rwlock_init(rwlock, NULL)) != 0)
__FATAL("failed on pthread_rwlock_init: %m");
}
static inline void
pthreadRWLockInitShared(pthread_rwlock_t *rwlock)
{
pthread_rwlockattr_t rwattr;
if ((errno = pthread_rwlockattr_init(&rwattr)) != 0)
__FATAL("failed on pthread_rwlockattr_init: %m");
if ((errno = pthread_rwlockattr_setpshared(&rwattr, 1)) != 0)
__FATAL("failed on pthread_rwlockattr_setpshared: %m");
if ((errno = pthread_rwlock_init(rwlock, &rwattr)) != 0)
__FATAL("failed on pthread_rwlock_init: %m");
}
static inline void
pthreadRWLockReadLock(pthread_rwlock_t *rwlock)
{
if ((errno = pthread_rwlock_rdlock(rwlock)) != 0)
__FATAL("failed on pthread_rwlock_rdlock: %m");
}
static inline void
pthreadRWLockWriteLock(pthread_rwlock_t *rwlock)
{
if ((errno = pthread_rwlock_wrlock(rwlock)) != 0)
__FATAL("failed on pthread_rwlock_wrlock: %m");
}
static inline void
pthreadRWLockUnlock(pthread_rwlock_t *rwlock)
{
if ((errno = pthread_rwlock_unlock(rwlock)) != 0)
__FATAL("failed on pthread_rwlock_unlock: %m");
}
static inline void
pthreadCondInit(pthread_cond_t *cond)
{
if ((errno = pthread_cond_init(cond, NULL)) != 0)
__FATAL("failed on pthread_cond_init: %m");
}
static inline void
pthreadCondInitShared(pthread_cond_t *cond)
{
pthread_condattr_t condattr;
if ((errno = pthread_condattr_init(&condattr)) != 0)
__FATAL("failed on pthread_condattr_init: %m");
if ((errno = pthread_condattr_setpshared(&condattr, 1)) != 0)
__FATAL("failed on pthread_condattr_setpshared: %m");
if ((errno = pthread_cond_init(cond, &condattr)) != 0)
__FATAL("failed on pthread_cond_init: %m");
if ((errno = pthread_condattr_destroy(&condattr)) != 0)
__FATAL("failed on pthread_condattr_destroy: %m");
}
static inline void
pthreadCondWait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
if ((errno = pthread_cond_wait(cond, mutex)) != 0)
__FATAL("failed on pthread_cond_wait: %m");
}
static inline bool
pthreadCondWaitTimeout(pthread_cond_t *cond, pthread_mutex_t *mutex,
long timeout_ms)
{
struct timespec tm;
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec += (timeout_ms / 1000);
tm.tv_nsec += (timeout_ms % 1000) * 1000000;
if (tm.tv_nsec >= 1000000000L)
{
tm.tv_sec += tm.tv_nsec / 1000000000;
tm.tv_nsec = tm.tv_nsec % 1000000000;
}
errno = pthread_cond_timedwait(cond, mutex, &tm);
if (errno == 0)
return true;
else if (errno == ETIMEDOUT)
return false;
__FATAL("failed on pthread_cond_timedwait: %m");
}
static inline void
pthreadCondBroadcast(pthread_cond_t *cond)
{
if ((errno = pthread_cond_broadcast(cond)) != 0)
__FATAL("failed on pthread_cond_broadcast: %m");
}
static inline void
pthreadCondSignal(pthread_cond_t *cond)
{
if ((errno = pthread_cond_signal(cond)) != 0)
__FATAL("failed on pthread_cond_signal: %m");
}
/*
* Misc debug functions
*/
INLINE_FUNCTION(void)
dump_tuple_desc(const TupleDesc tdesc)
{
fprintf(stderr, "tupdesc %p { natts=%d, tdtypeid=%u, tdtypmod=%d, tdrefcount=%d }\n",
tdesc,
tdesc->natts,
tdesc->tdtypeid,
tdesc->tdtypmod,
tdesc->tdrefcount);
for (int j=0; j < tdesc->natts; j++)
{
Form_pg_attribute attr = TupleDescAttr(tdesc, j);
fprintf(stderr, "attr[%d] { attname='%s', atttypid=%u, attlen=%d, attnum=%d, atttypmod=%d, attbyval=%c, attalign=%c, attnotnull=%c attisdropped=%c }\n",
j,
NameStr(attr->attname),
attr->atttypid,
(int)attr->attlen,
(int)attr->attnum,
(int)attr->atttypmod,
attr->attbyval ? 't' : 'f',
attr->attalign,
attr->attnotnull ? 't' : 'f',
attr->attisdropped ? 't' : 'f');
}
}
/*
* dump_kern_data_store
*/
INLINE_FUNCTION(void)
dump_kern_data_store(const kern_data_store *kds)
{
fprintf(stderr, "kds %p { length=%lu, usage=%lu, nitems=%u, ncols=%u, format=%c, has_varlena=%c, tdhasoid=%c, tdtypeid=%u, tdtypmod=%d, table_oid=%u, hash_nslots=%u, block_offset=%u, block_nloaded=%u, nr_colmeta=%u }\n",
kds,
kds->length,
kds->usage,
kds->nitems,
kds->ncols,
kds->format,
kds->has_varlena ? 't' : 'f',
kds->tdhasoid ? 't' : 'f',
kds->tdtypeid,
kds->tdtypmod,
kds->table_oid,
kds->hash_nslots,
kds->block_offset,
kds->block_nloaded,
kds->nr_colmeta);
for (int j=0; j < kds->nr_colmeta; j++)
{
const kern_colmeta *cmeta = &kds->colmeta[j];
fprintf(stderr, "cmeta[%d] { attbyval=%c, attalign=%d, attlen=%d, attnum=%d, attcacheoff=%d, atttypid=%u, atttypmod=%d, atttypkind=%c, kds_format=%c, kds_offset=%u, idx_subattrs=%u, num_subattrs=%u, attname='%s' }\n",
j,
cmeta->attbyval ? 't' : 'f',
(int)cmeta->attalign,
(int)cmeta->attlen,
(int)cmeta->attnum,
(int)cmeta->attcacheoff,
cmeta->atttypid,
cmeta->atttypmod,
cmeta->atttypkind,
cmeta->kds_format,
cmeta->kds_offset,
(unsigned int)cmeta->idx_subattrs,
(unsigned int)cmeta->num_subattrs,
cmeta->attname);
}
}
#endif /* PG_UTILS_H */