| 119 | } |
| 120 | |
| 121 | XMLWriter to_svg() const { |
| 122 | using namespace std::chrono_literals; |
| 123 | XMLWriter writer; |
| 124 | auto&& [addr_begin, addr_end] = address_range(); |
| 125 | auto&& [time_begin, time_end] = time_range(); |
| 126 | writer.doctype( |
| 127 | "svg", "PUBLIC", |
| 128 | {"\"-//W3C//DTD SVG 1.1//EN\"", |
| 129 | "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\""}); |
| 130 | auto svg = writer.element("svg"); |
| 131 | svg.attr("xmlns", std::string{"http://www.w3.org/2000/svg"}); |
| 132 | svg.attr("xmlns:tag", std::string{"https://megengine.org.cn"}); |
| 133 | auto time_scale = 100us; |
| 134 | double addr_scale = 1e6; |
| 135 | svg.attr("width", (time_end - time_begin) / time_scale); |
| 136 | svg.attr("height", (addr_end - addr_begin) / addr_scale); |
| 137 | { |
| 138 | auto rect = writer.element("rect"); |
| 139 | rect.attr("x", 0); |
| 140 | rect.attr("y", 0); |
| 141 | rect.attr("width", (time_end - time_begin) / time_scale); |
| 142 | rect.attr("height", (addr_end - addr_begin) / addr_scale); |
| 143 | rect.attr("fill", std::string{"blue"}); |
| 144 | } |
| 145 | double us = 1e3, ms = 1e6; |
| 146 | std::map<double, std::string> time2color = { |
| 147 | {0 * us, "#DDDDDD"}, |
| 148 | {100 * us, "#CCCCCC"}, |
| 149 | {1 * ms, "#BBBBBB"}, |
| 150 | {10 * ms, "#AAAAAA"}, |
| 151 | {100 * ms, "#999999"}, |
| 152 | {1000 * ms, "#888888"}, |
| 153 | {std::numeric_limits<double>::infinity(), "#555555"}, |
| 154 | }; |
| 155 | auto time2str = [](profiler::Duration ns) { |
| 156 | using pair_t = std::pair<uint64_t, const char*>; |
| 157 | static pair_t units[] = { |
| 158 | {1, "ns "}, |
| 159 | {1e3, "us "}, |
| 160 | {1e6, "ms "}, |
| 161 | {1e9, "s "}, |
| 162 | }; |
| 163 | std::string builder; |
| 164 | auto comparator = [](const pair_t& lhs, const pair_t& rhs) { |
| 165 | return lhs.first < rhs.first; |
| 166 | }; |
| 167 | while (ns.count() > 0) { |
| 168 | auto iter = std::upper_bound( |
| 169 | std::begin(units), std::end(units), |
| 170 | std::make_pair(ns.count(), ""), comparator) - |
| 171 | 1; |
| 172 | builder += std::to_string(ns.count() / iter->first) + iter->second; |
| 173 | ns = ns % iter->first; |
| 174 | } |
| 175 | return builder; |
| 176 | }; |
| 177 | auto size2str = [](size_t sz) { |
| 178 | using pair_t = std::pair<size_t, const char*>; |