Skip to content

Commit 90757f9

Browse files
author
Sokolov Yura
committed
cfs: make cfs_control_gc to be counter instead of boolean
1 parent 3012d74 commit 90757f9

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/backend/access/transam/xlog.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ int CheckPointSegments;
128128
/* Estimated distance between checkpoints, in bytes */
129129
static double CheckPointDistanceEstimate = 0;
130130
static double PrevCheckPointDistance = 0;
131-
static bool SavedGCState = false;
132131

133132
/*
134133
* GUC support
@@ -9894,7 +9893,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
98949893
XLogCtl->Insert.forcePageWrites = true;
98959894
WALInsertLockRelease();
98969895

9897-
SavedGCState = cfs_control_gc(false); /* disable GC during backup */
9896+
cfs_control_gc_lock(); /* disable GC during backup */
98989897

98999898
/* Ensure we release forcePageWrites if fail below */
99009899
PG_ENSURE_ERROR_CLEANUP(pg_start_backup_callback, (Datum) BoolGetDatum(exclusive));
@@ -10273,7 +10272,7 @@ pg_start_backup_callback(int code, Datum arg)
1027310272
}
1027410273
WALInsertLockRelease();
1027510274

10276-
cfs_control_gc(SavedGCState); /* Restore CFS GC activity */
10275+
cfs_control_gc_unlock(); /* Restore CFS GC activity */
1027710276
}
1027810277

1027910278
/*
@@ -10473,7 +10472,7 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
1047310472
/* Clean up session-level lock */
1047410473
sessionBackupState = SESSION_BACKUP_NONE;
1047510474

10476-
cfs_control_gc(SavedGCState); /* Restore CFS GC activity */
10475+
cfs_control_gc_unlock(); /* Restore CFS GC activity */
1047710476

1047810477
/*
1047910478
* Read and parse the START WAL LOCATION line (this code is pretty crude,
@@ -10718,7 +10717,7 @@ do_pg_abort_backup(void)
1071810717
}
1071910718
WALInsertLockRelease();
1072010719

10721-
cfs_control_gc(SavedGCState); /* Restore CFS GC activity */
10720+
cfs_control_gc_unlock(); /* Restore CFS GC activity */
1072210721
}
1072310722

1072410723
/*

src/backend/storage/file/cfs.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void cfs_initialize()
415415
pg_atomic_init_u32(&cfs_state->n_active_gc, 0);
416416
cfs_state->n_workers = 0;
417417
cfs_state->background_gc_enabled = cfs_gc_enabled;
418-
cfs_state->gc_enabled = true;
418+
pg_atomic_init_u32(&cfs_state->gc_disabled, 0);
419419
cfs_state->max_iterations = 0;
420420

421421
for (i = 0; i < MaxBackends; i++)
@@ -796,15 +796,16 @@ static bool cfs_gc_file(char* map_path, GC_CALL_KIND background)
796796
pg_atomic_fetch_add_u32(&cfs_state->n_active_gc, 1);
797797
if (background == CFS_IMPLICIT)
798798
{
799-
if (!cfs_state->gc_enabled)
799+
if (pg_atomic_read_u32(&cfs_state->gc_disabled) != 0)
800800
{
801801
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
802802
return false;
803803
}
804804
}
805805
else
806806
{
807-
while (!cfs_state->gc_enabled || (background == CFS_BACKGROUND && !cfs_state->background_gc_enabled))
807+
while (pg_atomic_read_u32(&cfs_state->gc_disabled) != 0 ||
808+
(background == CFS_BACKGROUND && !cfs_state->background_gc_enabled))
808809
{
809810
pg_atomic_fetch_sub_u32(&cfs_state->n_active_gc, 1);
810811

@@ -1430,13 +1431,11 @@ void cfs_gc_start_bgworkers()
14301431
elog(LOG, "Start %d background garbage collection workers for CFS", i);
14311432
}
14321433

1433-
/* Enable/disable garbage collection. */
1434-
bool cfs_control_gc(bool enabled)
1434+
/* Disable garbage collection. */
1435+
void cfs_control_gc_lock(void)
14351436
{
1436-
bool was_enabled = cfs_state->gc_enabled;
1437-
cfs_state->gc_enabled = enabled;
1438-
pg_memory_barrier();
1439-
if (was_enabled && !enabled)
1437+
uint32 was_disabled = pg_atomic_fetch_add_u32(&cfs_state->gc_disabled, 1);
1438+
if (!was_disabled)
14401439
{
14411440
/* Wait until there are no active GC workers */
14421441
while (pg_atomic_read_u32(&cfs_state->n_active_gc) != 0)
@@ -1451,7 +1450,12 @@ bool cfs_control_gc(bool enabled)
14511450
CHECK_FOR_INTERRUPTS();
14521451
}
14531452
}
1454-
return was_enabled;
1453+
}
1454+
1455+
/* Enable garbage collection. */
1456+
void cfs_control_gc_unlock(void)
1457+
{
1458+
pg_atomic_fetch_sub_u32(&cfs_state->gc_disabled, 1);
14551459
}
14561460

