ic-explorer.html 8.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<html>
  <head>
<style>
  .entry-details { 
  }
  .entry-details TD { 
  }
  .details {
    width: 2em;
    border: 1px black dotted;
  }
  .count {
    text-align: right;
    width: 5em;
    font-family: monospace;
  }
  .percentage { 
    text-align: right;
    width: 5em;
    font-family: monospace;
  }
  .key {
    padding-left: 1em;
  }
25 26 27 28
  .drilldown-group-title {
    font-weight: bold;
    padding: 0.5em 0 0.2em 0;
  }
29 30 31 32 33 34 35 36
</style>
  <script>
"use strict"
var entries = [];

class Entry {
  constructor(id, line) {
    this.id = id;
37
    this.line = line;
38
    var parts = line.split(" ");
39
    if (parts.length < 6) return
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    this.isValid = false;
    if (parts[0][0] !== "[") return;
    if (parts[1] === "patching") return;
    this.type = parts[0].substr(1);
    this.category = "Other";
    if (this.type.indexOf("Store") !== -1) {
      this.category = "Store";
    } else if (this.type.indexOf("Load") !== -1) {
      this.category = "Load";
    }
    if (this.type.length == 0) return;
    if (this.type.indexOf('BinaryOpIC(') === 0) {
      this.type = "BinaryOpIC";
      var split = parts[0].split('(');
      this.state = "(" + split[1] + " => " + parts[2];
55 56 57
      var offset = this.parsePositionAndFile(parts, 6);
      if (offset == -1) return
      if (this.file === undefined) return
58 59
      this.file = this.file.slice(0,-1);
    } else {
60 61 62
      var offset = this.parsePositionAndFile(parts, 2);
      if (offset == -1) return
      this.state = parts[++offset];
63 64
      if (this.type !== "CompareIC") {
        // if there is no address we have a smi key
65
        var address = parts[++offset];
66
        if (address !== undefined && address.indexOf("0x") === 0) {
67
          this.key = parts.slice(++offset).join(" ");
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        } else {
          this.key = address;
        }
      }
    }
    this.filePosition = this.file + " " + this.position;
    if (this.key) {
      var isStringKey = false
      if (this.key.indexOf("<String[") === 0) {
       isStringKey = true;
        this.key = "\"" + this.key.slice(this.key.indexOf(']')+3);
      } else if (this.key.indexOf("<") === 0) {
        this.key = this.key.slice(1);
      }
      if (this.key.endsWith(">]")) {
        this.key = this.key.slice(0, -2);
      } else if (this.key.endsWith("]")) {
        this.key = this.key.slice(0, -1);
      }
      if (isStringKey) {
        this.key = this.key + "\"";
      }
    }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    this.isValid = true;
  }
  
  parsePositionAndFile(parts, start) {
    // find the position of 'at' in the parts array.
    var offset = start;
    for (var i = start+1; i<parts.length; i++) {
      offset++;
      if (parts[i] == 'at') break;
    }
    if (parts[offset] !== 'at') return -1;
    this.position = parts.slice(start, offset).join(' ');
    offset += 1;
    this.isNative = parts[offset] == "native"
    offset += this.isNative ? 1 : 0;
    this.file = parts[offset];
    return offset;
108 109 110
  }
}

111
function loadFile() {
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
  var files = document.getElementById("uploadInput").files;

  var file = files[0];
  var reader = new FileReader();

  reader.onload = function(evt) {
    entries = [];
    var end = this.result.length;
    var current = 0;
    var next = 0;
    var line;
    var i = 0;
    var entry;
    while (current < end) {
      next = this.result.indexOf("\n", current);
      if (next === -1) break;
      i++;
                            
      line = this.result.substring(current, next);
      current = next+1;
      entry = new Entry(i, line);
      if (entry.isValid) entries.push(entry);
    }
                          
    document.getElementById("count").innerHTML = i;
    updateTable();
  }
  reader.readAsText(file);
  initGroupKeySelect(); 
}



var properties = ['type', 'category', 'file', 'filePosition', 'state' , 'key', 'isNative']

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
class Group {
  constructor(property, key, entry) {
    this.property = property;
    this.key = key;
    this.count = 1;
    this.entries = [entry];
    this.percentage = undefined;
    this.groups = undefined;
  }
  
  add(entry) {
    this.count ++;
    this.entries.push(entry)
  }
  
  createSubGroups() {
    this.groups = {};
    for (var i=0; i<properties.length; i++) {
      var subProperty = properties[i];
      if (this.property == subProperty) continue;
      this.groups[subProperty] = groupBy(this.entries, subProperty);
    }
  }
}

