-
Notifications
You must be signed in to change notification settings - Fork 256
/
jupyter_notebook.py
642 lines (506 loc) · 17.7 KB
/
jupyter_notebook.py
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
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the MIT license.
# See the LICENSE file in the project root for more information.
# See `docs/jupyter notebook.md` for how to use this file.
#%% setup cell (must run this)
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import List, Optional, Sequence
from src.analysis.analyze_joins import (
analyze_joins_all_gcs_for_jupyter,
analyze_joins_single_gc_for_jupyter,
StagesOrPhases,
)
from src.analysis.analyze_single import (
analyze_single_for_processed_trace,
analyze_single_gc_for_processed_trace_file,
SortGCsBy,
)
from src.analysis.chart_individual_gcs import (
chart_individual_gcs_for_jupyter,
chart_individual_gcs_histogram_for_jupyter,
)
from src.analysis.chart_utils import (
basic_chart,
BasicHistogram,
BasicLine,
BasicLineChart,
chart_histograms_from_fields,
chart_lines_from_fields,
chart_heaps,
Trace,
)
from src.analysis.condemned_reasons import (
show_brief_condemned_reasons_for_gc,
show_condemned_reasons_for_jupyter,
show_condemned_reasons_for_gc_for_jupyter,
)
from src.analysis.enums import Gens, GCType
from src.analysis.parse_metrics import (
parse_run_metrics_arg,
parse_single_gc_metric_arg,
parse_single_gc_metrics_arg,
parse_single_heap_metrics_arg,
)
from src.analysis.process_trace import ProcessedTraces, test_result_from_path
from src.analysis.report import diff_for_jupyter, report_reasons_for_jupyter
from src.analysis.single_gc_metrics import get_bytes_allocated_since_last_gc
from src.analysis.single_heap_metrics import ALL_GC_GENS
from src.analysis.trace_commands import print_events_for_jupyter
from src.analysis.types import ProcessedGC, ProcessedTrace, ProcessQuery, SpecialSampleKind
from src.commonlib.bench_file import Vary
from src.commonlib.collection_util import repeat
from src.commonlib.document import Cell, handle_doc, Row, single_table_document, Table
from src.commonlib.option import non_null
from src.commonlib.result_utils import unwrap
from src.commonlib.type_utils import enum_value, with_slots
from src.commonlib.util import add_extension, bytes_to_mb, get_percent
ALL_TRACES = ProcessedTraces()
def get_trace_with_everything(
path: Path, process: ProcessQuery = None, dont_cache: bool = False
) -> ProcessedTrace:
return unwrap(
ALL_TRACES.get(
test_result=test_result_from_path(path),
process=process,
# TODO: disabling mechanisms and join info for now
# as it doesn't work without updating TraceEvent
need_mechanisms_and_reasons=False,
need_join_info=False,
dont_cache=dont_cache,
)
)
class GCKind(Enum):
NGC0 = 0
NGC1 = 1
BGC = 2
NGC2 = 3
def get_kind(gc: ProcessedGC) -> GCKind:
return {
Gens.Gen0: GCKind.NGC0,
Gens.Gen1: GCKind.NGC1,
Gens.Gen2: GCKind.BGC if gc.Type == GCType.BackgroundGC else GCKind.NGC2,
}[gc.Generation]
def show_summary(trace: ProcessedTrace) -> None:
print(f"{trace.NumberGCs} GCs")
for k, v in trace.number_gcs_in_each_generation.items():
print(f" {k.name}: {v}")
print(f"{trace.HeapCount} heaps")
for m in ("HeapSizeAfterMB_Mean", "HeapSizeAfterMB_Max"):
print(f"{m}: {trace.unwrap_metric_from_name(m)}")
# TODO: how to get mean/max memory load? Is it possible?
metrics: Sequence[str] = ("PauseDurationMSec", "PromotedMBPerSec", "HeapSizeAfterMB")
num_metrics = len(metrics)
kind_to_metric_to_values: List[List[List[float]]] = [
[[] for _ in range(num_metrics)] for _ in range(4)
]
for gc in trace.gcs:
gc_kind = get_kind(gc)
for metric_index, metric in enumerate(metrics):
metric_index_to_values = kind_to_metric_to_values[enum_value(gc_kind)]
values = metric_index_to_values[metric_index]
values.append(gc.unwrap_metric_from_name(metric))
for kind in GCKind:
histograms: List[BasicHistogram] = []
for metric_index, metric in enumerate(metrics):
histograms.append(
BasicHistogram(
values=kind_to_metric_to_values[enum_value(kind)][metric_index],
name=metric,
x_label=kind.name,
)
)
basic_chart(histograms)
#%%
_BENCH = Path("bench")
_SUITE = Path("bench") / "suite"
_LOW_MEMORY_CONTAINER = _SUITE / "low_memory_container.yaml"
_OUT = add_extension(_LOW_MEMORY_CONTAINER, ".out")
_TRACE = get_trace_with_everything(_OUT / "a__only_config__tlgb0.2__0.yaml")
_TRACE2 = get_trace_with_everything(_OUT / "b__only_config__tlgb0.2__0.yaml")
#%% show summary
show_summary(_TRACE)
#%% analyze-single
handle_doc(
analyze_single_for_processed_trace(
_TRACE,
print_events=False,
run_metrics=parse_run_metrics_arg(("important",)),
gc_where_filter=lambda gc: True,
sort_gcs_by=SortGCsBy(metric=parse_single_gc_metric_arg("Number"), sort_reverse=False),
single_gc_metrics=parse_single_gc_metrics_arg(
("DurationMSec", "Generation", "Number", "StartMSec")
),
single_heap_metrics=parse_single_heap_metrics_arg(("InMB", "OutMB")),
show_first_n_gcs=5,
show_last_n_gcs=None,
show_reasons=False,
)
)
#%% analyze-single-gc
handle_doc(
analyze_single_gc_for_processed_trace_file(
_TRACE,
gc_number=42,
single_gc_metrics=parse_single_gc_metrics_arg(
("DurationMSec", "Generation", "Number", "StartMSec")
),
)
)
#%% analyze-joins-all-gcs
handle_doc(
analyze_joins_all_gcs_for_jupyter(
_TRACE, show_n_worst_stolen_time_instances=10, show_n_worst_joins=10
)
)
#%% analyze-joins-single-gc
handle_doc(
analyze_joins_single_gc_for_jupyter(
trace=_TRACE,
gc_number=42,
kind=StagesOrPhases.both,
only_stages_with_percent_time=5,
show_n_worst_stolen_time_instances=10,
)
)
#%% chart-individual-gcs
# matplotlib automatically outputs to jupyter notebook through magic
chart_individual_gcs_for_jupyter(
traces=(_TRACE,),
gc_where_filter=lambda gc: gc.index < 100,
x_single_gc_metric=parse_single_gc_metric_arg("Number"),
y_single_gc_metrics=parse_single_gc_metrics_arg(("DurationMSec",)),
single_heap_metrics=parse_single_heap_metrics_arg(()),
show_gen_as_xticks=True,
show_n_heaps=4,
)
#%% chart-individual-gcs-histogram
chart_individual_gcs_histogram_for_jupyter(
trace=_TRACE,
single_gc_metrics=parse_single_gc_metrics_arg(("DurationMSec",)),
gc_where_filter=lambda gc: gc.Generation == Gens.Gen0,
bins=16,
)
#%% diff
handle_doc(
diff_for_jupyter(
traces=ALL_TRACES,
trace_paths=(_LOW_MEMORY_CONTAINER,),
run_metrics=parse_run_metrics_arg(("important",)),
machines=None,
vary=Vary.config,
test_where=None,
sample_kind=SpecialSampleKind.median,
max_iterations=None,
metrics_as_columns=False,
no_summary=False,
# Only for metrics_as_columns
sort_by_metric=None,
min_difference_pct=5,
)
)
#%% report-reasons
handle_doc(
report_reasons_for_jupyter(
traces=ALL_TRACES,
bench_file_path=_LOW_MEMORY_CONTAINER,
process=("name:corerun",),
max_iterations=None,
)
)
#%% show-condemned-reasons
handle_doc(show_condemned_reasons_for_jupyter(trace=_TRACE, max_gcs=16))
#%% show-condemned-reasons-for-gc
handle_doc(show_condemned_reasons_for_gc_for_jupyter(trace=_TRACE, gc_number=42))
#%% show-condemned-reasons custom
def _show_condemned_reasons_for_gen2(trace: ProcessedTrace) -> None:
gcs = [gc for gc in trace.gcs if gc.IsGen2]
for gc in gcs:
print(f"gc {gc.Number}")
print(show_brief_condemned_reasons_for_gc(gc))
_show_condemned_reasons_for_gen2(_TRACE)
#%% print-events
print_events_for_jupyter(
path=non_null(_TRACE.test_result.trace_path), time_span_msec=(0, 100), include="cswitch"
)
#%% custom analysis
total_duration = sum(gc.DurationMSec for gc in _TRACE.gcs)
assert total_duration == _TRACE.unwrap_metric_from_name("DurationMSec_Sum")
print(
f"{len(_TRACE.gcs)} gcs * {total_duration / len(_TRACE.gcs)} msec avg = {total_duration} msec"
)
#%% custom charting
def _custom_chart() -> None:
xs = tuple(range(8))
basic_chart(
(
BasicLineChart(
lines=(
BasicLine(name="linear", xs=xs, ys=xs),
BasicLine(name="quadratic", xs=xs, ys=[x ** 2 for x in xs]),
),
x_label="x",
y_label="y",
),
BasicHistogram(values=[x for n in range(4) for x in repeat(n, n)], x_label="number"),
)
)
_custom_chart()
#%% more custom charting
# gen size of gen2 after a bgc
# free list space before and after a bgc
# Create a 'trace' which contains the data we want
@with_slots
@dataclass(frozen=True)
class MyGCData:
Number: int
start_time: float
duration_msec: float
alloced_mb: float
Gen2SizeBeforeMB: float
Gen2SizeAfterMB: float
Gen2FreeListSpaceBeforeMB: float
Gen2FreeListSpaceAfterMB: float
def get_data(trace: ProcessedTrace) -> Trace[MyGCData]:
data = []
for gc in _TRACE.gcs:
if gc.Generation == Gens.Gen2:
assert gc.IsConcurrent
data.append(
MyGCData(
Number=gc.Number,
start_time=gc.StartRelativeMSec,
duration_msec=gc.DurationMSec,
alloced_mb=gc.AllocedSinceLastGCMB,
Gen2SizeBeforeMB=gc.Gen2SizeBeforeMB,
Gen2SizeAfterMB=gc.Gen2SizeAfterMB,
Gen2FreeListSpaceBeforeMB=gc.Gen2FreeListSpaceBeforeMB,
Gen2FreeListSpaceAfterMB=gc.Gen2FreeListSpaceAfterMB,
)
)
return Trace(name=trace.name, data=data)
traces: Sequence[Trace[MyGCData]] = [get_data(trace) for trace in (_TRACE, _TRACE2)]
chart_lines_from_fields(
t=MyGCData,
traces=traces,
x_property_name="Number",
y_property_names=("Gen2FreeListSpaceBeforeMB", "Gen2FreeListSpaceAfterMB"),
)
#%%
chart_histograms_from_fields(
t=MyGCData, gcs=get_data(_TRACE).data, property_names=("duration_msec", "alloced_mb")
)
#%% per-heap custom charting
@with_slots
@dataclass(frozen=True)
class MyHeapData:
gen0_in_mb: float
gen0_out_mb: float
def _custom_chart_heaps(trace: ProcessedTrace) -> None:
heaps: Sequence[List[MyHeapData]] = [[] for _ in range(trace.HeapCount)]
for gc in trace.gcs:
if gc.Generation == Gens.Gen0:
for hp in gc.heaps:
gn = hp.gen(Gens.Gen0)
heaps[hp.index].append(MyHeapData(gen0_in_mb=gn.in_mb, gen0_out_mb=gn.out_mb))
chart_heaps(
t=MyHeapData,
heaps=heaps,
y_property_names=("gen0_in_mb", "gen0_out_mb"),
heap_indices=(0, 1),
xs=None,
x_label=None,
)
_custom_chart_heaps(_TRACE)
#%% another per-heap custom charting
@with_slots
@dataclass(frozen=True)
class _HeapData:
gc_number: int
budget_mb: float
allocated_mb: float
def _custom_chart_heaps_2(trace: ProcessedTrace) -> None:
# A different chart for each heap
heaps: Sequence[List[_HeapData]] = [[] for _ in range(trace.HeapCount)]
prev_non_free_size_after: List[int] = [0 for _ in range(trace.HeapCount)]
for gc in trace.gcs:
if gc.Generation != Gens.Gen2:
continue
budget_per_heap = gc.LOHBudgetMB / len(heaps)
for hp_i, hp in enumerate(gc.heaps):
# Want the difference in size before -- get prev gen2 gc
prev_size_after = prev_non_free_size_after[hp_i]
gen = hp.gen(Gens.GenLargeObj)
size_before_now = gen.non_free_size_before
size_after_now = gen.non_free_size_after
allocated_bytes = size_before_now - prev_size_after
prev_non_free_size_after[hp_i] = size_after_now
# gen.budget is before equalizing
heaps[hp_i].append(
_HeapData(
gc_number=gc.Number,
budget_mb=budget_per_heap,
allocated_mb=bytes_to_mb(allocated_bytes),
)
)
# Chart each hp
lines = []
for i, hp_data in enumerate(heaps):
xs = [d.gc_number for d in hp_data]
line0 = BasicLine(name="budget (MB)", xs=xs, ys=[d.budget_mb for d in hp_data])
line1 = BasicLine(name="allocated (MB)", xs=xs, ys=[d.allocated_mb for d in hp_data])
lines.append(BasicLineChart(name=f"hp{i}", lines=(line0, line1)))
basic_chart(lines)
_custom_chart_heaps_2(_TRACE)
#%% summary
show_summary(_TRACE)
#%% custom analysis
@with_slots
@dataclass(frozen=True)
class _SuspensionData:
PauseToStartMSec: float
DurationMSec: float
PauseDurationMSec: float
SuspendDurationMSec: float
PromotedMB: float
@property
def SuspensionPercent(self) -> float:
return get_percent(self.SuspendDurationMSec / self.PauseDurationMSec)
@property
def PromotedMBPerSec(self) -> float:
return self.PromotedMB / (self.PauseDurationMSec / 1000)
@property
def PctPauseFromSuspend(self) -> float:
return get_percent(self.SuspendDurationMSec / self.PauseToStartMSec)
def _custom2(trace: ProcessedTrace) -> None:
gen_to_suspension_datas: List[List[_SuspensionData]] = [[] for _ in range(3)]
for gc in trace.gcs:
if gc.PauseDurationMSec < 50:
continue
gen_to_suspension_datas[enum_value(gc.Generation)].append(
_SuspensionData(
PauseToStartMSec=gc.SuspendToGCStartMSec,
DurationMSec=gc.DurationMSec,
PauseDurationMSec=gc.PauseDurationMSec,
SuspendDurationMSec=gc.SuspendDurationMSec,
PromotedMB=gc.PromotedMB,
)
)
for gen in ALL_GC_GENS:
print(f"\n=== {gen.name} suspensions ===\n")
rows = []
for susp_data in sorted(
gen_to_suspension_datas[0], key=lambda sd: sd.DurationMSec, reverse=True
):
rows.append(
[
Cell(x)
for x in (
susp_data.PauseDurationMSec,
susp_data.DurationMSec,
# susp_data.PctPauseFromSuspend,
# susp_data.PauseToStartMSec,
susp_data.SuspendDurationMSec,
# susp_data.SuspensionPercent,
susp_data.PromotedMB,
susp_data.PromotedMBPerSec,
)
]
)
handle_doc(
single_table_document(
Table(
headers=(
"pause msec",
"duration msec",
# "pause %",
# "pause to start",
"suspend msec",
# "suspend %",
"promoted mb",
"promoted mb/sec",
),
rows=rows,
)
)
)
_custom2(_TRACE)
#%% another custom analysis
@with_slots
@dataclass(frozen=True)
class _GCData:
Number: int
MBSOHSinceLastGen2: Optional[float]
MBLOHSinceLastGen2: Optional[float]
Gen2BudgetMB: Optional[float]
LOHBudgetMB: Optional[float]
def _custom(trace: ProcessedTrace) -> None:
gen2_gcs = [gc for gc in trace.gcs if gc.IsGen2]
datas: List[_GCData] = []
for gc in gen2_gcs:
bytes_since_last_same_gen_gc = (
unwrap(
get_bytes_allocated_since_last_gc(trace.gcs, trace.gcs.index(gc), Gens.GenLargeObj)
)
if gc.IsGen2
else None
)
datas.append(
_GCData(
Number=gc.Number,
MBSOHSinceLastGen2=bytes_to_mb(
unwrap(
get_bytes_allocated_since_last_gc(trace.gcs, trace.gcs.index(gc), Gens.Gen2)
)
)
if gc.IsGen2
else None,
MBLOHSinceLastGen2=bytes_to_mb(non_null(bytes_since_last_same_gen_gc))
if gc.IsGen2
else None,
Gen2BudgetMB=gc.Gen2BudgetMB if gc.IsGen2 else None,
LOHBudgetMB=gc.LOHBudgetMB if gc.IsGen2 else None,
)
)
rows = []
for data in datas:
rows.append(
[
Cell(str(int(x))) if x is not None else Cell()
for x in (
data.Number,
data.MBSOHSinceLastGen2,
data.MBLOHSinceLastGen2,
data.Gen2BudgetMB,
data.LOHBudgetMB,
)
]
)
g2_numbers = ", ".join(str(gc.Number) for gc in gen2_gcs)
gens = f"Gen2 numbers are: {g2_numbers}"
doc = single_table_document(
Table(
text=gens,
headers=(
"number",
"MB on SOH since last gen2",
"MB on LOH since last gen2",
"gen2 budget MB",
"loh budget MB",
),
rows=rows,
)
)
handle_doc(doc)
_custom(_TRACE)
#%% yet more custom charting
def _more_custom(trace: ProcessedTrace) -> None:
rows: List[Row] = []
for gc in trace.gcs:
if not gc.IsBackground:
continue
rows.append((Cell(gc.Number), Cell(str(gc.reason))))
handle_doc(single_table_document(Table(headers=("number", "reason"), rows=rows)))
_more_custom(_TRACE)
# %%