Skip to content

Commit 2fc3b07

Browse files
some cleanup and functionality for tracing
1 parent b4c2b45 commit 2fc3b07

File tree

3 files changed

+35
-75
lines changed

3 files changed

+35
-75
lines changed

‎src/util/trace.cpp‎

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,79 +44,85 @@ std::ofstream tout(".z3-trace");
4444

4545
static bool g_enable_all_trace_tags = false;
4646

47-
static bool s_tag_enabled[] = {
48-
#define X(tag, tc, desc) false,
47+
inline unsigned tag2u(TraceTag tag) { return static_cast<unsigned>(tag); }
48+
49+
struct tag_info {
50+
bool is_enabled;
51+
bool is_class;
52+
TraceTag next;
53+
};
54+
55+
static tag_info s_tag_infos[] = {
56+
#define X(tag, tc, desc) { false, false, TraceTag::tag },
4957
#include "util/trace_tags.def"
5058
#undef X
5159
};
5260

5361

5462
bool tag_enabled(TraceTag tag) {
55-
return tag < TraceTag::Count && s_tag_enabled[static_cast<unsigned>(tag)];
63+
return tag < TraceTag::Count && s_tag_infos[tag2u(tag)].is_enabled;
5664
}
5765

5866
static void enable_tag(TraceTag tag) {
5967
if (tag < TraceTag::Count)
60-
s_tag_enabled[static_cast<unsigned>(tag)] = true;
68+
s_tag_infos[tag2u(tag)].is_enabled = true;
6169
}
6270

6371
static void disable_tag(TraceTag tag) {
6472
if (tag < TraceTag::Count)
65-
s_tag_enabled[static_cast<unsigned>(tag)] = false;
73+
s_tag_infos[tag2u(tag)].is_enabled = false;
6674
}
6775

68-
69-
void finalize_trace() {
70-
}
71-
72-
static const TraceTag* get_tag_classes() {
76+
static const tag_info* get_tag_infos() {
7377
static bool tag_class_init = false; // ignore thread safety assuming TRACE is already under a lock.
7478
if (!tag_class_init) {
7579
// Arrays to track first and last tag in each class
7680
TraceTag first[static_cast<unsigned>(TraceTag::Count)];
7781
TraceTag last[static_cast<unsigned>(TraceTag::Count)];
78-
for (unsigned i = 0; i < static_cast<unsigned>(TraceTag::Count); ++i)
82+
for (unsigned i = 0; i < tag2u(TraceTag::Count); ++i)
7983
first[i] = last[i] = TraceTag::Count;
8084

8185
// Link tags in each class
82-
for (unsigned i = 0; i < static_cast<unsigned>(TraceTag::Count); ++i) {
86+
for (unsigned i = 0; i < tag2u(TraceTag::Count); ++i) {
8387
TraceTag tag = static_cast<TraceTag>(i);
8488
TraceTag tag_class = get_trace_tag_class(tag);
85-
unsigned cls = static_cast<unsigned>(tag_class);
89+
s_tag_infos[tag2u(tag_class)].is_class = true;
90+
unsigned cls = tag2u(tag_class);
8691
if (first[cls] == TraceTag::Count)
8792
first[cls] = tag;
8893
else
89-
tag_classes[static_cast<unsigned>(last[cls])] = tag;
94+
s_tag_infos[tag2u(last[cls])].next = tag;
9095
last[cls] = tag;
9196
}
9297
// Close the circular list for each class
93-
for (unsigned cls = 0; cls < static_cast<unsigned>(TraceTag::Count); ++cls)
98+
for (unsigned cls = 0; cls < tag2u(TraceTag::Count); ++cls)
9499
if (last[cls] != TraceTag::Count && first[cls] != TraceTag::Count)
95-
tag_classes[static_cast<unsigned>(last[cls])] = first[cls];
100+
s_tag_infos[tag2u(last[cls])].next = first[cls];
96101

97102
tag_class_init = true;
98103
}
99-
return tag_classes;
104+
return s_tag_infos;
100105
}
101106

102107

103108

104-
void enable_trace(const char * tag) {
105-
TraceTag tag_str = find_trace_tag_by_string(tag);
106-
if (tag_str == TraceTag::Count)
109+
void enable_trace(const char * tag_str) {
110+
TraceTag tag = find_trace_tag_by_string(tag_str);
111+
if (tag == TraceTag::Count) {
112+
warning_msg("trace tag '%s' does not exist", tag_str);
107113
return;
114+
}
108115

109-
enable_tag(tag_str);
116+
enable_tag(tag);
110117

111-
auto tag_class = get_trace_tag_class(tag_str);
112-
if (tag_class != tag_str)
113-
return; // Only enable the tag if it is a class tag.
114-
auto const& next_tag = get_tag_classes();
118+
auto const& tag_infos = get_tag_infos();
119+
if (!tag_infos[tag2u(tag)].is_class)
120+
return;
115121

116-
auto t = next_tag[static_cast<unsigned>(tag_str)];
117-
while (t != tag_str) {
122+
auto t = tag_infos[tag2u(tag)].next;
123+
while (t != tag) {
118124
enable_tag(t);
119-
t = next_tag[static_cast<unsigned>(t)];
125+
t = tag_infos[tag2u(t)].next;
120126
}
121127
}
122128

‎src/util/trace.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ void disable_trace(const char * tag);
5959
bool is_trace_enabled(TraceTag tag);
6060
void close_trace();
6161
void open_trace();
62-
void finalize_trace();
63-
/*
64-
ADD_FINALIZER('finalize_trace();')
65-
*/
6662

6763
#else
6864
#define TRACE_CODE(CODE) ((void) 0)

‎src/util/trace_tags.h‎

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,53 +38,11 @@ inline TraceTag get_trace_tag_class(TraceTag tag) {
3838
}
3939
}
4040

41-
// Return the number of TraceTags
42-
inline constexpr int trace_tag_count() {
43-
return static_cast<int>(TraceTag::Count);
44-
}
45-
46-
47-
// Helper function to count tags in a class
48-
inline constexpr int count_tags_in_class(TraceTag cls) {
49-
int count = 0;
50-
#define X(tag, tag_class, desc) if (TraceTag::tag_class == cls) count++;
51-
#include "util/trace_tags.def"
52-
#undef X
53-
return count;
54-
}
55-
56-
57-
static TraceTag tag_classes[] = {
58-
#define X(tag, tc, desc) TraceTag::tag,
59-
#include "util/trace_tags.def"
60-
#undef X
61-
};
62-
6341

64-
// TODO(#7663): Implement tag_class activation of all associated tags
65-
// TODO: Need to consider implementation approach and memory management
66-
// Return all tags that belong to the given class
67-
// inline const TraceTag* get_tags_by_class(TraceTag cls, int& count) {
68-
// count = count_tags_in_class(cls);
69-
// static TraceTag* class_tags = nullptr;
70-
// if (class_tags) delete[] class_tags;
71-
72-
// class_tags = new TraceTag[count];
73-
// int idx = 0;
74-
75-
// #define X(tag, tag_class, desc) \
76-
// if (TraceTag::tag_class == cls) { \
77-
// class_tags[idx++] = TraceTag::tag; \
78-
// }
79-
// include "util/trace_tags.def"
80-
// #undef X
81-
82-
// return class_tags;
83-
// }
8442

8543
// Find TraceTag by string
8644
inline TraceTag find_trace_tag_by_string(const char* tag_str) {
87-
#define X(tag, tag_class, desc) if (strncmp(#tag, tag_str, strlen(#tag)) == 0) return TraceTag::tag;
45+
#define X(tag, tag_class, desc) if (strcmp(#tag, tag_str) == 0) return TraceTag::tag;
8846
#include "util/trace_tags.def"
8947
#undef X
9048
return TraceTag::Count;

0 commit comments

Comments
 (0)