PostgreSQL Source Code: src/backend/utils/adt/mcxtfuncs.c File Reference (original) (raw)

#include "[postgres.h](postgres%5F8h%5Fsource.html)"
#include "[funcapi.h](funcapi%5F8h%5Fsource.html)"
#include "[mb/pg_wchar.h](pg%5F%5Fwchar%5F8h%5Fsource.html)"
#include "[miscadmin.h](miscadmin%5F8h%5Fsource.html)"
#include "[access/twophase.h](twophase%5F8h%5Fsource.html)"
#include "catalog/pg_authid_d.h"
#include "[storage/proc.h](proc%5F8h%5Fsource.html)"
#include "[storage/procarray.h](procarray%5F8h%5Fsource.html)"
#include "[utils/acl.h](acl%5F8h%5Fsource.html)"
#include "[utils/array.h](array%5F8h%5Fsource.html)"
#include "[utils/builtins.h](builtins%5F8h%5Fsource.html)"
#include "[utils/hsearch.h](hsearch%5F8h%5Fsource.html)"
#include "[utils/memutils.h](memutils%5F8h%5Fsource.html)"
#include "utils/wait_event_types.h"

Go to the source code of this file.

Macros
#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE 1024
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 10
#define MEMSTATS_WAIT_TIMEOUT 100
#define PG_GET_PROCESS_MEMORY_CONTEXTS_COLS 12
Functions
static Datum int_list_to_array (const List *list)
static void PutMemoryContextsStatsTupleStore (Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, HTAB *context_id_lookup)
const char * ContextTypeToString (NodeTag type)
Datum pg_get_backend_memory_contexts (PG_FUNCTION_ARGS)
Datum pg_log_backend_memory_contexts (PG_FUNCTION_ARGS)
Datum pg_get_process_memory_contexts (PG_FUNCTION_ARGS)
Size MemoryContextReportingShmemSize (void)
void MemoryContextReportingShmemInit (void)

MEMORY_CONTEXT_IDENT_DISPLAY_SIZE

#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE 1024

MEMSTATS_WAIT_TIMEOUT

#define MEMSTATS_WAIT_TIMEOUT 100

PG_GET_BACKEND_MEMORY_CONTEXTS_COLS

#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 10

PG_GET_PROCESS_MEMORY_CONTEXTS_COLS

#define PG_GET_PROCESS_MEMORY_CONTEXTS_COLS 12

ContextTypeToString()

const char * ContextTypeToString ( NodeTag type )

Definition at line 165 of file mcxtfuncs.c.

166{

167 const char *context_type;

168

169 switch (type)

170 {

171 case T_AllocSetContext:

172 context_type = "AllocSet";

173 break;

174 case T_GenerationContext:

175 context_type = "Generation";

176 break;

177 case T_SlabContext:

178 context_type = "Slab";

179 break;

180 case T_BumpContext:

181 context_type = "Bump";

182 break;

183 default:

184 context_type = "???";

185 break;

186 }

187 return context_type;

188}

References type.

Referenced by pg_get_process_memory_contexts(), and PutMemoryContextsStatsTupleStore().

int_list_to_array()

static Datum int_list_to_array ( const List * list) static

Definition at line 45 of file mcxtfuncs.c.

46{

47 Datum *datum_array;

48 int length;

50

53

56

58

60}

ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)

static int list_length(const List *l)

#define foreach_current_index(var_or_cell)

#define foreach_int(var, lst)

static Datum PointerGetDatum(const void *X)

static Datum Int32GetDatum(int32 X)

References construct_array_builtin(), foreach_current_index, foreach_int, i, Int32GetDatum(), sort-test::list, list_length(), palloc(), and PointerGetDatum().

Referenced by PutMemoryContextsStatsTupleStore().

MemoryContextReportingShmemInit()

void MemoryContextReportingShmemInit ( void )

Definition at line 637 of file mcxtfuncs.c.

638{

639 bool found;

640

644

645 if (!found)

646 {

649 }

650

654 &found);

655

656 if (found)

657 return;

658

660 {

664 }

665}

void ConditionVariableInit(ConditionVariable *cv)

#define InvalidDsaPointer

#define DSA_HANDLE_INVALID

void LWLockInitialize(LWLock *lock, int tranche_id)

@ LWTRANCHE_MEMORY_CONTEXT_REPORTING_PROC

@ LWTRANCHE_MEMORY_CONTEXT_REPORTING_STATE

struct MemoryStatsBackendState * memCxtState

struct MemoryStatsCtl * memCxtArea

#define NUM_AUXILIARY_PROCS

void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)

dsa_pointer memstats_dsa_pointer

dsa_handle memstats_dsa_handle

References ConditionVariableInit(), DSA_HANDLE_INVALID, i, InvalidDsaPointer, MemoryStatsCtl::lw_lock, LWLockInitialize(), LWTRANCHE_MEMORY_CONTEXT_REPORTING_PROC, LWTRANCHE_MEMORY_CONTEXT_REPORTING_STATE, MaxBackends, memCxtArea, memCxtState, MemoryStatsCtl::memstats_dsa_handle, MemoryStatsBackendState::memstats_dsa_pointer, NUM_AUXILIARY_PROCS, and ShmemInitStruct().

