Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/electron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ If you pass `app` to `launchMain`, the main process also reports what Electron c
- A `child_process_gone` system event (`{ type, reason, exitCode, name, serviceName }`, the last two when Electron supplies them; `serviceName` is e.g. `network.mojom.NetworkService`) when a GPU or utility process exits, including a normal exit (`reason: 'clean-exit'`), so filter on `reason` if you only want crashes. It is recorded only while `captureSystemEvents` is on, and the `child-process-gone` listener is removed again by `client.stop()`.
- `environment.hardware.gpu` from `app.getGPUInfo(gpuInfoType)` (`'basic'` by default). A usable result is kept; a failed read, or a different `gpuInfoType` on a later launch, asks again.

A `relaunch()` from `@bugsee/node` or `@bugsee/bugsee` is not Electron-aware: after it `hardware.gpu` is still served, but `app_metrics`, `child_process_gone` and the power, display and window telemetry below stop until `launchMain` runs again.
A `relaunch()` from `@bugsee/node` or `@bugsee/bugsee` is not Electron-aware: after it `hardware.gpu` is still served, but `app_metrics`, `child_process_gone` and the power (including `thermal_state` and `cpu_speed_limit`), display and window telemetry below stop until `launchMain` runs again.

## Power, display and window telemetry

Expand All @@ -85,6 +85,7 @@ Pass any of `powerMonitor`, `screen` and `BrowserWindow` (the Electron objects,
**Pass `screen` safely.** Electron's real `screen` throws on any property read until the app is ready, so `import { screen } from 'electron'` and passing it straight to a `launchMain` call made before `app.whenReady()` turns the display domain off (the failure is reported through `onError`; the launch and the other domains are unaffected). To get display telemetry, either call `launchMain` after `app.whenReady()`, or leave `screen` out of the early call. The `powerMonitor` has the same rule on some platforms.

- `powerMonitor` -> `events.system`: `power_suspend`, `power_resume`, `power_lock`, `power_unlock`, `power_ac`, `power_battery`. No params. Platform dependent, per Electron's typings: `lock-screen`/`unlock-screen` are macOS and Windows, `on-ac` is macOS and Windows, and `on-battery` is macOS only, so an event that never fires is not evidence about the power state.
- `powerMonitor` -> system traces `thermal_state` and `cpu_speed_limit` (on whenever `powerMonitor` is passed and `captureSystemTraces` is on; supplying the legacy `systemMetricsSampler` option replaces the trace producers, so these stay absent then). They are event-driven and sampled, never held: a state the OS stops reporting leaves a gap. `thermal_state` is macOS only: it is written once at start from `getCurrentThermalState()` and on every `thermal-state-change` (a state outside Electron's four named ones reads as `unknown`, which writes nothing). `cpu_speed_limit` is an integer percent 0-100 (fractions are rounded to the nearest integer; out-of-range or non-numeric values are dropped) from `speed-limit-change` only, on macOS and Windows: Electron has no `getCurrentCPUSpeedLimit()`, so there is no value until the OS reports a limit, and a measured 0 is written. `thermal-state-change` and `speed-limit-change` pass a details object (`{ state }` / `{ limit }`) as the first listener argument; the tracker also accepts a bare value.
- `screen` -> `events.system`: `display_added` and `display_removed` (camelCase `{ id, nativeId, width, height, naturalWidth, naturalHeight, scale, maxFps, rotation, type }`, absent keys omitted; `id` is the display's number in the CURRENT `displays` list (a removed display is no longer listed and takes the number after the last listed one, so correlate display events by `nativeId`, which is the stable key), `width`/`height` are device pixels, `rotation` is quarter turns; a display whose id is -1, Electron's "invalid or not yet known" marker, is ignored) and `display_metrics_changed` (the same plus `changed`, Electron's changed metrics). **Pass `screen` (after app ready) to get the `displays` trace: without it an Electron session has no display trace at all, because renderers never write it (the host owns it).** It also feeds the `displays` system trace, the mobile layout: a list `[{ id, native_id, state?, type?, width, height, natural_width?, natural_height?, scale?, max_fps?, rotation? }]` (`id` 0 is the display showing the KEY window (the focused window, else the most recently focused, else the first created; the primary display when there is no window yet), the others follow in ascending order of Electron's id with Electron's own id in `native_id`; the numbering is recomputed, and the list re-written, when focus or a window move changes the key window's display; `state: "on"` marks id 0; windows on other displays are not recorded; the retired `electron_displays` name is gone), written when it changes. The trace is absent only before its first value: if a later refresh cannot read a usable list, the previous list stays held and keeps being re-recorded (the same open decision as other held traces).
- `app` (plus `BrowserWindow` for windows that already exist) -> `events.system`: `window_focus`, `window_blur`, `window_show`, `window_hide` (`{ windowId }`), and `renderer_unresponsive` / `renderer_responsive` (`{ webContentsId }`). These are on whenever `app` is passed (they add two `browser-window-created` listeners, for focus and for show/hide, and one `web-contents-created` listener); pass `captureWindowEvents: false` to opt out of the events. That switch does NOT stop key-window tracking: when `screen` is passed the `displays` trace follows the focused window, so two `browser-window-created` listeners (focus, moved) and focus/blur/moved/closed listeners on every window stay armed even with the option off (with window events on and `screen` passed, `app` carries three `browser-window-created` listeners). Windows created after `launchMain` are tracked too. The hang pair is listened for on `webContents` only, because `BrowserWindow` re-emits it and each hang would otherwise be recorded twice; a `<webview>` or `BrowserView` is covered when Electron announces it through `web-contents-created`.

Expand Down
4 changes: 2 additions & 2 deletions packages/electron/src/launch-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ export function launchMain(appToken: string, options: LaunchMainOptions): Bugsee
// relaunch option, so a relaunch keeps THIS wrapper (closing over the tracker) and `hardware.gpu` keeps being served
// (stop() does not clear it; a GPU that had not yet resolved is not re-requested). `systemTraceProducers` and
// `wrapSystemEventsSource` are RESET options, so a relaunched client has the plain node producers and events source:
// every Electron trace and event (`app_metrics`, `displays`, `child_process_gone`, `power_*`, `display_*`,
// `window_*`, `renderer_*`) stops until `launchMain` is called again (the carrier client must be cleared by a stop for
// every Electron trace and event (`app_metrics`, `displays`, `thermal_state`, `cpu_speed_limit`, `child_process_gone`,
// `power_*`, `display_*`, `window_*`, `renderer_*`) stops until `launchMain` is called again (the carrier client must be cleared by a stop for
// that, as for any launch).
const callerExtras = nodeOptions.hardwareExtras;
const reportExtrasOnce = reportOncePerMessage(nodeOptions.onError);
Expand Down
3 changes: 2 additions & 1 deletion packages/electron/src/system-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ class ThermalStateProducer extends TraceProducerBase {
if (wire !== undefined) this.emit(TraceName.THERMAL_STATE, wire);
}
protected onStart(): void {
// subscribe FIRST (arming the listener), then the baseline read: no change can fall between the two
// subscribe before the baseline read so the listener is armed first; both run synchronously on the one JS thread, so
// no change event can fire between them (the order only matters if the read ever becomes asynchronous)
this.#off = this.tracker.on('thermalState', (e) => this.#publish(e.state));
const current = this.tracker.current('thermalState');
if (current !== undefined) this.#publish(current.state);
Expand Down
9 changes: 9 additions & 0 deletions packages/trackers/src/electron/power-tracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ describe('ElectronPowerTracker', () => {
off();
expect(m.total()).toBe(0);
});
it('rounds to the NEAREST integer (not floor, not ceil) at the .5 and .6 / .4 boundaries', () => {
const m = fakeMonitor();
const seen = vi.fn();
track(m).on('speedLimit', seen);
for (const limit of [62.5, 99.6, 99.4, 0.4, 0.5, 37.49]) {
m.fire('speed-limit-change', { limit });
}
expect(seen.mock.calls.map((c) => c[0].limit)).toEqual([63, 100, 99, 0, 1, 37]);
});
it('a BARE limit (the listener receives the number itself) is read too', () => {
const m = fakeMonitor();
const seen = vi.fn();
Expand Down
Loading