Chrome nettleser, Nyheter

Announcing the second Compute Pressure origin trial

Over the last year, Intel has been collaborating with Google and other parties
on the Compute Pressure API. In Chrome 115, you can register for an origin
trial to help test this new API, and this post explains the problems the API has
been designed to solve, and shows how to use it.

The Compute Pressure API was in origin trial from Chrome 92 to
Chrome 94. Since this first origin trial, many changes have been made to
the API. The existing article on the
Compute Pressure API is updated with more details.

The problem

The web is becoming a key application platform, with new capabilities making
applications such as video conferencing not just possible, but a delightful
experience for users. Web-based experiences load instantly, they are accessible
from anywhere, and need no up-front installation.

Users want fast-loading and responsive applications. They also want to get as
much as possible out of their battery life, and silent devices that are not hot
to touch. These things can sometimes be hard to achieve when also creating
advanced experiences as smooth animations and background video blurring use a
lot of processing power, pushing hardware to its limits and draining batteries.

In addition, there are a huge variety of devices being used to access web
applications. A five-year old laptop will have very different capabilities to a
brand new desktop computer, even when running the same browser version.

Developers often opt to develop for the lowest common denominator, avoiding
using some features that would tax older or less capable devices. However, if it
were possible to optimize the experience for users who have capable equipment
and are in the right environment to benefit from it, why not do it? As an
example, when joining a video call from your phone, just seeing the current
speaker is likely the best experience. On a desktop however, it would be nice to
see everyone on the call, and the hardware is usually up to the task. To achieve
this, you need live hardware telemetry, without sacrificing the users’ privacy,
that can be used for scheduling tasks and progressively turning on and off
features to ensure a smooth user experience. This is where the Compute Pressure
API can help.

What is the Compute Pressure API?

The Compute Pressure API offers high-level states that represent the pressure on
the system. These high-level states ensure a good balance between privacy (not
sharing too much specific information that could identify a user) and
information that developers can easily reason about. Additionally, it allows the
implementation to use the right underlying hardware metrics to ensure that users
can take advantage of all the processing power available to them as long as the
system is not under unmanageable stress.

Modern CPUs, for instance, are designed to run fine at 100% utilization in most
situations, on a single core or across all cores, so an API that hardcodes 80%
utilization as being critical could result in developers under-utilizing the
hardware’s capabilities and offering a suboptimal user experience. On the other
hand, a system might not have proper cooling, or the ambient temperature might
be very high as in the summer, and the system might be throttling even before
reaching high CPU utilization. The current API works on global CPU pressure, but
we plan to experiment with enabling CPU pressure per page across the main thread
and workers.

Compute pressure has the following states:

  • Nominal: Current workloads are causing minimal pressure, allowing the
    system to run at a lower clock frequency to preserve power.
  • Fair: The system is doing fine; everything is smooth, and it can take on
    additional work without issues.
  • Serious: There is some serious pressure on the system, but it is
    manageable, and the system is doing well, but could be getting close to its
    limits:

    • Clock speed (depending on AC or DC power) is consistently high.
    • Thermals are high but still manageable and not causing throttling.

At this point if you add more work the system may move into a critical state.

  • Critical: The system is now about to reach its limits, but it hasn’t
    reached the limit yet. Critical doesn’t mean that the system is being actively
    throttled, but this state is not sustainable for the long run and might result
    in throttling if the workload remains the same. This signal is the last call
    for the web application to lighten its workload.

Enable the Compute Pressure API

By default, the Compute Pressure API is not enabled in Chrome, but it can be
experimented with in Chrome 115 by explicitly enabling the functionality.
You can activate it locally by enabling the
enable-experimental-web-platform-features flag.

To enable it for all visitors to your app, an
origin trial is currently
underway and set to end in Chrome 118 (July 18, 2023). To
participate in the trial, sign up and include a meta element with the origin
trial token in either the HTML or HTTP header. For more information, refer to
the Get started with origin trials post.

Observe compute pressure

The following code snippet illustrates how to monitor and act on changes of
compute pressure:

// The `records` parameter is a sequence of records between two
// consecutive callbacks. Currently it contains ten entries, but
// this is an implementation detail.
function callback(records) {
const lastRecord = records.pop();
console.log(`Current pressure ${lastRecord.state}`);
if (lastRecord.state === 'critical') {
// Reduce workers load by 4.
} else if (lastRecord.state === 'serious') {
// Reduce workers load by 2.
} else {
// Do not reduce.
}
}

const observer = new PressureObserver(callback, {
// Sample rate in Hertz.
sampleRate: 1,
});
observer.observe('cpu');

The following code snippet illustrates how to use the Compute Pressure API from
an iframe:

<iframe src="https://mysite.com/" allow="compute-pressure">
<script>
// Use Compute Pressure API.
</script>
</iframe>

Platform support

The Compute Pressure API is available in Chrome 115 on Linux, ChromeOS,
macOS, and Windows.

While due to resource constraints Android support is currently not
supported during the origin trial phase, it’s planned to support the operating
system in the future.

Demo

Try the demo embedded below to see how the compute pressure state changes based
on some artificial pressure.

In case your browser doesn’t support the API, the video below shows a recording
of the demo.

Feedback

Developer feedback is really important at this stage, so please
file issues on GitHub with
suggestions and questions.

Acknowledgements

The hero image was created by Robert Anasch on
Unsplash.
This article was reviewed by Rachel Andrew and Thomas Steiner.

This post is also available in: English

author-avatar

About Aksel Lian

En selvstendig full stack webutvikler med en bred variasjon av kunnskaper herunder SEO, CMS, Webfotografi, Webutvikling inkl. kodespråk..