Closed Bug 1134591 Opened 9 years ago Closed 9 years ago

Consider sampling for per-compartment CPU monitoring

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: Yoric, Unassigned)

References

Details

Bug 674779 introduces per-compartment CPU monitoring. The current approach is exact, but might be expensive in terms of CPU.

An alternative would be to adopt sampling, e.g. every n milliseconds, check `getrusage` off the main thread and attach the cost to the current compartment. A naive implementation, however, would probably cause Firefox to wakeup way too often, which would make it quite bad for power usage.
Sketching out a possible solution to adopt sampling without killing battery use. Essentially, the idea is that the timer thread should be able to go to sleep shortly after realizing that the main thread is inactive.

Atomic variables:
* bool mainThreadHeartBeat   // `true` if the main thread has processed at least one event recently
* bool isTimerThreadSleeping // `true` when the timer thread is waiting for the main thread
* resourceUsage // the result of `getrusage`


Main thread (in XPConnect's OnProcessNextEvent())
1. Set `mainThreadHeartBeat` to `true`;
2. If `isTimerThreadSleeping` is `true`
  2.1 set `isTimerThreadSleeping` to `false`;
  2.2 wake up timer thread;
3. Process events as usual.


Timer thread:
1. Sleep until awakened by the main thread;
2. Set `isTimerThreadSleeping` to `false`;
3. Wait for n milliseconds;
4. If `mainThreadHeartBeat` is `false`;
  4.1 set `isTimerThreadSleeping` to `true`;
  4.2 go to 1;
5. Call `getrusage` and put the result in `resourceUsage`;
6. Set `mainThreadHeartBeat` to `false`;
7. Goto 3.
Notes: the timer thread should probably not go to sleep immediately if `mainThreadHeartBeat` is `false`, but rather allow several missed heart beats, in case the main thread is just busy.

The main thread may also set `mainThreadHeartBeat` to `true` both when entering and leaving the event.
You might want to look at the xpconnect watchdog thread.
http://mxr.mozilla.org/mozilla-central/source/js/xpconnect/src/XPCJSRuntime.cpp#980

It's responsible for triggering the slow script dialog. It seems like this functionality could be added to it without too much trouble though.
This doesn't seem necessary anymore.
Status: NEW → RESOLVED
Closed: 9 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.