1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*-------------------------------------------------------------------------
15  *
16  * Created:		H5B2dbg.c
17  *			Feb  2 2005
18  *			Quincey Koziol <koziol@ncsa.uiuc.edu>
19  *
20  * Purpose:		Dump debugging information about a v2 B-tree.
21  *
22  *-------------------------------------------------------------------------
23  */
24 
25 /****************/
26 /* Module Setup */
27 /****************/
28 
29 #define H5B2_PACKAGE		/*suppress error about including H5B2pkg  */
30 
31 /***********/
32 /* Headers */
33 /***********/
34 #include "H5private.h"		/* Generic Functions			*/
35 #include "H5B2pkg.h"		/* v2 B-trees				*/
36 #include "H5Eprivate.h"		/* Error handling		  	*/
37 #include "H5FLprivate.h"	/* Free Lists                           */
38 
39 /****************/
40 /* Local Macros */
41 /****************/
42 
43 
44 /******************/
45 /* Local Typedefs */
46 /******************/
47 
48 
49 /********************/
50 /* Package Typedefs */
51 /********************/
52 
53 
54 /********************/
55 /* Local Prototypes */
56 /********************/
57 
58 
59 /*********************/
60 /* Package Variables */
61 /*********************/
62 
63 
64 /*****************************/
65 /* Library Private Variables */
66 /*****************************/
67 
68 
69 /*******************/
70 /* Local Variables */
71 /*******************/
72 
73 
74 /*-------------------------------------------------------------------------
75  * Function:	H5B2_hdr_debug
76  *
77  * Purpose:	Prints debugging info about a B-tree header.
78  *
79  * Return:	Non-negative on success/Negative on failure
80  *
81  * Programmer:	Quincey Koziol
82  *		koziol@ncsa.uiuc.edu
83  *		Feb  2 2005
84  *
85  *-------------------------------------------------------------------------
86  */
87 herr_t
H5B2_hdr_debug(H5F_t * f,hid_t dxpl_id,haddr_t addr,FILE * stream,int indent,int fwidth,const H5B2_class_t * type,haddr_t obj_addr)88 H5B2_hdr_debug(H5F_t *f, hid_t dxpl_id, haddr_t addr, FILE *stream, int indent, int fwidth,
89     const H5B2_class_t *type, haddr_t obj_addr)
90 {
91     H5B2_hdr_t	*hdr = NULL;            /* B-tree header info */
92     void        *dbg_ctx = NULL;	/* v2 B-tree debugging context */
93     unsigned    u;                      /* Local index variable */
94     char        temp_str[128];          /* Temporary string, for formatting */
95     H5B2_hdr_cache_ud_t cache_udata;    /* User-data for callback */
96     herr_t      ret_value = SUCCEED;    /* Return value */
97 
98     FUNC_ENTER_NOAPI(FAIL)
99 
100     /*
101      * Check arguments.
102      */
103     HDassert(f);
104     HDassert(H5F_addr_defined(addr));
105     HDassert(H5F_addr_defined(obj_addr));
106     HDassert(stream);
107     HDassert(indent >= 0);
108     HDassert(fwidth >= 0);
109     HDassert(type);
110     HDassert((type->crt_dbg_ctx && type->dst_dbg_ctx) ||
111         (NULL == type->crt_dbg_ctx && NULL == type->dst_dbg_ctx));
112 
113     /* Check for debugging context callback available */
114     if(type->crt_dbg_ctx) {
115         /* Create debugging context */
116         if(NULL == (dbg_ctx = (type->crt_dbg_ctx)(f, dxpl_id, obj_addr)))
117 	    HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "unable to create v2 B-tree debugging context")
118     } /* end if */
119 
120     /*
121      * Load the B-tree header.
122      */
123     cache_udata.f = f;
124     cache_udata.ctx_udata = dbg_ctx;
125     if(NULL == (hdr = (H5B2_hdr_t *)H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, addr, &cache_udata, H5AC_READ)))
126 	HGOTO_ERROR(H5E_BTREE, H5E_CANTLOAD, FAIL, "unable to load B-tree header")
127 
128     /* Set file pointer for this B-tree operation */
129     hdr->f = f;
130 
131     /* Print opening message */
132     HDfprintf(stream, "%*sv2 B-tree Header...\n", indent, "");
133 
134     /*
135      * Print the values.
136      */
137     HDfprintf(stream, "%*s%-*s %s (%u)\n", indent, "", fwidth,
138 	      "Tree type ID:", hdr->cls->name, (unsigned)hdr->cls->id);
139     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
140 	      "Size of node:",
141 	      hdr->node_size);
142     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
143 	      "Size of raw (disk) record:",
144 	      hdr->rrec_size);
145     HDfprintf(stream, "%*s%-*s %s\n", indent, "", fwidth,
146 	      "Dirty flag:",
147 	      hdr->cache_info.is_dirty ? "True" : "False");
148     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
149 	      "Depth:",
150 	      hdr->depth);
151     HDfprintf(stream, "%*s%-*s %Hu\n", indent, "", fwidth,
152 	      "Number of records in tree:",
153 	      hdr->root.all_nrec);
154     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
155 	      "Number of records in root node:",
156 	      hdr->root.node_nrec);
157     HDfprintf(stream, "%*s%-*s %a\n", indent, "", fwidth,
158 	      "Address of root node:",
159 	      hdr->root.addr);
160     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
161 	      "Split percent:",
162 	      hdr->split_percent);
163     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
164 	      "Merge percent:",
165 	      hdr->merge_percent);
166 
167     /* Print relevant node info */
168     HDfprintf(stream, "%*sNode Info: (max_nrec/split_nrec/merge_nrec)\n", indent, "");
169     for(u = 0; u < (unsigned)(hdr->depth + 1); u++) {
170         HDsnprintf(temp_str, sizeof(temp_str), "Depth %u:", u);
171         HDfprintf(stream, "%*s%-*s (%u/%u/%u)\n", indent + 3, "", MAX(0, fwidth - 3),
172             temp_str,
173             hdr->node_info[u].max_nrec, hdr->node_info[u].split_nrec, hdr->node_info[u].merge_nrec);
174     } /* end for */
175 
176 done:
177     if(dbg_ctx && (type->dst_dbg_ctx)(dbg_ctx) < 0)
178         HDONE_ERROR(H5E_BTREE, H5E_CANTRELEASE, FAIL, "unable to release v2 B-tree debugging context")
179     if(hdr) {
180         hdr->f = NULL;
181         if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_HDR, addr, hdr, H5AC__NO_FLAGS_SET) < 0)
182             HDONE_ERROR(H5E_BTREE, H5E_PROTECT, FAIL, "unable to release B-tree header")
183     } /* end if */
184 
185     FUNC_LEAVE_NOAPI(ret_value)
186 } /* end H5B2_hdr_debug() */
187 
188 
189 /*-------------------------------------------------------------------------
190  * Function:	H5B2_int_debug
191  *
192  * Purpose:	Prints debugging info about a B-tree internal node
193  *
194  * Return:	Non-negative on success/Negative on failure
195  *
196  * Programmer:	Quincey Koziol
197  *		koziol@ncsa.uiuc.edu
198  *		Feb  4 2005
199  *
200  *-------------------------------------------------------------------------
201  */
202 herr_t
H5B2_int_debug(H5F_t * f,hid_t dxpl_id,haddr_t addr,FILE * stream,int indent,int fwidth,const H5B2_class_t * type,haddr_t hdr_addr,unsigned nrec,unsigned depth,haddr_t obj_addr)203 H5B2_int_debug(H5F_t *f, hid_t dxpl_id, haddr_t addr, FILE *stream, int indent, int fwidth,
204     const H5B2_class_t *type, haddr_t hdr_addr, unsigned nrec, unsigned depth, haddr_t obj_addr)
205 {
206     H5B2_hdr_t	*hdr = NULL;            /* B-tree header */
207     H5B2_internal_t	*internal = NULL;   /* B-tree internal node */
208     void        *dbg_ctx = NULL;	/* v2 B-tree debugging context */
209     unsigned	u;                      /* Local index variable */
210     char        temp_str[128];          /* Temporary string, for formatting */
211     H5B2_hdr_cache_ud_t cache_udata;    /* User-data for callback */
212     herr_t      ret_value=SUCCEED;      /* Return value */
213 
214     FUNC_ENTER_NOAPI(FAIL)
215 
216     /*
217      * Check arguments.
218      */
219     HDassert(f);
220     HDassert(H5F_addr_defined(addr));
221     HDassert(stream);
222     HDassert(indent >= 0);
223     HDassert(fwidth >= 0);
224     HDassert(type);
225     HDassert((type->crt_dbg_ctx && type->dst_dbg_ctx) ||
226         (NULL == type->crt_dbg_ctx && NULL == type->dst_dbg_ctx));
227     HDassert(H5F_addr_defined(hdr_addr));
228     HDassert(H5F_addr_defined(obj_addr));
229     HDassert(nrec > 0);
230 
231     /* Check for debugging context callback available */
232     if(type->crt_dbg_ctx) {
233         /* Create debugging context */
234         if(NULL == (dbg_ctx = (type->crt_dbg_ctx)(f, dxpl_id, obj_addr)))
235 	    HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "unable to create v2 B-tree debugging context")
236     } /* end if */
237 
238     /*
239      * Load the B-tree header.
240      */
241     cache_udata.f = f;
242     cache_udata.ctx_udata = dbg_ctx;
243     if(NULL == (hdr = (H5B2_hdr_t *)H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, hdr_addr, &cache_udata, H5AC_READ)))
244 	HGOTO_ERROR(H5E_BTREE, H5E_CANTLOAD, FAIL, "unable to load B-tree header")
245 
246     /* Set file pointer for this B-tree operation */
247     hdr->f = f;
248 
249     /*
250      * Load the B-tree internal node
251      */
252     if(NULL == (internal = H5B2_protect_internal(hdr, dxpl_id, addr, nrec, depth, H5AC_READ)))
253 	HGOTO_ERROR(H5E_BTREE, H5E_CANTLOAD, FAIL, "unable to load B-tree internal node")
254 
255     /* Print opening message */
256     if(internal->depth == 1)
257         HDfprintf(stream, "%*sv2 B-tree Internal 'Leaf' Node...\n", indent, "");
258     else
259         HDfprintf(stream, "%*sv2 B-tree Internal 'Branch' Node...\n", indent, "");
260 
261     /*
262      * Print the values.
263      */
264     HDfprintf(stream, "%*s%-*s %s (%u)\n", indent, "", fwidth,
265 	      "Tree type ID:", hdr->cls->name, (unsigned)hdr->cls->id);
266     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
267 	      "Size of node:",
268 	      hdr->node_size);
269     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
270 	      "Size of raw (disk) record:",
271 	      hdr->rrec_size);
272     HDfprintf(stream, "%*s%-*s %s\n", indent, "", fwidth,
273 	      "Dirty flag:",
274 	      internal->cache_info.is_dirty ? "True" : "False");
275     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
276 	      "Number of records in node:",
277 	      internal->nrec);
278 
279     /* Print all node pointers and records */
280     for(u = 0; u < internal->nrec; u++) {
281         /* Print node pointer */
282         HDsnprintf(temp_str, sizeof(temp_str), "Node pointer #%u: (all/node/addr)", u);
283         HDfprintf(stream, "%*s%-*s (%Hu/%u/%a)\n", indent + 3, "", MAX(0, fwidth - 3),
284                   temp_str,
285                   internal->node_ptrs[u].all_nrec,
286                   internal->node_ptrs[u].node_nrec,
287                   internal->node_ptrs[u].addr);
288 
289         /* Print record */
290         HDsnprintf(temp_str, sizeof(temp_str), "Record #%u:", u);
291         HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(0, fwidth - 3),
292                   temp_str);
293         HDassert(H5B2_INT_NREC(internal, hdr, u));
294         (void)(type->debug)(stream, f, dxpl_id, indent + 6, MAX (0, fwidth-6),
295             H5B2_INT_NREC(internal, hdr, u), dbg_ctx);
296     } /* end for */
297 
298     /* Print final node pointer */
299     HDsnprintf(temp_str, sizeof(temp_str), "Node pointer #%u: (all/node/addr)", u);
300     HDfprintf(stream, "%*s%-*s (%Hu/%u/%a)\n", indent + 3, "", MAX(0, fwidth - 3),
301               temp_str,
302               internal->node_ptrs[u].all_nrec,
303               internal->node_ptrs[u].node_nrec,
304               internal->node_ptrs[u].addr);
305 
306 done:
307     if(dbg_ctx && (type->dst_dbg_ctx)(dbg_ctx) < 0)
308         HDONE_ERROR(H5E_BTREE, H5E_CANTRELEASE, FAIL, "unable to release v2 B-tree debugging context")
309     if(hdr) {
310         hdr->f = NULL;
311         if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_HDR, hdr_addr, hdr, H5AC__NO_FLAGS_SET) < 0)
312             HDONE_ERROR(H5E_BTREE, H5E_PROTECT, FAIL, "unable to release B-tree header")
313     } /* end if */
314     if(internal && H5AC_unprotect(f, dxpl_id, H5AC_BT2_INT, addr, internal, H5AC__NO_FLAGS_SET) < 0)
315         HDONE_ERROR(H5E_BTREE, H5E_PROTECT, FAIL, "unable to release B-tree internal node")
316 
317     FUNC_LEAVE_NOAPI(ret_value)
318 } /* end H5B2_int_debug() */
319 
320 
321 /*-------------------------------------------------------------------------
322  * Function:	H5B2_leaf_debug
323  *
324  * Purpose:	Prints debugging info about a B-tree leaf node
325  *
326  * Return:	Non-negative on success/Negative on failure
327  *
328  * Programmer:	Quincey Koziol
329  *		koziol@ncsa.uiuc.edu
330  *		Feb  7 2005
331  *
332  *-------------------------------------------------------------------------
333  */
334 herr_t
H5B2_leaf_debug(H5F_t * f,hid_t dxpl_id,haddr_t addr,FILE * stream,int indent,int fwidth,const H5B2_class_t * type,haddr_t hdr_addr,unsigned nrec,haddr_t obj_addr)335 H5B2_leaf_debug(H5F_t *f, hid_t dxpl_id, haddr_t addr, FILE *stream, int indent, int fwidth,
336     const H5B2_class_t *type, haddr_t hdr_addr, unsigned nrec, haddr_t obj_addr)
337 {
338     H5B2_hdr_t	*hdr = NULL;            /* B-tree header */
339     H5B2_leaf_t	*leaf = NULL;           /* B-tree leaf node */
340     H5B2_hdr_cache_ud_t cache_udata;    /* User-data for callback */
341     void        *dbg_ctx = NULL;	/* v2 B-tree debugging context */
342     unsigned	u;                      /* Local index variable */
343     char        temp_str[128];          /* Temporary string, for formatting */
344     herr_t      ret_value = SUCCEED;    /* Return value */
345 
346     FUNC_ENTER_NOAPI(FAIL)
347 
348     /*
349      * Check arguments.
350      */
351     HDassert(f);
352     HDassert(H5F_addr_defined(addr));
353     HDassert(stream);
354     HDassert(indent >= 0);
355     HDassert(fwidth >= 0);
356     HDassert(type);
357     HDassert((type->crt_dbg_ctx && type->dst_dbg_ctx) ||
358         (NULL == type->crt_dbg_ctx && NULL == type->dst_dbg_ctx));
359     HDassert(H5F_addr_defined(hdr_addr));
360     HDassert(H5F_addr_defined(obj_addr));
361     HDassert(nrec > 0);
362 
363     /* Check for debugging context callback available */
364     if(type->crt_dbg_ctx) {
365         /* Create debugging context */
366         if(NULL == (dbg_ctx = (type->crt_dbg_ctx)(f, dxpl_id, obj_addr)))
367 	    HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "unable to create v2 B-tree debugging context")
368     } /* end if */
369 
370     /*
371      * Load the B-tree header.
372      */
373     cache_udata.f = f;
374     cache_udata.ctx_udata = dbg_ctx;
375     if(NULL == (hdr = (H5B2_hdr_t *)H5AC_protect(f, dxpl_id, H5AC_BT2_HDR, hdr_addr, &cache_udata, H5AC_READ)))
376 	HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree header")
377 
378     /* Set file pointer for this B-tree operation */
379     hdr->f = f;
380 
381     /*
382      * Load the B-tree leaf node
383      */
384     if(NULL == (leaf = H5B2_protect_leaf(hdr, dxpl_id, addr, nrec, H5AC_READ)))
385 	HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
386 
387     /* Print opening message */
388     HDfprintf(stream, "%*sv2 B-tree Leaf Node...\n", indent, "");
389 
390     /*
391      * Print the values.
392      */
393     HDfprintf(stream, "%*s%-*s %s (%u)\n", indent, "", fwidth,
394 	      "Tree type ID:", hdr->cls->name, (unsigned)hdr->cls->id);
395     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
396 	      "Size of node:",
397 	      hdr->node_size);
398     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
399 	      "Size of raw (disk) record:",
400 	      hdr->rrec_size);
401     HDfprintf(stream, "%*s%-*s %s\n", indent, "", fwidth,
402 	      "Dirty flag:",
403 	      leaf->cache_info.is_dirty ? "True" : "False");
404     HDfprintf(stream, "%*s%-*s %u\n", indent, "", fwidth,
405 	      "Number of records in node:",
406 	      leaf->nrec);
407 
408     /* Print all node pointers and records */
409     for(u = 0; u < leaf->nrec; u++) {
410         /* Print record */
411         HDsnprintf(temp_str, sizeof(temp_str), "Record #%u:", u);
412         HDfprintf(stream, "%*s%-*s\n", indent + 3, "", MAX(0, fwidth - 3),
413                   temp_str);
414         HDassert(H5B2_LEAF_NREC(leaf, hdr, u));
415         (void)(type->debug)(stream, f, dxpl_id, indent + 6, MAX (0, fwidth-6),
416             H5B2_LEAF_NREC(leaf, hdr, u), dbg_ctx);
417     } /* end for */
418 
419 done:
420     if(dbg_ctx && (type->dst_dbg_ctx)(dbg_ctx) < 0)
421         HDONE_ERROR(H5E_BTREE, H5E_CANTRELEASE, FAIL, "unable to release v2 B-tree debugging context")
422     if(hdr) {
423         hdr->f = NULL;
424         if(H5AC_unprotect(f, dxpl_id, H5AC_BT2_HDR, hdr_addr, hdr, H5AC__NO_FLAGS_SET) < 0)
425             HDONE_ERROR(H5E_BTREE, H5E_PROTECT, FAIL, "unable to release B-tree header")
426     } /* end if */
427     if(leaf && H5AC_unprotect(f, dxpl_id, H5AC_BT2_LEAF, addr, leaf, H5AC__NO_FLAGS_SET) < 0)
428         HDONE_ERROR(H5E_BTREE, H5E_PROTECT, FAIL, "unable to release B-tree leaf node")
429 
430     FUNC_LEAVE_NOAPI(ret_value)
431 } /* end H5B2_leaf_debug() */
432 
433