-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
2548 lines (2492 loc) · 95.8 KB
/
index.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Compute Pressure Level 1</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove" defer></script>
<script class="remove">
function experimentalPreProcess(config, document, utils) {
const params = new URLSearchParams(location.search);
const q = parseInt(params.get("experimental"));
document.querySelectorAll('.experimental').forEach(el => {
if (q) {
el.classList.remove("experimental");
} else {
el.remove();
}
})
document.querySelectorAll('.non-experimental').forEach(el => {
if (q) {
el.remove();
} else {
el.classList.add("hidden");
}
})
}
// All config options at https://respec.org/docs/
const respecConfig = {
shortName: "compute-pressure",
group: "das",
specStatus: "ED",
xref: "web-platform",
github: "https://github.com/w3c/compute-pressure",
formerEditors: [
{
name: "Olivier Yiptong",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 81110,
},
{
name: "Victor Costan",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 72312,
}
],
editors: [
{
name: "Kenneth Rohde Christiansen",
company: "Intel Corporation",
companyURL: "https://intel.com",
w3cid: 57705,
},
{
name: "Arnaud Mandy",
company: "Intel Corporation",
companyURL: "https://intel.com",
w3cid: 126342,
}
],
formerEditors: [
{
name: "Raphael Kubo da Costa",
company: "Intel Corporation",
companyURL: "https://intel.com",
w3cid: 95850,
},
],
testSuiteURI: "https://github.com/web-platform-tests/wpt/labels/compute-pressure",
xref: {
profile: "web-platform",
specs: [
"permissions-policy", "hr-time", "picture-in-picture", "mediacapture-streams"
]
},
preProcess: [experimentalPreProcess]
};
</script>
<style>
@counter-style pressure-states-emoji {
system: cyclic;
symbols: "\026AA" "\01F7E2" "\01F7E1" "\01F534";
suffix: " ";
}
.pressure-states {
list-style-type: pressure-states-emoji;
}
@counter-style pressure-source-emoji {
system: cyclic;
symbols: "\1F321" "\1F4A1";
suffix: " ";
}
.pressure-source {
list-style-type: pressure-source-emoji;
}
ul li::marker {
font-family: "Segoe UI Emoji", "Noto Color Emoji";
}
.experimental, .hidden {
display: none;
}
</style>
<section id="abstract">
<p>
The <cite>Compute Pressure API</cite> provides a way for websites to react to changes
in the CPU pressure of the target device, such that websites can trade off
resources for an improved user experience.
</p>
</section>
<section id="sotd"></section>
<section class="informative">
<h2>Introduction</h2>
<p>
Modern applications often need to balance the trade offs and advantages of fully utilizing
the system's computing resources, in order to provide a modern and delightful user experience.
</p>
<p>
As an example, many applications can render video effects with varying degrees of sophistication.
These applications aim to provide the best user experience, while avoiding driving the user's
device into a high pressure regime.
</p>
<p>
Utilization of [=processing units=] close to and often reaching 100% can lead to a bad
user experience, as different tasks are fighting for the processing time.
This can lead to slowless, which is especially noticeable with input delay.
Further, a prolonged utilization close 100% can cause the [=processing units=] to heat up due to prolonged
boosting, which can lead to throttling, resulting in an even worse user experience.
</p>
<p>
As a result of thermal limits, many smartphones, tablets and laptops can become uncomfortably hot
to the touch. The fans in laptops and desktops can become so loud that they disrupt conversations
or the users’ ability to focus.
</p>
<p>
In many cases, a device under high pressure appears to be unresponsive, as the operating
system may fail to schedule the threads advancing the task that the user is waiting for. See also
<a href="https://github.com/wicg/compute-pressure/#goals--motivating-use-cases">Use Cases</a>.
</p>
</section>
<section>
<h2>A Note on Feature Detection</h2>
<p><i>This section is non-normative.</i></p>
<p>
Feature detection is an established web development best practice. Resources on the topic are plentiful on- and
offline and the purpose of this section is not to discuss it further, but rather to put it in the context of
detecting hardware-dependent features.
</p>
<p>
Consider the below feature detection examples:
</p>
<aside class="example" title="Checking existence of PressureObserver interface">
<p>
This simple example illustrates how to check whether the [=User Agent=] exposes the
{{PressureObserver}} interface
</p>
<pre class="js">
if ("PressureObserver" in globalThis) {
// Use PressureObserver interface
}
</pre>
</aside>
<aside class="note">
<p>
Checking against {{globalThis/globalThis}} will work on any [=ECMAScript/agent=] as defined by
[[ECMAScript]], thus also in workers.
</p>
<p>
It does however not tell you whether that API is actually connected to a
real [=platform collector=], whether the [=platform collector=] is collecting real telemetry
readings, or whether the user is going to allow you to access it.
</p>
</aside>
</section>
<section>
<h2>Concepts</h2>
<p>
This specification defines the following concepts:
</p>
<section>
<h3>Processing Units</h3>
<p>
Computing devices consist of a multitude of different <dfn>processing units</dfn> such as the Central
Processing Unit (CPU), the Graphics Processing Unit (GPU) and many specialized
processing units. The latter are becoming popular such as ones designed to accelerate specific
tasks like machine learning or computer vision.
</p>
</section>
<section>
<h3>Pressure sources</h3>
<p>
The specification currently defines the <dfn>valid source types</dfn> as
<span class="experimental"><em>global system thermals</em> and </span>
the <em>central [=processing unit=]</em>, also known as the CPU.
Future levels of this specification MAY introduce additional [=source types=].
</p>
<pre class="non-experimental idl">
enum PressureSource { "cpu" };
</pre>
<pre class="experimental idl">
enum PressureSource { "cpu", "thermals" };
</pre>
<p>
The <dfn>PressureSource</dfn> enum represents the [=valid source types=]:
</p>
<p>
<ul class="pressure-source">
<li class="experimental">
{{PressureSource/"thermals"}} represents the global thermal state of the system.
<aside class="issue" data-number="294">
<p>
The thermal pressure source is at-risk given implementation difficulties.
</p>
</aside>
</li>
<li>
{{PressureSource/"cpu"}} represents the average pressure of the central [=processing unit=]
across all its cores.
</li>
</ul>
</p>
<aside class="note">
<p>
If a user calls {{PressureObserver/observe()}} with a [=source type=] not part of
{{PressureSource}}, at the level of this specification the [=user agent=] supports,
the method will throw a {{TypeError}}.
</p>
<p>
If the [=source type=] is known by the [=user agent=] (part of {{PressureSource}}),
but not supported by it, the host OS or the underlying hardware, the method will instead
throw {{NotSupportedError}}.
</p>
<p>
To list the known [=source types=], a user can call the static attribute
{{PressureObserver/knownSources}}.
</p>
</aside>
</section>
<section>
<h3>Sampling and Reporting Rate</h3>
<p>
The <dfn>requested sampling interval</dfn> represents the desired
interval between samples to be obtained from the hardware,
expressed in milliseconds.
</p>
<p>
Interval and frequency are inverses of each other, so the
[=requested sampling interval=] can also be expressed as a
<dfn>requested sampling rate</dfn> in Hertz (cycles per second) by
dividing 1000 by the [=requested sampling interval=] value.
</p>
<p>
The <dfn>sampling rate</dfn> for a [=platform collector=] is defined as a rate
at which the [=user agent=] obtains telemetry readings from the underlying platform,
and it might differ from the pressure observers' [=requested sampling rates=].
The rate is measured in Hertz (cycles per second).
</p>
<p>
The <dfn>reporting rate</dfn> for a pressure observer is the rate at which it runs
the [=data collection=] steps, and it will never exceed the [=sampling rate=].
</p>
<p>
The [=sampling rate=] differs from the [=requested sampling rate=] when the
[=requested sampling rate=] exceeds upper or lower sampling rate bounds
supported or accepted by the underlying platform and [=user agent=]<sup>†</sup>.
</p>
<p>
<sup>†</sup>The specification additionally obfuscates the rate as outlined
in [[[#rate-obfuscation]]].
</p>
<p>
In case the user didn't request a [=sampling rate=], the [=sampling rate=]
is [=implementation-defined=].
</p>
<aside class="note">
In case there are multiple instances of {{PressureObserver}} active with different
[=requested sampling intervals=], it is up to the [=user agent=] to set a
[=platform collector=] level [=sampling rate=] that best fulfills these requests,
while making sure that the [=reporting rate=] of all {{PressureObserver}}s does
not exceed their respective [=requested sampling rates=].
</aside>
</section>
</section>
<section> <h2>Platform primitives</h2>
<p>
A <dfn>pressure source</dfn> is an abstract, [=implementation-defined=]
interface to hardware counters or an underlying framework that provides
telemetry data about a <dfn>source type</dfn>
defined by {{PressureSource}}. A [=pressure source=] can make use of data
fusion with data from additional sources if that provides more precise
results.
</p>
<p>
The telemetry data provided by a [=pressure source=] is represented in this
specification as a <dfn>pressure source sample</dfn>, a [=struct=]
consisting of the following [=struct/items=]:
<ul>
<li>
<dfn data-dfn-for="pressure source sample">data</dfn>: [=contributing
factors=] obtained from the underlying hardware or operating system or,
in the case of a [=virtual pressure source=], a {{PressureState}}.
</li>
<li>
<dfn data-dfn-for="pressure source sample">timestamp</dfn>: the
[=unsafe shared current time=] when [=pressure source sample/data=] was
obtained.
<aside class="note">
<p>
The purpose of [=pressure source sample/timestamp=] is to use the
same [=monotonic clock/unsafe current time=] for a sample across
all globals and invocations of the [=data collection=] algorithm
which are processing the same [=pressure source sample=].
</p>
</aside>
</li>
</ul>
</p>
<p>
A [=pressure source=] has an associated <dfn
data-dfn-for="pressure source">latest sample</dfn>, a [=pressure source
sample=] or null. It is initially null.
</p>
<p>
A <dfn>platform collector</dfn> is an abstract interface responsible for
obtaining telemetry samples from a [=pressure source=], transforming them
into [=pressure states=] and providing them to the [=user agent=].
</p>
<p>
A [=platform collector=] has the following associated data:
</p>
<ul>
<li>
an <dfn data-dfn-for="platform collector">associated pressure
source</dfn>, which is a [=pressure source=] or null.
</li>
<li>
an <dfn data-dfn-for="platform collector">activated</dfn> boolean,
initially false.
</li>
</ul>
<p>
The format of the telemetry data provided by a [=pressure source=]
and stored in its [=pressure source/latest sample=]'s [=pressure source
sample/data=] is [=implementation-defined=], and so is the process through
which a [=platform collector=] transforms it into a [=pressure state=].
</p>
<p>
For this specification's purposes, [=platform collectors=] are scoped to a
[=global object=] via the [=platform collector mapping=].
</p>
<p>
For automation purposes, a [=platform collector=] must have the ability to
connect to [=virtual pressure sources=] and use their simulated [=pressure
source sample/data=] as [=pressure states=] rather than raw platform data
that must be transformed into an [=adjusted pressure state=].
</p>
<p>
As collecting telemetry data often means polling hardware counters, it is not a free operation and thus,
it should not happen if there are no one observing the data. See [[[#life-cycle]]] for more information.
</p>
<p>
A [=platform collector=] samples data at a specific rate. A [=user agent=] may modify this rate
(if possible) for privacy reasons, or ignore and fuse certain readings.
</p>
</section>
<section>
<h3>
User notifications
</h3>
<p>
It is RECOMMENDED that a [=user agent=] show some form of user-visible
notification that informs the user when a pressure observer is active,
as well as provides the user with the means to block the ongoing operation,
or simply dismiss the notification.
</p>
</section>
<section>
<h3>
Policy control
</h3>
<p>
The Compute Pressure API defines a [=policy-controlled feature=]
identified by the token "compute-pressure".
Its [=policy-controlled feature/default allowlist=] is `'self'`.
</p>
<p>
Workers (dedicated and shared) adhere to the permission policy set by their
owning document(s).
</p>
<p>
Shared workers often have multiple owning documents as they can be obtained
by other documents with the [=same origin=].
In this case, all owning documents must be [=allowed to use=] the [=policy-controlled
feature=] defined by this specification.
</p>
<p>
Dedicated workers can be created from other workers,
in which case the permission policy of the first owning document
(or owning documents, in case of a shared worker) up the owner
chain will be used.
</p>
<aside class="note">
There has been discussion on allowing setting permission policy directly
on a worker on creation, in which case that would have to be consulted
as well.
</aside>
<aside class="note">
<p>
The [=policy-controlled feature/default allowlist=] of `'self'` allows usage in
same-origin nested frames but prevents third-party content from using
the feature.
</p>
<p>
Third-party usage can be selectively enabled by adding
`allow="compute-pressure"` attribute to the frame container element:
</p>
<pre class="example html" title=
"Enabling compute pressure on remote content">
<iframe src="https://third-party.com" allow="compute-pressure"/></iframe>
</pre>
<p>
Alternatively, the Compute Pressure API can be disabled completely by
specifying the permissions policy in a HTTP response header:
</p>
<pre class="example http" title="Feature Policy over HTTP">
Permissions-Policy: {"compute-pressure": []}
</pre>
<p>
See [[[PERMISSIONS-POLICY]]] for more details.
</p>
</aside>
</section>
<section>
<h3>
Internal Slot Definitions
</h3>
<p>
Each [=global object=] has:
<ul>
<li>
a <dfn>pressure observer task queued</dfn> (a boolean), which is initially false.
</li>
<li>
a <dfn>registered observer list</dfn> per supported [=source type=], which is initially empty.
</li>
<li>
a <dfn>platform collector mapping</dfn>, an [=ordered map=] of [=source
types=] to [=platform collectors=].
</li>
</ul>
A <dfn>registered observer</dfn> consists of an <dfn>observer</dfn> (a {{PressureObserver}} object).
</p>
<p>
The [=user agent=] has:
<ul>
<li>
a <dfn>max queued records</dfn> integer, which is set to an [=implementation-defined=] value, greater than 0.
</li>
<li>
a <dfn>supported source types</dfn> [=list=] of [=implementation-defined=] {{PressureSource}} values.
</li>
</ul>
</p>
<p>
A constructed {{PressureObserver}} object has the following internal slots:
</p>
<ul data-dfn-for="PressureObserver">
<li>
a <dfn>[[\Callback]]</dfn> of type {{PressureUpdateCallback}} set on creation.
</li>
<li>
a <dfn>[[\PendingObservePromises]]</dfn> [=list=] of zero or more source-promise [=tuples=], initially empty,
where source holds a {{PressureSource}} string and promise holds a {{Promise}} object.
</li>
<li>
a <dfn>[[\QueuedRecords]]</dfn> [=queue=] of zero or more {{PressureRecord}}
objects, which is initially empty.
</li>
<li>
a <dfn>[[\LastRecordMap]]</dfn> [=ordered map=] of {{PressureSource}} to
the latest {{PressureRecord}}.
</li>
<li>
a <dfn>[[\SampleIntervalMap]]</dfn> [=ordered map=] of {{PressureSource}} to
positive numbers. It represents the sample interval given source type.
</li>
</ul>
<p>
For the [=rate obfuscation=] mitigation the constructed {{PressureObserver}} object additionally
has the following internal slots:
</p>
<ul data-dfn-for="PressureObserver">
<li>
an <dfn>[[\ObservationWindow]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\MaxChangesThreshold]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\PenaltyDuration]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\ChangesCountMap]]</dfn> [=ordered map=], [=map/keyed=] on a {{PressureSource}},
representing the [=source type=] that triggered transition to the current [=pressure state=].
The [=ordered map=]'s [=map/value=] is an integer representing the number of state changes in the
current observation window timeframe.
</li>
<li>
a <dfn>[[\AfterPenaltyRecordMap]]</dfn> [=ordered map=], [=map/keyed=] on a {{PressureSource}},
representing the [=source type=] of the last {{PressureRecord}}.
The [=ordered map=]'s [=map/value=] is a {{PressureRecord}}.
</li>
</ul>
</section>
<section> <h2>Pressure States</h2>
<p>
<dfn>Pressure states</dfn> represents the minimal set of useful states that allows websites
to react to changes in compute and system pressure with minimal degration in quality or service,
or user experience.
</p>
<pre class="idl">
enum PressureState { "nominal", "fair", "serious", "critical" };
</pre>
<p>
The <dfn>PressureState</dfn> enum represents the [=pressure state=] with the following states:
</p>
<p>
<ul class="pressure-states">
<li>
{{PressureState/"nominal"}}: The conditions of the target device are at an acceptable level with no noticeable
adverse effects on the user.
</li>
<li>
{{PressureState/"fair"}}: Target device pressure, temperature and/or energy usage are slightly elevated, potentially
resulting in reduced battery-life, as well as fans (or systems with fans) becoming active and audible.
Apart from that the target device is running flawlessly and can take on additional work.
</li>
<li>
{{PressureState/"serious"}}: Target device pressure, temperature and/or energy usage is consistently highly elevated.
The system may be throttling as a countermeasure to reduce thermals.
</li>
<li>
{{PressureState/"critical"}}: The temperature of the target device or system is significantly elevated and it requires
cooling down to avoid any potential issues.
</li>
</ul>
</p>
</section>
<section> <h2>Contributing Factors</h2>
<p>
<dfn>Contributing factors</dfn> represent the underlying hardware metrics contributing to the current [=pressure state=]
and can be [=implementation-defined=].
</p>
<p>
The <dfn>adjusted pressure state</dfn> is a [=pressure state=] determined by an [=implementation-defined=]
algorithm that takes as input [=source type=] and any other [=implementation-defined=] data from
[=contributing factors=]. This algorithm MUST not be deterministic to ensure [=break calibration=] mitigation effectiveness.
</p>
<p>
The <dfn>change in contributing factors is substantial</dfn> steps are as follows:
<ol>
<li>
If [=implementation-defined=] low-level hardware metrics that contribute to the
current [=pressure state=] drop below or exceed an, per metric,
[=implementation-defined=] threshold
for the current [=pressure state=], return true.
</li>
<li>
Return false.
</li>
</ol>
<aside class="note">
<p>
The [=change in contributing factors is substantial=] algorithm allows [=user agents=] to avoid flip-
flopping between states in certain circumstances. For example, a state might otherwise change too rapidly
in response to a certain system metric that fluctuates around a boundary condition that triggers a state
change.
</p>
<p>
This specification does not define the precise algorithm
to allow implementations optimize this algorithm to match the underlying hardware platform's behavior.
One possible implementation of this algorithm is to use a
<a href="https://en.wikipedia.org/wiki/Preisach_model_of_hysteresis#Nonideal_relay">nonideal relay</a>
as a model and identify appropriate lower threshold α and upper threshold β for each
[=pressure state=] taking special characteristics of each contributing factors into consideration.
</p>
</aside>
</p>
</section>
<section> <h2>Pressure Observer</h2>
The Compute Pressure API enables developers to understand the pressure
of system resources such as the CPU.
<section data-dfn-for="PressureObserverCallback">
<h3>The <a>PressureUpdateCallback</a> callback</h3>
<pre class="idl">
callback PressureUpdateCallback = undefined (
sequence<PressureRecord> changes,
PressureObserver observer
);
</pre>
This callback will be invoked when the [=pressure state=] changes.
</section>
<section data-dfn-for="PressureObserver"> <h2>The <a>PressureObserver</a> object</h2>
<p>
The {{PressureObserver}} can be used to observe changes in the [=pressure states=].
</p>
<pre class="idl">
[Exposed=(DedicatedWorker,SharedWorker,Window), SecureContext]
interface PressureObserver {
constructor(PressureUpdateCallback callback);
Promise<undefined> observe(PressureSource source, optional PressureObserverOptions options = {});
undefined unobserve(PressureSource source);
undefined disconnect();
sequence<PressureRecord> takeRecords();
[SameObject] static readonly attribute FrozenArray<PressureSource> knownSources;
};
</pre>
<p>The <dfn>PressureObserver</dfn> interface represents a {{PressureObserver}}.</p>
<section>
<h3>The <dfn>constructor()</dfn> method</h3>
<p>
The `new` {{PressureObserver(callback)}} constructor steps are:
<ol class="algorithm">
<li>
Set |this|.{{PressureObserver/[[Callback]]}} to |callback:PressureUpdateCallback|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>observe()</dfn> method</h3>
<p>
The {{PressureObserver/observe(source, options)}} method steps are:
<ol class="algorithm">
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
[=list/For each=] |document:Document| in |relevantGlobal|'s [=owning document set=]:
<ol>
<li>
If |document| is not [=allowed to use=] the [=policy-controlled
feature=] token "compute-pressure", return [=a promise rejected with=] {{NotAllowedError}}.
</li>
</ol>
<aside class="note">
Workers have their own [=WorkerGlobalScope/policy container=] which can be set via HTTP
headers. See the example in [[[#policy-control]]].
</aside>
</li>
</li>
<li>
Set [=this=].{{PressureObserver/[[SampleIntervalMap]]}}[|source|] to |options:PressureObserverOptions|'s {{PressureObserverOptions/sampleInterval}}.
</li>
<li>
Let |promise:Promise| be [=a new promise=].
</li>
<li>
Let |pendingPromiseTuple| be (|source|, |promise|).
</li>
<li>
[=list/Append=] |pendingPromiseTuple| to [=this=].{{PressureObserver/[[PendingObservePromises]]}}.
</li>
<li>
[=promise/React=] to |promise|:
<ul>
<li>
If |promise| was [=resolved|fulfilled=] or [=rejected=], then:
<ol>
<li>
[=list/Remove=] |tuple| from [=this=].{{PressureObserver/[[PendingObservePromises]]}}.
</li>
</ol>
</li>
</ul>
</li>
<li>
Run the following steps [=in parallel=]:
<ol>
<li>
Let |platformCollector| be null.
</li>
<li>
If |relevantGlobal|'s [=platform collector mapping=]
[=map/contains=] |source|:
<ol>
<li>
Set |platformCollector| to |relevantGlobal|'s [=platform
collector mapping=][|source|].
</li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>
Let |newCollector| be a new [=platform collector=] whose
[=platform collector/associated pressure source=] is null.
</li>
<li>
Let |virtualPressureSource:virtual pressure source or null|
be the result of invoking [=get a virtual pressure source=]
with |source| and |relevantGlobal|.
</li>
<li>
If |virtualPressureSource| is not null:
<ol>
<li>
If |virtualPressureSource|'s [=virtual pressure
source/can provide samples=] is true:
<ol>
<li>
Set |newCollector|'s [=platform collector/associated
pressure source=] to |virtualPressureSource|.
</li>
<li>
[=set/Append=] |newCollector| to
|virtualPressureSource|'s [=virtual pressure
source/connected platform collectors=].
</li>
</ol>
</li>
</ol>
</li>
<li>
Otherwise:
<ol>
<li>
Let |realPressureSource| be an [=implementation-defined=]
[=pressure source=] that provides telemetry data about
|source|, or null if none exists.
</li>
<li>
Set |newCollector|'s [=platform collector/associated
pressure source=] to |realPressureSource|.
</li>
</ol>
</li>
<li>
If |newCollector|'s [=platform collector/associated
pressure source=] is not null:
<ol>
<li>
Set |platformCollector| to |newCollector|.
</li>
<li>
Set |relevantGlobal|'s [=platform collector
mapping=][|source|] to |platformCollector|.
</li>
</ol>
</li>
</ol>
</li>
<li>
If |platformCollector| is null, [=queue a global task=] on the
[=PressureObserver task source=] given |relevantGlobal| to reject
|promise| {{NotSupportedError}} and abort these steps.
</li>
<li>
Invoke [=activate data collection=] with |source| and
|relevantGlobal|.
</li>
<li>
[=Queue a global task=] on the [=PressureObserver task source=] given
|relevantGlobal| to run these steps:
<ol>
<li>
If |promise| was rejected, run the following substeps:
<ol>
<li>
If |relevantGlobal|'s [=registered observer list=] for |source| is [=list/empty=],
invoke [=deactivate data collection=] with |source| and |relevantGlobal|.
</li>
<li>
Return.
</li>
</ol>
</li>
<li>
[=list/Append=] a new [=registered observer=] whose [=observer=] is [=this=]
to |relevantGlobal|'s [=registered observer list=] for |source|.
</li>
<li>
Resolve |promise|.
</li>
</ol>
</li>
</ol>
</li>
<li>
Return |promise|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>unobserve()</dfn> method</h3>
<p>
The {{PressureObserver/unobserve(source)}} method steps are:
<ol class="algorithm">
<li>
If |source:PressureSource| is not a [=supported source type=], throw {{"NotSupportedError"}}.
</li>
<li>
[=list/Remove=] from |this|.{{PressureObserver/[[QueuedRecords]]}} all
|records| associated with |source|.
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[SampleIntervalMap]]}}[|source|].
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[LastRecordMap]]}}[|source|].
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[AfterPenaltyRecordMap]]}}[|source|].
</li>
<li>
[=list/For each=] (|promiseSource|, |pendingPromise|) of [=this=].{{PressureObserver/[[PendingObservePromises]]}},
if |source| is equal to |promiseSource|, [=reject=] |pendingPromise| with an {{AbortError}}.
</li>
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
Let |registeredObserverList| be |relevantGlobal|'s [=registered
observer list=] for |source|.
<li>
[=list/Remove=] any [=registered observer=] from
|registeredObserverList| whose [=observer=] is [=this=].
</li>
<li>
If |registeredObserverList| is [=list/empty=]:
<ol>
<li>
Invoke [=deactivate data collection=] with |source| and
|relevantGlobal|.
</li>
<li>
[=map/Remove=] |relevantGlobal|'s [=platform collector
mapping=][|source|].
</li>
</ol>
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>disconnect()</dfn> method</h3>
<p>
The {{PressureObserver/disconnect()}} method steps are:
<ol class="algorithm">
<li>
[=list/Empty=] |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[SampleIntervalMap]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[LastRecordMap]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[AfterPenaltyRecordMap]]}}.
</li>
<li>
[=list/For each=] (|promiseSource|, |pendingPromise|) of [=this=].{{PressureObserver/[[PendingObservePromises]]}},
[=reject=] |pendingPromise| with an {{AbortError}}.
</li>
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
[=map/For each=] |source| → |registeredObserverList| of
|relevantGlobal|'s [=registered observer list=] [=ordered map=]:
<ol>
<li>
[=list/Remove=] any [=registered observer=] from
|registeredObserverList| whose [=observer=] is [=this=].
</li>
<li>
If |registeredObserverList| is [=list/empty=]:
<ol>
<li>
Invoke [=deactivate data collection=] with |source| and
|relevantGlobal|.
</li>
<li>
[=map/Remove=] |relevantGlobal|'s [=platform collector
mapping=][|source|].
</li>
</ol>
</li>
</ol>
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>takeRecords()</dfn> method</h3>
<aside class="note">
<p>
A common use case for this is to immediately fetch all pending state change records
immediately prior to disconnecting the observer, so that any pending state changes
can be processed when shutting down the observer.
</p>
<p>
Another use case is the ability to receive any pending, already-generated states
changes without waiting for the callbacks to be invoked. Callbacks are invoked from
a task queue as part of the event loop cycle, so this allows for conferring the
current state outside of the event loop cycle.
</p>
</aside>
<p>
The {{PressureObserver/takeRecords()}} method steps are:
<ol class="algorithm">
<li>
Let |records| be a [=list/clone=] of |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
[=list/Empty=] |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
Return |records|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>knownSources</dfn> attribute</h3>
<p>
The {{PressureObserver/knownSources}} getter steps are:
<ol class="algorithm">
<li>
Return [=user agent=]'s [=supported source types=] in alphabetical order.
</li>
</ol>
</p>
<aside class="note">
<p>
The attribute property is a merely hint about the [=source types=] the [=user agent=] supports.
It is not guaranteed pressure observation will work on the underlying operating system or hardware.
Call {{PressureObserver/observe()}} and check for {{NotSupportedError}} if pressure observation is possible.
</p>
</aside>
</section>
</section>
<section data-dfn-for="PressureRecord">
<h3>The <dfn>PressureRecord</dfn> interface</h3>
<pre class="idl">
[Exposed=(DedicatedWorker,SharedWorker,Window), SecureContext]
interface PressureRecord {
readonly attribute PressureSource source;
readonly attribute PressureState state;
readonly attribute DOMHighResTimeStamp time;
[Default] object toJSON();
};
</pre>
<pre class="experimental idl">
partial interface PressureRecord {
readonly attribute double? ownContributionEstimate;
};
</pre>
<p>
A constructed {{PressureRecord}} object has the following internal slots:
</p>
<ul data-dfn-for="PressureRecord">
<li>
a <dfn>[[\Source]]</dfn> value of type {{PressureSource}}, which represents the current [=source type=].
</li>
<li>
a <dfn>[[\State]]</dfn> value of type {{PressureState}}, which represents the current [=pressure state=].
</li>
<li class="experimental">
<p>
an <dfn>[[\OwnContributionEstimate]]</dfn> value of type double?, which represents an estimated percentage
(value between 0 and 1) of the current web site contribution to the global pressure for the current source.
If the [=user agent=] cannot provide an estimate, the value will be null.
</p>
<p>
For CPU pressure, this will include the pressure of the CPU core responsible for loading and running the
web site, as well as any other associated CPU cores, in the case more than one are used. This can be the
case with web workers and shared processes e.g., a compositor or networking process, which some
[=user agents=] may use.
</p>
</li>
<li>