summaryrefslogtreecommitdiffstats
path: root/src/H5SMcache.c
blob: e245b9b519e18ab357c7b34b20d9888736b0638d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/****************/
/* Module Setup */
/****************/

#define H5F_PACKAGE		/*suppress error about including H5Fpkg 	  */
#define H5SM_PACKAGE		/*suppress error about including H5SMpkg	  */


/***********/
/* Headers */
/***********/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Fpkg.h"		/* File access                          */
#include "H5FLprivate.h"	/* Free Lists                           */
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5SMpkg.h"            /* Shared object header messages        */
#include "H5WBprivate.h"        /* Wrapped Buffers                      */


/****************/
/* Local Macros */
/****************/

/* Size of stack buffer for serialized tables */
#define H5SM_TBL_BUF_SIZE       1024

/* Size of stack buffer for serialized list indices */
#define H5SM_LST_BUF_SIZE       1024


/******************/
/* Local Typedefs */
/******************/


/********************/
/* Local Prototypes */
/********************/

/* Metadata cache (H5AC) callbacks */

static void *H5SM_table_deserialize(haddr_t addr, size_t len, const void *image,
    void *udata, hbool_t *dirty);
static herr_t H5SM_table_serialize(const H5F_t * f, hid_t dxpl_id, haddr_t addr,
    size_t len, void *image, void *thing, unsigned *flags, haddr_t *new_addr,
    size_t *new_len, void **new_image);
static herr_t H5SM_table_free_icr(haddr_t addr, size_t len, void *thing);

static void *H5SM_list_deserialize(haddr_t addr, size_t len, const void *image,
    void *udata, hbool_t *dirty);
static herr_t H5SM_list_serialize(const H5F_t * f, hid_t dxpl_id, haddr_t addr,
    size_t len, void *image, void *thing, unsigned *flags, haddr_t *new_addr,
    size_t *new_len, void **new_image);
static herr_t H5SM_list_free_icr(haddr_t addr, size_t len, void *thing);


/*********************/
/* Package Variables */
/*********************/
/* H5SM inherits cache-like properties from H5AC */
const H5AC_class_t H5AC_SOHM_TABLE[1] = {{
    H5AC_SOHM_TABLE_ID,
    "shared object header message",
    H5FD_MEM_SOHM_TABLE,
    H5SM_table_deserialize,
    NULL,
    H5SM_table_serialize,
    H5SM_table_free_icr,
    NULL
}};

const H5AC_class_t H5AC_SOHM_LIST[1] = {{
    H5AC_SOHM_LIST_ID,
    "shared object header message",
    H5FD_MEM_SOHM_INDEX,
    H5SM_list_deserialize,
    NULL,
    H5SM_list_serialize,
    H5SM_list_free_icr,
    NULL
}};


/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/



/*-------------------------------------------------------------------------
 * Function:	H5SM_table_deserialize
 *
 * Purpose:	Deserialize the data structure from disk.
 *
 * Return:	Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:	James Laird
 *		November 6, 2006
 *
 *-------------------------------------------------------------------------
 */