14571461
/* ----------------------------------------------------------------

src/backend/storage/file/copydir.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ copydir(char *fromdir, char *todir, bool recurse)
4141
struct dirent *xlde;
4242
char fromfile[MAXPGPATH];
4343
char tofile[MAXPGPATH];
44-
bool savedGCState = false;
4544

4645
if (mkdir(todir, S_IRWXU) != 0)
4746
ereport(ERROR,
@@ -55,7 +54,7 @@ copydir(char *fromdir, char *todir, bool recurse)
5554
errmsg("could not open directory \"%s\": %m", fromdir)));
5655

5756

58-
savedGCState = cfs_control_gc(false); /* disable GC during copy */
57+
cfs_control_gc_lock(); /* disable GC during copy */
5958

6059
PG_TRY();
6160
{
@@ -91,11 +90,11 @@ copydir(char *fromdir, char *todir, bool recurse)
9190
}
9291
PG_CATCH();
9392
{
94-
cfs_control_gc(savedGCState);
93+
cfs_control_gc_unlock();
9594
PG_RE_THROW();
9695
}
9796
PG_END_TRY();
98-
cfs_control_gc(savedGCState);
97+
cfs_control_gc_unlock();
9998

10099
/*
101100
* Be paranoid here and fsync all files to ensure the copy is really done.
@@ -154,7 +153,6 @@ copyzipdir(char *fromdir, bool from_compressed,
154153
struct dirent *xlde;
155154
char fromfile[MAXPGPATH];
156155
char tofile[MAXPGPATH];
157-
bool savedGCState;
158156

159157
if (mkdir(todir, S_IRWXU) != 0)
160158
ereport(ERROR,
@@ -167,7 +165,7 @@ copyzipdir(char *fromdir, bool from_compressed,
167165
(errcode_for_file_access(),
168166
errmsg("could not open directory \"%s\": %m", fromdir)));
169167

170-
savedGCState = cfs_control_gc(false); /* disable GC during copy */
168+
cfs_control_gc_lock(); /* disable GC during copy */
171169

172170
PG_TRY();
173171
{
@@ -199,11 +197,11 @@ copyzipdir(char *fromdir, bool from_compressed,
199197
}
200198
PG_CATCH();
201199
{
202-
cfs_control_gc(savedGCState);
200+
cfs_control_gc_unlock();
203201
PG_RE_THROW();
204202
}
205203
PG_END_TRY();
206-
cfs_control_gc(savedGCState);
204+
cfs_control_gc_unlock();
207205

208206
/*
209207
* Be paranoid here and fsync all files to ensure the copy is really done.

src/include/storage/cfs.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ typedef struct
8787
* Manually started GC performs just one iteration. */
8888
int64 max_iterations;
8989
/* Flag for temporary disabling GC */
90-
volatile bool gc_enabled;
90+
pg_atomic_uint32 gc_disabled;
9191
/* Flag for controlling background GC */
9292
volatile bool background_gc_enabled;
9393
/* CFS GC statatistic */
@@ -128,7 +128,8 @@ void cfs_lock_file(FileMap* map, int fd, char const* path);
128128
void cfs_unlock_file(FileMap* map, char const* path);
129129
uint32 cfs_alloc_page(FileMap* map, uint32 oldSize, uint32 newSize);
130130
void cfs_extend(FileMap* map, uint32 pos);
131-
bool cfs_control_gc(bool enabled);
131+
void cfs_control_gc_lock(void);
132+
void cfs_control_gc_unlock(void);
132133
int cfs_msync(FileMap* map);
133134
FileMap* cfs_mmap(int md);
134135
int cfs_munmap(FileMap* map);

0 commit comments

Comments
 (0)