function groupBy(entries, property) {
173 174 175 176 177 178 179
  var accumulator = {};
  accumulator.__proto__ = null;
  var length = entries.length;
  for (var i = 0; i < length; i++) {
    var entry = entries[i];
    var key = entry[property];
    if (accumulator[key] == undefined) {
180
      accumulator[key] = new Group(property, key, entry)
181
    } else {
182 183 184
      var group = accumulator[key];
      if (group.entries == undefined) console.log([group, entry]);
      group.add(entry)
185 186 187 188
    }
  }
  var result = []
  for (var key in accumulator) {
189 190 191
    var group = accumulator[key];
    group.percentage = Math.round(group.count / length * 100 * 100) / 100;
    result.push(group);
192 193 194 195 196 197 198 199 200 201 202 203 204 205
  }
  result.sort((a,b) => { return b.count - a.count });
  return result;
}




function updateTable() {
  var select = document.getElementById("group-key");
  var key = select.options[select.selectedIndex].text;
  console.log(key);
  var tableBody = document.getElementById("table-body");
  removeAllChildren(tableBody);
206 207
  var groups = groupBy(entries, key, true);
  display(groups, tableBody);
208 209 210 211 212 213 214 215 216 217 218 219
}

function selecedOption(node) {
 return node.options[node.selectedIndex]
}

function removeAllChildren(node) {
  while (node.firstChild) {
    node.removeChild(node.firstChild);
  }
}

220
function display(entries, parent) {
221 222 223 224 225 226 227
  var fragment = document.createDocumentFragment();

  function td(tr, content, className) { 
    var td = document.createElement("td");
    td.innerHTML = content;
    td.className = className
    tr.appendChild(td);
228
    return td
229
  }
230 231
  var max = Math.min(1000, entries.length)
  for (var i = 0; i<max; i++) {
232 233
    var entry = entries[i];
    var tr = document.createElement("tr");
234 235
    tr.entry = entry;
    td(tr, '<span onclick="toggleDetails(this)">details</a>', 'details');
236 237 238 239
    td(tr, entry.percentage +"%", 'percentage');
    td(tr, entry.count, 'count');
    td(tr, entry.key, 'key');
    fragment.appendChild(tr);
240 241 242 243 244 245 246
  }
  var omitted = entries.length - max;
  if (omitted > 0) {
    var tr = document.createElement("tr");
    var td = td(tr, 'Omitted ' + omitted + " entries.");
    td.colSpan = 4;
    fragment.appendChild(tr);
247 248 249 250
  }
  parent.appendChild(fragment);
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
function displayDrilldown(entry, previousSibling) {
  var tr = document.createElement('tr');
  tr.className = "entry-details";
  tr.style.display = "none";
  // indent by one td.
  tr.appendChild(document.createElement("td"));
  var td = document.createElement("td");
  td.colSpan = 3;
  for (var key in entry.groups) {
    td.appendChild(displayDrilldownGroup(entry, key));
  }
  tr.appendChild(td);
  // Append the new TR after previousSibling.
  previousSibling.parentNode.insertBefore(tr, previousSibling.nextSibling)
}

function displayDrilldownGroup(entry, key) {
  var max = 20;
  var group = entry.groups[key];
  var div = document.createElement("div")
  div.className = 'drilldown-group-title'
  div.innerHTML = key + ' [top ' + max + ']';
  var table = document.createElement("table");
  display(group.slice(0, max), table, false)
  div.appendChild(table);
  return div;
}

279 280
function toggleDetails(node) {
  var tr = node.parentNode.parentNode;
281 282 283 284 285 286 287 288
  var entry = tr.entry;

  // Create subgroup in-place if the don't exist yet.
  if (entry.groups === undefined) {
    entry.createSubGroups();
    displayDrilldown(entry, tr);
  }
  var details = tr.nextSibling;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  var display = details.style.display;
  if (display != "none") {
    display = "none";
  }else {
    display = "table-row"
  };
  details.style.display = display;
}

function initGroupKeySelect() {
  var select = document.getElementById("group-key");
  for (var i in properties) {
    var option = document.createElement("option");
    option.text = properties[i];
    select.add(option);
  }
}

  </script>
  </head>
  <body>
    <h1>
      <span style="color: #00FF00">I</span>
      <span style="color: #FF00FF">C</span>
      <span style="color: #00FFFF">E</span>
    </h1>
    Your IC-Explorer.
    <h2>Usage</h2>
    Run your script with <code>--trace_ic</code> and upload on this page:<br/>
    <code>/path/to/d8 --trace_ic your_script.js > trace.txt</code>
    <h2>Data</h2>
    <form name="fileForm">
      <p>
322
        <input id="uploadInput" type="file" name="files" onchange="loadFile();" >
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        trace entries: <span id="count">0</span>
      </p>
    </form>
    <h2>Result</h2>
    <p>
    Group-Key:
    <select id="group-key" onchange="updateTable()"></select>
    </p>
    <p>
      <table id="table" width="100%">
        <tbody id="table-body"> 
        </tbody>
      </table>
    </p>
  </body>
</html>