static void *
H5SM_table_deserialize(haddr_t UNUSED addr, size_t UNUSED len,
    const void *image, void *_udata, hbool_t UNUSED *dirty)
{
    H5SM_master_table_t *table = NULL;
    H5F_t *f = (H5F_t *)_udata;         /* File pointer */
    const uint8_t *p;                   /* Pointer into input buffer */
    uint32_t      stored_chksum;        /* Stored metadata checksum value */
    uint32_t      computed_chksum;      /* Computed metadata checksum value */
    size_t        x;                    /* Counter variable for index headers */
    H5SM_master_table_t *ret_value;

    FUNC_ENTER_NOAPI_NOINIT(H5SM_table_deserialize)

    /* Verify that we're reading version 0 of the table; this is the only
     * version defined so far.
     */
    HDassert(f->shared->sohm_vers == HDF5_SHAREDHEADER_VERSION);
    HDassert(image);

    /* Allocate space for the master table in memory */
    if(NULL == (table = H5FL_CALLOC(H5SM_master_table_t)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Read number of indexes and version from file superblock */
    table->num_indexes = f->shared->sohm_nindexes;

    HDassert(addr == f->shared->sohm_addr);
    HDassert(addr != HADDR_UNDEF);
    HDassert(table->num_indexes > 0);

    /* Get temporary pointer to serialized table */
    p = image;

    /* Check magic number */
    if(HDmemcmp(p, H5SM_TABLE_MAGIC, (size_t)H5SM_SIZEOF_MAGIC))
	HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "bad SOHM table signature")
    p += H5SM_SIZEOF_MAGIC;

    /* Don't count the checksum in the table size yet, since it comes after
     * all of the index headers
     */
    HDassert((size_t)(p - (const uint8_t *)image) == H5SM_TABLE_SIZE(f) - H5SM_SIZEOF_CHECKSUM);

    /* Allocate space for the index headers in memory*/
    if(NULL == (table->indexes = (H5SM_index_header_t *)H5FL_ARR_MALLOC(H5SM_index_header_t, (size_t)table->num_indexes)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed for SOHM indexes")

    /* Read in the index headers */
    for(x = 0; x < table->num_indexes; ++x) {
        /* Verify correct version of index list */
        if(H5SM_LIST_VERSION != *p++)
            HGOTO_ERROR(H5E_FILE, H5E_VERSION, NULL, "bad shared message list version number")

        /* Type of the index (list or B-tree) */
        table->indexes[x].index_type= *p++;

        /* Type of messages in the index */
        UINT16DECODE(p, table->indexes[x].mesg_types);

        /* Minimum size of message to share */
        UINT32DECODE(p, table->indexes[x].min_mesg_size);

        /* List cutoff; fewer than this number and index becomes a list */
        UINT16DECODE(p, table->indexes[x].list_max);

        /* B-tree cutoff; more than this number and index becomes a B-tree */
        UINT16DECODE(p, table->indexes[x].btree_min);

        /* Number of messages shared */
        UINT16DECODE(p, table->indexes[x].num_messages);

        /* Address of the actual index */
        H5F_addr_decode(f, &p, &(table->indexes[x].index_addr));

        /* Address of the index's heap */
        H5F_addr_decode(f, &p, &(table->indexes[x].heap_addr));
    } /* end for */

    /* Read in checksum */
    UINT32DECODE(p, stored_chksum);

    /* Sanity check */
    HDassert((size_t)(p - (const uint8_t *)image) == len);

    /* Compute checksum on entire header */
    computed_chksum = H5_checksum_metadata(image, (len - H5SM_SIZEOF_CHECKSUM), 0);

    /* Verify checksum */
    if(stored_chksum != computed_chksum)
        HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, NULL, "incorrect metadata checksum for shared message table")

    /* Set return value */
    ret_value = table;

    /* Sanity check */
    HDassert((size_t)((const uint8_t *)p - (const uint8_t *)image) <= len);

done:
    /* Release resources */
     if(!ret_value && table)
        (void)H5SM_table_dest(table);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM_table_deserialize() */


/*-------------------------------------------------------------------------
 * Function:	H5SM_table_serialize
 *
 * Purpose:	Serialize the data structure for writing to disk.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:	James Laird
 *		November 6, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM_table_serialize(const H5F_t * f, hid_t UNUSED dxlp_id, haddr_t UNUSED addr,
    size_t UNUSED len, void *image, void *_thing, unsigned *flags,
    haddr_t UNUSED *new_addr, size_t UNUSED *new_len, void UNUSED **new_image)
{
    H5SM_master_table_t *table = (H5SM_master_table_t *)_thing;
    uint8_t  *p;                 /* Pointer into raw data buffer */
    uint32_t computed_chksum;    /* Computed metadata checksum value */
    size_t   x;                  /* Counter variable */

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5SM_table_serialize)

    /* check arguments */
    HDassert(f);
    HDassert(image);
    HDassert(table);
    HDassert(H5F_addr_defined(addr));


    /* Verify that we're writing version 0 of the table; this is the only
     * version defined so far.
     */
    HDassert(f->shared->sohm_vers == HDF5_SHAREDHEADER_VERSION);

    /* Get temporary pointer to buffer for serialized table */
    p = image;

    /* Encode magic number */
    HDmemcpy(p, H5SM_TABLE_MAGIC, (size_t)H5SM_SIZEOF_MAGIC);
    p += H5SM_SIZEOF_MAGIC;

    /* Encode each index header */
    for(x = 0; x < table->num_indexes; ++x) {
        /* Version for this list. */
        *p++ = H5SM_LIST_VERSION;

        /* Is message index a list or a B-tree? */
        *p++ = table->indexes[x].index_type;

        /* Type of messages in the index */
        UINT16ENCODE(p, table->indexes[x].mesg_types);

        /* Minimum size of message to share */
        UINT32ENCODE(p, table->indexes[x].min_mesg_size);

        /* List cutoff; fewer than this number and index becomes a list */
        UINT16ENCODE(p, table->indexes[x].list_max);

        /* B-tree cutoff; more than this number and index becomes a B-tree */
        UINT16ENCODE(p, table->indexes[x].btree_min);

        /* Number of messages shared */
        UINT16ENCODE(p, table->indexes[x].num_messages);

        /* Address of the actual index */
        H5F_addr_encode(f, &p, table->indexes[x].index_addr);

        /* Address of the index's heap */
        H5F_addr_encode(f, &p, table->indexes[x].heap_addr);
    } /* end for */

    /* Compute checksum on buffer */
    computed_chksum = H5_checksum_metadata(image, (len - H5SM_SIZEOF_CHECKSUM), 0);
    UINT32ENCODE(p, computed_chksum);

    /* Reset the cache flags for this operation (metadata not resize or renamed) */
    *flags = 0;

    /* Sanity check */
    HDassert((size_t)((const uint8_t *)p - (const uint8_t *)image) <= len);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM_table_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5SM_table_free_icr
 *
 * Purpose:     Destroy/release an "in core representation" of a data structure
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  Mike McGreevy
 *              mcgreevy@hdfgroup.org
 *              Jun 03, 2008
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM_table_free_icr(haddr_t UNUSED addr, size_t UNUSED len, void *thing)
{

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5SM_table_free_icr)

    /* Check arguments */
    HDassert(thing);

    /* Destroy Shared Object Header Message */
    H5SM_table_dest(thing);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM_table_free_icr() */


/*-------------------------------------------------------------------------
 * Function:	H5SM_list_deserialize
 *
 * Purpose:	Deserialize the data structure from disk.
 *
 * Return:	Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:	James Laird
 *		November 6, 2006
 *
 *-------------------------------------------------------------------------
 */
static void *
H5SM_list_deserialize(haddr_t UNUSED addr, size_t UNUSED len, const void *image,
    void *_udata, hbool_t UNUSED *dirty)
{
    H5SM_list_t *list;          /* The SOHM list being read in */
    H5SM_list_cache_ud_t *udata = (H5SM_list_cache_ud_t *)_udata; /* User data for callback */
    size_t size;                /* Size of SOHM list on disk */
    const uint8_t *p;           /* Pointer into input buffer */
    uint32_t stored_chksum;     /* Stored metadata checksum value */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    size_t x;                   /* Counter variable for messages in list */
    H5SM_list_t *ret_value;     /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5SM_list_deserialize)

    /* Sanity check */
    HDassert(udata->header);

    /* Allocate space for the SOHM list data structure */
    if(NULL == (list = H5FL_MALLOC(H5SM_list_t)))
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "memory allocation failed")
    HDmemset(&list->cache_info, 0, sizeof(H5AC_info_t));

    /* Allocate list in memory as an array*/
    if((list->messages = (H5SM_sohm_t *)H5FL_ARR_MALLOC(H5SM_sohm_t, udata->header->list_max)) == NULL)
	HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "file allocation failed for SOHM list")

    list->header = udata->header;

    /* Compute the size of the SOHM list on disk */
    size = H5SM_LIST_SIZE(udata->f, udata->header->num_messages);

    /* Get temporary pointer to serialized list index */
    p = image;

    /* Check magic number */
    if(HDmemcmp(p, H5SM_LIST_MAGIC, (size_t)H5SM_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "bad SOHM list signature")
    p += H5SM_SIZEOF_MAGIC;

    /* Read messages into the list array */
    for(x = 0; x < udata->header->num_messages; x++) {
        if(H5SM_message_decode(udata->f, p, &(list->messages[x])) < 0)
            HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "can't decode shared message")
        p += H5SM_SOHM_ENTRY_SIZE(udata->f);
    } /* end for */

    /* Read in checksum */
    UINT32DECODE(p, stored_chksum);

    /* Sanity check */
    HDassert((size_t)(p - (const uint8_t *)image) <= len);

    /* Sanity check */
    HDassert((size_t)(p - (const uint8_t *)image) == size);

    /* Compute checksum on entire header */
    computed_chksum = H5_checksum_metadata(image, (size - H5SM_SIZEOF_CHECKSUM), 0);

    /* Verify checksum */
    if(stored_chksum != computed_chksum)
        HGOTO_ERROR(H5E_SOHM, H5E_BADVALUE, NULL, "incorrect metadata checksum for shared message list")

    /* Initialize the rest of the array */
    for(x = udata->header->num_messages; x < udata->header->list_max; x++)
        list->messages[x].location = H5SM_NO_LOC;

    /* Set return value */
    ret_value = list;

