| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <exploragram/hexdom/time_log.h> | ||
| 41 | |||
| 42 | LogTime logt ; | ||
| 43 | |||
| 44 | ✗ | bool LogTime::is_start_section(unsigned int i) { return check[i].right != i + 1; } | |
| 45 | ✗ | bool LogTime::is_end_section(unsigned int i) { return check[i].n == "end section"; } | |
| 46 | ✗ | bool LogTime::is_final(unsigned int i) { return i + 1 == check.size(); } | |
| 47 | ✗ | double LogTime::time(unsigned int i) { return double(check[check[i].right].t - check[i].t) / double(CLOCKS_PER_SEC); } | |
| 48 | |||
| 49 | |||
| 50 | ✗ | unsigned int LogTime::dec(unsigned int i){ | |
| 51 | ✗ | if (i == (unsigned int)(-1)) | |
| 52 | ✗ | i = (unsigned int)(check.size() - 1); | |
| 53 | unsigned int res = 0; | ||
| 54 | ✗ | i = check[i].up; | |
| 55 | ✗ | while (i != (unsigned int)(-1)) { | |
| 56 | ✗ | res++; | |
| 57 | ✗ | i = check[i].up; | |
| 58 | } | ||
| 59 | ✗ | return res; | |
| 60 | } | ||
| 61 | |||
| 62 | ✗ | unsigned int LogTime::lastdec(){ | |
| 63 | ✗ | if (!check.empty()) | |
| 64 | ✗ | return dec((unsigned int)(check.size() - 1)); | |
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | ✗ | void LogTime::debug(){ | |
| 69 | ✗ | GEO::Logger::out("HexDom") << std::endl; | |
| 70 | ✗ | GEO::Logger::out("HexDom") << "--------------BEGIN DEBUG-------------------" << std::endl; | |
| 71 | ✗ | for (size_t i = 0; i < check.size(); i++) { | |
| 72 | ✗ | std::cerr << std::string((unsigned int)(4 * dec((unsigned int)(i))), ' ') << i << " r = " << check[i].right << " u = " << check[i].up | |
| 73 | ✗ | << "\tstart" << check[i].t << "\tname" << check[i].n << std::endl; | |
| 74 | } | ||
| 75 | ✗ | GEO::Logger::out("HexDom") << std::endl; | |
| 76 | ✗ | GEO::Logger::out("HexDom") <<"-------------- END DEBUG-------------------" << std::endl; | |
| 77 | ✗ | } | |
| 78 | |||
| 79 | ✗ | std::string LogTime::cur_stack(){ | |
| 80 | std::string res; | ||
| 81 | std::vector<unsigned int> stack; | ||
| 82 | { | ||
| 83 | ✗ | unsigned int i = (unsigned int)(check.size() - 1); | |
| 84 | ✗ | while (i != (unsigned int)(-1)) { stack.push_back(i); i = check[i].up; } | |
| 85 | } | ||
| 86 | ✗ | for (int i = int(stack.size()) - 1; i >= 0; i--) { | |
| 87 | ✗ | res.append(check[stack[size_t(i)]].n); | |
| 88 | ✗ | if (i > 0) res.append(" ==> "); | |
| 89 | } | ||
| 90 | ✗ | return res; | |
| 91 | } | ||
| 92 | |||
| 93 | ✗ | void LogTime::report(std::ostream &out, unsigned int timing_depth){ | |
| 94 | ✗ | if (check.empty()) return; | |
| 95 | ✗ | if (check.back().n != "the end") { add_step("the end"); } | |
| 96 | |||
| 97 | ✗ | if (timing_depth != (unsigned int)(-1)) { | |
| 98 | out << "\n***********************************************************" << std::endl; | ||
| 99 | out << " TIMING SUMMARY " << std::endl; | ||
| 100 | ✗ | for (unsigned int i = 0; i < check.size() - 1; i++){ | |
| 101 | ✗ | if (dec(i)>timing_depth) continue; | |
| 102 | ✗ | if (is_start_section(i)) | |
| 103 | ✗ | out << std::string(4 * dec(i), ' ') << time(i) << "\t====> " << check[i].n << std::endl; | |
| 104 | ✗ | else if (!is_end_section(i)){ | |
| 105 | ✗ | if (check[i].n != "begin section"){ | |
| 106 | ✗ | out << std::string(4 * dec(i), ' ') << time(i) << "\t" << check[i].n << std::endl; | |
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | ✗ | out << double(check[check.size() - 1].t - check[0].t) / double(CLOCKS_PER_SEC) << "\tTOTAL" << std::endl; | |
| 112 | } | ||
| 113 | out << "\n***********************************************************" << std::endl; | ||
| 114 | out << " OUPUT VALUES" << std::endl; | ||
| 115 | ✗ | for (size_t i = 0; i < out_values.size(); i++) | |
| 116 | ✗ | out << out_values[i].second << " \t" << out_values[i].first << std::endl; | |
| 117 | |||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | ✗ | void LogTime::report_py(std::ostream &out, unsigned int timing_depth){ | |
| 123 | ✗ | if (check.empty()) return; | |
| 124 | ✗ | if (check.back().n != "the end") { add_step("the end"); } | |
| 125 | |||
| 126 | ✗ | out << "{ \"charlie\": 2"; | |
| 127 | // bool first = true; | ||
| 128 | ✗ | if (timing_depth != (unsigned int)(-1)) { | |
| 129 | ✗ | for (unsigned int i = 0; i < check.size() - 1; i++){ | |
| 130 | ✗ | if (dec(i)>timing_depth) continue; | |
| 131 | ✗ | if (is_start_section(i)) out <<",\"TIME_" << check[i].n <<"\" : "<< time(i); | |
| 132 | ✗ | else if (!is_end_section(i)){ | |
| 133 | ✗ | if (check[i].n != "begin section"){ | |
| 134 | ✗ | out << ",\"TIME_" << check[i].n << "\": " << time(i) ; | |
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | //out << "\ttest[\'TIME_TOTAL\'] = " << double(check[check.size() - 1].t - check[0].t) / double(CLOCKS_PER_SEC) << std::endl; | ||
| 140 | } | ||
| 141 | ✗ | for (size_t i = 0; i < out_values.size(); i++) | |
| 142 | ✗ | out << ", \"" << out_values[i].first << "\": " << out_values[i].second ; | |
| 143 | ✗ | for (size_t i = 0; i < out_strings.size(); i++) | |
| 144 | ✗ | out << ",\"" << out_strings[i].first << "\": \"" << out_strings[i].second <<"\""; | |
| 145 | out << "}" << std::endl; | ||
| 146 | |||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | // construct API | ||
| 154 | |||
| 155 | |||
| 156 | ✗ | void LogTime::add_value(std::string str, double val) { | |
| 157 | ✗ | GEO::Logger::out("HexDom") << "log new value : " << str<< post_fix << " = " << val << std::endl; | |
| 158 | ✗ | out_values.push_back(std::pair<std::string, double>(str+ post_fix, val)); | |
| 159 | ✗ | } | |
| 160 | ✗ | void LogTime::add_string(std::string str , std::string val) { | |
| 161 | ✗ | GEO::Logger::out("HexDom") << "log new string : " << str << post_fix << " = " << val << std::endl; | |
| 162 | ✗ | out_strings.push_back(std::pair<std::string, std::string>(str+ post_fix, val)); | |
| 163 | ✗ | } | |
| 164 | |||
| 165 | ✗ | void LogTime::add_step(const std::string& name){ | |
| 166 | ✗ | if (check.empty()){ | |
| 167 | ✗ | CheckPoint c(name + post_fix, (unsigned int)(-1)); | |
| 168 | ✗ | check.push_back(c); | |
| 169 | } | ||
| 170 | else { | ||
| 171 | ✗ | CheckPoint c(name + post_fix, check.back().up); | |
| 172 | ✗ | check.back().right = (unsigned int)(check.size()); | |
| 173 | ✗ | check.push_back(c); | |
| 174 | } | ||
| 175 | const char *symbol = "#>=_-~.............."; | ||
| 176 | ✗ | GEO::Logger::out("HexDom") << std::endl << std::string(4 * lastdec(), ' ') << std::string(80 - 4 * lastdec(), symbol[dec()]) << std::endl; | |
| 177 | ✗ | GEO::Logger::out("HexDom") << std::string(4 * lastdec() + 4, ' ') << cur_stack() << std::endl << std::endl; | |
| 178 | ✗ | } | |
| 179 | |||
| 180 | ✗ | void LogTime::start_section(const std::string& secname, const std::string& name ){ | |
| 181 | ✗ | add_step(secname + post_fix); | |
| 182 | ✗ | CheckPoint c(name + post_fix, (unsigned int)(check.size()) - 1); | |
| 183 | ✗ | check.push_back(c); | |
| 184 | ✗ | } | |
| 185 | ✗ | void LogTime::end_section(){ | |
| 186 | ✗ | unsigned int u = check.back().up; | |
| 187 | ✗ | check[u].right = (unsigned int)(check.size()); | |
| 188 | ✗ | check.back().right = (unsigned int)(check.size()); | |
| 189 | ✗ | CheckPoint c("end section", check[u].up); | |
| 190 | ✗ | check.push_back(c); | |
| 191 | ✗ | } | |
| 192 | |||
| 193 | |||
| 194 | // output | ||
| 195 | ✗ | void LogTime::show(){ | |
| 196 | ✗ | report(std::cerr); | |
| 197 | ✗ | } | |
| 198 | |||
| 199 | ✗ | void LogTime::drop_file(std::string filename, bool append, unsigned int timing_depth){ | |
| 200 | ✗ | std::ofstream f; | |
| 201 | ✗ | if (append) | |
| 202 | ✗ | f.open(filename.c_str(), std::fstream::app); | |
| 203 | else | ||
| 204 | ✗ | f.open(filename.c_str()); | |
| 205 | ✗ | report_py(f, timing_depth); | |
| 206 | ✗ | f.close(); | |
| 207 | ✗ | } | |
| 208 |