• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5export const KB = 1024;
6export const MB = KB * KB;
7export const GB = MB * KB;
8export const kMillis2Seconds = 1 / 1000;
9export const kMicro2Milli = 1 / 1000;
10
11export function formatBytes(bytes, digits = 2) {
12  const units = ['B', 'KiB', 'MiB', 'GiB'];
13  const divisor = 1024;
14  let index = 0;
15  while (index < units.length && bytes >= divisor) {
16    index++;
17    bytes /= divisor;
18  }
19  return bytes.toFixed(digits) + units[index];
20}
21
22export function formatMicroSeconds(micro) {
23  return (micro * kMicro2Milli).toFixed(1) + 'ms';
24}
25
26export function formatDurationMicros(micros, secondsDigits = 3) {
27  return formatDurationMillis(micros * kMicro2Milli, secondsDigits);
28}
29
30export function formatDurationMillis(millis, secondsDigits = 3) {
31  if (millis < 1000) {
32    if (millis < 1) {
33      return (millis / kMicro2Milli).toFixed(1) + 'ns';
34    }
35    return millis.toFixed(2) + 'ms';
36  }
37  let seconds = millis / 1000;
38  const hours = Math.floor(seconds / 3600);
39  const minutes = Math.floor((seconds % 3600) / 60);
40  seconds = seconds % 60;
41  let buffer = '';
42  if (hours > 0) buffer += hours + 'h ';
43  if (hours > 0 || minutes > 0) buffer += minutes + 'm ';
44  buffer += seconds.toFixed(secondsDigits) + 's';
45  return buffer;
46}
47
48// Get the offset in the 4GB virtual memory cage.
49export function calcOffsetInVMCage(address) {
50  let mask = (1n << 32n) - 1n;
51  let ret = Number(address & mask);
52  return ret;
53}
54
55export function delay(time) {
56  return new Promise(resolver => setTimeout(resolver, time));
57}
58
59export function defer() {
60  let resolve_func, reject_func;
61  const p = new Promise((resolve, reject) => {
62    resolve_func = resolve;
63    reject_func = resolve;
64  });
65  p.resolve = resolve_func;
66  p.reject = reject_func;
67  return p;
68}
69