Skip to content

Commit f8e52a0

Browse files
committed
Implement annotation levels
1 parent 209a2b3 commit f8e52a0

19 files changed

+173
-27
lines changed

include/caliper/cali.h

+17
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,23 @@ cali_begin_region(const char* name);
373373
void
374374
cali_end_region(const char* name);
375375

376+
/**
377+
* \brief Begin phase region \a name
378+
*
379+
* A phase marks high-level, long(er)-running code regions. While regular
380+
* regions use the "region" attribute with annotation level 0, phase regions
381+
* use the "phase" attribute with annotation level 4. Otherwise phases behave
382+
* identical to regular Caliper regions.
383+
*/
384+
void
385+
cali_begin_phase(const char* name);
386+
387+
/**
388+
* \brief End phase region \a name
389+
*/
390+
void
391+
cali_end_phase(const char* name);
392+
376393
/**
377394
* \brief Begin region where the value for \a attr is `true` on the blackboard.
378395
*/

include/caliper/cali_macros.h

+12
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ extern cali_id_t cali_annotation_attr_id;
203203
#define CALI_MARK_END(name) \
204204
cali_end_region(name)
205205

206+
/// \brief Mark begin of a phase region
207+
///
208+
/// A phase marks high-level, long(er)-running code regions. While regular
209+
/// regions use the "region" attribute with annotation level 0, phase regions
210+
/// use the "phase" attribute with annotation level 4. Otherwise phases behave
211+
/// identical to regular Caliper regions.
212+
#define CALI_MARK_PHASE_BEGIN(name) \
213+
cali_begin_phase(name)
214+
215+
/// \brief Mark end of a phase region
216+
#define CALI_MARK_PHASE_END(name) \
217+
cali_end_phase(name)
206218
/**
207219
* \} (group)
208220
*/

include/caliper/common/Attribute.h

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class Attribute
7878
bool is_global() const {
7979
return properties() & CALI_ATTR_GLOBAL;
8080
}
81+
int level() const {
82+
return (properties() & CALI_ATTR_LEVEL_MASK) >> 16;
83+
}
8184

8285
static Attribute make_attribute(Node* node);
8386

include/caliper/common/cali_types.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,33 @@ typedef enum {
123123
* \a aggregate service. This flag replaces the previous
124124
* \a class.aggregatable meta-attribute.
125125
*/
126-
CALI_ATTR_AGGREGATABLE = 2048
126+
CALI_ATTR_AGGREGATABLE = 2048,
127+
128+
/**
129+
* \brief Annotation level 1
130+
*
131+
* Annotation levels can be used to describe and set the granularity of
132+
* region annotations. Level 0 is the finest level, level 7 is the
133+
* coarsest.
134+
*/
135+
CALI_ATTR_LEVEL_1 = 0x10000,
136+
/** \brief Annotation level 2 */
137+
CALI_ATTR_LEVEL_2 = 0x20000,
138+
/** \brief Annotation level 3 */
139+
CALI_ATTR_LEVEL_3 = 0x30000,
140+
/** \brief Annotation level 4 */
141+
CALI_ATTR_LEVEL_4 = 0x40000,
142+
/** \brief Annotation level 5 */
143+
CALI_ATTR_LEVEL_5 = 0x50000,
144+
/** \brief Annotation level 6 */
145+
CALI_ATTR_LEVEL_6 = 0x60000,
146+
/** \brief Annotation level 7 */
147+
CALI_ATTR_LEVEL_7 = 0x70000
127148
} cali_attr_properties;
128149

150+
#define CALI_ATTR_LEVEL_0 0x0
129151
#define CALI_ATTR_SCOPE_MASK 60
152+
#define CALI_ATTR_LEVEL_MASK 0x70000
130153

