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-03-20 23:27:52 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/executor/instrument.c,v 1.10 2005/03/20 22:27:51 tgl Exp $
|
2001-09-18 03:59:07 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "executor/instrument.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Allocate new instrumentation structure */
|
|
|
|
Instrumentation *
|
|
|
|
InstrAlloc(void)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
Instrumentation *instr = palloc(sizeof(Instrumentation));
|
2001-09-18 03:59:07 +02:00
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
memset(instr, 0, sizeof(Instrumentation));
|
2001-09-18 03:59:07 +02:00
|
|
|
|
|
|
|
return instr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Entry to a plan node */
|
|
|
|
void
|
|
|
|
InstrStartNode(Instrumentation *instr)
|
|
|
|
{
|
|
|
|
if (!instr)
|
|
|
|
return;
|
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
if (!INSTR_TIME_IS_ZERO(instr->starttime))
|
2004-01-30 23:44:21 +01:00
|
|
|
elog(DEBUG2, "InstrStartNode called twice in a row");
|
2001-09-18 03:59:07 +02:00
|
|
|
else
|
2005-03-20 23:27:52 +01:00
|
|
|
INSTR_TIME_SET_CURRENT(instr->starttime);
|
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
|
|
|
|
2001-09-18 03:59:07 +02:00
|
|
|
if (!instr)
|
|
|
|
return;
|
|
|
|
|
2005-03-20 23:27:52 +01:00
|
|
|
if (INSTR_TIME_IS_ZERO(instr->starttime))
|
2001-09-18 03:59:07 +02:00
|
|
|
{
|
2003-07-21 19:05:12 +02:00
|
|
|
elog(DEBUG2, "InstrStopNode 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
|
|
|
}
|
|
|
|
|
|
|
|
if (returnedTuple)
|
|
|
|
instr->tuplecount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
if (!instr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Skip if nothing has happened, or already shut down */
|
|
|
|
if (!instr->running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Accumulate statistics */
|
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;
|
|
|
|
}
|