aboutsummaryrefslogtreecommitdiffstats
path: root/resources/about-memory.html
blob: 380cee3d6ece6fb4ca42839f0ff76600636f1916 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
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
173
174
175
176
177
<!DOCTYPE html>
<html>
  <head>
    <title>about:memory</title>
    <script>
      document.addEventListener("DOMContentLoaded", start);

      function insertNode(root, report) {
        let currentNode = root;
        for (let path of report.path) {
          if (!currentNode[path]) {
            currentNode[path] = { total: 0, container: true };
          }
          currentNode = currentNode[path];
          currentNode.total += report.size;
        }
        currentNode.size = report.size;
        currentNode.container = false;
      }

      function formatBytes(bytes) {
        if (bytes < 1024) {
          return bytes + " B";
        } else if (bytes < 1024 * 1024) {
          return (bytes / 1024).toFixed(2) + " KiB";
        } else if (bytes < 1024 * 1024 * 1024) {
          return (bytes / (1024 * 1024)).toFixed(2) + " MiB";
        } else {
          return (bytes / (1024 * 1024 * 1024)).toFixed(2) + " GiB";
        }
      }

      function formattedSize(size) {
        // Use enough padding to take into account the "MiB" part.
        return formatBytes(size).padStart(10);
      }

      function convertNodeToDOM(node, name = null) {
        let result = document.createDocumentFragment();

        if (node.container) {
          let details = document.createElement("details");
          let summary = document.createElement("summary");
          summary.textContent = `${formattedSize(node.total)} -- ${name}`;
          details.append(summary);
          result.append(details);

          // Add the children in descending order of total size.
          let entries = Object.entries(node)
            .filter((item) => {
              return !["total", "size", "container"].includes(item[0]);
            })
            .sort((a, b) => b[1].total - a[1].total)
            .forEach((item) =>
              details.append(convertNodeToDOM(item[1], item[0]))
            );
        } else {
          let inner = document.createElement("div");
          inner.textContent = `${formattedSize(node.size)} -- ${name}`;
          result.append(inner);
        }

        return result;
      }

      function start() {
        window.startButton.onclick = async () => {
          let content = await navigator.servo.reportMemory();
          let reports = JSON.parse(content);
          if (reports.error) {
            console.error(reports.error);
            return;
          }
          window.report.innerHTML = "";
          window.report.classList.remove("hidden");

          let explicitRoot = {};
          let nonExplicitRoot = {};

          let jemallocHeapReportedSize = 0;
          let systemHeapReportedSize = 0;

          let jemallocHeapAllocatedSize = NaN;
          let systemHeapAllocatedSize = NaN;

          reports.forEach((report) => {
            // Add "explicit" to the start of the path, when appropriate.
            if (report.kind.startsWith("Explicit")) {
              report.path.unshift("explicit");
            }

            // Update the reported fractions of the heaps, when appropriate.
            if (report.kind == "ExplicitJemallocHeapSize") {
              jemallocHeapReportedSize += report.size;
            } else if (report.kind == "ExplicitSystemHeapSize") {
              systemHeapReportedSize += report.size;
            }

            // Record total size of the heaps, when we see them.
            if (report.path.length == 1) {
              if (report.path[0] == "jemalloc-heap-allocated") {
                jemallocHeapAllocatedSize = report.size;
              } else if (report.path[0] == "system-heap-allocated") {
                systemHeapAllocatedSize = report.size;
              }
            }

            // Insert this report at the proper position.
            insertNode(
              report.kind.startsWith("Explicit")
                ? explicitRoot
                : nonExplicitRoot,
              report
            );
          });

          // Compute and insert the heap-unclassified values.
          if (!isNaN(jemallocHeapAllocatedSize)) {
            insertNode(explicitRoot, {
              path: ["explicit", "jemalloc-heap-unclassified"],
              size: jemallocHeapAllocatedSize - jemallocHeapReportedSize,
            });
          }
          if (!isNaN(systemHeapAllocatedSize)) {
            insertNode(explicitRoot, {
              path: ["explicit", "system-heap-unclassified"],
              size: systemHeapAllocatedSize - systemHeapReportedSize,
            });
          }

          window.report.append(
            convertNodeToDOM(explicitRoot.explicit, "explicit")
          );

          for (let prop in nonExplicitRoot) {
            window.report.append(convertNodeToDOM(nonExplicitRoot[prop], prop));
          }
        };
      }
    </script>
    <style>
      html {
        font-family: sans-serif;
      }

      details,
      details div {
        margin-left: 1em;
      }

      summary:hover {
        cursor: pointer;
      }

      #report {
        line-height: 1.5em;
        border: 2px solid gray;
        border-radius: 10px;
        padding: 5px;
        background-color: lightgray;
      }

      #report > details {
        margin-bottom: 1em;
      }

      .hidden {
        display: none;
      }
    </style>
  </head>
  <body>
    <h2>Memory Reports</h2>
    <button id="startButton">Measure</button>
    <pre id="report" class="hidden"></pre>
  </body>
</html>