forked from LLNL/Caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcali.cpp
877 lines (674 loc) · 19.3 KB
/
cali.cpp
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// Copyright (c) 2015-2022, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
// Caliper C interface implementation
#include "caliper/caliper-config.h"
#include "caliper/cali.h"
#include "caliper/Caliper.h"
#include "caliper/SnapshotRecord.h"
#include "caliper/common/CompressedSnapshotRecord.h"
#include "caliper/common/Log.h"
#include "caliper/common/Node.h"
#include "caliper/common/OutputStream.h"
#include "caliper/common/RuntimeConfig.h"
#include "caliper/common/Variant.h"
#include "caliper/reader/CalQLParser.h"
#include "caliper/reader/QueryProcessor.h"
#include <cstring>
#include <unordered_map>
#include <mutex>
#define SNAP_MAX 120
namespace cali
{
extern Attribute region_attr;
extern Attribute phase_attr;
}
using namespace cali;
//
// --- Miscellaneous
//
const char*
cali_caliper_version()
{
return CALIPER_VERSION;
}
//
// --- Attribute interface
//
cali_id_t
cali_create_attribute(const char* name, cali_attr_type type, int properties)
{
Attribute a = Caliper::instance().create_attribute(name, type, properties);
return a.id();
}
cali_id_t
cali_create_attribute_with_metadata(const char* name, cali_attr_type type, int properties,
int n,
const cali_id_t meta_attr_list[],
const cali_variant_t meta_val_list[])
{
if (n < 1)
return cali_create_attribute(name, type, properties);
Caliper c;
Attribute* meta_attr = new Attribute[n];
Variant* meta_data = new Variant[n];
for (int i = 0; i < n; ++i) {
meta_attr[i] = c.get_attribute(meta_attr_list[i]);
if (meta_attr[i] == Attribute::invalid)
continue;
meta_data[i] = Variant(meta_val_list[i]);
}
Attribute attr =
c.create_attribute(name, type, properties, n, meta_attr, meta_data);
delete[] meta_data;
delete[] meta_attr;
return attr.id();
}
cali_id_t
cali_find_attribute(const char* name)
{
Attribute a = Caliper::instance().get_attribute(name);
return a.id();
}
cali_attr_type
cali_attribute_type(cali_id_t attr_id)
{
Attribute a = Caliper::instance().get_attribute(attr_id);
return a.type();
}
int
cali_attribute_properties(cali_id_t attr_id)
{
Attribute a = Caliper::instance().get_attribute(attr_id);
return a.properties();
}
const char*
cali_attribute_name(cali_id_t attr_id)
{
return Caliper::instance().get_attribute(attr_id).name_c_str();
}
//
// --- Context interface
//
void
cali_push_snapshot(int /*scope*/, int n,
const cali_id_t trigger_info_attr_list[],
const cali_variant_t trigger_info_val_list[])
{
Caliper c;
Attribute attr[64];
Variant data[64];
n = std::min(std::max(n, 0), 64);
for (int i = 0; i < n; ++i) {
attr[i] = c.get_attribute(trigger_info_attr_list[i]);
data[i] = Variant(trigger_info_val_list[i]);
}
FixedSizeSnapshotRecord<64> trigger_info;
c.make_record(n, attr, data, trigger_info.builder());
for (auto chn : c.get_all_channels())
if (chn->is_active())
c.push_snapshot(chn, trigger_info.view());
}
void
cali_channel_push_snapshot(cali_id_t chn_id, int /*scope*/, int n,
const cali_id_t trigger_info_attr_list[],
const cali_variant_t trigger_info_val_list[])
{
Caliper c;
Attribute attr[64];
Variant data[64];
n = std::min(std::max(n, 0), 64);
for (int i = 0; i < n; ++i) {
attr[i] = c.get_attribute(trigger_info_attr_list[i]);
data[i] = Variant(trigger_info_val_list[i]);
}
FixedSizeSnapshotRecord<64> trigger_info;
c.make_record(n, attr, data, trigger_info.builder());
Channel* chn = c.get_channel(chn_id);
if (chn && chn->is_active())
c.push_snapshot(chn, trigger_info.view());
}
size_t
cali_channel_pull_snapshot(cali_id_t chn_id, int /* scopes */, size_t len, unsigned char* buf)
{
Caliper c = Caliper::sigsafe_instance();
if (!c)
return 0;
FixedSizeSnapshotRecord<SNAP_MAX> snapshot;
Channel* chn = c.get_channel(chn_id);
if (chn)
c.pull_snapshot(chn, SnapshotView(), snapshot.builder());
else
Log(0).stream() << "cali_channel_pull_snapshot(): invalid channel id " << chn_id << std::endl;
CompressedSnapshotRecord rec(len, buf);
SnapshotView view(snapshot.view());
rec.append(view.size(), view.data());
return rec.needed_len();
}
//
// --- Snapshot parsing
//
namespace
{
// Helper operator to unpack entries from
// CompressedSnapshotRecordView::unpack()
class UnpackEntryOp {
void* m_arg;
cali_entry_proc_fn m_fn;
public:
UnpackEntryOp(cali_entry_proc_fn fn, void* user_arg)
: m_arg(user_arg), m_fn(fn)
{ }
inline bool operator()(const Entry& e) {
if (e.is_immediate()) {
if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0)
return false;
} else {
for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent())
if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0)
return false;
}
return true;
}
};
class UnpackAttributeEntryOp {
void* m_arg;
cali_entry_proc_fn m_fn;
cali_id_t m_id;
public:
UnpackAttributeEntryOp(cali_id_t id, cali_entry_proc_fn fn, void* user_arg)
: m_arg(user_arg), m_fn(fn), m_id(id)
{ }
inline bool operator()(const Entry& e) {
if (e.is_immediate() && e.attribute() == m_id) {
if ((*m_fn)(m_arg, e.attribute(), e.value().c_variant()) == 0)
return false;
} else {
for (const Node* node = e.node(); node && node->id() != CALI_INV_ID; node = node->parent())
if (node->attribute() == m_id)
if ((*m_fn)(m_arg, node->attribute(), node->data().c_variant()) == 0)
return false;
}
return true;
}
};
}
void
cali_unpack_snapshot(const unsigned char* buf,
size_t* bytes_read,
cali_entry_proc_fn proc_fn,
void* user_arg)
{
size_t pos = 0;
::UnpackEntryOp op(proc_fn, user_arg);
// FIXME: Need sigsafe instance? Only does read-only
// node-by-id lookup though, so we should be safe
Caliper c;
CompressedSnapshotRecordView(buf, &pos).unpack(&c, op);
if (bytes_read)
*bytes_read += pos;
}
cali_variant_t
cali_find_first_in_snapshot(const unsigned char* buf,
cali_id_t attr_id,
size_t* bytes_read)
{
size_t pos = 0;
Variant res;
Caliper c;
CompressedSnapshotRecordView(buf, &pos).unpack(&c, [&res,attr_id](const Entry& e) {
if (e.is_immediate()) {
if (e.attribute() == attr_id) {
res = e.value();
return false;
}
} else {
for (const Node* node = e.node(); node; node = node->parent())
if (node->attribute() == attr_id) {
res = node->data();
return false;
}
}
return true;
});
if (bytes_read)
*bytes_read += pos;
return res.c_variant();
}
void
cali_find_all_in_snapshot(const unsigned char* buf,
cali_id_t attr_id,
size_t* bytes_read,
cali_entry_proc_fn proc_fn,
void* user_arg)
{
size_t pos = 0;
::UnpackAttributeEntryOp op(attr_id, proc_fn, user_arg);
// FIXME: Need sigsafe instance? Only does read-only
// node-by-id lookup though, so we should be safe
Caliper c;
CompressedSnapshotRecordView(buf, &pos).unpack(&c, op);
if (bytes_read)
*bytes_read += pos;
}
//
// --- Blackboard access interface
//
cali_variant_t
cali_get(cali_id_t attr_id)
{
Caliper c = Caliper::sigsafe_instance();
if (!c)
return cali_make_empty_variant();
return c.get(c.get_attribute(attr_id)).value().c_variant();
}
cali_variant_t
cali_channel_get(cali_id_t chn_id, cali_id_t attr_id)
{
Caliper c = Caliper::sigsafe_instance();
Channel* channel = c.get_channel(chn_id);
if (!c)
return cali_make_empty_variant();
return c.get(channel, c.get_attribute(attr_id)).value().c_variant();
}
const char*
cali_get_current_region_or(const char* alt)
{
Caliper c = Caliper::sigsafe_instance();
if (!c)
return alt;
Entry e = c.get_path_node();
if (!e.empty() && e.value().type() == CALI_TYPE_STRING)
return static_cast<const char*>(e.value().data());
return alt;
}
//
// --- Annotation interface
//
void
cali_begin_region(const char* name)
{
Caliper c;
c.begin(cali::region_attr, Variant(name));
}
void
cali_end_region(const char* name)
{
Caliper c;
c.end_with_value_check(cali::region_attr, Variant(name));
}
void
cali_begin_phase(const char* name)
{
Caliper c;
c.begin(cali::phase_attr, Variant(name));
}
void
cali_end_phase(const char* name)
{
Caliper c;
c.end_with_value_check(cali::phase_attr, Variant(name));
}
void
cali_begin(cali_id_t attr_id)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.begin(attr, Variant(true));
}
void
cali_end(cali_id_t attr_id)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.end(attr);
}
void
cali_set(cali_id_t attr_id, const void* value, size_t size)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.set(attr, Variant(attr.type(), value, size));
}
void
cali_begin_double(cali_id_t attr_id, double val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.begin(attr, Variant(val));
}
void
cali_begin_int(cali_id_t attr_id, int val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.begin(attr, Variant(val));
}
void
cali_begin_string(cali_id_t attr_id, const char* val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.begin(attr, Variant(CALI_TYPE_STRING, val, strlen(val)));
}
void
cali_set_double(cali_id_t attr_id, double val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.set(attr, Variant(val));
}
void
cali_set_int(cali_id_t attr_id, int val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.set(attr, Variant(val));
}
void
cali_set_string(cali_id_t attr_id, const char* val)
{
Caliper c;
Attribute attr = c.get_attribute(attr_id);
c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val)));
}
//
// --- By-name annotation interface
//
void
cali_begin_byname(const char* attr_name)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_BOOL, CALI_ATTR_DEFAULT);
c.begin(attr, Variant(true));
}
void
cali_begin_double_byname(const char* attr_name, double val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_DEFAULT);
c.begin(attr, Variant(val));
}
void
cali_begin_int_byname(const char* attr_name, int val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_DEFAULT);
c.begin(attr, Variant(val));
}
void
cali_begin_string_byname(const char* attr_name, const char* val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_DEFAULT);
c.begin(attr, Variant(CALI_TYPE_STRING, val, strlen(val)));
}
void
cali_set_double_byname(const char* attr_name, double val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_DOUBLE, CALI_ATTR_UNALIGNED);
c.set(attr, Variant(val));
}
void
cali_set_int_byname(const char* attr_name, int val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_INT, CALI_ATTR_UNALIGNED);
c.set(attr, Variant(val));
}
void
cali_set_string_byname(const char* attr_name, const char* val)
{
Caliper c;
Attribute attr =
c.create_attribute(attr_name, CALI_TYPE_STRING, CALI_ATTR_UNALIGNED);
c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val)));
}
void
cali_end_byname(const char* attr_name)
{
Caliper c;
Attribute attr = c.get_attribute(attr_name);
c.end(attr);
}
// --- Set globals
//
void
cali_set_global_double_byname(const char* name, double val)
{
Caliper c;
Attribute attr =
c.create_attribute(name, CALI_TYPE_DOUBLE, CALI_ATTR_GLOBAL | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS);
// TODO: check for existing incompatible attribute key
c.set(attr, cali_make_variant_from_double(val));
}
void
cali_set_global_int_byname(const char* name, int val)
{
Caliper c;
Attribute attr =
c.create_attribute(name, CALI_TYPE_INT, CALI_ATTR_GLOBAL | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS);
// TODO: check for existing incompatible attribute key
c.set(attr, cali_make_variant_from_int(val));
}
void
cali_set_global_string_byname(const char* name, const char* val)
{
Caliper c;
Attribute attr =
c.create_attribute(name, CALI_TYPE_STRING, CALI_ATTR_GLOBAL | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS);
// TODO: check for existing incompatible attribute key
c.set(attr, Variant(CALI_TYPE_STRING, val, strlen(val)+1));
}
void
cali_set_global_uint_byname(const char* name, uint64_t val)
{
Caliper c;
Attribute attr =
c.create_attribute(name, CALI_TYPE_UINT, CALI_ATTR_GLOBAL | CALI_ATTR_UNALIGNED | CALI_ATTR_SKIP_EVENTS);
// TODO: check for existing incompatible attribute key
c.set(attr, cali_make_variant_from_uint(val));
}
// --- Config API
//
void
cali_config_preset(const char* key, const char* value)
{
if (Caliper::is_initialized())
Log(0).stream() << "Warning: Caliper is already initialized. "
<< "cali_config_preset(\"" << key << "\", \"" << value
<< "\") has no effect." << std::endl;
RuntimeConfig::get_default_config().preset(key, value);
}
void
cali_config_set(const char* key, const char* value)
{
if (Caliper::is_initialized())
Log(0).stream() << "Warning: Caliper is already initialized. "
<< "cali_config_set(\"" << key << "\", \"" << value
<< "\") has no effect." << std::endl;
RuntimeConfig::get_default_config().set(key, value);
}
void
cali_config_allow_read_env(int allow)
{
RuntimeConfig::get_default_config().allow_read_env(allow != 0);
}
struct _cali_configset_t {
std::map<std::string, std::string> cfgset;
};
cali_configset_t
cali_create_configset(const char* keyvallist[][2])
{
cali_configset_t cfg = new _cali_configset_t;
if (!keyvallist)
return cfg;
for ( ; (*keyvallist)[0] && (*keyvallist)[1]; ++keyvallist)
cfg->cfgset.insert(std::make_pair((*keyvallist)[0], (*keyvallist)[1]));
return cfg;
}
void
cali_delete_configset(cali_configset_t cfg)
{
delete cfg;
}
void
cali_configset_set(cali_configset_t cfg, const char* key, const char* value)
{
cfg->cfgset[key] = value;
}
cali_id_t
cali_create_channel(const char* name, int flags, cali_configset_t cfgset)
{
RuntimeConfig cfg;
cfg.allow_read_env(flags & CALI_CHANNEL_ALLOW_READ_ENV);
cfg.import(cfgset->cfgset);
Caliper c;
Channel* chn = c.create_channel(name, cfg);
if (!chn)
return CALI_INV_ID;
if (flags & CALI_CHANNEL_LEAVE_INACTIVE)
c.deactivate_channel(chn);
return chn->id();
}
void
cali_delete_channel(cali_id_t chn_id)
{
Caliper c;
Channel* chn = c.get_channel(chn_id);
if (chn)
c.delete_channel(chn);
else
Log(0).stream() << "cali_channel_delete(): invalid channel id " << chn_id << std::endl;
}
void
cali_activate_channel(cali_id_t chn_id)
{
Caliper c;
Channel* chn = c.get_channel(chn_id);
if (chn)
c.activate_channel(chn);
else
Log(0).stream() << "cali_activate_channel(): invalid channel id " << chn_id << std::endl;
}
void
cali_deactivate_channel(cali_id_t chn_id)
{
Caliper c;
Channel* chn = c.get_channel(chn_id);
if (chn)
c.deactivate_channel(chn);
else
Log(0).stream() << "cali_deactivate_channel(): invalid channel id " << chn_id << std::endl;
}
int
cali_channel_is_active(cali_id_t chn_id)
{
Channel* chn = Caliper::instance().get_channel(chn_id);
if (!chn) {
Log(0).stream() << "cali_channel_is_active(): invalid channel id " << chn_id << std::endl;
return 0;
}
return (chn->is_active() ? 1 : 0);
}
void
cali_flush(int flush_opts)
{
Caliper c;
Channel* channel = c.get_channel(0); // channel 0 should be the default channel
if (channel && channel->is_active()) {
c.flush_and_write(channel, SnapshotView());
if (flush_opts & CALI_FLUSH_CLEAR_BUFFERS)
c.clear(channel);
}
}
void
cali_channel_flush(cali_id_t chn_id, int flush_opts)
{
Caliper c;
Channel* chn = c.get_channel(chn_id);
c.flush_and_write(chn, SnapshotView());
if (flush_opts & CALI_FLUSH_CLEAR_BUFFERS)
c.clear(chn);
}
void
cali_init()
{
Caliper::instance();
}
int
cali_is_initialized()
{
return Caliper::is_initialized() ? 1 : 0;
}
//
// --- Helper functions for high-level macro interface
//
namespace cali
{
extern Attribute class_iteration_attr;
}
cali_id_t
cali_make_loop_iteration_attribute(const char* name)
{
Variant v_true(true);
Caliper c;
Attribute attr =
c.create_attribute(std::string("iteration#").append(name),
CALI_TYPE_INT,
CALI_ATTR_ASVALUE,
1, &class_iteration_attr, &v_true);
return attr.id();
}
//
// --- C++ convenience API
//
namespace cali
{
cali_id_t
create_channel(const char* name, int flags, const config_map_t& cfgmap)
{
RuntimeConfig cfg;
cfg.allow_read_env(flags & CALI_CHANNEL_ALLOW_READ_ENV);
cfg.import(cfgmap);
Caliper c;
Channel* chn = c.create_channel(name, cfg);
if (!chn)
return CALI_INV_ID;
if (flags & CALI_CHANNEL_LEAVE_INACTIVE)
c.deactivate_channel(chn);
return chn->id();
}
void
write_report_for_query(cali_id_t chn_id, const char* query, int flush_opts, std::ostream& os)
{
Caliper c;
Channel* chn = c.get_channel(chn_id);
if (!chn) {
Log(0).stream() << "write_report_for_query(): invalid channel id " << chn_id
<< std::endl;
return;
}
CalQLParser parser(query);
if (parser.error()) {
Log(0).stream() << "write_report_for_query(): query parse error: "
<< parser.error_msg()
<< std::endl;
return;
}
QuerySpec spec(parser.spec());
OutputStream stream;
stream.set_stream(&os);
QueryProcessor queryP(spec, stream);
c.flush(chn, SnapshotView(), [&queryP](CaliperMetadataAccessInterface& db, const std::vector<Entry>& rec){
queryP.process_record(db, rec);
});
queryP.flush(c);
}
}