131154
/**
132155
* \brief Provides descriptive string of given attribute property flags, separated with ':'

src/caliper/api.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ cali_id_t cali_subscription_event_attr_id = CALI_INV_ID;
1717

1818
cali_id_t cali_loop_attr_id = CALI_INV_ID;
1919
cali_id_t cali_region_attr_id = CALI_INV_ID;
20+
cali_id_t cali_phase_attr_id = CALI_INV_ID;
2021

2122
cali_id_t cali_alloc_fn_attr_id = CALI_INV_ID;
2223
cali_id_t cali_alloc_label_attr_id = CALI_INV_ID;
@@ -36,6 +37,7 @@ namespace cali
3637
Attribute subscription_event_attr;
3738

3839
Attribute region_attr;
40+
Attribute phase_attr;
3941
Attribute loop_attr;
4042

4143
void init_attribute_classes(Caliper* c) {
@@ -61,8 +63,11 @@ namespace cali
6163
c->create_attribute("loop", CALI_TYPE_STRING, CALI_ATTR_NESTED);
6264
region_attr =
6365
c->create_attribute("region", CALI_TYPE_STRING, CALI_ATTR_NESTED);
66+
phase_attr =
67+
c->create_attribute("phase", CALI_TYPE_STRING, CALI_ATTR_NESTED | CALI_ATTR_LEVEL_4);
6468

6569
cali_region_attr_id = region_attr.id();
70+
cali_phase_attr_id = phase_attr.id();
6671
cali_loop_attr_id = loop_attr.id();
6772
}
6873
}

src/caliper/cali.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace cali
3030
{
3131

3232
extern Attribute region_attr;
33+
extern Attribute phase_attr;
3334

3435
}
3536

@@ -385,9 +386,21 @@ void
385386
cali_end_region(const char* name)
386387
{
387388
Caliper c;
388-
Variant v_n(name);
389+
c.end_with_value_check(cali::region_attr, Variant(name));
390+
}
389391

390-
c.end_with_value_check(cali::region_attr, v_n);
392+
void
393+
cali_begin_phase(const char* name)
394+
{
395+
Caliper c;
396+
c.begin(cali::phase_attr, Variant(name));
397+
}
398+
399+
void
400+
cali_end_phase(const char* name)
401+
{
402+
Caliper c;
403+
c.end_with_value_check(cali::phase_attr, Variant(name));
391404
}
392405

393406
void

src/caliper/controllers/controllers.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ const char* builtin_option_specs = R"json(
263263
}
264264
]
265265
},
266+
{
267+
"name" : "level",
268+
"type" : "string",
269+
"description" : "Minimum region level that triggers snapshots",
270+
"category" : "event",
271+
"config" : { "CALI_EVENT_REGION_LEVEL": "{}" }
272+
},
266273
{
267274
"name" : "include_regions",
268275
"type" : "string",

src/caliper/test/test_attribute.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ TEST(AttributeAPITest, ValidAttribute) {
5757
std::vector<std::string> props;
5858
util::split(std::string(buf), ':', std::back_inserter(props));
5959

60-
std::vector<std::string> props_exp { "nested", "process_scope", "nomerge", "default" };
60+
std::vector<std::string> props_exp { "nested", "process_scope", "nomerge", "default", "level_0" };
6161

6262
for (auto s : props) {
6363
auto it = std::find(props_exp.begin(), props_exp.end(), s);

src/common/cali_types.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ cali_string2type(const char* str)
4545
}
4646

4747
static const struct propmap_t {
48-
const char* str; cali_attr_properties prop; int mask;
48+
const char* str; int prop; int mask;
4949
} propmap[] = {
5050
{ "default", CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT },
5151
{ "asvalue", CALI_ATTR_ASVALUE, CALI_ATTR_ASVALUE },
@@ -59,6 +59,14 @@ static const struct propmap_t {
5959
{ "global", CALI_ATTR_GLOBAL, CALI_ATTR_GLOBAL },
6060
{ "unaligned", CALI_ATTR_UNALIGNED, CALI_ATTR_UNALIGNED },
6161
{ "aggregatable", CALI_ATTR_AGGREGATABLE, CALI_ATTR_AGGREGATABLE },
62+
{ "level_0", CALI_ATTR_LEVEL_0, CALI_ATTR_LEVEL_MASK },
63+
{ "level_1", CALI_ATTR_LEVEL_1, CALI_ATTR_LEVEL_MASK },
64+
{ "level_2", CALI_ATTR_LEVEL_2, CALI_ATTR_LEVEL_MASK },
65+
{ "level_3", CALI_ATTR_LEVEL_3, CALI_ATTR_LEVEL_MASK },
66+
{ "level_4", CALI_ATTR_LEVEL_4, CALI_ATTR_LEVEL_MASK },
67+
{ "level_5", CALI_ATTR_LEVEL_5, CALI_ATTR_LEVEL_MASK },
68+
{ "level_6", CALI_ATTR_LEVEL_6, CALI_ATTR_LEVEL_MASK },
69+
{ "level_7", CALI_ATTR_LEVEL_7, CALI_ATTR_LEVEL_MASK },
6270
{ 0, CALI_ATTR_DEFAULT, CALI_ATTR_DEFAULT }
6371
};
6472

src/services/event/EventTrigger.cpp

+31-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ using namespace std;
3131
namespace cali
3232
{
3333
extern Attribute subscription_event_attr; // From api.cpp
34+
extern Attribute phase_attr;
3435
}
3536

3637
namespace
@@ -52,7 +53,8 @@ class EventTrigger
5253

5354
std::vector<std::string> trigger_attr_names;
5455

55-
bool enable_snapshot_info;
56+
bool enable_snapshot_info { true };
57+
int region_level { 0 };
5658

5759
Node event_root_node;
5860

@@ -62,6 +64,26 @@ class EventTrigger
6264
// --- Helpers / misc
6365
//
6466

67+
void parse_region_level(Channel* channel, const std::string& str) {
68+
if (str == "phase") {
69+
region_level = phase_attr.level();
70+
} else {
71+
bool ok = false;
72+
int level = StringConverter(str).to_int(&ok);
73+
74+
if (!ok || level < 0 || level > 7) {
75+
Log(0).stream() << channel->name()
76+
<< ": event: Invalid region level \"" << str << "\"\n";
77+
region_level = 0;
78+
} else {
79+
region_level = level;
80+
}
81+
}
82+
83+
Log(2).stream() << channel->name()
84+
<< ": event: Using region level " << region_level << "\n";
85+
}
86+
6587
void mark_attribute(Caliper* c, Channel* chn, const Attribute& attr) {
6688
cali_id_t evt_attr_ids[3] = { CALI_INV_ID };
6789

@@ -102,6 +124,8 @@ class EventTrigger
102124

103125
if (trigger_attr_names.size() > 0 && it == trigger_attr_names.end())
104126
return;
127+
if (attr.level() < region_level)
128+
return;
105129

106130
mark_attribute(c, chn, attr);
107131
}
@@ -251,6 +275,7 @@ class EventTrigger
251275

252276
trigger_attr_names = cfg.get("trigger").to_stringlist(",:");
253277
enable_snapshot_info = cfg.get("enable_snapshot_info").to_bool();
278+
parse_region_level(channel, cfg.get("region_level").to_string());
254279

255280
{
256281
std::string i_filter =
@@ -335,6 +360,11 @@ const char* EventTrigger::s_spec = R"json(
335360
"type" : "stringlist",
336361
"description" : "List of attributes that trigger measurements (optional)"
337362
},
363+
{ "name" : "region_level",
364+
"type" : "string",
365+
"description" : "Minimum region level that triggers snapshots",
366+
"value" : "0"
367+
},
338368
{ "name" : "enable_snapshot_info",
339369
"type" : "bool",
340370
"description" : "If true, add begin/end attributes at each event. Increases overhead.",

test/ci_app_tests/ci_test_basic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main(int argc, char* argv[])
1717
if (argc > 1 && strcmp(argv[1], "newline") == 0)
1818
cali_set_string_byname("newline", "A newline:\n!");
1919

20-
cali::Annotation phase_ann("phase", metadata);
20+
cali::Annotation phase_ann("myphase", metadata);
2121
std::size_t size = 8;
2222
cali::Annotation size_annot("dgs");
2323
size_annot.begin(size);

test/ci_app_tests/ci_test_macros.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ void foo(int count, int sleep_usec)
3030
CALI_MARK_LOOP_END(fooloop);
3131
}
3232

33+
void bar()
34+
{
35+
CALI_CXX_MARK_FUNCTION;
36+
}
37+
3338
int main(int argc, char* argv[])
3439
{
3540
int sleep_usec = 0;
@@ -62,14 +67,17 @@ int main(int argc, char* argv[])
6267
}
6368

6469
CALI_CXX_MARK_LOOP_BEGIN(mainloop, "main loop");
65-
6670
for (int i = 0; i < count; ++i) {
6771
CALI_CXX_MARK_LOOP_ITERATION(mainloop, i);
6872

6973
foo(count, sleep_usec);
7074
}
71-
7275
CALI_CXX_MARK_LOOP_END(mainloop);
76+
77+
CALI_MARK_PHASE_BEGIN("after_loop");
78+
bar();
79+
CALI_MARK_PHASE_END("after_loop");
80+
7381
CALI_MARK_FUNCTION_END;
7482

7583
mgr.flush();

test/ci_app_tests/test_basictrace.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ def test_basic_trace(self):
2424
self.assertTrue(len(snapshots) > 10)
2525

2626
self.assertTrue(cat.has_snapshot_with_keys(
27-
snapshots, {'iteration', 'phase', 'time.inclusive.duration.ns'}))
27+
snapshots, {'iteration', 'myphase', 'time.inclusive.duration.ns'}))
2828
self.assertTrue(cat.has_snapshot_with_attributes(
29-
snapshots, {'event.end#phase': 'initialization', 'phase': 'initialization'}))
29+
snapshots, {'event.end#myphase': 'initialization', 'myphase': 'initialization'}))
3030
self.assertTrue(cat.has_snapshot_with_attributes(
31-
snapshots, {'event.end#iteration': '3', 'iteration': '3', 'phase': 'loop'}))
31+
snapshots, {'event.end#iteration': '3', 'iteration': '3', 'myphase': 'loop'}))
3232

3333
def test_cali_config(self):
3434
# Test the builtin ConfigManager (CALI_CONFIG env var)
@@ -47,11 +47,11 @@ def test_cali_config(self):
4747
self.assertTrue(len(snapshots) > 10)
4848

4949
self.assertTrue(cat.has_snapshot_with_keys(
50-
snapshots, {'iteration', 'phase', 'time.inclusive.duration.ns'}))
50+
snapshots, {'iteration', 'myphase', 'time.inclusive.duration.ns'}))
5151
self.assertTrue(cat.has_snapshot_with_attributes(
52-
snapshots, {'event.end#phase': 'initialization', 'phase': 'initialization'}))
52+
snapshots, {'event.end#myphase': 'initialization', 'myphase': 'initialization'}))
5353
self.assertTrue(cat.has_snapshot_with_attributes(
54-
snapshots, {'event.end#iteration': '3', 'iteration': '3', 'phase': 'loop'}))
54+
snapshots, {'event.end#iteration': '3', 'iteration': '3', 'myphase': 'loop'}))
5555

5656
def test_ann_metadata(self):
5757
target_cmd = [ './ci_test_basic' ]
@@ -67,7 +67,7 @@ def test_ann_metadata(self):
6767
snapshots = cat.get_snapshots_from_text(query_output)
6868

6969
self.assertTrue(cat.has_snapshot_with_attributes(
70-
snapshots, { 'cali.attribute.name': 'phase',
70+
snapshots, { 'cali.attribute.name': 'myphase',
7171
'meta.int': '42',
7272
'cali.attribute.prop': '20' # default property: CALI_ATTR_SCOPE_THREAD
7373
}))
@@ -87,7 +87,7 @@ def test_default_scope_switch(self):
8787
snapshots = cat.get_snapshots_from_text(query_output)
8888

8989
self.assertTrue(cat.has_snapshot_with_attributes(
90-
snapshots, { 'cali.attribute.name': 'phase',
90+
snapshots, { 'cali.attribute.name': 'myphase',
9191
'meta.int': '42',
9292
'cali.attribute.prop': '12' # CALI_ATTR_SCOPE_PROCESS
9393
}))
@@ -280,5 +280,25 @@ def test_exclusive_region_filter(self):
280280
snapshots, {
281281
'region' : 'main/before_loop' }))
282282

283+
def test_region_level_filter(self):
284+
target_cmd = [ './ci_test_macros', '0', 'hatchet-region-profile,level=phase,output=stdout' ]
285+
query_cmd = [ '../../src/tools/cali-query/cali-query', '-e' ]
286+
287+
query_output = cat.run_test_with_query(target_cmd, query_cmd, None)
288+
snapshots = cat.get_snapshots_from_text(query_output)
289+
290+
self.assertFalse(cat.has_snapshot_with_attributes(
291+
snapshots, {
292+
'region' : 'main/foo',
293+
'loop' : 'main loop/fooloop' }))
294+
self.assertFalse(cat.has_snapshot_with_attributes(
295+
snapshots, {
296+
'region' : 'main/bar',
297+
'phase' : 'after_loop' }))
298+
self.assertTrue(cat.has_snapshot_with_attributes(
299+
snapshots, {
300+
'region' : 'main',
301+
'phase' : 'after_loop' }))
302+
283303
if __name__ == "__main__":
284304
unittest.main()

test/ci_app_tests/test_cpuinfo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_papi(self):
2525
self.assertTrue(calitest.has_snapshot_with_keys(
2626
snapshots, { 'cpuinfo.cpu',
2727
'cpuinfo.numa_node',
28-
'phase',
28+
'myphase',
2929
'iteration' }))
3030

3131

0 commit comments

Comments
 (0)