Skip to content

Commit

Permalink
Add a reindent script, and do another indent run so that everyone tha…
Browse files Browse the repository at this point in the history
…t works one the project is happy. It should also be closer to PostgreSQL's indentation.
  • Loading branch information
Marc Cousin committed Aug 26, 2014
1 parent 6b602f4 commit 4eadf38
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 84 deletions.
188 changes: 104 additions & 84 deletions powa.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

PG_MODULE_MAGIC;

void _PG_init(void);
void die_on_too_small_frequency(void);
void _PG_init(void);
void die_on_too_small_frequency(void);


static bool got_sigterm = false;
Expand All @@ -36,19 +36,20 @@ static void powa_main(Datum main_arg);
static void powa_sigterm(SIGNAL_ARGS);
static void powa_sighup(SIGNAL_ARGS);

static int powa_frequency;
static int min_powa_frequency = 5000;
static int powa_retention;
static int powa_coalesce;
static int powa_frequency;
static int min_powa_frequency = 5000;
static int powa_retention;
static int powa_coalesce;
static char *powa_database = NULL;

void die_on_too_small_frequency(void)
{
if (powa_frequency > 0 && powa_frequency < min_powa_frequency) {
elog(LOG, "POWA frequency cannot be smaller than %i milliseconds",
min_powa_frequency);
exit(1);
}
if (powa_frequency > 0 && powa_frequency < min_powa_frequency)
{
elog(LOG, "POWA frequency cannot be smaller than %i milliseconds",
min_powa_frequency);
exit(1);
}
}

void _PG_init(void)
Expand All @@ -57,40 +58,40 @@ void _PG_init(void)
BackgroundWorker worker;

DefineCustomIntVariable("powa.frequency",
"Defines the frequency in seconds of the snapshots",
NULL,
&powa_frequency,
300000,
-1,
INT_MAX / 1000,
PGC_SUSET, GUC_UNIT_MS, NULL, NULL, NULL);
"Defines the frequency in seconds of the snapshots",
NULL,
&powa_frequency,
300000,
-1,
INT_MAX / 1000,
PGC_SUSET, GUC_UNIT_MS, NULL, NULL, NULL);

DefineCustomIntVariable("powa.coalesce",
"Defines the amount of records to group together in the table (more compact)",
NULL,
&powa_coalesce,
100,
5, INT_MAX, PGC_SUSET, 0, NULL, NULL, NULL);
"Defines the amount of records to group together in the table (more compact)",
NULL,
&powa_coalesce,
100, 5, INT_MAX, PGC_SUSET, 0, NULL, NULL, NULL);

DefineCustomIntVariable("powa.retention",
"Automatically purge data older than N minutes",
NULL,
&powa_retention,
HOURS_PER_DAY * MINS_PER_HOUR,
0,
INT_MAX / SECS_PER_MINUTE,
PGC_SUSET, GUC_UNIT_MIN, NULL, NULL, NULL);
"Automatically purge data older than N minutes",
NULL,
&powa_retention,
HOURS_PER_DAY * MINS_PER_HOUR,
0,
INT_MAX / SECS_PER_MINUTE,
PGC_SUSET, GUC_UNIT_MIN, NULL, NULL, NULL);

DefineCustomStringVariable("powa.database",
"Defines the database of the workload repository",
NULL,
&powa_database,
"powa",
PGC_POSTMASTER, 0, NULL, NULL, NULL);
/* Register the worker processes */
"Defines the database of the workload repository",
NULL,
&powa_database,
"powa", PGC_POSTMASTER, 0, NULL, NULL, NULL);
/*
Register the worker processes
*/
worker.bgw_flags =
BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished; /* Must write to the database */
BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished; /* Must write to the database */
worker.bgw_main = powa_main;
snprintf(worker.bgw_name, BGW_MAXLEN, "powa");
worker.bgw_restart_time = 10;
Expand All @@ -104,29 +105,36 @@ void _PG_init(void)

