HPCToolkit
hpcrun-fmt.c
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 // Low-level types and functions for reading/writing a call path
54 // profile as formatted data.
55 //
56 // See hpcrun-fmt.txt.
57 //
58 // These routines *must not* allocate dynamic memory; if such memory
59 // is needed, callbacks to the user's allocator should be used.
60 //
61 // Description:
62 // [The set of functions, macros, etc. defined in the file]
63 //
64 //***************************************************************************
65 
66 //************************* System Include Files ****************************
67 
68 #include <stdio.h>
69 #include <fcntl.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <errno.h>
73 #include <string.h>
74 
75 #include <sys/stat.h>
76 
77 //*************************** User Include Files ****************************
78 
79 #include <include/gcc-attr.h>
80 
81 #include "hpcio.h"
82 #include "hpcio-buffer.h"
83 #include "hpcfmt.h"
84 #include "hpcrun-fmt.h"
85 
86 //***************************************************************************
87 
88 //***************************************************************************
89 // hdr
90 //***************************************************************************
91 
92 int
94 {
95  char tag[HPCRUN_FMT_MagicLen + 1];
96 
97  int nr = fread(tag, 1, HPCRUN_FMT_MagicLen, infs);
98  tag[HPCRUN_FMT_MagicLen] = '\0';
99 
100  if (nr != HPCRUN_FMT_MagicLen) {
101  return HPCFMT_ERR;
102  }
103  if (strcmp(tag, HPCRUN_FMT_Magic) != 0) {
104  return HPCFMT_ERR;
105  }
106 
107  nr = fread(hdr->versionStr, 1, HPCRUN_FMT_VersionLen, infs);
108  hdr->versionStr[HPCRUN_FMT_VersionLen] = '\0';
109  if (nr != HPCRUN_FMT_VersionLen) {
110  return HPCFMT_ERR;
111  }
112  hdr->version = atof(hdr->versionStr);
113 
114  nr = fread(&hdr->endian, 1, HPCRUN_FMT_EndianLen, infs);
115  if (nr != HPCRUN_FMT_EndianLen) {
116  return HPCFMT_ERR;
117  }
118 
119  hpcfmt_nvpairList_fread(&(hdr->nvps), infs, alloc);
120 
121  return HPCFMT_OK;
122 }
123 
124 
125 int
126 hpcrun_fmt_hdr_fwrite(FILE* fs, ...)
127 {
128  va_list args;
129  va_start(args, fs);
130 
134 
135  hpcfmt_nvpairs_vfwrite(fs, args);
136 
137  va_end(args);
138 
139  return HPCFMT_OK;
140 }
141 
142 
143 int
145 {
146  fprintf(fs, "%s\n", HPCRUN_FMT_Magic);
147 
148  fprintf(fs, "[hdr:\n");
149  fprintf(fs, " (version: %s)\n", hdr->versionStr);
150  fprintf(fs, " (endian: %c)\n", hdr->endian);
151  hpcfmt_nvpairList_fprint(&hdr->nvps, fs, " ");
152  fprintf(fs, "]\n");
153 
154  return HPCFMT_OK;
155 }
156 
157 
158 void
160 {
161  if (dealloc) {
162  hpcfmt_nvpairList_free(&(hdr->nvps), dealloc);
163  }
164 }
165 
166 
167 //***************************************************************************
168 // epoch-hdr
169 //***************************************************************************
170 
171 int
174 {
175  char tag[HPCRUN_FMT_EpochTagLen + 1];
176 
177  int nr = fread(tag, 1, HPCRUN_FMT_EpochTagLen, fs);
178  tag[HPCRUN_FMT_EpochTagLen] = '\0';
179 
180  if (nr != HPCRUN_FMT_EpochTagLen) {
181  return (nr == 0 && feof(fs)) ? HPCFMT_EOF : HPCFMT_ERR;
182  }
183 
184  if (strcmp(tag, HPCRUN_FMT_EpochTag) != 0) {
185  return HPCFMT_ERR;
186  }
187 
188  // removed m_raToCallsiteOfst from epoch Hdr. don't change file format!
189  uint32_t dummy;
190 
194  HPCFMT_ThrowIfError(hpcfmt_nvpairList_fread(&(ehdr->nvps), fs, alloc));
195 
196  return HPCFMT_OK;
197 }
198 
199 
200 int
202  uint64_t measurementGranularity,
203  ...)
204 {
205  va_list args;
206  va_start(args, measurementGranularity);
207 
209  if (nw != HPCRUN_FMT_EpochTagLen) return HPCFMT_ERR;
210 
211  // removed m_raToCallsiteOfst from epoch Hdr. don't change file format!
212  uint32_t dummy = 0;
213 
215  HPCFMT_ThrowIfError(hpcfmt_int8_fwrite(measurementGranularity, fs));
217 
219 
220  va_end(args);
221 
222  return HPCFMT_OK;
223 }
224 
225 
226 int
228 {
229  fprintf(fs, "%s\n", HPCRUN_FMT_EpochTag);
230  fprintf(fs, "[epoch-hdr:\n");
231  fprintf(fs, " (flags: 0x%"PRIx64")\n", ehdr->flags.bits);
232  fprintf(fs, " (measurement-granularity: %"PRIu64")\n",
233  ehdr->measurementGranularity);
234  hpcfmt_nvpairList_fprint(&(ehdr->nvps), fs, " ");
235  fprintf(fs, "]\n");
236 
237  return HPCFMT_OK;
238 }
239 
240 
241 void
243 {
244  if (dealloc) {
245  hpcfmt_nvpairList_free(&(ehdr->nvps), dealloc);
246  }
247 }
248 
249 
250 //***************************************************************************
251 // metric-tbl
252 //***************************************************************************
253 
255  .name = NULL,
256  .description = NULL,
257  .flags.bits_big[0] = 0,
258  .flags.bits_big[1] = 0,
259  .period = 0,
260  .properties = {.time = 0,.cycles = 0},
261  .formula = NULL,
262  .format = NULL,
263 };
264 
267  .fields.valTy = MetricFlags_ValTy_NULL,
268  .fields.valFmt = MetricFlags_ValFmt_NULL,
269  .fields.unused0 = 0,
270 
271  .fields.partner = 0,
272  .fields.show = (uint8_t)true,
273  .fields.showPercent = (uint8_t)true,
274 
275  .fields.unused1 = 0,
276 };
277 
279 
280 //***************************************************************************
281 
282 int
284  FILE* fs, double fmtVersion, hpcfmt_alloc_fn alloc)
285 {
286  HPCFMT_ThrowIfError(hpcfmt_int4_fread(&(metric_tbl->len), fs));
287  if (alloc) {
288  metric_tbl->lst =
289  (metric_desc_t*) alloc(metric_tbl->len * sizeof(metric_desc_t));
290  }
291 
292  size_t aux_info_size = sizeof(metric_aux_info_t) * metric_tbl->len;
293  metric_aux_info_t *perf_info = (metric_aux_info_t*)malloc(aux_info_size);
294  memset(perf_info, 0, aux_info_size);
295 
296  for (uint32_t i = 0; i < metric_tbl->len; i++) {
297  metric_desc_t* x = &metric_tbl->lst[i];
298  HPCFMT_ThrowIfError(hpcrun_fmt_metricDesc_fread(x, &(perf_info)[i], fs, fmtVersion, alloc));
299  }
300  *aux_info = perf_info;
301 
302  return HPCFMT_OK;
303 }
304 
305 
306 int
307 hpcrun_fmt_metricTbl_fwrite(metric_desc_p_tbl_t* metric_tbl, metric_aux_info_t *aux_info, FILE* fs)
308 {
309  hpcfmt_int4_fwrite(metric_tbl->len, fs);
310 
311  for (uint32_t i = 0; i < metric_tbl->len; i++) {
312 
313  // corner case: for other sampling sources than perf event, the
314  // value of aux_info is NULL
315 
316  metric_aux_info_t info_tmp;
317  metric_aux_info_t *info_ptr = aux_info;
318 
319  if (aux_info == NULL) {
320  memset(&info_tmp, 0, sizeof(metric_aux_info_t));
321  info_ptr = &info_tmp;
322  } else {
323  info_ptr = &(aux_info[i]);
324  }
325 
326  hpcrun_fmt_metricDesc_fwrite(metric_tbl->lst[i], info_ptr, fs);
327  }
328 
329  return HPCFMT_OK;
330 }
331 
332 
333 int
334 hpcrun_fmt_metricTbl_fprint(metric_tbl_t* metric_tbl, metric_aux_info_t *aux_info, FILE* fs)
335 {
336  fprintf(fs, "[metric-tbl: (num-entries: %u)\n", metric_tbl->len);
337  for (uint32_t i = 0; i < metric_tbl->len; i++) {
338  metric_desc_t* x = &metric_tbl->lst[i];
339  hpcrun_fmt_metricDesc_fprint(x, &(aux_info[i]), fs, " ");
340  }
341  fputs("]\n", fs);
342 
343  return HPCFMT_OK;
344 }
345 
346 
347 void
349 {
350  for (uint32_t i = 0; i < metric_tbl->len; i++) {
351  metric_desc_t* x = &metric_tbl->lst[i];
352  hpcrun_fmt_metricDesc_free(x, dealloc);
353  }
354  dealloc((void*)metric_tbl->lst);
355  metric_tbl->lst = NULL;
356 }
357 
358 
359 //***************************************************************************
360 
361 int
363  double GCC_ATTR_UNUSED fmtVersion,
365 {
366  HPCFMT_ThrowIfError(hpcfmt_str_fread(&(x->name), fs, alloc));
369 
370  // FIXME: tallent: temporarily support old non-portable convention
371  if ( !(x->flags.fields.ty == MetricFlags_Ty_Raw
373  || x->flags.fields.unused0 != 0
374  || x->flags.fields.unused1 != 0) {
375  fseek(fs, -sizeof(x->flags), SEEK_CUR);
376 
377  hpcrun_metricFlags_XXX_t x_flags_old;
378  HPCFMT_ThrowIfError(hpcfmt_int8_fread(&(x_flags_old.bits[0]), fs));
379  HPCFMT_ThrowIfError(hpcfmt_int8_fread(&(x_flags_old.bits[1]), fs));
380 
381  x->flags.bits_big[0] = 0;
382  x->flags.bits_big[1] = 0;
383 
384  x->flags.fields.ty = x_flags_old.fields.ty;
385  x->flags.fields.valTy = x_flags_old.fields.valTy;
386  x->flags.fields.valFmt = x_flags_old.fields.valFmt;
387  x->flags.fields.partner = (uint16_t) x_flags_old.fields.partner;
388  x->flags.fields.show = x_flags_old.fields.show;
389  x->flags.fields.showPercent = x_flags_old.fields.showPercent;
390  }
391 
393  HPCFMT_ThrowIfError(hpcfmt_str_fread(&(x->formula), fs, alloc));
394  HPCFMT_ThrowIfError(hpcfmt_str_fread(&(x->format), fs, alloc));
395 
397  HPCFMT_ThrowIfError(hpcfmt_int2_fread ((uint16_t*)&(aux_info->is_multiplexed), fs));
399  // disabled temporarily
400  //HPCFMT_ThrowIfError(hpcfmt_real8_fread(&(x->info_data.threshold_stdev), fs));
402 
403  // These two aren't written into the hpcrun file; hence manually set them.
404  x->properties.time = 0;
405  x->properties.cycles = 0;
406 
407  return HPCFMT_OK;
408 }
409 
410 
411 int
413 {
420 
422 
426 
427  return HPCFMT_OK;
428 }
429 
430 
431 int
432 hpcrun_fmt_metricDesc_fprint(metric_desc_t* x, metric_aux_info_t *aux_info, FILE* fs, const char* pre)
433 {
434  fprintf(fs, "%s[(nm: %s) (desc: %s) "
435  "((ty: %d) (val-ty: %d) (val-fmt: %d) (partner: %u) (show: %d) (showPercent: %d)) "
436  "(period: %"PRIu64") (formula: %s) (format: %s)\n" ,
438  (int)x->flags.fields.ty, (int)x->flags.fields.valTy,
439  (int)x->flags.fields.valFmt,
441  x->period,
443  fprintf(fs, " (frequency: %d) (multiplexed: %d) (period-mean: %f) (num-samples: %d)]\n",
444  (int)x->is_frequency_metric, (int)aux_info->is_multiplexed,
445  aux_info->threshold_mean, (int) aux_info->num_samples);
446  return HPCFMT_OK;
447 }
448 
449 
450 void
452 {
453  hpcfmt_str_free(x->name, dealloc);
454  x->name = NULL;
455  hpcfmt_str_free(x->description, dealloc);
456  x->description = NULL;
457  hpcfmt_str_free(x->formula, dealloc);
458  x->formula = NULL;
459  hpcfmt_str_free(x->format, dealloc);
460  x->format = NULL;
461 }
462 
463 double
465 {
466  if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Int) {
467  return (double) metric.i;
468  }
469  else if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Real) {
470  return metric.r;
471  }
472  else if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Address) {
473  double *pointer_double = (double*)metric.p;
474  return *pointer_double;
475  }
476  // TODO: default value
477  return metric.r;
478 }
479 
480 // set a new value into a metric
481 void
483  hpcrun_metricVal_t *metric, double value)
484 {
485  if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Int) {
486  metric->i = (int) value;
487  }
488  else if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Real) {
489  metric->r = value;
490  }
491  else if (metric_desc.flags.fields.valFmt == MetricFlags_ValFmt_Address) {
492  // conversion yuck
493  double *ptr_double = (double*) &value;
494  metric->p = *(void**) ptr_double;
495  }
496 }
497 
498 // set a new integer value into a metric, and force it to be
499 // an integer type metric
500 void
502  hpcrun_metricVal_t *metric, int value)
503 {
505  metric->i = value;
506 }
507 
508 // set a new double value into a metric, and force it to be
509 // a real type metric
510 void
512  hpcrun_metricVal_t *metric, double value)
513 {
515  metric->r = value;
516 }
517 
518 void
520  hpcrun_metricVal_t *metric, double value)
521 {
523  metric->r = value;
524 }
525 
526 //***************************************************************************
527 // loadmap
528 //***************************************************************************
529 
530 int
531 hpcrun_fmt_loadmap_fread(loadmap_t* loadmap, FILE* fs, hpcfmt_alloc_fn alloc)
532 {
533  HPCFMT_ThrowIfError(hpcfmt_int4_fread(&(loadmap->len), fs));
534  if (alloc) {
535  loadmap->lst = alloc(loadmap->len * sizeof(loadmap_entry_t));
536  }
537 
538  for (uint32_t i = 0; i < loadmap->len; i++) {
539  loadmap_entry_t* e = &loadmap->lst[i];
541  }
542 
543  return HPCFMT_OK;
544 }
545 
546 
547 int
548 hpcrun_fmt_loadmap_fwrite(loadmap_t* loadmap, FILE* fs)
549 {
550  HPCFMT_ThrowIfError(hpcfmt_int4_fwrite(loadmap->len, fs));
551  for (uint32_t i = 0; i < loadmap->len; i++) {
552  loadmap_entry_t* e = &loadmap->lst[i];
554  }
555 
556  return HPCFMT_OK;
557 }
558 
559 
560 int
561 hpcrun_fmt_loadmap_fprint(loadmap_t* loadmap, FILE* fs)
562 {
563  fprintf(fs, "[loadmap: (num-entries: %u)\n", loadmap->len);
564  for (uint32_t i = 0; i < loadmap->len; i++) {
565  loadmap_entry_t* e = &loadmap->lst[i];
567  }
568  fprintf(fs, "]\n");
569 
570  return HPCFMT_OK;
571 }
572 
573 
574 void
575 hpcrun_fmt_loadmap_free(loadmap_t* loadmap, hpcfmt_free_fn dealloc)
576 {
577  for (uint32_t i = 0; i < loadmap->len; i++) {
578  loadmap_entry_t* e = &loadmap->lst[i];
579  hpcrun_fmt_loadmapEntry_free(e, dealloc);
580  }
581  dealloc(loadmap->lst);
582  loadmap->lst = NULL;
583 }
584 
585 
586 //***************************************************************************
587 
588 int
591 {
593  HPCFMT_ThrowIfError(hpcfmt_str_fread(&(x->name), fs, alloc));
595  return HPCFMT_OK;
596 }
597 
598 
599 int
601 {
605  return HPCFMT_OK;
606 }
607 
608 
609 int
610 hpcrun_fmt_loadmapEntry_fprint(loadmap_entry_t* x, FILE* fs, const char* pre)
611 {
612  fprintf(fs, "%s[(id: %u) (nm: %s) (flg: 0x%"PRIx64")]\n",
613  pre, (uint)x->id, x->name, x->flags);
614  return HPCFMT_OK;
615 }
616 
617 
618 void
620 {
621  hpcfmt_str_free(x->name, dealloc);
622  x->name = NULL;
623 }
624 
625 
626 //***************************************************************************
627 // cct
628 //***************************************************************************
629 
630 int
632  epoch_flags_t flags, FILE* fs)
633 {
635 
638 
640  if (flags.fields.isLogicalUnwind) {
642  }
643 
646 
647  lush_lip_init(&x->lip);
648  if (flags.fields.isLogicalUnwind) {
649  hpcrun_fmt_lip_fread(&x->lip, fs);
650  }
651 
652  for (int i = 0; i < x->num_metrics; ++i) {
654  }
655  return HPCFMT_OK;
656 }
657 
658 
659 int
661  epoch_flags_t flags, FILE* fs)
662 {
664 
667 
668  if (flags.fields.isLogicalUnwind) {
670  }
671 
674 
675  if (flags.fields.isLogicalUnwind) {
677  }
678 
679  for (int i = 0; i < x->num_metrics; ++i) {
681  }
682 
683  return HPCFMT_OK;
684 }
685 
686 
687 int
689  epoch_flags_t flags, const metric_tbl_t* metricTbl,
690  const char* pre)
691 {
692  // N.B.: convert 'id' and 'id_parent' to ints so leaf flag
693  // (negative) is apparent
694  fprintf(fs, "%s[node: (id: %d) (id-parent: %d) (type: %d) ",
695  pre, (int)x->id, (int)x->id_parent, x->node_type);
696 
697  if (flags.fields.isLogicalUnwind) {
698  char as_str[LUSH_ASSOC_INFO_STR_MIN_LEN];
699  lush_assoc_info_sprintf(as_str, x->as_info);
700 
701  fprintf(fs, "(as: %s) ", as_str);
702  }
703 
704  fprintf(fs, "(lm-id: %u) (lm-ip: 0x%"PRIx64") ", (uint)x->lm_id, x->lm_ip);
705 
706  if (flags.fields.isLogicalUnwind) {
707  hpcrun_fmt_lip_fprint(&x->lip, fs, "");
708  }
709 
710  fprintf(fs, "\n");
711 
712  fprintf(fs, "%s(metrics:", pre);
713  for (uint i = 0; i < x->num_metrics; ++i) {
715  if (metricTbl) {
716  const metric_desc_t* mdesc = &(metricTbl->lst[i]);
717  mflags = mdesc->flags;
718  }
719 
720  switch (mflags.fields.valFmt) {
721  default:
723  fprintf(fs, " %"PRIu64, x->metrics[i].i);
724  break;
726  fprintf(fs, " %g", x->metrics[i].r);
727  break;
729  fprintf(fs, " %p", x->metrics[i].p);
730  break;
731  }
732 
733  if (i + 1 < x->num_metrics) {
734  fprintf(fs, " ");
735  }
736  }
737  fprintf(fs, ")\n");
738 
739  fprintf(fs, "%s]\n", pre);
740 
741  return HPCFMT_OK;
742 }
743 
744 
745 //***************************************************************************
746 
747 int
749 {
750  for (int i = 0; i < LUSH_LIP_DATA8_SZ; ++i) {
752  }
753 
754  return HPCFMT_OK;
755 }
756 
757 
758 int
760 {
761  for (int i = 0; i < LUSH_LIP_DATA8_SZ; ++i) {
763  }
764 
765  return HPCFMT_OK;
766 }
767 
768 
769 int
770 hpcrun_fmt_lip_fprint(lush_lip_t* x, FILE* fs, const char* pre)
771 {
772  char lip_str[LUSH_LIP_STR_MIN_LEN];
773  lush_lip_sprintf(lip_str, x);
774 
775  fprintf(fs, "%s(lip: %s)", pre, lip_str);
776 
777  return HPCFMT_OK;
778 }
779 
780 
781 //***************************************************************************
782 // hpctrace (located here for now)
783 //***************************************************************************
784 
785 //***************************************************************************
786 // [hpctrace] hdr
787 //***************************************************************************
788 
790  .bits = 0
791 };
792 
793 
794 int
796 {
797  char tag[HPCTRACE_FMT_MagicLen + 1];
798 
799  int nr = fread(tag, 1, HPCTRACE_FMT_MagicLen, infs);
800  tag[HPCTRACE_FMT_MagicLen] = '\0';
801 
802  if (nr != HPCTRACE_FMT_MagicLen) {
803  return HPCFMT_ERR;
804  }
805  if (strcmp(tag, HPCTRACE_FMT_Magic) != 0) {
806  return HPCFMT_ERR;
807  }
808 
809  nr = fread(hdr->versionStr, 1, HPCTRACE_FMT_VersionLen, infs);
810  hdr->versionStr[HPCTRACE_FMT_VersionLen] = '\0';
811  if (nr != HPCTRACE_FMT_VersionLen) {
812  return HPCFMT_ERR;
813  }
814  hdr->version = atof(hdr->versionStr);
815 
816  nr = fread(&hdr->endian, 1, HPCTRACE_FMT_EndianLen, infs);
817  if (nr != HPCTRACE_FMT_EndianLen) {
818  return HPCFMT_ERR;
819  }
820 
822  if (hdr->version > 1.0) {
824  }
825 
826  return HPCFMT_OK;
827 }
828 
829 
830 // Writer based on outbuf.
831 // Returns: HPCFMT_OK on success, else HPCFMT_ERR.
832 int
834 {
835  ssize_t ret;
836 
837  const int bufSZ = sizeof(flags);
838  unsigned char buf[bufSZ];
839 
840  uint64_t flag_bits = flags.bits;
841  int k = 0;
842  for (int shift = 56; shift >= 0; shift -= 8) {
843  buf[k] = (flag_bits >> shift) & 0xff;
844  k++;
845  }
846 
850  ret = hpcio_outbuf_write(outbuf, buf, bufSZ);
851 
852  if (ret != bufSZ) {
853  return HPCFMT_ERR;
854  }
855 
856  return HPCFMT_OK;
857 }
858 
859 
860 // N.B.: not async safe
861 int
863 {
864  int nw;
865 
867  if (nw != HPCTRACE_FMT_MagicLen) return HPCFMT_ERR;
868 
870  if (nw != HPCTRACE_FMT_VersionLen) return HPCFMT_ERR;
871 
873  if (nw != HPCTRACE_FMT_EndianLen) return HPCFMT_ERR;
874 
876 
877  return HPCFMT_OK;
878 }
879 
880 
881 int
883 {
884  fprintf(fs, "%s\n", HPCTRACE_FMT_Magic);
885 
886  fprintf(fs, "[hdr:\n");
887  fprintf(fs, " (version: %s)\n", hdr->versionStr);
888  fprintf(fs, " (endian: %c)\n", hdr->endian);
889  fprintf(fs, " (flags: 0x%"PRIx64")\n", hdr->flags.bits);
890  fprintf(fs, "]\n");
891 
892  return HPCFMT_OK;
893 }
894 
895 
896 //***************************************************************************
897 // [hpctrace] datum (trace record)
898 //***************************************************************************
899 
900 int
902  FILE* fs)
903 {
904  int ret = HPCFMT_OK;
905 
906  ret = hpcfmt_int8_fread(&(x->time), fs);
907  if (ret != HPCFMT_OK) {
908  return ret; // can be HPCFMT_EOF
909  }
910 
912 
913  if (flags.fields.isDataCentric) {
915  }
916  else {
918  }
919 
920  return HPCFMT_OK;
921 }
922 
923 
924 // Append the trace record to the outbuf.
925 // Returns: HPCFMT_OK on success, else HPCFMT_ERR.
926 int
928  hpcio_outbuf_t* outbuf)
929 {
930  const int bufSZ = sizeof(hpctrace_fmt_datum_t);
931  unsigned char buf[bufSZ];
932  int shift, k;
933 
934  k = 0;
935 
936  uint64_t time = x->time;
937  for (shift = 56; shift >= 0; shift -= 8) {
938  buf[k] = (time >> shift) & 0xff;
939  k++;
940  }
941 
942  uint32_t cpId = x->cpId;
943  for (shift = 24; shift >= 0; shift -= 8) {
944  buf[k] = (cpId >> shift) & 0xff;
945  k++;
946  }
947 
948  if (flags.fields.isDataCentric) {
949  uint32_t metricId = x->metricId;
950  for (shift = 24; shift >= 0; shift -= 8) {
951  buf[k] = (metricId >> shift) & 0xff;
952  k++;
953  }
954  }
955 
956  if (hpcio_outbuf_write(outbuf, buf, k) != k) {
957  return HPCFMT_ERR;
958  }
959 
960  return HPCFMT_OK;
961 }
962 
963 
964 int
966  FILE* outfs)
967 {
970  if (flags.fields.isDataCentric) {
972  }
973 
974  return HPCFMT_OK;
975 }
976 
977 
978 int
980  FILE* fs)
981 {
982  fprintf(fs, "(%"PRIu64", %u", x->time, x->cpId);
983  if (flags.fields.isDataCentric) {
984  fprintf(fs, ", %u", x->metricId);
985  }
986  fputs(")\n", fs);
987  return HPCFMT_OK;
988 }
989 
990 
991 //***************************************************************************
992 // hpcprof-metricdb (located here for now)
993 //***************************************************************************
994 
995 //***************************************************************************
996 // [hpcprof-metricdb] hdr
997 //***************************************************************************
998 
999 int
1001 {
1002  char tag[HPCMETRICDB_FMT_MagicLen + 1];
1003  char version[HPCMETRICDB_FMT_VersionLen + 1];
1004  char endian[HPCMETRICDB_FMT_EndianLen + 1];
1005 
1006  int nr = fread(tag, 1, HPCMETRICDB_FMT_MagicLen, infs);
1007  tag[HPCMETRICDB_FMT_MagicLen] = '\0';
1008 
1009  if (nr != HPCMETRICDB_FMT_MagicLen) {
1010  return HPCFMT_ERR;
1011  }
1012  if (strcmp(tag, HPCMETRICDB_FMT_Magic) != 0) {
1013  return HPCFMT_ERR;
1014  }
1015 
1016  nr = fread(&version, 1, HPCMETRICDB_FMT_VersionLen, infs);
1017  version[HPCMETRICDB_FMT_VersionLen] = '\0';
1018  if (nr != HPCMETRICDB_FMT_VersionLen) {
1019  return HPCFMT_ERR;
1020  }
1021  hdr->version = atof(hdr->versionStr);
1022 
1023  nr = fread(&endian, 1, HPCMETRICDB_FMT_EndianLen, infs);
1024  if (nr != HPCMETRICDB_FMT_EndianLen) {
1025  return HPCFMT_ERR;
1026  }
1027 
1030 
1031  return HPCFMT_OK;
1032 }
1033 
1034 
1035 int
1037 {
1038  int nw;
1039 
1041  if (nw != HPCTRACE_FMT_MagicLen) return HPCFMT_ERR;
1042 
1044  if (nw != HPCMETRICDB_FMT_VersionLen) return HPCFMT_ERR;
1045 
1047  if (nw != HPCMETRICDB_FMT_EndianLen) return HPCFMT_ERR;
1048 
1051 
1052  return HPCFMT_OK;
1053 }
1054 
1055 
1056 int
1058 {
1059  fprintf(outfs, "%s\n", HPCMETRICDB_FMT_Magic);
1060  fprintf(outfs, "[hdr:...]\n");
1061 
1062  fprintf(outfs, "(num-nodes: %u)\n", hdr->numNodes);
1063  fprintf(outfs, "(num-metrics: %u)\n", hdr->numMetrics);
1064 
1065  return HPCFMT_OK;
1066 }
1067 
void hpcrun_fmt_epochHdr_free(hpcrun_fmt_epochHdr_t *ehdr, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:242
int hpcrun_fmt_hdr_fprint(hpcrun_fmt_hdr_t *hdr, FILE *fs)
Definition: hpcrun-fmt.c:144
bool is_frequency_metric
Definition: hpcrun-fmt.h:381
static const char HPCRUN_FMT_Magic[]
Definition: hpcrun-fmt.h:113
static const int HPCMETRICDB_FMT_MagicLen
Definition: hpcrun-fmt.h:734
static const int HPCTRACE_FMT_MagicLen
Definition: hpcrun-fmt.h:649
int hpcmetricDB_fmt_hdr_fread(hpcmetricDB_fmt_hdr_t *hdr, FILE *infs)
Definition: hpcrun-fmt.c:1000
static struct perf_mem_metric metric
Definition: pmu_x86.c:114
static const char HPCTRACE_FMT_Version[]
Definition: hpcrun-fmt.h:626
MetricFlags_ValFmt_t valFmt
Definition: hpcrun-fmt.h:289
ssize_t hpcio_outbuf_write(hpcio_outbuf_t *outbuf, const void *data, size_t size)
Definition: hpcio-buffer.c:175
int hpctrace_fmt_datum_fwrite(hpctrace_fmt_datum_t *x, hpctrace_hdr_flags_t flags, FILE *outfs)
Definition: hpcrun-fmt.c:965
const metric_desc_t metricDesc_NULL
Definition: hpcrun-fmt.c:254
#define LUSH_LIP_DATA8_SZ
Definition: lush-support.h:309
void hpcrun_fmt_metric_set_value_int(hpcrun_metricFlags_t *flags, hpcrun_metricVal_t *metric, int value)
Definition: hpcrun-fmt.c:501
hpcfmt_vma_t lm_ip
Definition: hpcrun-fmt.h:554
void hpcfmt_free_fn(void *mem)
Definition: hpcfmt.h:137
int hpctrace_fmt_hdr_fprint(hpctrace_fmt_hdr_t *hdr, FILE *fs)
Definition: hpcrun-fmt.c:882
int hpcfmt_nvpairList_fread(HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:237
MetricFlags_Ty_t ty
Definition: hpcrun-fmt.h:261
MetricFlags_ValTy_t valTy
Definition: hpcrun-fmt.h:262
static const int HPCMETRICDB_FMT_VersionLen
Definition: hpcrun-fmt.h:735
void hpcrun_fmt_metric_set_value_real(hpcrun_metricFlags_t *flags, hpcrun_metricVal_t *metric, double value)
Definition: hpcrun-fmt.c:511
MetricFlags_ValFmt_t valFmt
Definition: hpcrun-fmt.h:263
struct hpctrace_fmt_datum_t hpctrace_fmt_datum_t
int hpcrun_fmt_metricDesc_fread(metric_desc_t *x, metric_aux_info_t *aux_info, FILE *fs, double GCC_ATTR_UNUSED fmtVersion, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:362
static int hpcfmt_int2_fwrite(uint16_t val, FILE *outfs)
Definition: hpcfmt.h:207
int hpcrun_fmt_lip_fread(lush_lip_t *x, FILE *fs)
Definition: hpcrun-fmt.c:748
int hpcrun_fmt_loadmapEntry_fprint(loadmap_entry_t *x, FILE *fs, const char *pre)
Definition: hpcrun-fmt.c:610
static int hpcfmt_int2_fread(uint16_t *val, FILE *infs)
Definition: hpcfmt.h:150
void hpcrun_fmt_metricDesc_free(metric_desc_t *x, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:451
int hpcrun_fmt_metricTbl_fprint(metric_tbl_t *metric_tbl, metric_aux_info_t *aux_info, FILE *fs)
Definition: hpcrun-fmt.c:334
int hpcrun_fmt_cct_node_fprint(hpcrun_fmt_cct_node_t *x, FILE *fs, epoch_flags_t flags, const metric_tbl_t *metricTbl, const char *pre)
Definition: hpcrun-fmt.c:688
static const char HPCRUN_FMT_EpochTag[]
Definition: hpcrun-fmt.h:170
static int hpcfmt_real8_fread(double *val, FILE *infs)
Definition: hpcfmt.h:194
lush_assoc_info_t lush_assoc_info_NULL
Definition: lush-support.c:83
static int hpcfmt_intX_fwrite(uint8_t *val, size_t size, FILE *outfs)
Definition: hpcfmt.h:237
Definition: fmt.c:108
const hpcrun_metricFlags_t hpcrun_metricFlags_NULL
Definition: hpcrun-fmt.c:265
static const int HPCRUN_FMT_VersionLen
Definition: hpcrun-fmt.h:118
char versionStr[sizeof(HPCRUN_FMT_Version)]
Definition: hpcrun-fmt.h:128
epoch_flags_t flags
Definition: hpcrun-fmt.h:188
static int hpcfmt_int4_fread(uint32_t *val, FILE *infs)
Definition: hpcfmt.h:161
int hpctrace_fmt_datum_outbuf(hpctrace_fmt_datum_t *x, hpctrace_hdr_flags_t flags, hpcio_outbuf_t *outbuf)
Definition: hpcrun-fmt.c:927
hpcrun_metricVal_t hpcrun_metricVal_ZERO
Definition: hpcrun-fmt.c:278
static const int HPCMETRICDB_FMT_EndianLen
Definition: hpcrun-fmt.h:736
uint8_t bits[2 *8]
Definition: hpcrun-fmt.h:278
static const int HPCTRACE_FMT_VersionLen
Definition: hpcrun-fmt.h:650
static int hpcfmt_int4_fwrite(uint32_t val, FILE *outfs)
Definition: hpcfmt.h:217
const hpctrace_hdr_flags_t hpctrace_hdr_flags_NULL
Definition: hpcrun-fmt.c:789
const char * lush_assoc_info_sprintf(char *str, lush_assoc_info_t as_info)
Definition: lush-support.c:101
int hpcrun_fmt_cct_node_fread(hpcrun_fmt_cct_node_t *x, epoch_flags_t flags, FILE *fs)
Definition: hpcrun-fmt.c:631
char versionStr[sizeof(HPCMETRICDB_FMT_Version)]
Definition: hpcrun-fmt.h:746
int hpcrun_fmt_loadmap_fread(loadmap_t *loadmap, FILE *fs, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:531
static const char HPCMETRICDB_FMT_Magic[]
Definition: hpcrun-fmt.h:726
const char * lush_lip_sprintf(char *str, const lush_lip_t *x)
Definition: lush-support.c:122
void hpcrun_fmt_metric_set_value_address(hpcrun_metricFlags_t *flags, hpcrun_metricVal_t *metric, double value)
Definition: hpcrun-fmt.c:519
int hpcrun_fmt_hdr_fwrite(FILE *fs,...)
Definition: hpcrun-fmt.c:126
hpcfmt_uint_t num_metrics
Definition: hpcrun-fmt.h:559
void hpcrun_fmt_loadmapEntry_free(loadmap_entry_t *x, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:619
uint64_t num_samples
Definition: hpcfmt.h:349
void hpcrun_fmt_metricTbl_free(metric_tbl_t *metric_tbl, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:348
void hpcrun_fmt_loadmap_free(loadmap_t *loadmap, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:575
int hpcrun_fmt_lip_fprint(lush_lip_t *x, FILE *fs, const char *pre)
Definition: hpcrun-fmt.c:770
char * description
Definition: hpcrun-fmt.h:370
int hpcrun_fmt_epochHdr_fprint(hpcrun_fmt_epochHdr_t *ehdr, FILE *fs)
Definition: hpcrun-fmt.c:227
int hpcrun_fmt_loadmap_fwrite(loadmap_t *loadmap, FILE *fs)
Definition: hpcrun-fmt.c:548
void hpcfmt_nvpairList_free(HPCFMT_List(hpcfmt_nvpair_t) *nvps, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:276
unsigned int uint
Definition: uint.h:124
static const int HPCRUN_FMT_EpochTagLen
Definition: hpcrun-fmt.h:171
#define LUSH_LIP_STR_MIN_LEN
Definition: lush-support.h:356
hpcrun_metricFlags_fields fields
Definition: hpcrun-fmt.h:276
uint16_t id
Definition: hpcrun-fmt.h:454
int hpcrun_fmt_metricTbl_fwrite(metric_desc_p_tbl_t *metric_tbl, metric_aux_info_t *aux_info, FILE *fs)
Definition: hpcrun-fmt.c:307
char * format
Definition: hpcrun-fmt.h:379
int hpcrun_fmt_metricDesc_fwrite(metric_desc_t *x, metric_aux_info_t *aux_info, FILE *fs)
Definition: hpcrun-fmt.c:412
bool is_multiplexed
Definition: hpcfmt.h:346
static int hpcfmt_intX_fread(uint8_t *val, size_t size, FILE *infs)
Definition: hpcfmt.h:183
Definition: hpcrun-fmt.h:452
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:226
int hpcfmt_str_fwrite(const char *str, FILE *outfs)
Definition: hpcfmt.c:115
int hpcfmt_nvpairs_vfwrite(FILE *out, va_list args)
Definition: hpcfmt.c:188
lush_assoc_info_t as_info
Definition: hpcrun-fmt.h:545
struct metric_aux_info_s metric_aux_info_t
static const int HPCTRACE_FMT_EndianLen
Definition: hpcrun-fmt.h:651
static const int HPCRUN_FMT_MagicLen
Definition: hpcrun-fmt.h:117
double threshold_mean
Definition: hpcfmt.h:347
static int hpcfmt_int8_fread(uint64_t *val, FILE *infs)
Definition: hpcfmt.h:172
#define HPCRUN_FMT_MetricId_NULL
Definition: hpcrun-fmt.h:691
int hpcrun_fmt_lip_fwrite(lush_lip_t *x, FILE *fs)
Definition: hpcrun-fmt.c:759
int hpctrace_fmt_datum_fprint(hpctrace_fmt_datum_t *x, hpctrace_hdr_flags_t flags, FILE *fs)
Definition: hpcrun-fmt.c:979
static int hpcfmt_int8_fwrite(uint64_t val, FILE *outfs)
Definition: hpcfmt.h:227
static void lush_lip_init(lush_lip_t *x)
Definition: lush-support.h:319
#define HPCFMT_ThrowIfError(v)
Definition: hpcfmt.h:97
epoch_flags_bitfield fields
Definition: hpcrun-fmt.h:181
uint64_t flags
Definition: hpcrun-fmt.h:456
uint64_t period
Definition: hpcrun-fmt.h:374
static const char HPCMETRICDB_FMT_Endian[]
Definition: hpcrun-fmt.h:728
static const int HPCRUN_FMT_EndianLen
Definition: hpcrun-fmt.h:119
int hpcrun_fmt_loadmap_fprint(loadmap_t *loadmap, FILE *fs)
Definition: hpcrun-fmt.c:561
uint64_t measurementGranularity
Definition: hpcrun-fmt.h:189
mem_alloc alloc
int hpcrun_fmt_hdr_fread(hpcrun_fmt_hdr_t *hdr, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:93
uint64_t bits_big[2]
Definition: hpcrun-fmt.h:280
hpctrace_hdr_flags_bitfield fields
Definition: hpcrun-fmt.h:637
char * formula
Definition: hpcrun-fmt.h:378
hpcrun_metricFlags_bitfield_XXX fields
Definition: hpcrun-fmt.h:302
void hpcfmt_str_free(const char *str, hpcfmt_free_fn dealloc)
Definition: hpcfmt.c:132
int hpcrun_fmt_cct_node_fwrite(hpcrun_fmt_cct_node_t *x, epoch_flags_t flags, FILE *fs)
Definition: hpcrun-fmt.c:660
int hpcfmt_nvpairList_fprint(const HPCFMT_List(hpcfmt_nvpair_t) *nvps, FILE *fs, const char *pre)
Definition: hpcfmt.c:252
hpctrace_hdr_flags_t flags
Definition: hpcrun-fmt.h:668
uint64_t bits
Definition: hpcrun-fmt.h:182
void *MONITOR_EXT_WRAP_NAME() malloc(size_t bytes)
void * hpcfmt_alloc_fn(size_t nbytes)
Definition: hpcfmt.h:133
int hpcrun_fmt_loadmapEntry_fwrite(loadmap_entry_t *x, FILE *fs)
Definition: hpcrun-fmt.c:600
#define NULL
Definition: ElfHelper.cpp:85
int hpcrun_fmt_metricDesc_fprint(metric_desc_t *x, metric_aux_info_t *aux_info, FILE *fs, const char *pre)
Definition: hpcrun-fmt.c:432
int hpcrun_fmt_metricTbl_fread(metric_tbl_t *metric_tbl, metric_aux_info_t **aux_info, FILE *fs, double fmtVersion, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:283
static const char HPCMETRICDB_FMT_Version[]
Definition: hpcrun-fmt.h:727
char versionStr[sizeof(HPCTRACE_FMT_Version)]
Definition: hpcrun-fmt.h:663
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:260
int hpcrun_fmt_epochHdr_fread(hpcrun_fmt_epochHdr_t *ehdr, FILE *fs, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:172
int hpctrace_fmt_hdr_fread(hpctrace_fmt_hdr_t *hdr, FILE *infs)
Definition: hpcrun-fmt.c:795
int hpctrace_fmt_hdr_outbuf(hpctrace_hdr_flags_t flags, hpcio_outbuf_t *outbuf)
Definition: hpcrun-fmt.c:833
#define LUSH_ASSOC_INFO_STR_MIN_LEN
Definition: lush-support.h:295
static const char HPCTRACE_FMT_Magic[]
Definition: hpcrun-fmt.h:625
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
void hpcrun_fmt_hdr_free(hpcrun_fmt_hdr_t *hdr, hpcfmt_free_fn dealloc)
Definition: hpcrun-fmt.c:159
int hpctrace_fmt_datum_fread(hpctrace_fmt_datum_t *x, hpctrace_hdr_flags_t flags, FILE *fs)
Definition: hpcrun-fmt.c:901
int hpcmetricDB_fmt_hdr_fprint(hpcmetricDB_fmt_hdr_t *hdr, FILE *outfs)
Definition: hpcrun-fmt.c:1057
static const char HPCTRACE_FMT_Endian[]
Definition: hpcrun-fmt.h:627
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
MetricFlags_ValTy_t valTy
Definition: hpcrun-fmt.h:288
int hpcrun_fmt_loadmapEntry_fread(loadmap_entry_t *x, FILE *fs, hpcfmt_alloc_fn alloc)
Definition: hpcrun-fmt.c:589
int hpcrun_fmt_epochHdr_fwrite(FILE *fs, epoch_flags_t flags, uint64_t measurementGranularity,...)
Definition: hpcrun-fmt.c:201
char * name
Definition: hpcrun-fmt.h:455
int hpcmetricDB_fmt_hdr_fwrite(hpcmetricDB_fmt_hdr_t *hdr, FILE *outfs)
Definition: hpcrun-fmt.c:1036
static const char HPCRUN_FMT_Version[]
Definition: hpcrun-fmt.h:114
metric_desc_properties_t properties
Definition: hpcrun-fmt.h:376
void hpcrun_fmt_metric_set_value(metric_desc_t metric_desc, hpcrun_metricVal_t *metric, double value)
Definition: hpcrun-fmt.c:482
double hpcrun_fmt_metric_get_value(metric_desc_t metric_desc, hpcrun_metricVal_t metric)
Definition: hpcrun-fmt.c:464
static metric_desc_p_tbl_t metric_tbl
Definition: metrics.c:114
hpcrun_metricFlags_t flags
Definition: hpcrun-fmt.h:372
hpcrun_metricVal_t * metrics
Definition: hpcrun-fmt.h:560
uint64_t data8[LUSH_LIP_DATA8_SZ]
Definition: lush-support.h:312
static const char HPCRUN_FMT_Endian[]
Definition: hpcrun-fmt.h:115
int hpctrace_fmt_hdr_fwrite(hpctrace_hdr_flags_t flags, FILE *fs)
Definition: hpcrun-fmt.c:862
int hpcfmt_str_fread(char **str, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:87