summaryrefslogtreecommitdiffstats
path: root/src/H5Clog.c
blob: e3e4388d6c1a9ab1f1b60a194c4129501d90b1c2 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:             H5Clog.c
 *                      May 30 2016
 *                      Quincey Koziol
 *
 * Purpose:             Functions for generic cache logging in JSON format
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/
#include "H5Cmodule.h"         /* This source code file is part of the H5C module */

/***********/
/* Headers */
/***********/
#include "H5private.h"          /* Generic Functions                    */
#ifdef H5_HAVE_PARALLEL
#define H5AC_FRIEND		/*suppress error about including H5ACpkg	  */
#include "H5ACpkg.h"            /* Metadata cache                       */
#endif /* H5_HAVE_PARALLEL */
#include "H5Cpkg.h"            /* Metadata cache                       */
#include "H5Eprivate.h"         /* Error handling                       */
#include "H5MMprivate.h"	/* Memory management			*/


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


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


/********************/
/* Package Typedefs */
/********************/


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


/*********************/
/* Package Variables */
/*********************/


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


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



/*-------------------------------------------------------------------------
 * Function:    H5C_set_up_logging
 *
 * Purpose:     Setup for metadata cache logging.
 *
 *              Metadata logging is enabled and disabled at two levels. This
 *              function and the associated tear_down function open and close
 *              the log file. the start_ and stop_logging functions are then
 *              used to switch logging on/off. Optionally, logging can begin
 *              as soon as the log file is opened (set via the start_immediately
 *              parameter to this function).
 *
 *              The log functionality is split between the H5C and H5AC
 *              packages. Log state and direct log manipulation resides in
 *              H5C. Log messages are generated in H5AC and sent to
 *              the H5C_write_log_message function.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_set_up_logging(H5C_t *cache_ptr, const char log_location[],
    hbool_t start_immediately)
{
#ifdef H5_HAVE_PARALLEL
    H5AC_aux_t *aux_ptr = NULL;
#endif /*H5_HAVE_PARALLEL*/
    char *file_name = NULL;
    size_t n_chars;
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(cache_ptr->logging_enabled)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging already set up")
    if(NULL == log_location)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL log location not allowed")

    /* Possibly fix up the log file name.
     * The extra 39 characters are for adding the rank to the file name
     * under parallel HDF5. 39 characters allows > 2^127 processes which
     * should be enough for anybody.
     *
     * allocation size = <path length> + dot + <rank # length> + \0
     */
    n_chars = HDstrlen(log_location) + 1 + 39 + 1;
    if(NULL == (file_name = (char *)H5MM_calloc(n_chars * sizeof(char))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTALLOC, FAIL, "can't allocate memory for mdc log file name manipulation")

#ifdef H5_HAVE_PARALLEL
    /* Add the rank to the log file name when MPI is in use */
    aux_ptr = (H5AC_aux_t *)(cache_ptr->aux_ptr);

    if(NULL == aux_ptr)
        HDsnprintf(file_name, n_chars, "%s", log_location);
    else {
        if(aux_ptr->magic != H5AC__H5AC_AUX_T_MAGIC)
            HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "bad aux_ptr->magic")
        HDsnprintf(file_name, n_chars, "%s.%d", log_location, aux_ptr->mpi_rank);
    } /* end else */
#else /* H5_HAVE_PARALLEL */
    HDsnprintf(file_name, n_chars, "%s", log_location);
#endif /* H5_HAVE_PARALLEL */

    /* Open log file */
    if(NULL == (cache_ptr->log_file_ptr = HDfopen(file_name, "w")))
        HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, FAIL, "can't create mdc log file")

    /* Set logging flags */
    cache_ptr->logging_enabled = TRUE;
    cache_ptr->currently_logging = start_immediately;

 done:
    if(file_name)
        file_name = (char *)H5MM_xfree(file_name);

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_set_up_logging() */


/*-------------------------------------------------------------------------
 * Function:    H5C_tear_down_logging
 *
 * Purpose:     Tear-down for metadata cache logging.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_tear_down_logging(H5C_t *cache_ptr)
{
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(FALSE == cache_ptr->logging_enabled)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging not enabled")

    /* Unset logging flags */
    cache_ptr->logging_enabled = FALSE;
    cache_ptr->currently_logging = FALSE;

    /* Close log file */
    if(EOF == HDfclose(cache_ptr->log_file_ptr))
        HGOTO_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, FAIL, "problem closing mdc log file")
    cache_ptr->log_file_ptr = NULL;

 done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_tear_down_logging() */


/*-------------------------------------------------------------------------
 * Function:    H5C_start_logging
 *
 * Purpose:     Start logging metadata cache operations.
 *
 *              TODO: Add a function that dumps the current state of the
 *                    metadata cache.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_start_logging(H5C_t *cache_ptr)
{
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(FALSE == cache_ptr->logging_enabled)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging not enabled")
    if(cache_ptr->currently_logging)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging already in progress")

    /* Set logging flags */
    cache_ptr->currently_logging = TRUE;

    /* TODO - Dump cache state */

 done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_start_logging() */


/*-------------------------------------------------------------------------
 * Function:    H5C_stop_logging
 *
 * Purpose:     Stop logging metadata cache operations.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_stop_logging(H5C_t *cache_ptr)
{
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(FALSE == cache_ptr->logging_enabled)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging not enabled")
    if(FALSE == cache_ptr->currently_logging)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "logging not in progress")

    /* Set logging flags */
    cache_ptr->currently_logging = FALSE;

 done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_stop_logging() */


/*-------------------------------------------------------------------------
 * Function:    H5C_get_logging_status
 *
 * Purpose:     Determines if the cache is actively logging (via the OUT
 *              parameter).
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_get_logging_status(const H5C_t *cache_ptr, /*OUT*/ hbool_t *is_enabled,
                       /*OUT*/ hbool_t *is_currently_logging)
{
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(NULL == is_enabled)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(NULL == is_currently_logging)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")

    *is_enabled = cache_ptr->logging_enabled;
    *is_currently_logging = cache_ptr->currently_logging;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_get_logging_status() */


/*-------------------------------------------------------------------------
 * Function:    H5C_write_log_message
 *
 * Purpose:     Write a message to the log file and flush the file. 
 *              The message string is neither modified nor freed.
 *
 * Return:      Non-negative on success/Negative on failure
 *
 * Programmer:	Dana Robinson
 *              Sunday, March 16, 2014
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5C_write_log_message(const H5C_t *cache_ptr, const char message[])
{
    size_t n_chars;
    herr_t ret_value = SUCCEED;      /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    /* Sanity checks */
    if(NULL == cache_ptr)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache_ptr == NULL")
    if(H5C__H5C_T_MAGIC != cache_ptr->magic)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "cache magic value incorrect")
    if(FALSE == cache_ptr->currently_logging)
        HGOTO_ERROR(H5E_CACHE, H5E_SYSTEM, FAIL, "not currently logging")
    if(NULL == message)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL log message not allowed")

    /* Write the log message and flush */
    n_chars = HDstrlen(message);
    if((int)n_chars != HDfprintf(cache_ptr->log_file_ptr, message))
        HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "error writing log message")
    if(EOF == HDfflush(cache_ptr->log_file_ptr))
        HGOTO_ERROR(H5E_FILE, H5E_WRITEERROR, FAIL, "error flushing log message")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5C_write_log_message() */