Skip to content

Commit

Permalink
[#19134] YSQL, ASH: Setting ASH circular buffer size based on the num…
Browse files Browse the repository at this point in the history
…ber of cores

Summary:
The default value of the guc/gflag yb_ash_circular_buffer_size is changed to zero.

If the user doesn't specify the value of guc/gflag, this is overridden based on the
number of cores available on the machine. If the user specifies the value, then
the circular buffer is created with the size the user provides.

The defaults are -
- 32 MiB for 1-2 cores
- 64 MiB for 3-4 cores
- 128 MiB for 5-8 cores
- 256 MiB for 9-16 cores
- 512 MiB for 17-32 cores
- 1024 MiB for > 32 cores
Jira: DB-7932

Test Plan:
Manually tested

In mac
```
$ sysctl -n hw.ncpu
10

$ bin/yb-ctl start --tserver_flags='"allowed_preview_flags_csv=ysql_yb_enable_ash,ysql_yb_ash_enable_infra",ysql_yb_ash_enable_infra=true,ysql_yb_enable_ash=true'

yugabyte=# show yb_ash_circular_buffer_size;
 yb_ash_circular_buffer_size
-----------------------------
 256MB

$ bin/yb-ctl restart --tserver_flags='"allowed_preview_flags_csv=ysql_yb_enable_ash,ysql_yb_ash_enable_infra",ysql_yb_ash_enable_infra=true,ysql_yb_enable_ash=true,ysql_yb_ash_circular_buffer_size=10240'

yugabyte=# show yb_ash_circular_buffer_size;
 yb_ash_circular_buffer_size
-----------------------------
 10MB
```

In alma linux 8

```
$ nproc
4

$ bin/yb-ctl start --tserver_flags='"allowed_preview_flags_csv=ysql_yb_enable_ash,ysql_yb_ash_enable_infra",ysql_yb_ash_enable_infra=true,ysql_yb_enable_ash=true'

yugabyte=# show yb_ash_circular_buffer_size;
 yb_ash_circular_buffer_size
-----------------------------
 64MB

$ bin/yb-ctl restart --tserver_flags='"allowed_preview_flags_csv=ysql_yb_enable_ash,ysql_yb_ash_enable_infra",ysql_yb_ash_enable_infra=true,ysql_yb_enable_ash=true,ysql_yb_ash_circular_buffer_size=10240'

yugabyte=# show yb_ash_circular_buffer_size;
 yb_ash_circular_buffer_size
-----------------------------
 10MB
```

Reviewers: jason, amitanand, hbhanawat

Reviewed By: jason

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D37713
  • Loading branch information
abhinab-yb committed Sep 5, 2024
1 parent 38d8ae8 commit a180bef
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/postgres/src/backend/utils/misc/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4158,12 +4158,12 @@ static struct config_int ConfigureNamesInt[] =
{
{"yb_ash_circular_buffer_size", PGC_POSTMASTER, STATS_MONITORING,
gettext_noop("Size (in KiBs) of ASH circular buffer that stores the samples"),
NULL,
gettext_noop("If this is 0, the size will be calculated based on the number of cores"),
GUC_UNIT_KB
},
&yb_ash_circular_buffer_size,
16 * 1024, 1, INT_MAX,
NULL, NULL, NULL
0, 0, INT_MAX,
yb_ash_circular_buffer_size_check_hook, NULL, NULL
},

{
Expand Down
16 changes: 14 additions & 2 deletions src/postgres/src/backend/utils/misc/yb_ash.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ yb_enable_ash_check_hook(bool *newval, void **extra, GucSource source)
return true;
}

bool
yb_ash_circular_buffer_size_check_hook(int *newval, void **extra, GucSource source)
{
/* Autocompute yb_ash_circular_buffer_size if zero */
if (*newval == 0)
*newval = YBCGetCircularBufferSizeInKiBs();

return true;
}

void
YbAshRegister(void)
{
Expand Down Expand Up @@ -219,6 +229,7 @@ YbAshSetDatabaseId(Oid database_id)
static int
yb_ash_cb_max_entries(void)
{
Assert(yb_ash_circular_buffer_size != 0);
return yb_ash_circular_buffer_size * 1024 / sizeof(YBCAshSample);
}

Expand Down Expand Up @@ -687,9 +698,10 @@ yb_ash_sighup(SIGNAL_ARGS)
void
YbAshMain(Datum main_arg)
{
Assert(yb_ash_circular_buffer_size != 0);
ereport(LOG,
(errmsg("starting bgworker yb_ash collector with max buffer entries %d",
yb_ash->max_entries)));
(errmsg("starting bgworker yb_ash collector with circular buffer size %d bytes",
yb_ash_circular_buffer_size * 1024)));

/* Register functions for SIGTERM/SIGHUP management */
pqsignal(SIGHUP, yb_ash_sighup);
Expand Down
3 changes: 3 additions & 0 deletions src/postgres/src/include/yb_ash.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ extern void YbAshFillSampleWeight(int samples_considered);
extern bool yb_enable_ash_check_hook(bool *newval,
void **extra,
GucSource source);
extern bool yb_ash_circular_buffer_size_check_hook(int *newval,
void **extra,
GucSource source);

extern void YbAshSetMetadata(void);
extern void YbAshUnsetMetadata(void);
Expand Down
22 changes: 21 additions & 1 deletion src/yb/ash/wait_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <arpa/inet.h>

#include "yb/util/debug-util.h"
#include "yb/util/size_literals.h"
#include "yb/util/tostring.h"
#include "yb/util/trace.h"

Expand Down Expand Up @@ -53,7 +54,7 @@ DEFINE_RUNTIME_PG_PREVIEW_FLAG(bool, yb_enable_ash, false,
"and various background activities. This does nothing if "
"ysql_yb_enable_ash_infra is disabled.");

DEFINE_NON_RUNTIME_PG_FLAG(int32, yb_ash_circular_buffer_size, 16 * 1024,
DEFINE_NON_RUNTIME_PG_FLAG(int32, yb_ash_circular_buffer_size, 0,
"Size (in KiBs) of ASH circular buffer that stores the samples");

DEFINE_RUNTIME_PG_FLAG(int32, yb_ash_sampling_interval_ms, 1000,
Expand Down Expand Up @@ -384,6 +385,25 @@ std::vector<WaitStatesDescription> WaitStateInfo::GetWaitStatesDescription() {
return desc;
}

int WaitStateInfo::GetCircularBufferSizeInKiBs() {
int num_cpus = base::NumCPUs();
int bytes;
if (num_cpus <= 2) {
bytes = 32_MB;
} else if (num_cpus <= 4) {
bytes = 64_MB;
} else if (num_cpus <= 8) {
bytes = 128_MB;
} else if (num_cpus <= 16) {
bytes = 256_MB;
} else if (num_cpus <= 32) {
bytes = 512_MB;
} else {
bytes = 1024_MB;
}
return bytes / 1024;
}

//
// ScopedAdoptWaitState
//
Expand Down
1 change: 1 addition & 0 deletions src/yb/ash/wait_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ class WaitStateInfo {
bool IsConcurrentUpdatesEnabled();

static std::vector<WaitStatesDescription> GetWaitStatesDescription();
static int GetCircularBufferSizeInKiBs();

protected:
void VTraceTo(Trace* trace, int level, GStringPiece data);
Expand Down
4 changes: 4 additions & 0 deletions src/yb/yql/pggate/util/ybc_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,10 @@ YBCWaitEventDescriptor YBCGetWaitEventDescription(size_t index) {
return { 0, nullptr };
}

int YBCGetCircularBufferSizeInKiBs() {
return ash::WaitStateInfo::GetCircularBufferSizeInKiBs();
}

int YBCGetCallStackFrames(void** result, int max_depth, int skip_count) {
return google::GetStackTrace(result, max_depth, skip_count);
}
Expand Down
1 change: 1 addition & 0 deletions src/yb/yql/pggate/util/ybc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ const char* YBCGetWaitEventType(uint32_t wait_event_info);
uint8_t YBCGetQueryIdForCatalogRequests();
int YBCGetRandomUniformInt(int a, int b);
YBCWaitEventDescriptor YBCGetWaitEventDescription(size_t index);
int YBCGetCircularBufferSizeInKiBs();

int YBCGetCallStackFrames(void** result, int max_depth, int skip_count);

Expand Down

0 comments on commit a180bef

Please sign in to comment.