chromium/tools/clang/scripts/include-analysis.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Chrome #include Analysis</title>

    <!--Generated by analyze_includes.py. Provides the 'data' object.-->
    <script src="include-analysis.js"></script>

    <style>
    tr td { text-align: right; }
    tr td:nth-child(1) { text-align: left; }
    tr td:nth-child(2) { text-align: left; }
    table#edges tr td:nth-child(3) { text-align: left; }
    tbody tr:hover { background-color: #dddddd; }
    th { position: sticky; top: 0; background-color: #ffffff }
    table#files th:nth-child(n+2) { cursor: pointer; }
    th.highlighted { background-color: #dddddd }
    th.reversed::after { content: " \2303"; }
    #filterResults, #edgeFilterResults { font-weight: bold }
    </style>
  </head>
  <body>

<h1>Chrome #include Analysis (go/chrome-includes) Beta</h1>

<p>Build target: <span id="buildTarget">x</span> (Linux). Revision: <span id="buildRevision">x</span>. Analyzed on: <span id="analysisDate">x</span>.</p>

<p>Number of translation units: <span id="numRoots">x</span>. Total build size (sum of expanded translation unit sizes): <span id="totBuildSize">x</span> bytes.</p>

<p>Number of files: <span id="numFiles">x</span>. Total file size: <span id="totFileSize">x</span> bytes.</p>

<p><a href="https://commondatastorage.googleapis.com/chromium-browser-clang/chrome_includes-index.html">Archive</a></p>

<button onClick="changeState({view: 'files'})">Per-File Analysis</button>
<button onClick="changeState({view: 'edges'})">Per-Edge Analysis</button>
<label for="limit">Display limit: </label>
<input type="number" id="limit" name="limit" onchange="changeState({limit: event.target.value})">

<hr>

<div id="filesview">

<h2>Per-File Analysis</h2>

<p>
<label for="filter">Filter: (regex)</label>
<input type="text" id="filter" name="filter" size="60">
<button onClick="changeState({filter: document.getElementById('filter').value})" id="apply">Apply</button>
<button onClick="changeState({filter: ''})">Clear</button>
<span id="filterResults"></span>
</p>

<table border="1" id="files">
  <thead>
    <tr>
      <th>#</th>
      <th id="filename" onclick="changeState({sort: 'filename'})">Filename</th>
      <th id="isize" colspan="2" onclick="changeState({sort: 'isize'})" title="The size of the individual file. Also shown as percentage of the total file size.">Individual Size (B) &#9432;</th>
      <th id="tsize" colspan="2" onclick="changeState({sort: 'tsize'})" title="The size of the file and all the files it includes, directly and indirectly. Also shown as percentage of the total build size.">Expanded Size (B) &#9432;</th>
      <th id="asize" colspan="2" onclick="changeState({sort: 'asize'})" title="The size added by this file being part of the build. In other words, if this file were empty and had no includes, how much smaller would the build be. Also shown as percentage of the total build size.">Added Size (B) &#9432;</th>
      <th id="prevalence" colspan="2" onclick="changeState({sort: 'prevalence'})" title="Number of translation units that this file is part of. Also shown as percentage of the number of translation units.">Occurrences &#9432;</th>
      <th id="includedby" onclick="changeState({sort: 'includedby'})" title="In how many files this file is included directly. Click the number to see the files.">Directly Included In &#9432;</th>
      <th id="includes" onclick="changeState({sort: 'includes'})" title="How many files this file includes directly. Click the number to see the files.">Direct Includes &#9432;</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

</div>

<div id="edgesview" style="display: none">

<h2>Per-Edge Analysis</h2>

<p>
<label for="includerFilter">Includer (regex):</label>
<input type="text" id="includerFilter" name="includerFilter" size="60">

<label for="includedFilter">Included (regex):</label>
<input type="text" id="includedFilter" name="includedFilter" size="60">

<button onClick="changeState({includer: document.getElementById('includerFilter').value, included: document.getElementById('includedFilter').value})" id="applyEdgeFilter">Apply Filter</button>
<button onClick="changeState({includer: '', included: ''})">Clear</button>
<span id="edgeFilterResults"></span>
</p>

<table border="1" id="edges">
  <thead>
    <tr>
      <th>#</th>
      <th>Includer</th>
      <th>Included</th>
      <th colspan="2" title="The size added by this include edge being part of the build. In other words, if this include edge didn't exist, how much smaller would the build be. Also shown as percentage of the total build size.">Added Size (B) &#9432;</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

</div>

<hr>

<p>File size does not correlate perfectly with compile time, but can serve as a rough guide to what files are slow to compile.</p>

<p>Analysis by <a href="https://source.chromium.org/chromium/chromium/src/+/HEAD:tools/clang/scripts/analyze_includes.py">analyze_includes.py</a>.</p>

<script>
"use strict";

document.getElementById('filter').addEventListener('keyup', function(event) {
  if (event.keyCode == 13) {
    document.getElementById('apply').click();
  }
});

document.getElementById('includerFilter').addEventListener('keyup', function(event) {
  if (event.keyCode == 13) {
    document.getElementById('applyEdgeFilter').click();
  }
});
document.getElementById('includedFilter').addEventListener('keyup', function(event) {
  if (event.keyCode == 13) {
    document.getElementById('applyEdgeFilter').click();
  }
});

function sum(arr) {
  return arr.reduce((a, b) => a + b, 0);
}

const numberFormat = new Intl.NumberFormat('en-US');
function fmt(num) {
  return numberFormat.format(num);
}

function regexEscape(str) {
  str = str.replaceAll('+', '\\+');
  str = str.replaceAll('.', '\\.');
  return str;
}

const totFileSize = sum(data.sizes);
const totBuildSize = sum(data.roots.map(r => data.tsizes[r]));
const numRoots = data.roots.length;

document.getElementById('buildTarget').textContent = data.target;
document.getElementById('buildRevision').innerHTML =
    `<a href="https://chromium.googlesource.com/chromium/src/+/${data.revision}">${data.revision}</a>`;
document.getElementById('analysisDate').textContent = data.date;

document.getElementById('numRoots').textContent = fmt(numRoots);
document.getElementById('totBuildSize').textContent = fmt(totBuildSize);
document.getElementById('numFiles').textContent = fmt(data.files.length);
document.getElementById('totFileSize').textContent = fmt(totFileSize);

function updateView() {
  let s = getState();

  document.getElementById('filesview').style = 'display: none';
  document.getElementById('edgesview').style = 'display: none';

  if (s.get('view') == 'files') {
    document.getElementById('filesview').style = 'display: block';
    buildFilesTable();
  }
  if (s.get('view') == 'edges') {
    document.getElementById('edgesview').style = 'display: block';
    buildEdgesTable();
  }

  document.getElementById('limit').value = s.get('limit')
}

function intersect(arr1, arr2) {
  let s2 = new Set(arr2);
  return arr1.filter(x => s2.has(x));
}

function buildFilesTable() {
  let fileNums = [...Array(data.files.length).keys()];
  const state = getState();

  const filter = state.get('filter');
  document.getElementById('filter').value = filter;

  if (filter !== '') {
    const re = new RegExp(filter);
    fileNums = fileNums.filter(i => data.files[i].match(re));

    document.getElementById('filterResults').innerHTML =
        `${fmt(fileNums.length)} result${fileNums.length == 1 ? '' : 's'}.`;
  } else {
    document.getElementById('filterResults').innerHTML = '';
  }

  const sortFuncs = {
    filename: (i, j) => data.files[i].localeCompare(data.files[j]),
    isize: (i, j) => data.sizes[j] - data.sizes[i],
    tsize: (i, j) => data.tsizes[j] - data.tsizes[i],
    prevalence: (i, j) => data.prevalence[j] - data.prevalence[i],
    includedby: (i, j) => data.included_by[j].length - data.included_by[i].length,
    includes: (i, j) => data.includes[j].length - data.includes[i].length,
    asize: (i, j) => data.asizes[j] - data.asizes[i],
  };

  document.querySelectorAll('th').forEach(th => th.classList.remove('highlighted', 'reversed'));

  document.getElementById(state.get('sort')).classList.add('highlighted', ...(state.get('reverse') ? ['reversed'] : []));
  fileNums.sort(sortFuncs[state.get('sort')]);
  if (state.get('reverse')) {
    fileNums.reverse();
  }

  const limit = Math.min(parseInt(state.get('limit')), fileNums.length);
  fileNums = fileNums.slice(0, limit);

  function buildRow(i, rank) {
    return `
<tr>
<td>${rank + 1}</td>
<td>
  <a href="#${changedStateHash({filter: '^' + regexEscape(data.files[i] + '$')})}">${data.files[i]}</a>
  [<a href="https://source.chromium.org/chromium/chromium/src/+/HEAD:${data.files[i]}">cs</a>]
</td>
<td>${fmt(data.sizes[i])}</td> <td>${(100 * data.sizes[i] / totFileSize).toFixed(2)}&nbsp;%</td>
<td>${fmt(data.tsizes[i])}</td> <td>${(100 * data.tsizes[i] / totBuildSize).toFixed(2)}&nbsp;%</td>
<td>${fmt(data.asizes[i])}</td> <td>${(100 * data.asizes[i] / totBuildSize).toFixed(2)}&nbsp;%</td>
<td>${fmt(data.prevalence[i])}</td> <td>${(100 * data.prevalence[i] / numRoots).toFixed(2)}&nbsp;%</td>
<td><a href="#${changedStateHash({view: 'edges', includer: '', included: '^' + regexEscape(data.files[i]) + '$'})}">${fmt(data.included_by[i].length)}</a></td>
<td><a href="#${changedStateHash({view: 'edges', included: '', includer: '^' + regexEscape(data.files[i]) + '$'})}">${fmt(data.includes[i].length)}</a></td>
</tr>
`;
  }

  const tbody = document.querySelector('table#files tbody');
  tbody.innerHTML = fileNums.map(buildRow).join('');
}


function buildEdgesTable() {
  const state = getState();
  const includerFilter = state.get('includer');
  const includedFilter = state.get('included');
  document.getElementById('includerFilter').value = includerFilter;
  document.getElementById('includedFilter').value = includedFilter;

  const includerRe = new RegExp(includerFilter);
  const includedRe = new RegExp(includedFilter);

  let edgeSizes = [];
  for (let src = 0; src < data.esizes.length; src++) {
    for (let dst = 0; dst < data.esizes[src].length; dst++) {
      const includer = src;
      const included = data.includes[src][dst];
      const esize = data.esizes[src][dst];

      if (!data.files[includer].match(includerRe))
        continue;
      if (!data.files[included].match(includedRe))
        continue;

      edgeSizes.push([src, data.includes[src][dst], data.esizes[src][dst]]);
    }
  }

  if (includerFilter == '' && includedFilter == '') {
    document.getElementById('edgeFilterResults').innerHTML = '';
  } else {
    document.getElementById('edgeFilterResults').innerHTML =
        `${fmt(edgeSizes.length)} result${edgeSizes.length == 1 ? '' : 's'}.`;
  }

  function buildRow(edgeNum) {
    const src  = edgeSizes[edgeNum][0];
    const dst  = edgeSizes[edgeNum][1];
    const size = edgeSizes[edgeNum][2];
    return `
<tr>
<td>${edgeNum + 1}</td>
<td>
  <a href="#${changedStateHash({view: 'files', filter: '^' + regexEscape(data.files[src]) + '$'})}">${data.files[src]}</a>
  [<a href="https://source.chromium.org/chromium/chromium/src/+/HEAD:${data.files[src]}">cs</a>]
</td>
<td>
  <a href="#${changedStateHash({view: 'files', filter: '^' + regexEscape(data.files[dst]) + '$'})}">${data.files[dst]}</a>
  [<a href="https://source.chromium.org/chromium/chromium/src/+/HEAD:${data.files[dst]}">cs</a>]
</td>
<td>${fmt(size)}</td> <td>${(100 * size / totBuildSize).toFixed(2)}&nbsp;%</td>
</tr>
`;
  }




  edgeSizes.sort((x, y) => y[2] - x[2]);

  const limit = Math.min(parseInt(state.get('limit')), edgeSizes.length);
  const edgeNums = [...Array(limit).keys()];

  const tbody = document.querySelector('table#edges tbody');
  tbody.innerHTML = edgeNums.map(buildRow).join('');
  document.querySelector('table#edges').appendChild(tbody);
}

function getState() {
  let params = new URLSearchParams(window.location.hash.substring(1));

  if (!params.has('view'))
    params.set('view', 'files');
  if (!params.has('filter'))
    params.set('filter', '');
  if (!params.has('sort'))
    params.set('sort', 'asize');
  if (!params.has('reverse'))
    params.set('reverse', '');
  if (!params.has('includer'))
    params.set('includer', '');
  if (!params.has('included'))
    params.set('included', '');
  if (!params.has('limit'))
    params.set('limit', '1000');

  return params;
}

function changedStateHash(changes) {
  let s = getState();
  for (const [key, value] of Object.entries(changes)) {
    if (key === 'sort') {
      if (value === s.get('sort')) {
        s.set('reverse', s.get('reverse') ? '' : '1');
        continue;
      } else {
        s.set('reverse', '');
      }
    }

    s.set(key, value);
  }
  return s.toString();
}

function changeState(changes) {
  window.location.hash = changedStateHash(changes);
}

window.onhashchange = updateView;
updateView();

</script>
  </body>
</html>