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
|
//===-- Results.cpp ---------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Results.h"
#include <assert.h>
#ifdef __APPLE__
#include "CFCMutableArray.h"
#include "CFCMutableDictionary.h"
#include "CFCReleaser.h"
#include "CFCString.h"
#endif
using namespace lldb_perf;
static void AddResultToArray(CFCMutableArray &array, Results::Result *result);
static void AddResultToDictionary(CFCMutableDictionary &parent_dict,
const char *key, Results::Result *result);
static void AddResultToArray(CFCMutableArray &parent_array,
Results::Result *result) {
switch (result->GetType()) {
case Results::Result::Type::Invalid:
break;
case Results::Result::Type::Array: {
Results::Array *value = result->GetAsArray();
CFCMutableArray array;
value->ForEach([&array](const Results::ResultSP &value_sp) -> bool {
AddResultToArray(array, value_sp.get());
return true;
});
parent_array.AppendValue(array.get(), true);
} break;
case Results::Result::Type::Dictionary: {
Results::Dictionary *value = result->GetAsDictionary();
CFCMutableDictionary dict;
value->ForEach([&dict](const std::string &key,
const Results::ResultSP &value_sp) -> bool {
AddResultToDictionary(dict, key.c_str(), value_sp.get());
return true;
});
if (result->GetDescription()) {
dict.AddValueCString(CFSTR("description"), result->GetDescription());
}
parent_array.AppendValue(dict.get(), true);
} break;
case Results::Result::Type::Double: {
double d = result->GetAsDouble()->GetValue();
CFCReleaser<CFNumberRef> cf_number(
::CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &d));
if (cf_number.get())
parent_array.AppendValue(cf_number.get(), true);
} break;
case Results::Result::Type::String: {
CFCString cfstr(result->GetAsString()->GetValue());
if (cfstr.get())
parent_array.AppendValue(cfstr.get(), true);
} break;
case Results::Result::Type::Unsigned: {
uint64_t uval64 = result->GetAsUnsigned()->GetValue();
CFCReleaser<CFNumberRef> cf_number(
::CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &uval64));
if (cf_number.get())
parent_array.AppendValue(cf_number.get(), true);
} break;
default:
assert(!"unhandled result");
break;
}
}
static void AddResultToDictionary(CFCMutableDictionary &parent_dict,
const char *key, Results::Result *result) {
assert(key && key[0]);
CFCString cf_key(key);
switch (result->GetType()) {
case Results::Result::Type::Invalid:
break;
case Results::Result::Type::Array: {
Results::Array *value = result->GetAsArray();
CFCMutableArray array;
value->ForEach([&array](const Results::ResultSP &value_sp) -> bool {
AddResultToArray(array, value_sp.get());
return true;
});
parent_dict.AddValue(cf_key.get(), array.get(), true);
} break;
case Results::Result::Type::Dictionary: {
Results::Dictionary *value = result->GetAsDictionary();
CFCMutableDictionary dict;
value->ForEach([&dict](const std::string &key,
const Results::ResultSP &value_sp) -> bool {
AddResultToDictionary(dict, key.c_str(), value_sp.get());
return true;
});
if (result->GetDescription()) {
dict.AddValueCString(CFSTR("description"), result->GetDescription());
}
parent_dict.AddValue(cf_key.get(), dict.get(), true);
} break;
case Results::Result::Type::Double: {
parent_dict.SetValueDouble(cf_key.get(), result->GetAsDouble()->GetValue(),
true);
} break;
case Results::Result::Type::String: {
parent_dict.SetValueCString(cf_key.get(), result->GetAsString()->GetValue(),
true);
} break;
case Results::Result::Type::Unsigned: {
parent_dict.SetValueUInt64(cf_key.get(),
result->GetAsUnsigned()->GetValue(), true);
} break;
default:
assert(!"unhandled result");
break;
}
}
void Results::Write(const char *out_path) {
#ifdef __APPLE__
CFCMutableDictionary dict;
m_results.ForEach(
[&dict](const std::string &key, const ResultSP &value_sp) -> bool {
AddResultToDictionary(dict, key.c_str(), value_sp.get());
return true;
});
CFDataRef xmlData = CFPropertyListCreateData(
kCFAllocatorDefault, dict.get(), kCFPropertyListXMLFormat_v1_0, 0, NULL);
if (out_path == NULL)
out_path = "/dev/stdout";
CFURLRef file = CFURLCreateFromFileSystemRepresentation(
NULL, (const UInt8 *)out_path, strlen(out_path), FALSE);
CFURLWriteDataAndPropertiesToResource(file, xmlData, NULL, NULL);
#endif
}
Results::ResultSP Results::Dictionary::AddUnsigned(const char *name,
const char *description,
uint64_t value) {
assert(name && name[0]);
if (description && description[0]) {
std::unique_ptr<Results::Dictionary> value_dict_ap(
new Results::Dictionary());
value_dict_ap->AddString("description", NULL, description);
value_dict_ap->AddUnsigned("value", NULL, value);
m_dictionary[std::string(name)] = ResultSP(value_dict_ap.release());
} else
m_dictionary[std::string(name)] =
ResultSP(new Unsigned(name, description, value));
return m_dictionary[std::string(name)];
}
Results::ResultSP Results::Dictionary::AddDouble(const char *name,
const char *description,
double value) {
assert(name && name[0]);
if (description && description[0]) {
std::unique_ptr<Results::Dictionary> value_dict_ap(
new Results::Dictionary());
value_dict_ap->AddString("description", NULL, description);
value_dict_ap->AddDouble("value", NULL, value);
m_dictionary[std::string(name)] = ResultSP(value_dict_ap.release());
} else
m_dictionary[std::string(name)] =
ResultSP(new Double(name, description, value));
return m_dictionary[std::string(name)];
}
Results::ResultSP Results::Dictionary::AddString(const char *name,
const char *description,
const char *value) {
assert(name && name[0]);
if (description && description[0]) {
std::unique_ptr<Results::Dictionary> value_dict_ap(
new Results::Dictionary());
value_dict_ap->AddString("description", NULL, description);
value_dict_ap->AddString("value", NULL, value);
m_dictionary[std::string(name)] = ResultSP(value_dict_ap.release());
} else
m_dictionary[std::string(name)] =
ResultSP(new String(name, description, value));
return m_dictionary[std::string(name)];
}
Results::ResultSP Results::Dictionary::Add(const char *name,
const char *description,
const ResultSP &result_sp) {
assert(name && name[0]);
if (description && description[0]) {
std::unique_ptr<Results::Dictionary> value_dict_ap(
new Results::Dictionary());
value_dict_ap->AddString("description", NULL, description);
value_dict_ap->Add("value", NULL, result_sp);
m_dictionary[std::string(name)] = ResultSP(value_dict_ap.release());
} else
m_dictionary[std::string(name)] = result_sp;
return m_dictionary[std::string(name)];
}
void Results::Dictionary::ForEach(
const std::function<bool(const std::string &, const ResultSP &)>
&callback) {
collection::const_iterator pos, end = m_dictionary.end();
for (pos = m_dictionary.begin(); pos != end; ++pos) {
if (callback(pos->first.c_str(), pos->second) == false)
return;
}
}
Results::ResultSP Results::Array::Append(const ResultSP &result_sp) {
m_array.push_back(result_sp);
return result_sp;
}
void Results::Array::ForEach(
const std::function<bool(const ResultSP &)> &callback) {
collection::const_iterator pos, end = m_array.end();
for (pos = m_array.begin(); pos != end; ++pos) {
if (callback(*pos) == false)
return;
}
}
|