done:
    /* Release resources */
    if(!ret_value && list) {
        if(list->messages)
            list->messages = H5FL_ARR_FREE(H5SM_sohm_t, list->messages);
        list = H5FL_FREE(H5SM_list_t, list);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM_list_deserialize() */


/*-------------------------------------------------------------------------
 * Function:	H5SM_list_serialize
 *
 * Purpose:	Serialize the data structure for writing to disk.
 *
 * Return:	Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:	James Laird
 *		November 6, 2006
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM_list_serialize(const H5F_t * f, hid_t UNUSED dxpl_id, haddr_t UNUSED addr,
    size_t UNUSED len, void *image, void *_thing, unsigned *flags,
    haddr_t UNUSED *new_addr, size_t UNUSED *new_len, void UNUSED **new_image)
{
    H5SM_list_t *list = (H5SM_list_t *)_thing;
    uint8_t *p;                 /* Pointer into raw data buffer */
    size_t size;                /* Header size on disk */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    size_t mesgs_written;       /* Number of messages written to list */
    size_t x;                   /* Local index variable */
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT(H5SM_list_serialize)

    /* check arguments */
    HDassert(f);
    HDassert(image);
    HDassert(list);
    HDassert(list->header);

    size = H5SM_LIST_SIZE(f, list->header->num_messages);

    /* Get temporary pointer to buffer for serialized list index */
    p = image;

    /* Encode magic number */
    HDmemcpy(p, H5SM_LIST_MAGIC, (size_t)H5SM_SIZEOF_MAGIC);
    p += H5SM_SIZEOF_MAGIC;

    /* Write messages from the messages array to disk */
    mesgs_written = 0;
    for(x = 0; x < list->header->list_max && mesgs_written < list->header->num_messages; x++) {
        if(list->messages[x].location != H5SM_NO_LOC) {
            if(H5SM_message_encode(f, p, &(list->messages[x])) < 0)
                HGOTO_ERROR(H5E_SOHM, H5E_CANTFLUSH, FAIL, "unable to write shared message to disk")

            p+=H5SM_SOHM_ENTRY_SIZE(f);
            ++mesgs_written;
        } /* end if */
    } /* end for */
    HDassert(mesgs_written == list->header->num_messages);

    /* Compute checksum on buffer */
    computed_chksum = H5_checksum_metadata(image, (size - H5SM_SIZEOF_CHECKSUM), 0);
    UINT32ENCODE(p, computed_chksum);

    /* Reset the cache flags for this operation (metadata not resize or renamed) */
    *flags = 0;

    /* Sanity check */
    HDassert((size_t)((const uint8_t *)p - (const uint8_t *)image) <= len);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM_list_serialize() */


/*-------------------------------------------------------------------------
 * Function:    H5SM_list_free_icr
 *
 * Purpose:     Destroy/release an "in core representation" of a data structure
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  Mike McGreevy
 *              mcgreevy@hdfgroup.org
 *              Jun 03, 2008
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM_list_free_icr(haddr_t UNUSED addr, size_t UNUSED len, void *thing)
{

    FUNC_ENTER_NOAPI_NOINIT_NOFUNC(H5SM_list_free_icr)

    /* Check arguments */
    HDassert(thing);

    /* Destroy Shared Object Header Message */
    H5SM_list_dest(thing);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM_list_free_icr() */