Skip to content

Commit

Permalink
Track startup memory for resource group
Browse files Browse the repository at this point in the history
Greenplum has several different counters to track virtual memory consumption
- `pResGroupControl->totalChunks` is the total number of memory on this segment
- `pResGroupControl->freeChunks` is the current amount of shared global memory
- `self->slot->memUsage` is the current session's slot consumption. It can be
 greater than self->slot->memQuota if group or global shared memory is consumed
- `self->group` is the current session's group consumption
- `self->memUsage` is the current process' consumption
- `MySessionState->sessionVmem` is the current session's consumption
- `segmentVmemChunks` is the segment's total consumption

You can see that some of these counters basically count the same things yet
they still have different variables for that. Such approach requires
consistency across all these counters, however, gpdb fails to provide it.

A newly forked procces calls `VmemTracker_RegisterStartupMemory` to track some
memory it inherited from a parent process, it's usually about 12MB. It adds
this memory to `MySessionState->sessionVmem` and `segmentVmemChunks` before
vmem tracker is initialized. When vmem tracker is ready and a group is assigned
to a proccess groupIncMemUsage is called to add this startup memory to group.
However, the arguments are `groupIncMemUsage(group, slot, self->memUsage)`
where `self->memUsage` is 0 at this moment because
`VmemTracker_RegisterStartupMemory` didn't increase it. This patch adds startup
memory to self->memUsage to fix this behavior.
  • Loading branch information
dnskvlnk committed Aug 26, 2024
1 parent 3567721 commit 398662f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/backend/utils/mmgr/vmem_tracker.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,8 @@ VmemTracker_RegisterStartupMemory(int64 bytes)
pg_atomic_add_fetch_u32((pg_atomic_uint32 *) segmentVmemChunks,
startupChunks);

selfAddStartupChunks(startupChunks);

/*
* Step 2, check if an OOM error should be raised by allocating 0 chunk.
*/
Expand All @@ -692,6 +694,8 @@ VmemTracker_UnregisterStartupMemory(void)
pg_atomic_sub_fetch_u32((pg_atomic_uint32 *) &MySessionState->sessionVmem,
startupChunks);

selfSubStartupChunks(startupChunks);

trackedBytes -= startupBytes;
trackedVmemChunks -= startupChunks;

Expand Down
20 changes: 20 additions & 0 deletions src/backend/utils/resgroup/resgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,26 @@ selfDetachResGroup(ResGroupData *group, ResGroupSlotData *slot)
selfUnsetGroup();
}

/*
* Add startup memory before a resgroup is assigned. This memory
* will later be added to resgroup via selfAttachResGroup
*/
void
selfAddStartupChunks(int32 chunks)
{
self->memUsage += chunks;
}

/*
* Sub startup memory at cleanup. This memory should already been
* subtracted from a resource group via selfDetachResGroup
*/
void
selfSubStartupChunks(int32 chunks)
{
self->memUsage -= chunks;
}

/*
* Initialize the members of a slot
*/
Expand Down
3 changes: 3 additions & 0 deletions src/include/utils/resgroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ extern Oid ResGroupGetGroupIdBySessionId(int sessionId);
extern char *getCpuSetByRole(const char *cpuset);
extern void checkCpuSetByRole(const char *cpuset);

extern void selfAddStartupChunks(int32 chunks);
extern void selfSubStartupChunks(int32 chunks);

#define LOG_RESGROUP_DEBUG(...) \
do {if (Debug_resource_group) elog(__VA_ARGS__); } while(false);

Expand Down

0 comments on commit 398662f

Please sign in to comment.