]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/regex/performance/table_helper.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / regex / performance / table_helper.cpp
1 // Copyright John Maddock 2015.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifdef _MSC_VER
7 # pragma warning (disable : 4224)
8 #endif
9
10 #include <boost/regex.hpp>
11 #include <boost/lexical_cast.hpp>
12 #include <boost/filesystem.hpp>
13 #include <boost/filesystem/fstream.hpp>
14 #include <vector>
15 #include <set>
16 #include <iostream>
17 #include <sstream>
18 #include <iomanip>
19
20 std::vector<std::vector<double> > data;
21
22 inline std::string sanitize_string(const std::string& s)
23 {
24 static const boost::regex e("[^a-zA-Z0-9]+");
25 std::string result = boost::regex_replace(s, e, "_");
26 while(result[0] == '_')
27 result.erase(0);
28 return result;
29 }
30
31 std::string format_precision(double val, int digits)
32 {
33 std::stringstream ss;
34 ss << std::setprecision(digits);
35 ss << std::fixed;
36 ss << val;
37 return ss.str();
38 }
39
40 static std::string content;
41 boost::filesystem::path path_to_content;
42
43 struct content_loader
44 {
45 content_loader()
46 {
47 boost::filesystem::path p(__FILE__);
48 p = p.parent_path();
49 p /= "doc";
50 p /= "performance_tables.qbk";
51 path_to_content = p;
52 if(boost::filesystem::exists(p))
53 {
54 boost::filesystem::ifstream is(p);
55 if(is.good())
56 {
57 do
58 {
59 char c = static_cast<char>(is.get());
60 if(c != EOF)
61 content.append(1, c);
62 } while(is.good());
63 }
64 }
65 }
66 ~content_loader()
67 {
68 boost::filesystem::ofstream os(path_to_content);
69 os << content;
70 }
71 void instantiate()const
72 {
73 }
74 };
75
76 static const content_loader loader;
77
78 void load_table(std::vector<std::vector<std::string> >& table, std::string::const_iterator begin, std::string::const_iterator end)
79 {
80 static const boost::regex item_e(
81 "\\["
82 "([^\\[\\]]*(?0)?)*"
83 "\\]"
84 );
85
86 boost::regex_token_iterator<std::string::const_iterator> i(begin, end, item_e), j;
87
88 while(i != j)
89 {
90 // Add a row:
91 table.push_back(std::vector<std::string>());
92 boost::regex_token_iterator<std::string::const_iterator> k(i->first + 1, i->second - 1, item_e);
93 while(k != j)
94 {
95 // Add a cell:
96 table.back().push_back(std::string(k->first + 1, k->second - 1));
97 ++k;
98 }
99 ++i;
100 }
101 }
102
103 std::string save_table(std::vector<std::vector<std::string> >& table)
104 {
105 std::string result;
106
107 for(std::vector<std::vector<std::string> >::const_iterator i = table.begin(), j = table.end(); i != j; ++i)
108 {
109 result += "[";
110 for(std::vector<std::string>::const_iterator k = i->begin(), l = i->end(); k != l; ++k)
111 {
112 result += "[";
113 result += *k;
114 result += "]";
115 }
116 result += "]\n";
117 }
118 return result;
119 }
120
121 void add_to_all_sections(const std::string& id, std::string list_name = "performance_all_sections")
122 {
123 std::string::size_type pos = content.find("[template " + list_name + "[]"), end_pos;
124 if(pos == std::string::npos)
125 {
126 //
127 // Just append to the end:
128 //
129 content.append("\n[template ").append(list_name).append("[]\n[").append(id).append("]\n]\n");
130 }
131 else
132 {
133 //
134 // Read in the all list of sections, add our new one (in alphabetical order),
135 // and then rewrite the whole thing:
136 //
137 static const boost::regex item_e(
138 "\\["
139 "((?=[^\\]])[^\\[\\]]*+(?0)?+)*+"
140 "\\]|\\]"
141 );
142 boost::regex_token_iterator<std::string::const_iterator> i(content.begin() + pos + 12 + list_name.size(), content.end(), item_e), j;
143 std::set<std::string> sections;
144 while(i != j)
145 {
146 if(i->length() == 1)
147 {
148 end_pos = i->first - content.begin();
149 break;
150 }
151 sections.insert(std::string(i->first + 1, i->second - 1));
152 ++i;
153 }
154 sections.insert(id);
155 std::string new_list = "\n";
156 for(std::set<std::string>::const_iterator sec = sections.begin(); sec != sections.end(); ++sec)
157 {
158 new_list += "[" + *sec + "]\n";
159 }
160 content.replace(pos + 12 + list_name.size(), end_pos - pos - 12 - list_name.size(), new_list);
161 }
162 }
163
164 std::string get_colour(boost::uintmax_t val, boost::uintmax_t best)
165 {
166 if(val <= best * 1.2)
167 return "green";
168 if(val > best * 4)
169 return "red";
170 return "blue";
171 }
172
173 boost::intmax_t get_value_from_cell(const std::string& cell)
174 {
175 static const boost::regex time_e("(\\d+)ns");
176 boost::smatch what;
177 if(regex_search(cell, what, time_e))
178 {
179 return boost::lexical_cast<boost::uintmax_t>(what.str(1));
180 }
181 return -1;
182 }
183
184 void add_cell(boost::intmax_t val, const std::string& table_name, const std::string& row_name, const std::string& column_heading)
185 {
186 //
187 // Load the table, add our data, and re-write:
188 //
189 std::string table_id = "table_" + sanitize_string(table_name);
190 boost::regex table_e("\\[table:" + table_id
191 + "\\s(?:[^\\[]|\\\\.)++"
192 "((\\["
193 "((?:[^\\[\\]]|\\\\.)*+(?2)?+)*+"
194 "\\]\\s*+)*+\\s*+)"
195 "\\]"
196 );
197
198 boost::smatch table_location;
199 if(regex_search(content, table_location, table_e))
200 {
201 std::vector<std::vector<std::string> > table_data;
202 load_table(table_data, table_location[1].first, table_location[1].second);
203 //
204 // Figure out which column we're on:
205 //
206 unsigned column_id = 1001u;
207 for(unsigned i = 0; i < table_data[0].size(); ++i)
208 {
209 if(table_data[0][i] == column_heading)
210 {
211 column_id = i;
212 break;
213 }
214 }
215 if(column_id > 1000)
216 {
217 //
218 // Need a new column, must be adding a new compiler to the table!
219 //
220 table_data[0].push_back(column_heading);
221 for(unsigned i = 1; i < table_data.size(); ++i)
222 table_data[i].push_back(std::string());
223 column_id = table_data[0].size() - 1;
224 }
225 //
226 // Figure out the row:
227 //
228 unsigned row_id = 1001;
229 for(unsigned i = 1; i < table_data.size(); ++i)
230 {
231 if(table_data[i][0] == row_name)
232 {
233 row_id = i;
234 break;
235 }
236 }
237 if(row_id > 1000)
238 {
239 //
240 // Need a new row, add it now:
241 //
242 table_data.push_back(std::vector<std::string>());
243 table_data.back().push_back(row_name);
244 for(unsigned i = 1; i < table_data[0].size(); ++i)
245 table_data.back().push_back(std::string());
246 row_id = table_data.size() - 1;
247 }
248 //
249 // Find the best result in this row:
250 //
251 boost::uintmax_t best = (std::numeric_limits<boost::uintmax_t>::max)();
252 std::vector<boost::intmax_t> values;
253 for(unsigned i = 1; i < table_data[row_id].size(); ++i)
254 {
255 if(i == column_id)
256 {
257 if(val < best)
258 best = val;
259 values.push_back(val);
260 }
261 else
262 {
263 std::cout << "Existing cell value was " << table_data[row_id][i] << std::endl;
264 boost::uintmax_t cell_val = get_value_from_cell(table_data[row_id][i]);
265 std::cout << "Extracted value: " << cell_val << std::endl;
266 if(cell_val < best)
267 best = cell_val;
268 values.push_back(cell_val);
269 }
270 }
271 //
272 // Update the row:
273 //
274 for(unsigned i = 1; i < table_data[row_id].size(); ++i)
275 {
276 std::string& s = table_data[row_id][i];
277 s = "[role ";
278 if(values[i - 1] < 0)
279 {
280 s += "grey -]";
281 }
282 else
283 {
284 s += get_colour(values[i - 1], best);
285 s += " ";
286 s += format_precision(static_cast<double>(values[i - 1]) / best, 2);
287 s += "[br](";
288 s += boost::lexical_cast<std::string>(values[i - 1]) + "ns)]";
289 }
290 }
291 //
292 // Convert back to a string and insert into content:
293 std::sort(table_data.begin() + 1, table_data.end(), [](std::vector<std::string> const& a, std::vector<std::string> const& b) { return a[0] < b[0]; } );
294 std::string c = save_table(table_data);
295 content.replace(table_location.position(1), table_location.length(1), c);
296 }
297 else
298 {
299 //
300 // Create a new table and try again:
301 //
302 std::string new_table = "\n[template " + table_id;
303 new_table += "[]\n[table:" + table_id;
304 new_table += " ";
305 new_table += table_name;
306 new_table += "\n[[Expression[br]Text][";
307 new_table += column_heading;
308 new_table += "]]\n";
309 new_table += "[[";
310 new_table += row_name;
311 new_table += "][[role blue 1.00[br](";
312 new_table += boost::lexical_cast<std::string>(val);
313 new_table += "ns)]]]\n]\n]\n";
314
315 std::string::size_type pos = content.find("[/tables:]");
316 if(pos != std::string::npos)
317 content.insert(pos + 10, new_table);
318 else
319 content += "\n\n[/tables:]\n" + new_table;
320 //
321 // Add a section for this table as well:
322 //
323 std::string section_id = "section_" + sanitize_string(table_name);
324 if(content.find(section_id + "[]") == std::string::npos)
325 {
326 std::string new_section = "\n[template " + section_id + "[]\n[section:" + section_id + " " + table_name + "]\n[" + table_id + "]\n[endsect]\n]\n";
327 pos = content.find("[/sections:]");
328 if(pos != std::string::npos)
329 content.insert(pos + 12, new_section);
330 else
331 content += "\n\n[/sections:]\n" + new_section;
332 add_to_all_sections(section_id);
333 }
334 //
335 // Add to list of all tables (not in sections):
336 //
337 add_to_all_sections(table_id, "performance_all_tables");
338 }
339 }
340
341 void report_execution_time(double t, std::string table, std::string row, std::string heading)
342 {
343 try {
344 add_cell(static_cast<boost::uintmax_t>(t / 1e-9), table, row, heading);
345 }
346 catch(const std::exception& e)
347 {
348 std::cout << "Error in adding cell: " << e.what() << std::endl;
349 throw;
350 }
351 }
352
353 std::string boost_name()
354 {
355 return "boost " + boost::lexical_cast<std::string>(BOOST_VERSION / 100000) + "." + boost::lexical_cast<std::string>((BOOST_VERSION / 100) % 1000);
356 }
357
358 std::string compiler_name()
359 {
360 #ifdef COMPILER_NAME
361 return COMPILER_NAME;
362 #else
363 return BOOST_COMPILER;
364 #endif
365 }
366
367 std::string platform_name()
368 {
369 #ifdef _WIN32
370 return "Windows x64";
371 #else
372 return BOOST_PLATFORM;
373 #endif
374 }
375
376
377 std::string get_compiler_options_name()
378 {
379 #if defined(BOOST_MSVC) || defined(__ICL)
380 std::string result;
381 #ifdef BOOST_MSVC
382 result = "cl ";
383 #else
384 result = "icl ";
385 #endif
386 #ifdef _M_AMD64
387 #ifdef __AVX__
388 result += "/arch:AVX /Ox";
389 #else
390 result += "/Ox";
391 #endif
392 result += " (x64 build)";
393 #else
394 #ifdef _DEBUG
395 result += "/Od";
396 #elif defined(__AVX2__)
397 result += "/arch:AVX2 /Ox";
398 #elif defined(__AVX__)
399 result += "/arch:AVX /Ox";
400 #elif _M_IX86_FP == 2
401 result += "/arch:sse2 /Ox";
402 #else
403 result += "/arch:ia32 /Ox";
404 #endif
405 result += " (x86 build)";
406 #endif
407 std::cout << "Compiler options are found as: " << result << std::endl;
408 return result;
409 #else
410 return "Unknown";
411 #endif
412 }
413