static void powa_main(Datum main_arg)
{
char *q1 = "SELECT powa_take_snapshot()";
char *q1 = "SELECT powa_take_snapshot()";
static char *q2 = "SET application_name = 'POWA collector'";
instr_time begin;
instr_time end;
long time_to_wait;
instr_time begin;
instr_time end;
long time_to_wait;

die_on_too_small_frequency();
/* Set up signal handlers, then unblock signalsl */
/*
Set up signal handlers, then unblock signalsl
*/
pqsignal(SIGHUP, powa_sighup);
pqsignal(SIGTERM, powa_sigterm);

BackgroundWorkerUnblockSignals();

/* We only connect when powa_frequency >0. If not, powa has been deactivated */
if (powa_frequency < 0) {
elog(LOG, "POWA is deactivated (powa.frequency = %i), exiting",
powa_frequency);
exit(1);
}
/*
We only connect when powa_frequency >0. If not, powa has been deactivated
*/
if (powa_frequency < 0)
{
elog(LOG, "POWA is deactivated (powa.frequency = %i), exiting",
powa_frequency);
exit(1);
}
// We got here: it means powa_frequency > 0. Let's connect


/* Connect to POWA database */
/*
Connect to POWA database
*/
BackgroundWorkerInitializeConnection(powa_database, NULL);

elog(LOG, "POWA connected to %s", powa_database);
Expand All @@ -140,48 +148,60 @@ static void powa_main(Datum main_arg)
PopActiveSnapshot();
CommitTransactionCommand();

/* let's store the current time. It will be used to
calculate a quite stable interval between each measure */
while (!got_sigterm) {
/* We can get here with a new value of powa_frequency
because of a reload. Let's suicide to disconnect
if this value is <0 */
if (powa_frequency < 0) {
elog(LOG, "POWA exits to disconnect from the database now");
exit(1);
}
INSTR_TIME_SET_CURRENT(begin);
ResetLatch(&MyProc->procLatch);
StartTransactionCommand();
SetCurrentStatementStartTimestamp();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
SPI_execute(q1, false, 0);
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
INSTR_TIME_SET_CURRENT(end);
INSTR_TIME_SUBTRACT(end, begin);
/* Wait powa.frequency, compensate for work time of last snapshot */
/* If we got off schedule (because of a compact or delete,
just do another operation right now */
time_to_wait = powa_frequency - INSTR_TIME_GET_MILLISEC(end);
if (time_to_wait > 0) {
WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
time_to_wait);
}
}
/*
let's store the current time. It will be used to
calculate a quite stable interval between each measure
*/
while (!got_sigterm)
{
/*
We can get here with a new value of powa_frequency
because of a reload. Let's suicide to disconnect
if this value is <0
*/
if (powa_frequency < 0)
{
elog(LOG, "POWA exits to disconnect from the database now");
exit(1);
}
INSTR_TIME_SET_CURRENT(begin);
ResetLatch(&MyProc->procLatch);
StartTransactionCommand();
SetCurrentStatementStartTimestamp();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
SPI_execute(q1, false, 0);
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
INSTR_TIME_SET_CURRENT(end);
INSTR_TIME_SUBTRACT(end, begin);
/*
Wait powa.frequency, compensate for work time of last snapshot
*/
/*
If we got off schedule (because of a compact or delete,
just do another operation right now
*/
time_to_wait = powa_frequency - INSTR_TIME_GET_MILLISEC(end);
if (time_to_wait > 0)
{
WaitLatch(&MyProc->procLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
time_to_wait);
}
}
proc_exit(0);
}


static void powa_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
int save_errno = errno;

got_sigterm = true;
if (MyProc)
SetLatch(&MyProc->procLatch);
SetLatch(&MyProc->procLatch);
errno = save_errno;
}

Expand Down
8 changes: 8 additions & 0 deletions reindent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#/bin/sh

# This reindents all C files in the directory following PG rules (ported to gnu indent)
# as some of us have tab=8 spaces, and others tab=4 spaces

indent -bad -bap -bbo -bbb -bc -bl -brs -c33 -cd33 -cdb -nce -ci4 -cli0 \
-cp33 -cs -d0 -di12 -nfc1 -nfca -nfc1 -i4 -nip -l79 -lp -nip -npcs \
-nprs -npsl -saf -sai -saw -nsc -nsob -nss -nut *.c

0 comments on commit 4eadf38

Please sign in to comment.