summaryrefslogtreecommitdiffstats
path: root/src/mercury/include/mercury_dlog.h
blob: 557b74517979ede44fec3a9b38fb2f48a17703a0 (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
/*
 * Copyright (C) 2013-2020 Argonne National Laboratory, Department of Energy,
 *                    UChicago Argonne, LLC and The HDF Group.
 * All rights reserved.
 *
 * The full copyright notice, including terms governing use, modification,
 * and redistribution, is contained in the COPYING file that can be
 * found at the root of the source code distribution tree.
 */

#ifndef MERCURY_DLOG_H
#define MERCURY_DLOG_H

#include "mercury_util_config.h"

#include "mercury_atomic.h"
#include "mercury_list.h"
#include "mercury_thread_mutex.h"
#include "mercury_time.h"

#include <stdio.h>

/*****************/
/* Public Macros */
/*****************/

/*
 * putting a magic number at the front of the dlog allows us to search
 * for a dlog in a coredump file after a crash and examine its contents.
 */
#define HG_DLOG_MAGICLEN 16         /* bytes to reserve for magic# */
#define HG_DLOG_STDMAGIC ">D.LO.G<" /* standard for first 8 bytes */

/*
 * HG_DLOG_INITIALIZER: initializer for a dlog in a global variable.
 * LESIZE is the number of entries in the LE array.  use it like this:
 *
 * #define FOO_NENTS 128
 * struct hg_dlog_entry foo_le[FOO_NENTS];
 * struct hg_dlog foo_dlog = HG_DLOG_INITIALIZER("foo", foo_le, FOO_NENTS, 0);
 */
#define HG_DLOG_INITIALIZER(NAME, LE, LESIZE, LELOOP)                                                        \
    {                                                                                                        \
        HG_DLOG_STDMAGIC NAME, HG_THREAD_MUTEX_INITIALIZER, HG_LIST_HEAD_INITIALIZER(cnts32),                \
            HG_LIST_HEAD_INITIALIZER(cnts64), LE, LESIZE, LELOOP, 0, 0, 0, 0                                 \
    }

/*************************************/
/* Public Type and Struct Definition */
/*************************************/

/*
 * hg_dlog_entry: an entry in the dlog
 */
struct hg_dlog_entry {
    const char * file; /* file name */
    unsigned int line; /* line number */
    const char * func; /* function name */
    const char * msg;  /* entry message (optional) */
    const void * data; /* user data (optional) */
    hg_time_t    time; /* time added to log */
};

/*
 * hg_dlog_dcount32: 32-bit debug counter in the dlog
 */
struct hg_dlog_dcount32 {
    const char *      name;            /* counter name (short) */
    const char *      descr;           /* description of counter */
    hg_atomic_int32_t c;               /* the counter itself */
    HG_LIST_ENTRY(hg_dlog_dcount32) l; /* linkage */
};

/*
 * hg_dlog_dcount64: 64-bit debug counter in the dlog
 */
struct hg_dlog_dcount64 {
    const char *      name;            /* counter name (short) */
    const char *      descr;           /* description of counter */
    hg_atomic_int64_t c;               /* the counter itself */
    HG_LIST_ENTRY(hg_dlog_dcount64) l; /* linkage */
};

/*
 * hg_dlog: main structure
 */
struct hg_dlog {
    char              dlog_magic[HG_DLOG_MAGICLEN]; /* magic number + name */
    hg_thread_mutex_t dlock;                        /* lock for this data struct */

    /* counter lists */
    HG_LIST_HEAD(hg_dlog_dcount32) cnts32; /* counter list */
    HG_LIST_HEAD(hg_dlog_dcount64) cnts64; /* counter list */

    /* log */
    struct hg_dlog_entry *le;     /* array of log entries */
    unsigned int          lesize; /* size of le[] array */
    int                   leloop; /* circular buffer? */
    unsigned int          lefree; /* next free entry in le[] */
    unsigned int          leadds; /* #adds done if < lesize */
    int                   lestop; /* stop taking new logs */

    int mallocd; /* allocated with malloc? */
};

/*********************/
/* Public Prototypes */
/*********************/

#ifdef __cplusplus
extern "C" {
#endif

/**
 * malloc and return a new dlog
 *
 * \param name [IN]             name of dlog (truncated past 8 bytes)
 * \param lesize [IN]           number of entries to allocate for log buffer
 * \param leloop [IN]           set to make log circular (can overwrite old
 *                              entries)
 *
 * \return the new dlog or NULL on malloc error
 */
HG_UTIL_PUBLIC struct hg_dlog *hg_dlog_alloc(char *name, unsigned int lesize, int leloop);

/**
 * free anything we malloc'd on a dlog.  assumes we have the final
 * active reference to dlog  and it won't be used anymore after this
 * call (so no need to lock it).
 *
 * \param d [IN]                the dlog to finalize
 */
HG_UTIL_PUBLIC void hg_dlog_free(struct hg_dlog *d);

/**
 * make a named atomic32 counter in a dlog and return a pointer to
 * it.  we use the dlock to ensure a counter under a given name only
 * gets created once (makes it easy to share a counter across files).
 * aborts if unable to alloc counter.  use it like this:
 *
 * hg_atomic_int32_t *foo_count;
 * static int init = 0;
 * if (init == 0) {
 *   hg_dlog_mkcount32(dlog, &foo_count, "foocount", "counts of foo");
 *   init = 1;
 * }
 *
 * \param d [IN]                dlog to create the counter in
 * \param cptr [IN/OUT]         pointer to use for counter (set to NULL to
 *                              start)
 * \param name [IN]             short one word name for counter
 * \param descr [IN]            short description of counter
 */
HG_UTIL_PUBLIC void hg_dlog_mkcount32(struct hg_dlog *d, hg_atomic_int32_t **cptr, const char *name,
                                      const char *descr);

/**
 * make a named atomic64 counter in a dlog and return a pointer to
 * it.  we use the dlock to ensure a counter under a given name only
 * gets created once (makes it easy to share a counter across files).
 * aborts if unable to alloc counter.  use it like this:
 *
 * hg_atomic_int64_t *foo_count;
 * static int init = 0;
 * if (init == 0) {
 *   hg_dlog_mkcount64(dlog, &foo_count, "foocount", "counts of foo");
 *   init = 1;
 * }
 *
 * \param d [IN]                dlog to create the counter in
 * \param cptr [IN/OUT]         pointer to use for counter (set to NULL to
 *                              start)
 * \param name [IN]             short one word name for counter
 * \param descr [IN]            short description of counter
 */
HG_UTIL_PUBLIC void hg_dlog_mkcount64(struct hg_dlog *d, hg_atomic_int64_t **cptr, const char *name,
                                      const char *descr);

/**
 * attempt to add a log record to a dlog.  the id and msg should point
 * to static strings that are valid throughout the life of the program
 * (not something that is is on the stack).
 *
 * \param d [IN]                the dlog to add the log record to
 * \param file [IN]             file entry
 * \param line [IN]             line entry
 * \param func [IN]             func entry
 * \param msg [IN]              log entry message (optional, NULL ok)
 * \param data [IN]             user data pointer for record (optional, NULL ok)
 *
 * \return 1 if added, 0 otherwise
 */
static HG_UTIL_INLINE unsigned int hg_dlog_addlog(struct hg_dlog *d, const char *file, unsigned int line,
                                                  const char *func, const char *msg, const void *data);

/**
 * set the value of stop for a dlog (to enable/disable logging)
 *
 * \param d [IN]                dlog to set stop in
 * \param stop [IN]             value of stop to use (1=stop, 0=go)
 */
HG_UTIL_PUBLIC void hg_dlog_setlogstop(struct hg_dlog *d, int stop);

/**
 * reset the log.  this does not change the counters (since users
 * have direct access to the hg_atomic_int64_t's, we don't need
 * an API to change them here).
 *
 * \param d [IN]                dlog to reset
 */
HG_UTIL_PUBLIC void hg_dlog_resetlog(struct hg_dlog *d);

/**
 * dump dlog info to a stream. set trylock if you want to dump even
 * if it is locked (e.g. you are crashing and you don't care about
 * locking).
 *
 * \param d [IN]                dlog to dump
 * \param log_func [IN]         log function to use (default printf)
 * \param stream [IN]           stream to use
 * \param trylock [IN]          just try to lock (warn if it fails)
 */
HG_UTIL_PUBLIC void hg_dlog_dump(struct hg_dlog *d, int (*log_func)(FILE *, const char *, ...), FILE *stream,
                                 int trylock);

/**
 * dump dlog info to a file.   set trylock if you want to dump even
 * if it is locked (e.g. you are crashing and you don't care about
 * locking).  the output file is "base.log" or base-pid.log" depending
 * on the value of addpid.
 *
 * \param d [IN]                dlog to dump
 * \param base [IN]             output file basename
 * \param addpid [IN]           add pid to output filename
 * \param trylock [IN]          just try to lock (warn if it fails)
 */
HG_UTIL_PUBLIC void hg_dlog_dump_file(struct hg_dlog *d, const char *base, int addpid, int trylock);

/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE unsigned int
hg_dlog_addlog(struct hg_dlog *d, const char *file, unsigned int line, const char *func, const char *msg,
               const void *data)
{
    unsigned int rv = 0;
    unsigned int idx;

    hg_thread_mutex_lock(&d->dlock);
    if (d->lestop)
        goto done;
    if (d->leloop == 0 && d->leadds >= d->lesize)
        goto done;
    idx       = d->lefree;
    d->lefree = (d->lefree + 1) % d->lesize;
    if (d->leadds < d->lesize)
        d->leadds++;
    d->le[idx].file = file;
    d->le[idx].line = line;
    d->le[idx].func = func;
    d->le[idx].msg  = msg;
    d->le[idx].data = data;
    hg_time_get_current(&d->le[idx].time);
    rv = 1;

done:
    hg_thread_mutex_unlock(&d->dlock);
    return rv;
}

#ifdef __cplusplus
}
#endif

#endif /* MERCURY_DLOG_H */