]> granicus.if.org Git - postgresql/blob - src/backend/access/heap/stats.c
7f39559069932eddc76d17ba5f0078f5581791da
[postgresql] / src / backend / access / heap / stats.c
1 /*-------------------------------------------------------------------------
2  *
3  * stats.c--
4  *    heap access method debugging statistic collection routines
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *    $Header: /cvsroot/pgsql/src/backend/access/heap/Attic/stats.c,v 1.6 1996/11/03 12:34:55 scrappy Exp $
11  *
12  * NOTES
13  *    initam should be moved someplace else.
14  *
15  *-------------------------------------------------------------------------
16  */
17
18 #include <stdio.h>
19
20 #include "postgres.h"
21
22 #include "access/relscan.h"
23
24 #include "access/heapam.h"
25
26 #include "nodes/memnodes.h"
27
28 #include "utils/mcxt.h"
29
30 #include "utils/palloc.h"
31
32 #ifndef HAVE_MEMMOVE
33 # include "regex/utils.h"
34 #else
35 # include <string.h>
36 #endif
37
38 /* ----------------
39  *      InitHeapAccessStatistics
40  * ----------------
41  */
42 HeapAccessStatistics heap_access_stats = (HeapAccessStatistics) NULL;
43      
44 void
45 InitHeapAccessStatistics()    
46 {
47     MemoryContext    oldContext;
48     HeapAccessStatistics stats;
49     
50     /* ----------------
51      *  make sure we don't initialize things twice
52      * ----------------
53      */
54     if (heap_access_stats != NULL)
55         return;
56     
57     /* ----------------
58      *  allocate statistics structure from the top memory context
59      * ----------------
60      */
61     oldContext = MemoryContextSwitchTo(TopMemoryContext);
62     
63     stats = (HeapAccessStatistics)
64         palloc(sizeof(HeapAccessStatisticsData));
65     
66     /* ----------------
67      *  initialize fields to default values
68      * ----------------
69      */
70     stats->global_open = 0;                     
71     stats->global_openr = 0;
72     stats->global_close = 0;
73     stats->global_beginscan = 0;
74     stats->global_rescan = 0;
75     stats->global_endscan = 0;
76     stats->global_getnext = 0;
77     stats->global_fetch = 0;
78     stats->global_insert = 0;
79     stats->global_delete = 0;
80     stats->global_replace = 0;
81     stats->global_markpos = 0;
82     stats->global_restrpos = 0;
83     stats->global_BufferGetRelation = 0;
84     stats->global_RelationIdGetRelation = 0;
85     stats->global_RelationIdGetRelation_Buf = 0;
86     stats->global_getreldesc = 0;
87     stats->global_heapgettup = 0;
88     stats->global_RelationPutHeapTuple = 0;
89     stats->global_RelationPutLongHeapTuple = 0;
90     
91     stats->local_open = 0;
92     stats->local_openr = 0;
93     stats->local_close = 0;
94     stats->local_beginscan = 0;
95     stats->local_rescan = 0;
96     stats->local_endscan = 0;
97     stats->local_getnext = 0;
98     stats->local_fetch = 0;
99     stats->local_insert = 0;
100     stats->local_delete = 0;
101     stats->local_replace = 0;
102     stats->local_markpos = 0;
103     stats->local_restrpos = 0;
104     stats->local_BufferGetRelation = 0;
105     stats->local_RelationIdGetRelation = 0;
106     stats->local_RelationIdGetRelation_Buf = 0;
107     stats->local_getreldesc = 0;
108     stats->local_heapgettup = 0;
109     stats->local_RelationPutHeapTuple = 0;
110     stats->local_RelationPutLongHeapTuple = 0;
111     stats->local_RelationNameGetRelation = 0;
112     stats->global_RelationNameGetRelation = 0;
113     
114     /* ----------------
115      *  record init times
116      * ----------------
117      */
118     time(&stats->init_global_timestamp);
119     time(&stats->local_reset_timestamp);
120     time(&stats->last_request_timestamp);
121     
122     /* ----------------
123      *  return to old memory context
124      * ----------------
125      */
126     (void) MemoryContextSwitchTo(oldContext);
127     
128     heap_access_stats = stats;
129 }
130
131 /* ----------------
132  *      ResetHeapAccessStatistics
133  * ----------------
134  */
135 void
136 ResetHeapAccessStatistics()    
137 {
138     HeapAccessStatistics stats;
139     
140     /* ----------------
141      *  do nothing if stats aren't initialized
142      * ----------------
143      */
144     if (heap_access_stats == NULL)
145         return;
146     
147     stats = heap_access_stats;
148     
149     /* ----------------
150      *  reset local counts
151      * ----------------
152      */
153     stats->local_open = 0;
154     stats->local_openr = 0;
155     stats->local_close = 0;
156     stats->local_beginscan = 0;
157     stats->local_rescan = 0;
158     stats->local_endscan = 0;
159     stats->local_getnext = 0;
160     stats->local_fetch = 0;
161     stats->local_insert = 0;
162     stats->local_delete = 0;
163     stats->local_replace = 0;
164     stats->local_markpos = 0;
165     stats->local_restrpos = 0;
166     stats->local_BufferGetRelation = 0;
167     stats->local_RelationIdGetRelation = 0;
168     stats->local_RelationIdGetRelation_Buf = 0;
169     stats->local_getreldesc = 0;
170     stats->local_heapgettup = 0;
171     stats->local_RelationPutHeapTuple = 0;
172     stats->local_RelationPutLongHeapTuple = 0;
173     
174     /* ----------------
175      *  reset local timestamps
176      * ----------------
177      */
178     time(&stats->local_reset_timestamp);
179     time(&stats->last_request_timestamp);
180 }
181
182 /* ----------------
183  *      GetHeapAccessStatistics
184  * ----------------
185  */
186 HeapAccessStatistics GetHeapAccessStatistics()    
187 {
188     HeapAccessStatistics stats;
189     
190     /* ----------------
191      *  return nothing if stats aren't initialized
192      * ----------------
193      */
194     if (heap_access_stats == NULL)
195         return NULL;
196     
197     /* ----------------
198      *  record the current request time
199      * ----------------
200      */
201     time(&heap_access_stats->last_request_timestamp);
202     
203     /* ----------------
204      *  allocate a copy of the stats and return it to the caller.
205      * ----------------
206      */
207     stats = (HeapAccessStatistics)
208         palloc(sizeof(HeapAccessStatisticsData));
209     
210      memmove(stats,
211              heap_access_stats,
212              sizeof(HeapAccessStatisticsData)); 
213     
214     return stats;
215 }
216
217 /* ----------------
218  *      PrintHeapAccessStatistics
219  * ----------------
220  */
221 void
222 PrintHeapAccessStatistics(HeapAccessStatistics stats)
223 {
224     /* ----------------
225      *  return nothing if stats aren't valid
226      * ----------------
227      */
228     if (stats == NULL)
229         return;
230     
231     printf("======== heap am statistics ========\n");
232     printf("init_global_timestamp:      %s",
233            ctime(&(stats->init_global_timestamp)));
234     
235     printf("local_reset_timestamp:      %s",
236            ctime(&(stats->local_reset_timestamp)));
237     
238     printf("last_request_timestamp:     %s",
239            ctime(&(stats->last_request_timestamp)));
240     
241     printf("local/global_open:                        %6d/%6d\n",
242            stats->local_open, stats->global_open);
243     
244     printf("local/global_openr:                       %6d/%6d\n",
245            stats->local_openr, stats->global_openr);
246     
247     printf("local/global_close:                       %6d/%6d\n",
248            stats->local_close, stats->global_close);
249     
250     printf("local/global_beginscan:                   %6d/%6d\n",
251            stats->local_beginscan, stats->global_beginscan);
252     
253     printf("local/global_rescan:                      %6d/%6d\n",
254            stats->local_rescan, stats->global_rescan);
255     
256     printf("local/global_endscan:                     %6d/%6d\n",
257            stats->local_endscan, stats->global_endscan);
258     
259     printf("local/global_getnext:                     %6d/%6d\n",
260            stats->local_getnext, stats->global_getnext);
261     
262     printf("local/global_fetch:                       %6d/%6d\n",
263            stats->local_fetch, stats->global_fetch);
264     
265     printf("local/global_insert:                      %6d/%6d\n",
266            stats->local_insert, stats->global_insert);
267     
268     printf("local/global_delete:                      %6d/%6d\n",
269            stats->local_delete, stats->global_delete);
270     
271     printf("local/global_replace:                     %6d/%6d\n",
272            stats->local_replace, stats->global_replace);
273     
274     printf("local/global_markpos:                     %6d/%6d\n",
275            stats->local_markpos, stats->global_markpos);
276     
277     printf("local/global_restrpos:                    %6d/%6d\n",
278            stats->local_restrpos, stats->global_restrpos);
279     
280     printf("================\n");
281     
282     printf("local/global_BufferGetRelation:             %6d/%6d\n",
283            stats->local_BufferGetRelation,
284            stats->global_BufferGetRelation);
285     
286     printf("local/global_RelationIdGetRelation:         %6d/%6d\n",
287            stats->local_RelationIdGetRelation,
288            stats->global_RelationIdGetRelation);
289     
290     printf("local/global_RelationIdGetRelation_Buf:     %6d/%6d\n",
291            stats->local_RelationIdGetRelation_Buf,
292            stats->global_RelationIdGetRelation_Buf);
293     
294     printf("local/global_getreldesc:                    %6d/%6d\n",
295            stats->local_getreldesc, stats->global_getreldesc);
296     
297     printf("local/global_heapgettup:                    %6d/%6d\n",
298            stats->local_heapgettup, stats->global_heapgettup);
299     
300     printf("local/global_RelationPutHeapTuple:          %6d/%6d\n",
301            stats->local_RelationPutHeapTuple,
302            stats->global_RelationPutHeapTuple);
303     
304     printf("local/global_RelationPutLongHeapTuple:      %6d/%6d\n",
305            stats->local_RelationPutLongHeapTuple,
306            stats->global_RelationPutLongHeapTuple);
307     
308     printf("===================================\n");
309     
310     printf("\n");
311 }
312
313 /* ----------------
314  *      PrintAndFreeHeapAccessStatistics
315  * ----------------
316  */
317 void
318 PrintAndFreeHeapAccessStatistics(HeapAccessStatistics stats)
319 {
320     PrintHeapAccessStatistics(stats);
321     if (stats != NULL)
322         pfree(stats);
323 }
324
325 /* ----------------------------------------------------------------
326  *                  access method initialization
327  * ----------------------------------------------------------------
328  */
329 /* ----------------
330  *      initam should someday be moved someplace else.
331  * ----------------
332  */
333 void
334 initam()
335 {
336     /* ----------------
337      *  initialize heap statistics.
338      * ----------------
339      */
340     InitHeapAccessStatistics();
341 }