-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
ProfilingLink.tsx
326 lines (305 loc) · 8.32 KB
/
ProfilingLink.tsx
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
import {
Button,
Checkbox,
Dialog,
DialogContent,
DialogTitle,
FormControlLabel,
InputLabel,
Link,
MenuItem,
Select,
TextField,
Typography,
} from "@mui/material";
import createStyles from "@mui/styles/createStyles";
import makeStyles from "@mui/styles/makeStyles";
import React, { PropsWithChildren, useState } from "react";
import { HelpInfo } from "../components/Tooltip";
import { ClassNameProps } from "./props";
const useStyles = makeStyles((theme) =>
createStyles({
buttonLink: {
cursor: "pointer",
},
dialogContent: {
padding: "12px",
display: "flex",
justifyContent: "flex-end",
},
secondaryButton: {
textTransform: "capitalize",
color: "#5F6469",
},
}),
);
type CpuProfilingLinkProps = PropsWithChildren<
{
pid: string | number | null | undefined;
ip: string | null | undefined;
type: string | null;
} & ClassNameProps
>;
type TaskProfilingStackTraceProps = {
taskId: string | null | undefined;
attemptNumber: number;
nodeId: string;
};
type MemoryProfilingProps = PropsWithChildren<
{
pid: string | number | null | undefined;
ip: string | null | undefined;
type?: string | null;
} & ClassNameProps
>;
type TaskMemoryProfilingProps = {
taskId: string | null | undefined;
attemptNumber: number;
nodeId: string;
};
type MemoryProfilingButtonProps = {
profilerUrl: string;
type?: string | null;
};
export const TaskCpuProfilingLink = ({
taskId,
attemptNumber,
nodeId,
}: TaskProfilingStackTraceProps) => {
if (!taskId) {
return null;
}
return (
<Link
href={`task/cpu_profile?task_id=${taskId}&attempt_number=${attemptNumber}&node_id=${nodeId}`}
target="_blank"
title="Profile the Python worker for 5 seconds (default) and display a CPU flame graph."
rel="noreferrer"
>
CPU Flame Graph
</Link>
);
};
export const TaskCpuStackTraceLink = ({
taskId,
attemptNumber,
nodeId,
}: TaskProfilingStackTraceProps) => {
if (!taskId) {
return null;
}
return (
<Link
href={`task/traceback?task_id=${taskId}&attempt_number=${attemptNumber}&node_id=${nodeId}`}
target="_blank"
title="Sample the current Python stack trace for this worker."
rel="noreferrer"
>
Stack Trace
</Link>
);
};
export const CpuStackTraceLink = ({
pid,
ip,
type = "",
}: CpuProfilingLinkProps) => {
if (!pid || !ip || typeof pid === "undefined" || typeof ip === "undefined") {
return <div></div>;
}
return (
<Link
href={`worker/traceback?pid=${pid}&ip=${ip}&native=0`}
target="_blank"
title="Sample the current Python stack trace for this worker."
rel="noreferrer"
>
Stack Trace{type ? ` (${type})` : ""}
</Link>
);
};
export const CpuProfilingLink = ({
pid,
ip,
type = "",
}: CpuProfilingLinkProps) => {
if (!pid || !ip) {
return <div></div>;
}
return (
<Link
href={`worker/cpu_profile?pid=${pid}&ip=${ip}&duration=5&native=0`}
target="_blank"
title="Profile the Python worker for 5 seconds (default) and display a CPU flame graph."
rel="noreferrer"
>
CPU Flame Graph{type ? ` (${type})` : ""}
</Link>
);
};
export const ProfilerButton = ({
profilerUrl,
type,
}: MemoryProfilingButtonProps) => {
const [duration, setDuration] = useState(5);
const [leaks, setLeaks] = useState(true);
const [native, setNative] = useState(false);
const [allocator, setAllocator] = useState(false);
const [open, setOpen] = useState(false);
const [format, setFormat] = useState("flamegraph");
const handleOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
const buttonLinkClasses = useStyles();
return (
<div>
<Link
onClick={handleOpen}
aria-label="Memory Profiling"
className={buttonLinkClasses.buttonLink}
>
Memory Profiling{type ? ` (${type})` : ""}
</Link>
<Dialog open={open} onClose={handleClose}>
<DialogTitle>Memory Profiling Config</DialogTitle>
<DialogContent>
<InputLabel id="format-label">Format</InputLabel>
<Select
labelId="format-label"
id="format"
value={format}
aria-label={format}
onChange={(e) => setFormat(e.target.value as string)}
fullWidth
style={{ marginBottom: "12px" }}
>
<MenuItem value="flamegraph">Flamegraph</MenuItem>
<MenuItem value="table">Table</MenuItem>
</Select>
<TextField
label="Duration (seconds)"
type="number"
value={duration !== null ? duration : ""}
onChange={(e) => setDuration(parseInt(e.target.value, 10))}
required
/>
<br />
<FormControlLabel
control={
<Checkbox
checked={leaks}
onChange={(e) => setLeaks(e.target.checked)}
/>
}
label={
<div style={{ display: "flex", alignItems: "center" }}>
<span style={{ marginRight: "4px" }}>Leaks</span>
<HelpInfo>
<Typography>
Enable memory leaks, instead of peak memory usage. Refer to
Memray documentation for more details.
</Typography>
</HelpInfo>
</div>
}
/>
<br />
<FormControlLabel
control={
<Checkbox
checked={native}
onChange={(e) => setNative(e.target.checked)}
/>
}
label={
<div style={{ display: "flex", alignItems: "center" }}>
<span style={{ marginRight: "4px" }}>Native</span>
<HelpInfo>
<Typography>
Track native (C/C++) stack frames. Refer to Memray
documentation for more details.
</Typography>
</HelpInfo>
</div>
}
/>
<br />
<FormControlLabel
control={
<Checkbox
checked={allocator}
onChange={(e) => setAllocator(e.target.checked)}
/>
}
label={
<div style={{ display: "flex", alignItems: "center" }}>
<span style={{ marginRight: "4px" }}>
Python Allocator Tracing
</span>
<HelpInfo>
<Typography>
Record allocations made by the pymalloc allocator. Refer to
Memray documentation for more details.
</Typography>
</HelpInfo>
</div>
}
/>
</DialogContent>
<div className={buttonLinkClasses.dialogContent}>
<Button
onClick={handleClose}
variant="text"
className={buttonLinkClasses.secondaryButton}
>
Cancel
</Button>
<Button
color="primary"
variant="text"
onClick={handleClose}
style={{ textTransform: "capitalize" }}
>
<Link
href={
`${profilerUrl}&format=${format}&duration=${duration}` +
`&leaks=${leaks ? "1" : "0"}` +
`&native=${native ? "1" : "0"}` +
`&trace_python_alocators=${allocator ? "1" : "0"}`
}
rel="noreferrer"
target="_blank"
>
Generate report
</Link>
</Button>
</div>
</Dialog>
</div>
);
};
export const MemoryProfilingButton = ({
pid,
ip,
type = "",
}: MemoryProfilingProps) => {
if (!pid || !ip) {
return <div></div>;
}
const profilerUrl = `/memory_profile?pid=${pid}&ip=${ip}`;
return <ProfilerButton profilerUrl={profilerUrl} type={type} />;
};
export const TaskMemoryProfilingButton = ({
taskId,
attemptNumber,
nodeId,
}: TaskMemoryProfilingProps) => {
if (!taskId) {
return null;
}
const profilerUrl = `/memory_profile?task_id=${taskId}&attempt_number=${attemptNumber}&node_id=${nodeId}`;
return <ProfilerButton profilerUrl={profilerUrl} />;
};