2001-09-18 03:59:07 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* instrument.c
|
2001-10-25 07:50:21 +02:00
|
|
|
* functions for instrumentation of plan execution
|
2001-09-18 03:59:07 +02:00
|
|
|
*
|
|
|
|
*
|
2005-01-01 06:43:09 +01:00
|
|
|
* Copyright (c) 2001-2005, PostgreSQL Global Development Group
|
2001-09-18 03:59:07 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2005-04-16 22:07:35 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/executor/instrument.c,v 1.12 2005/04/16 20:07:35 tgl Exp $
|
2001-09-18 03:59:07 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "executor/instrument.h"
|
|
|
|
|
|
|
|
|
2005-03-25 22:58:00 +01:00
|
|
|
/* Allocate new instrumentation structure(s) */
|
2001-09-18 03:59:07 +02:00
|
|
|
Instrumentation *
|
2005-03-25 22:58:00 +01:00
|
|
|
InstrAlloc(int n)
|
2001-09-18 03:59:07 +02:00
|
|
|
{
|
2005-03-25 22:58:00 +01:00
|
|
|
Instrumentation *instr = palloc0(n * sizeof(Instrumentation));
|
2001-09-18 03:59:07 +02:00
|
|
|
|
2005-03-25 22:58:00 +01:00
|
|
|
/* we don't need to do any initialization except zero 'em */
|
2001-09-18 03:59:07 +02:00
|
|
|
|
|
|
|
return instr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entry to a plan node */
|
|
|
|
void
|
|
|
|
InstrStartNode(Instrumentation *instr)
|
|
|
|
{
|
2005-04-16 22:07:35 +02:00
|
|
|
if (INSTR_TIME_IS_ZERO(instr->starttime))
|
2005-03-20 23:27:52 +01:00
|
|
|
INSTR_TIME_SET_CURRENT(instr->starttime);
|
2005-04-16 22:07:35 +02:00
|
|
|
else
|
|
|
|
elog(DEBUG2, "InstrStartNode called twice in a row");
|
2001-09-18 03:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit from a plan node */
|
|
|
|
void
|
|
|
|
InstrStopNode(Instrumentation *instr, bool returnedTuple)
|
|
|
|
{
|
2005-03-20 23:27:52 +01:00
|
|
|
instr_time endtime;
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2005-04-16 22:07:35 +02:00
|
|
|
/* count the returned tuples */
|
|
|
|
if (returnedTuple)
|
|
|
|
instr->tuplecount += 1;
|
2001-09-18 03:59:07 +02:00
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
if (INSTR_TIME_IS_ZERO(instr->starttime))
|
2001-09-18 03:59:07 +02:00
|
|
|
{
|
2005-04-16 22:07:35 +02:00
|
|
|
elog(DEBUG2, "InstrStopNode called without start");
|
2001-09-18 03:59:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
INSTR_TIME_SET_CURRENT(endtime);
|
2001-10-25 07:50:21 +02:00
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
#ifndef WIN32
|
2001-10-25 07:50:21 +02:00
|
|
|
instr->counter.tv_sec += endtime.tv_sec - instr->starttime.tv_sec;
|
2001-09-18 03:59:07 +02:00
|
|
|
instr->counter.tv_usec += endtime.tv_usec - instr->starttime.tv_usec;
|
|
|
|
|
|
|
|
/* Normalize after each add to avoid overflow/underflow of tv_usec */
|
|
|
|
while (instr->counter.tv_usec < 0)
|
|
|
|
{
|
|
|
|
instr->counter.tv_usec += 1000000;
|
|
|
|
instr->counter.tv_sec--;
|
|
|
|
}
|
|
|
|
while (instr->counter.tv_usec >= 1000000)
|
|
|
|
{
|
|
|
|
instr->counter.tv_usec -= 1000000;
|
|
|
|
instr->counter.tv_sec++;
|
|
|
|
}
|
2005-03-20 23:27:52 +01:00
|
|
|
#else /* WIN32 */
|
|
|
|
instr->counter.QuadPart += (endtime.QuadPart - instr->starttime.QuadPart);
|
|
|
|
#endif
|
2001-09-18 03:59:07 +02:00
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
INSTR_TIME_SET_ZERO(instr->starttime);
|
2001-09-18 03:59:07 +02:00
|
|
|
|
|
|
|
/* Is this the first tuple of this cycle? */
|
|
|
|
if (!instr->running)
|
|
|
|
{
|
|
|
|
instr->running = true;
|
2005-03-20 23:27:52 +01:00
|
|
|
instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
|
2001-09-18 03:59:07 +02:00
|
|
|
}
|
2005-04-16 22:07:35 +02:00
|
|
|
}
|
2001-09-18 03:59:07 +02:00
|
|
|
|
2005-04-16 22:07:35 +02:00
|
|
|
/* As above, but count multiple tuples returned at once */
|
|
|
|
void
|
|
|
|
InstrStopNodeMulti(Instrumentation *instr, double nTuples)
|
|
|
|
{
|
|
|
|
/* count the returned tuples */
|
|
|
|
instr->tuplecount += nTuples;
|
|
|
|
|
|
|
|
/* delegate the rest */
|
|
|
|
InstrStopNode(instr, false);
|
2001-09-18 03:59:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish a run cycle for a plan node */
|
2001-10-25 07:50:21 +02:00
|
|
|
void
|
2001-09-18 03:59:07 +02:00
|
|
|
InstrEndLoop(Instrumentation *instr)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
double totaltime;
|
2001-09-18 03:59:07 +02:00
|
|
|
|
|
|
|
/* Skip if nothing has happened, or already shut down */
|
|
|
|
if (!instr->running)
|
|
|
|
return;
|
|
|
|
|
2005-04-16 22:07:35 +02:00
|
|
|
if (!INSTR_TIME_IS_ZERO(instr->starttime))
|
|
|
|
elog(DEBUG2, "InstrEndLoop called on running node");
|
|
|
|
|
|
|
|
/* Accumulate per-cycle statistics into totals */
|
2005-03-20 23:27:52 +01:00
|
|
|
totaltime = INSTR_TIME_GET_DOUBLE(instr->counter);
|
2001-09-18 03:59:07 +02:00
|
|
|
|
|
|
|
instr->startup += instr->firsttuple;
|
2001-10-25 07:50:21 +02:00
|
|
|
instr->total += totaltime;
|
2001-09-18 03:59:07 +02:00
|
|
|
instr->ntuples += instr->tuplecount;
|
|
|
|
instr->nloops += 1;
|
|
|
|
|
|
|
|
/* Reset for next cycle (if any) */
|
|
|
|
instr->running = false;
|
2005-03-20 23:27:52 +01:00
|
|
|
INSTR_TIME_SET_ZERO(instr->starttime);
|
|
|
|
INSTR_TIME_SET_ZERO(instr->counter);
|
2001-09-18 03:59:07 +02:00
|
|
|
instr->firsttuple = 0;
|
|
|
|
instr->tuplecount = 0;
|
|
|
|
}
|