table_helper.cpp 11 KB

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