HPCToolkit
hpcfmt.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // General and helper functions for reading/writing a HPC data
54 // files from/to a binary file.
55 //
56 // These routines *must not* allocate dynamic memory; if such memory
57 // is needed, callbacks to the user's allocator should be used.
58 //
59 // Description:
60 // [The set of functions, macros, etc. defined in the file]
61 //
62 //***************************************************************************
63 
64 #ifndef prof_lean_hpcfmt_h
65 #define prof_lean_hpcfmt_h
66 
67 //************************* System Include Files ****************************
68 
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <stddef.h>
72 #include <stdint.h>
73 #include <stdbool.h>
74 #include <stdarg.h>
75 #include <inttypes.h>
76 
77 
78 //*************************** User Include Files ****************************
79 
80 #include "hpcio.h"
81 
82 
83 //*************************** Forward Declarations **************************
84 
85 #if defined(__cplusplus)
86 extern "C" {
87 #endif
88 
89 //***************************************************************************
90 
91 enum {
92  HPCFMT_OK = 1,
93  HPCFMT_ERR = -1,
94  HPCFMT_EOF = -2,
95 };
96 
97 #define HPCFMT_ThrowIfError(v) if ( (v) != HPCFMT_OK ) { return HPCFMT_ERR; }
98 
99 //***************************************************************************
100 
101 // Types with known sizes (for reading/writing)
102 typedef uint64_t hpcfmt_vma_t;
103 typedef uint64_t hpcfmt_uint_t;
104 
105 typedef union {
106  double r8;
107  uint64_t i8;
109 
110 
111 //***************************************************************************
112 // Macros for defining "list of" things
113 //***************************************************************************
114 
115 #define HPCFMT_List(t) list_of_ ## t
116 
117 #define HPCFMT_ListRep(t) \
118  struct HPCFMT_List(t) { \
119  uint32_t len; \
120  t* lst; \
121  }
122 
123 #define HPCFMT_List_declare(t) \
124  typedef HPCFMT_ListRep(t) HPCFMT_List(t)
125 
126 
127 //***************************************************************************
128 // Memory allocator/deallocator function types
129 //***************************************************************************
130 
131 // Allocates space for 'nbytes' bytes and returns a pointer to the
132 // beginning of the chuck. 'sz' should never be 0.
133 typedef void* hpcfmt_alloc_fn(size_t nbytes);
134 
135 // Deallocates the memory chunk (allocated with the above) beginning
136 // at 'mem'. Note that 'mem' may be NULL.
137 typedef void hpcfmt_free_fn(void* mem);
138 
139 
140 //***************************************************************************
141 // Generic reader/writer primitives
142 //***************************************************************************
143 
144 int hpcfmt_fread(void *data, size_t size, FILE *infs);
145 
146 int hpcfmt_fwrite(void *data, size_t size, FILE *outfs);
147 
148 
149 static inline int
150 hpcfmt_int2_fread(uint16_t* val, FILE* infs)
151 {
152  size_t sz = hpcio_be2_fread(val, infs);
153  if ( sz != sizeof(uint16_t) ) {
154  return (sz == 0 && feof(infs)) ? HPCFMT_EOF : HPCFMT_ERR;
155  }
156  return HPCFMT_OK;
157 }
158 
159 
160 static inline int
161 hpcfmt_int4_fread(uint32_t* val, FILE* infs)
162 {
163  size_t sz = hpcio_be4_fread(val, infs);
164  if ( sz != sizeof(uint32_t) ) {
165  return (sz == 0 && feof(infs)) ? HPCFMT_EOF : HPCFMT_ERR;
166  }
167  return HPCFMT_OK;
168 }
169 
170 
171 static inline int
172 hpcfmt_int8_fread(uint64_t* val, FILE* infs)
173 {
174  size_t sz = hpcio_be8_fread(val, infs);
175  if ( sz != sizeof(uint64_t) ) {
176  return (sz == 0 && feof(infs)) ? HPCFMT_EOF : HPCFMT_ERR;
177  }
178  return HPCFMT_OK;
179 }
180 
181 
182 static inline int
183 hpcfmt_intX_fread(uint8_t* val, size_t size, FILE* infs)
184 {
185  size_t sz = hpcio_beX_fread(val, size, infs);
186  if (sz != size) {
187  return (sz == 0 && feof(infs)) ? HPCFMT_EOF : HPCFMT_ERR;
188  }
189  return HPCFMT_OK;
190 }
191 
192 
193 static inline int
194 hpcfmt_real8_fread(double* val, FILE* infs)
195 {
196  size_t sz = hpcio_be8_fread((uint64_t*)val, infs);
197  if ( sz != sizeof(double) ) {
198  return (sz == 0 && feof(infs)) ? HPCFMT_EOF : HPCFMT_ERR;
199  }
200  return HPCFMT_OK;
201 }
202 
203 
204 //***************************************************************************
205 
206 static inline int
207 hpcfmt_int2_fwrite(uint16_t val, FILE* outfs)
208 {
209  if ( sizeof(uint16_t) != hpcio_be2_fwrite(&val, outfs) ) {
210  return HPCFMT_ERR;
211  }
212  return HPCFMT_OK;
213 }
214 
215 
216 static inline int
217 hpcfmt_int4_fwrite(uint32_t val, FILE* outfs)
218 {
219  if ( sizeof(uint32_t) != hpcio_be4_fwrite(&val, outfs) ) {
220  return HPCFMT_ERR;
221  }
222  return HPCFMT_OK;
223 }
224 
225 
226 static inline int
227 hpcfmt_int8_fwrite(uint64_t val, FILE* outfs)
228 {
229  if ( sizeof(uint64_t) != hpcio_be8_fwrite(&val, outfs) ) {
230  return HPCFMT_ERR;
231  }
232  return HPCFMT_OK;
233 }
234 
235 
236 static inline int
237 hpcfmt_intX_fwrite(uint8_t* val, size_t size, FILE* outfs)
238 {
239  if (size != hpcio_beX_fwrite(val, size, outfs)) {
240  return HPCFMT_ERR;
241  }
242  return HPCFMT_OK;
243 }
244 
245 
246 static inline int
247 hpcfmt_real8_fwrite(double val, FILE* outfs)
248 {
249  // N.B.: This apparently breaks C99's "strict" anti-aliasing rules
250  //uint64_t* v = (uint64_t*)(&val);
251 
253  if ( sizeof(double) != hpcio_be8_fwrite(&(v->i8), outfs) ) {
254  return HPCFMT_ERR;
255  }
256  return HPCFMT_OK;
257 }
258 
259 
260 //***************************************************************************
261 // hpcfmt_str_t
262 //***************************************************************************
263 
264 typedef struct hpcfmt_str_t {
265 
266  uint32_t len; // length of string
267  char str[]; // string data. [Variable length data]
268 
269 } hpcfmt_str_t;
270 
271 
272 int
273 hpcfmt_str_fread(char** str, FILE* infs, hpcfmt_alloc_fn alloc);
274 
275 int
276 hpcfmt_str_fwrite(const char* str, FILE* outfs);
277 
278 void
279 hpcfmt_str_free(const char* str, hpcfmt_free_fn dealloc);
280 
281 
282 static inline const char*
283 hpcfmt_str_ensure(const char* x)
284 {
285  return (x) ? x : "";
286 }
287 
288 
289 //***************************************************************************
290 // hpcfmt_nvpair_t
291 //***************************************************************************
292 
293 typedef struct hpcfmt_nvpair_t {
294 
295  char* name;
296  char* val;
297 
299 
300 int
301 hpcfmt_nvpair_fwrite(hpcfmt_nvpair_t* nvp, FILE* fs);
302 
303 int
304 hpcfmt_nvpairs_vfwrite(FILE* out, va_list args);
305 
306 int
308 
309 int
310 hpcfmt_nvpair_fprint(hpcfmt_nvpair_t* nvp, FILE* fs, const char* pre);
311 
312 
313 //***************************************************************************
314 // List of hpcfmt_nvpair_t
315 //***************************************************************************
316 
318 
319 int
321  FILE* infs, hpcfmt_alloc_fn alloc);
322 
323 int
325  FILE* fs, const char* pre);
326 
327 const char*
329  const char* name);
330 
331 void
333  hpcfmt_free_fn dealloc);
334 
335 
336 //***************************************************************************
337 
338 
339 // --------------------------------------------------------------
340 // additional metric information
341 // this data is optional in metric description.
342 // at the moment, only perf event sample source needs this info
343 // --------------------------------------------------------------
344 typedef struct metric_aux_info_s {
345 
346  bool is_multiplexed; // flag if the event is multiplexed
347  double threshold_mean; // average threshold (if multiplexed)
348 
349  uint64_t num_samples; // number of samples
350 
352 
353 #if defined(__cplusplus)
354 } /* extern "C" */
355 #endif
356 
357 #endif /* prof_lean_hpcfmt_h */
size_t hpcio_be2_fread(uint16_t *val, FILE *fs)
Definition: hpcio.c:277
#define HPCFMT_List_declare(t)
Definition: hpcfmt.h:123
void hpcfmt_free_fn(void *mem)
Definition: hpcfmt.h:137
static int hpcfmt_int2_fwrite(uint16_t val, FILE *outfs)
Definition: hpcfmt.h:207
static int hpcfmt_int2_fread(uint16_t *val, FILE *infs)
Definition: hpcfmt.h:150
struct hpcfmt_nvpair_t hpcfmt_nvpair_t
int hpcfmt_nvpair_fprint(hpcfmt_nvpair_t *nvp, FILE *fs, const char *pre)
Definition: hpcfmt.c:225
static int hpcfmt_real8_fread(double *val, FILE *infs)
Definition: hpcfmt.h:194
uint64_t hpcfmt_vma_t
Definition: hpcfmt.h:102
static int hpcfmt_intX_fwrite(uint8_t *val, size_t size, FILE *outfs)
Definition: hpcfmt.h:237
size_t hpcio_be2_fwrite(uint16_t *val, FILE *fs)
Definition: hpcio.c:348
static int hpcfmt_int4_fread(uint32_t *val, FILE *infs)
Definition: hpcfmt.h:161
static int hpcfmt_int4_fwrite(uint32_t val, FILE *outfs)
Definition: hpcfmt.h:217
int hpcfmt_nvpair_fread(hpcfmt_nvpair_t *inp, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:215
uint64_t hpcfmt_uint_t
Definition: hpcfmt.h:103
int hpcfmt_str_fread(char **str, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:87
int hpcfmt_nvpair_fwrite(hpcfmt_nvpair_t *nvp, FILE *fs)
Definition: hpcfmt.c:169
size_t hpcio_be8_fwrite(uint64_t *val, FILE *fs)
Definition: hpcio.c:378
#define HPCFMT_List(t)
Definition: hpcfmt.h:115
uint64_t num_samples
Definition: hpcfmt.h:349
bool is_multiplexed
Definition: hpcfmt.h:346
static int hpcfmt_intX_fread(uint8_t *val, size_t size, FILE *infs)
Definition: hpcfmt.h:183
const char * hpcfmt_nvpairList_search(const HPCFMT_List(hpcfmt_nvpair_t) *lst, const char *name)
Definition: hpcfmt.c:263
struct metric_aux_info_s metric_aux_info_t
double threshold_mean
Definition: hpcfmt.h:347
static int hpcfmt_int8_fread(uint64_t *val, FILE *infs)
Definition: hpcfmt.h:172
char * val
Definition: hpcfmt.h:296
uint32_t len
Definition: hpcfmt.h:266
static int hpcfmt_int8_fwrite(uint64_t val, FILE *outfs)
Definition: hpcfmt.h:227
size_t hpcio_be4_fwrite(uint32_t *val, FILE *fs)
Definition: hpcio.c:363
struct hpcfmt_str_t hpcfmt_str_t
void hpcfmt_str_free(const char *str, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:132
int hpcfmt_str_fwrite(const char *str, FILE *outfs)
Definition: hpcfmt.c:115
mem_alloc alloc
void hpcfmt_nvpairList_free(HPCFMT_List(hpcfmt_nvpair_t) *nvps, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:276
int hpcfmt_fwrite(void *data, size_t size, FILE *outfs)
Definition: hpcfmt.c:155
void * hpcfmt_alloc_fn(size_t nbytes)
Definition: hpcfmt.h:133
int hpcfmt_nvpairs_vfwrite(FILE *out, va_list args)
Definition: hpcfmt.c:188
int hpcfmt_nvpairList_fprint(const HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *fs, const char *pre)
Definition: hpcfmt.c:252
size_t hpcio_be4_fread(uint32_t *val, FILE *fs)
Definition: hpcio.c:294
static int hpcfmt_real8_fwrite(double val, FILE *outfs)
Definition: hpcfmt.h:247
static const char * hpcfmt_str_ensure(const char *x)
Definition: hpcfmt.h:283
size_t hpcio_be8_fread(uint64_t *val, FILE *fs)
Definition: hpcio.c:311
size_t hpcio_beX_fread(uint8_t *val, size_t size, FILE *fs)
Definition: hpcio.c:328
char * name
Definition: hpcfmt.h:295
int hpcfmt_nvpairList_fread(HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:237
char str[]
Definition: hpcfmt.h:267
int hpcfmt_fread(void *data, size_t size, FILE *infs)
Definition: hpcfmt.c:144
size_t hpcio_beX_fwrite(uint8_t *val, size_t size, FILE *fs)
Definition: hpcio.c:393