1 /*-------------------------------------------------------------------------
2  *
3  * instrument.h
4  *	  definitions for run-time statistics collection
5  *
6  *
7  * Copyright (c) 2001-2016, PostgreSQL Global Development Group
8  *
9  * src/include/executor/instrument.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef INSTRUMENT_H
14 #define INSTRUMENT_H
15 
16 #include "portability/instr_time.h"
17 
18 
19 typedef struct BufferUsage
20 {
21 	long		shared_blks_hit;	/* # of shared buffer hits */
22 	long		shared_blks_read;		/* # of shared disk blocks read */
23 	long		shared_blks_dirtied;	/* # of shared blocks dirtied */
24 	long		shared_blks_written;	/* # of shared disk blocks written */
25 	long		local_blks_hit; /* # of local buffer hits */
26 	long		local_blks_read;	/* # of local disk blocks read */
27 	long		local_blks_dirtied;		/* # of shared blocks dirtied */
28 	long		local_blks_written;		/* # of local disk blocks written */
29 	long		temp_blks_read; /* # of temp blocks read */
30 	long		temp_blks_written;		/* # of temp blocks written */
31 	instr_time	blk_read_time;	/* time spent reading */
32 	instr_time	blk_write_time; /* time spent writing */
33 } BufferUsage;
34 
35 /* Flag bits included in InstrAlloc's instrument_options bitmask */
36 typedef enum InstrumentOption
37 {
38 	INSTRUMENT_TIMER = 1 << 0,	/* needs timer (and row counts) */
39 	INSTRUMENT_BUFFERS = 1 << 1,	/* needs buffer usage */
40 	INSTRUMENT_ROWS = 1 << 2,	/* needs row count */
41 	INSTRUMENT_ALL = PG_INT32_MAX
42 } InstrumentOption;
43 
44 typedef struct Instrumentation
45 {
46 	/* Parameters set at node creation: */
47 	bool		need_timer;		/* TRUE if we need timer data */
48 	bool		need_bufusage;	/* TRUE if we need buffer usage data */
49 	/* Info about current plan cycle: */
50 	bool		running;		/* TRUE if we've completed first tuple */
51 	instr_time	starttime;		/* Start time of current iteration of node */
52 	instr_time	counter;		/* Accumulated runtime for this node */
53 	double		firsttuple;		/* Time for first tuple of this cycle */
54 	double		tuplecount;		/* Tuples emitted so far this cycle */
55 	BufferUsage bufusage_start; /* Buffer usage at start */
56 	/* Accumulated statistics across all completed cycles: */
57 	double		startup;		/* Total startup time (in seconds) */
58 	double		total;			/* Total total time (in seconds) */
59 	double		ntuples;		/* Total tuples produced */
60 	double		nloops;			/* # of run cycles for this node */
61 	double		nfiltered1;		/* # tuples removed by scanqual or joinqual */
62 	double		nfiltered2;		/* # tuples removed by "other" quals */
63 	BufferUsage bufusage;		/* Total buffer usage */
64 } Instrumentation;
65 
66 typedef struct WorkerInstrumentation
67 {
68 	int			num_workers;	/* # of structures that follow */
69 	Instrumentation instrument[FLEXIBLE_ARRAY_MEMBER];
70 } WorkerInstrumentation;
71 
72 extern PGDLLIMPORT BufferUsage pgBufferUsage;
73 
74 extern Instrumentation *InstrAlloc(int n, int instrument_options);
75 extern void InstrInit(Instrumentation *instr, int instrument_options);
76 extern void InstrStartNode(Instrumentation *instr);
77 extern void InstrStopNode(Instrumentation *instr, double nTuples);
78 extern void InstrEndLoop(Instrumentation *instr);
79 extern void InstrAggNode(Instrumentation *dst, Instrumentation *add);
80 extern void InstrStartParallelQuery(void);
81 extern void InstrEndParallelQuery(BufferUsage *result);
82 extern void InstrAccumParallelQuery(BufferUsage *result);
83 
84 #endif   /* INSTRUMENT_H */
85