summaryrefslogtreecommitdiffstats
path: root/tools/profiler/tests/xpcshell/test_feature_fileioall.js
blob: e5ac040b98495d05614579d4d72af56e8c4334e5 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

add_task(async () => {
  info(
    "Test that off-main thread fileio is captured for a profiled thread, " +
      "and that it will be sent to the main thread."
  );
  const filename = "test_marker_fileio";
  const profile = await startProfilerAndTriggerFileIO({
    features: ["fileioall"],
    threadsFilter: ["GeckoMain", "BgIOThreadPool"],
    filename,
  });

  const threads = getThreads(profile);
  const mainThread = threads.find(thread => thread.name === "GeckoMain");
  const mainThreadFileIO = getInflatedFileIOMarkers(mainThread, filename);
  let backgroundThread;
  let backgroundThreadFileIO;
  for (const thread of threads) {
    // Check for FileIO in any of the background threads.
    if (thread.name.startsWith("BgIOThreadPool")) {
      const markers = getInflatedFileIOMarkers(thread, filename);
      if (markers.length) {
        backgroundThread = thread;
        backgroundThreadFileIO = markers;
        break;
      }
    }
  }

  info("Check all of the main thread FileIO markers.");
  checkInflatedFileIOMarkers(mainThreadFileIO, filename);
  for (const { data, name } of mainThreadFileIO) {
    equal(
      name,
      "FileIO (non-main thread)",
      "The markers from off main thread are labeled as such."
    );
    equal(
      data.threadId,
      backgroundThread.tid,
      "The main thread FileIO markers were all sent from the background thread."
    );
  }

  info("Check all of the background thread FileIO markers.");
  checkInflatedFileIOMarkers(backgroundThreadFileIO, filename);
  for (const { data, name } of backgroundThreadFileIO) {
    equal(
      name,
      "FileIO",
      "The markers on the thread where they were generated just say FileIO"
    );
    equal(
      data.threadId,
      undefined,
      "The background thread FileIO correctly excludes the threadId."
    );
  }
});

add_task(async () => {
  info(
    "Test that off-main thread fileio is captured for a thread that is not profiled, " +
      "and that it will be sent to the main thread."
  );
  const filename = "test_marker_fileio";
  const profile = await startProfilerAndTriggerFileIO({
    features: ["fileioall"],
    threadsFilter: ["GeckoMain"],
    filename,
  });

  const threads = getThreads(profile);
  const mainThread = threads.find(thread => thread.name === "GeckoMain");
  const mainThreadFileIO = getInflatedFileIOMarkers(mainThread, filename);

  info("Check all of the main thread FileIO markers.");
  checkInflatedFileIOMarkers(mainThreadFileIO, filename);
  for (const { data, name } of mainThreadFileIO) {
    equal(
      name,
      "FileIO (non-profiled thread)",
      "The markers from off main thread are labeled as such."
    );
    equal(typeof data.threadId, "number", "A thread ID is captured.");
  }
});

/**
 * @typedef {Object} TestConfig
 * @prop {Array} features The list of profiler features
 * @prop {string[]} threadsFilter The list of threads to profile
 * @prop {string} filename A filename to trigger a write operation
 */

/**
 * Start the profiler and get FileIO markers.
 * @param {TestConfig}
 * @returns {Profile}
 */
async function startProfilerAndTriggerFileIO({
  features,
  threadsFilter,
  filename,
}) {
  const entries = 10000;
  const interval = 10;
  await Services.profiler.StartProfiler(
    entries,
    interval,
    features,
    threadsFilter
  );

  const path = PathUtils.join(PathUtils.tempDir, filename);

  info(`Using a temporary file to test FileIO: ${path}`);

  if (fileExists(path)) {
    console.warn(
      "This test is triggering FileIO by writing to a file. However, the test found an " +
        "existing file at the location it was trying to write to. This could happen " +
        "because a previous run of the test failed to clean up after itself. This test " +
        " will now clean up that file before running the test again."
    );
    await removeFile(path);
  }

  info("Write to the file, but do so using a background thread.");

  // IOUtils handles file operations using a background thread.
  await IOUtils.write(path, new TextEncoder().encode("Test data."));
  const exists = await fileExists(path);
  ok(exists, `Created temporary file at: ${path}`);

  info("Remove the file");
  await removeFile(path);

  return stopNowAndGetProfile();
}

async function fileExists(file) {
  try {
    let { type } = await IOUtils.stat(file);
    return type === "regular";
  } catch (_error) {
    return false;
  }
}

async function removeFile(file) {
  await IOUtils.remove(file);
  const exists = await fileExists(file);
  ok(!exists, `Removed temporary file: ${file}`);
}