Referenced by CreateOrAttachShmemStructs().

MemoryContextReportingShmemSize()

Size MemoryContextReportingShmemSize ( void )

pg_get_backend_memory_contexts()

Definition at line 195 of file mcxtfuncs.c.

196{

198 int context_id;

199 List *contexts;

201 HTAB *context_id_lookup;

202

206

207 context_id_lookup = hash_create("pg_get_backend_memory_contexts",

208 256,

211

213

214

215

216

217

218

219

220

221

222

223

225

226

227 context_id = 1;

228

230 {

232 bool found;

233

234

235

236

237

238

243

247 context_id_lookup);

248

249

250

251

252

254 contexts = lappend(contexts, c);

255 }

256

258

259 return (Datum) 0;

260}

void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)

void hash_destroy(HTAB *hashp)

HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)

void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)

Assert(PointerIsAligned(start, uint64))

List * lappend(List *list, void *datum)

MemoryContext TopMemoryContext

MemoryContext CurrentMemoryContext

static void PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore, TupleDesc tupdesc, MemoryContext context, HTAB *context_id_lookup)

struct MemoryStatsContextId MemoryStatsContextId

#define foreach_ptr(type, var, lst)

Tuplestorestate * setResult

References Assert(), MemoryStatsContextId::context_id, ctl, cur, CurrentMemoryContext, foreach_ptr, HASH_BLOBS, HASH_CONTEXT, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_search(), InitMaterializedSRF(), lappend(), list_make1, PutMemoryContextsStatsTupleStore(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, and TopMemoryContext.

pg_get_process_memory_contexts()

Definition at line 356 of file mcxtfuncs.c.

357{

363 bool proc_is_aux = false;

367

368

369

370

371

373 if (proc == NULL)

374 {

376 proc_is_aux = true;

377 }

378

379

380

381

382

383

384 if (proc == NULL)

385 {

386

387

388

389

391 errmsg("PID %d is not a PostgreSQL server process", pid));

393 }

394

396

398

402

404

405

406

407

408

410 {

412 errmsg("could not send signal to process %d: %m", pid));

414 }

415

416

417

418

419

420

421

422

423

424

425 while (1)

426 {

427 long msecs;

428

429

430

431

432

433

434

435

436

437

439

440

441

442

443

444

445

446

447 if (memCxtState[procNumber].proc_id == pid)

448 {

449

450

451

452

453

455 memCxtState[procNumber].stats_timestamp);

456

458 && msecs > 0)

459 break;

460 }

462

463

464

465

466

467

468 if (proc_is_aux)

470 else

472

473

474

475

476

477 if (proc == NULL)

478 {

480 errmsg("PID %d is no longer a PostgreSQL server process",

481 pid));

483 }

484

486

487

488

489

490

491 if (msecs > 0 && msecs < (timeout * 1000))

492 {

493

494

495

496

497

498

499

500

502 ((timeout * 1000) - msecs), WAIT_EVENT_MEM_CXT_PUBLISH))

503 {

505

507 break;

508 else

509 {

512 }

513 }

514 }

515 else

516 {

518

520 break;

521 else

522 {

525 }

526 }

527 }

528

529

530

531

532

533

535

537 {

539

544 }

545

546

547

548

551

552#define PG_GET_PROCESS_MEMORY_CONTEXTS_COLS 12

554 {

561 Datum *path_datum = NULL;

562 int *path_int = NULL;

563

565 memset(nulls, 0, sizeof(nulls));

566

568 {

571 }

572 else

573 nulls[0] = true;

574

576 {

579 }

580 else

581 nulls[1] = true;

582

584

588 {

594 }

595 else

596 nulls[3] = true;

597

604 memcxt_info[i].freespace);

607

610 }

612

614

616}

long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)

TimestampTz GetCurrentTimestamp(void)

static Datum values[MAXATTR]

#define CStringGetTextDatum(s)

bool ConditionVariableCancelSleep(void)

bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)

dsa_area * dsa_attach(dsa_handle handle)

void * dsa_get_address(dsa_area *area, dsa_pointer dp)

void dsa_pin_mapping(dsa_area *area)

#define DsaPointerIsValid(x)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

Datum Int64GetDatum(int64 X)

#define PG_GETARG_FLOAT8(n)

#define PG_GETARG_INT32(n)

#define PG_GETARG_BOOL(n)

Datum path_length(PG_FUNCTION_ARGS)

bool LWLockAcquire(LWLock *lock, LWLockMode mode)

void LWLockRelease(LWLock *lock)

dsa_area * MemoryStatsDsaArea

const char * ContextTypeToString(NodeTag type)

#define PG_GET_PROCESS_MEMORY_CONTEXTS_COLS

static MemoryContext MemoryContextSwitchTo(MemoryContext context)

