zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
# DO NOT EDIT THIS FILE!
#
# This file is generated from the CDP specification. If you need to make
# changes, edit the generator and regenerate all of the modules.
#
# CDP domain: Memory (experimental)
from __future__ import annotations
from .util import event_class, T_JSON_DICT
from dataclasses import dataclass
import enum
import typing
 
class PressureLevel(enum.Enum):
    '''
    Memory pressure level.
    '''
    MODERATE = "moderate"
    CRITICAL = "critical"
 
    def to_json(self):
        return self.value
 
    @classmethod
    def from_json(cls, json):
        return cls(json)
 
 
@dataclass
class SamplingProfileNode:
    '''
    Heap profile sample.
    '''
    #: Size of the sampled allocation.
    size: float
 
    #: Total bytes attributed to this sample.
    total: float
 
    #: Execution stack at the point of allocation.
    stack: typing.List[str]
 
    def to_json(self):
        json = dict()
        json['size'] = self.size
        json['total'] = self.total
        json['stack'] = [i for i in self.stack]
        return json
 
    @classmethod
    def from_json(cls, json):
        return cls(
            size=float(json['size']),
            total=float(json['total']),
            stack=[str(i) for i in json['stack']],
        )
 
 
@dataclass
class SamplingProfile:
    '''
    Array of heap profile samples.
    '''
    samples: typing.List[SamplingProfileNode]
 
    modules: typing.List[Module]
 
    def to_json(self):
        json = dict()
        json['samples'] = [i.to_json() for i in self.samples]
        json['modules'] = [i.to_json() for i in self.modules]
        return json
 
    @classmethod
    def from_json(cls, json):
        return cls(
            samples=[SamplingProfileNode.from_json(i) for i in json['samples']],
            modules=[Module.from_json(i) for i in json['modules']],
        )
 
 
@dataclass
class Module:
    '''
    Executable module information
    '''
    #: Name of the module.
    name: str
 
    #: UUID of the module.
    uuid: str
 
    #: Base address where the module is loaded into memory. Encoded as a decimal
    #: or hexadecimal (0x prefixed) string.
    base_address: str
 
    #: Size of the module in bytes.
    size: float
 
    def to_json(self):
        json = dict()
        json['name'] = self.name
        json['uuid'] = self.uuid
        json['baseAddress'] = self.base_address
        json['size'] = self.size
        return json
 
    @classmethod
    def from_json(cls, json):
        return cls(
            name=str(json['name']),
            uuid=str(json['uuid']),
            base_address=str(json['baseAddress']),
            size=float(json['size']),
        )
 
 
def get_dom_counters() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,typing.Tuple[int, int, int]]:
    '''
 
 
    :returns: A tuple with the following items:
 
        0. **documents** - 
        1. **nodes** - 
        2. **jsEventListeners** - 
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.getDOMCounters',
    }
    json = yield cmd_dict
    return (
        int(json['documents']),
        int(json['nodes']),
        int(json['jsEventListeners'])
    )
 
 
def prepare_for_leak_detection() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
 
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.prepareForLeakDetection',
    }
    json = yield cmd_dict
 
 
def forcibly_purge_java_script_memory() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Simulate OomIntervention by purging V8 memory.
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.forciblyPurgeJavaScriptMemory',
    }
    json = yield cmd_dict
 
 
def set_pressure_notifications_suppressed(
        suppressed: bool
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Enable/disable suppressing memory pressure notifications in all processes.
 
    :param suppressed: If true, memory pressure notifications will be suppressed.
    '''
    params: T_JSON_DICT = dict()
    params['suppressed'] = suppressed
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.setPressureNotificationsSuppressed',
        'params': params,
    }
    json = yield cmd_dict
 
 
def simulate_pressure_notification(
        level: PressureLevel
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Simulate a memory pressure notification in all processes.
 
    :param level: Memory pressure level of the notification.
    '''
    params: T_JSON_DICT = dict()
    params['level'] = level.to_json()
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.simulatePressureNotification',
        'params': params,
    }
    json = yield cmd_dict
 
 
def start_sampling(
        sampling_interval: typing.Optional[int] = None,
        suppress_randomness: typing.Optional[bool] = None
    ) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Start collecting native memory profile.
 
    :param sampling_interval: *(Optional)* Average number of bytes between samples.
    :param suppress_randomness: *(Optional)* Do not randomize intervals between samples.
    '''
    params: T_JSON_DICT = dict()
    if sampling_interval is not None:
        params['samplingInterval'] = sampling_interval
    if suppress_randomness is not None:
        params['suppressRandomness'] = suppress_randomness
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.startSampling',
        'params': params,
    }
    json = yield cmd_dict
 
 
def stop_sampling() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
    '''
    Stop collecting native memory profile.
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.stopSampling',
    }
    json = yield cmd_dict
 
 
def get_all_time_sampling_profile() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,SamplingProfile]:
    '''
    Retrieve native memory allocations profile
    collected since renderer process startup.
 
    :returns: 
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.getAllTimeSamplingProfile',
    }
    json = yield cmd_dict
    return SamplingProfile.from_json(json['profile'])
 
 
def get_browser_sampling_profile() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,SamplingProfile]:
    '''
    Retrieve native memory allocations profile
    collected since browser process startup.
 
    :returns: 
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.getBrowserSamplingProfile',
    }
    json = yield cmd_dict
    return SamplingProfile.from_json(json['profile'])
 
 
def get_sampling_profile() -> typing.Generator[T_JSON_DICT,T_JSON_DICT,SamplingProfile]:
    '''
    Retrieve native memory allocations profile collected since last
    ``startSampling`` call.
 
    :returns: 
    '''
    cmd_dict: T_JSON_DICT = {
        'method': 'Memory.getSamplingProfile',
    }
    json = yield cmd_dict
    return SamplingProfile.from_json(json['profile'])