#define GetNumberFromPGProc(proc)

PGPROC * BackendPidGetProc(int pid)

#define INVALID_PROC_NUMBER

int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)

@ PROCSIG_GET_MEMORY_CONTEXT

PGPROC * AuxiliaryPidGetProc(int pid)

void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)

static Datum TimestampTzGetDatum(TimestampTz X)

References Assert(), AuxiliaryPidGetProc(), BackendPidGetProc(), ConditionVariableCancelSleep(), ConditionVariableTimedSleep(), construct_array_builtin(), ContextTypeToString(), CStringGetTextDatum, CurrentMemoryContext, dsa_attach(), dsa_get_address(), DSA_HANDLE_INVALID, dsa_pin_mapping(), DsaPointerIsValid, ereport, errmsg(), GetCurrentTimestamp(), GetNumberFromPGProc, i, ident, InitMaterializedSRF(), Int32GetDatum(), Int64GetDatum(), INVALID_PROC_NUMBER, j, LW_EXCLUSIVE, MemoryStatsCtl::lw_lock, LWLockAcquire(), LWLockRelease(), memCxtArea, memCxtState, MemoryContextSwitchTo(), MemoryStatsDsaArea, MemoryStatsCtl::memstats_dsa_handle, name, palloc(), path_length(), PG_GET_PROCESS_MEMORY_CONTEXTS_COLS, PG_GETARG_BOOL, PG_GETARG_FLOAT8, PG_GETARG_INT32, PG_RETURN_NULL, PointerGetDatum(), PROCSIG_GET_MEMORY_CONTEXT, SendProcSignal(), ReturnSetInfo::setDesc, ReturnSetInfo::setResult, MemoryStatsBackendState::summary, TimestampDifferenceMilliseconds(), TimestampTzGetDatum(), TopMemoryContext, MemoryStatsBackendState::total_stats, tuplestore_putvalues(), type, values, and WARNING.

pg_log_backend_memory_contexts()

Definition at line 276 of file mcxtfuncs.c.

277{

281

282

283

284

286 if (proc == NULL)

288

289

290

291

292

293

294

295

296

297

298 if (proc == NULL)

299 {

300

301

302

303

305 (errmsg("PID %d is not a PostgreSQL server process", pid)));

307 }

308

311 {

312

314 (errmsg("could not send signal to process %d: %m", pid)));

316 }

317

319}

#define PG_RETURN_BOOL(x)

@ PROCSIG_LOG_MEMORY_CONTEXT

References AuxiliaryPidGetProc(), BackendPidGetProc(), ereport, errmsg(), GetNumberFromPGProc, INVALID_PROC_NUMBER, PG_GETARG_INT32, PG_RETURN_BOOL, PROCSIG_LOG_MEMORY_CONTEXT, SendProcSignal(), and WARNING.

PutMemoryContextsStatsTupleStore()

Definition at line 67 of file mcxtfuncs.c.

70{

71#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 10

72

77 const char *name;

78 const char *ident;

79 const char *type;

80

82

83

84

85

86

88 {

90 bool found;

91

93

94 if (!found)

95 elog(ERROR, "hash table corrupted");

97 }

98

99

100 memset(&stat, 0, sizeof(stat));

101 (*context->methods->stats) (context, NULL, NULL, &stat, true);

102

104 memset(nulls, 0, sizeof(nulls));

105

108

109

110

111

112

113 if (ident && strcmp(name, "dynahash") == 0)

114 {

117 }

118

121 else

122 nulls[0] = true;

123

125 {

126 int idlen = strlen(ident);

128

129

130

131

132

135

136 memcpy(clipped_ident, ident, idlen);

137 clipped_ident[idlen] = '\0';

139 }

140 else

141 nulls[1] = true;

142

144

153

156}

List * lcons_int(int datum, List *list)

void list_free(List *list)

int pg_mbcliplen(const char *mbstr, int len, int limit)

static Datum int_list_to_array(const List *list)

#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS

#define MEMORY_CONTEXT_IDENT_DISPLAY_SIZE

#define MemoryContextIsValid(context)

const MemoryContextMethods * methods

void(* stats)(MemoryContext context, MemoryStatsPrintFunc printfunc, void *passthru, MemoryContextCounters *totals, bool print_to_stderr)

References Assert(), MemoryStatsContextId::context_id, ContextTypeToString(), CStringGetTextDatum, cur, elog, ERROR, HASH_FIND, hash_search(), MemoryContextData::ident, ident, Int32GetDatum(), Int64GetDatum(), int_list_to_array(), lcons_int(), list_free(), list_length(), MEMORY_CONTEXT_IDENT_DISPLAY_SIZE, MemoryContextIsValid, MemoryContextData::methods, name, MemoryContextData::name, NIL, PG_GET_BACKEND_MEMORY_CONTEXTS_COLS, pg_mbcliplen(), stat, MemoryContextMethods::stats, tuplestore_putvalues(), type, and values.

Referenced by pg_get_backend_memory_contexts().

memCxtArea

memCxtState