legion_prof 0.2406.0

Legion Prof: a profiler for Legion
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
use std::cmp::max;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, Mutex};

use legion_prof_viewer::{
    data::{
        Color32, DataSource, DataSourceDescription, DataSourceInfo, EntryID, EntryInfo, Field,
        FieldID, FieldSchema, Item, ItemLink, ItemMeta, ItemUID, Rgba, SlotMetaTile,
        SlotMetaTileData, SlotTile, SlotTileData, SummaryTile, SummaryTileData, TileID, TileSet,
        UtilPoint,
    },
    timestamp as ts,
};

#[cfg(debug_assertions)]
use log::info;

use slice_group_by::GroupBy;

use crate::backend::common::{
    ChanEntryFieldsPretty, ChanEntryShort, DimOrderPretty, FSpaceShort, FieldsPretty, ISpacePretty,
    InstShort, MemGroup, ProcGroup, SizePretty, StatePostprocess,
};
use crate::conditional_assert;
use crate::state::{
    ChanEntry, ChanID, Color, Config, Container, ContainerEntry, Copy, CopyInstInfo, DeviceKind,
    Fill, FillInstInfo, Inst, InstUID, MemID, MemKind, NodeID, OpID, ProcEntryKind, ProcID,
    ProcKind, ProfUID, State, TimeRange, Timestamp,
};

impl Into<ts::Timestamp> for Timestamp {
    fn into(self) -> ts::Timestamp {
        ts::Timestamp(self.to_ns().try_into().unwrap())
    }
}

impl Into<Timestamp> for ts::Timestamp {
    fn into(self) -> Timestamp {
        Timestamp::from_ns(self.0.try_into().unwrap())
    }
}

impl Into<ts::Interval> for TimeRange {
    fn into(self) -> ts::Interval {
        ts::Interval::new(self.start.unwrap().into(), self.stop.unwrap().into())
    }
}

impl Into<ItemUID> for ProfUID {
    fn into(self) -> ItemUID {
        ItemUID(self.0)
    }
}

impl Into<Color32> for Color {
    fn into(self) -> Color32 {
        Color32::from_rgb(
            ((self.0 >> 16) & 0xFF) as u8,
            ((self.0 >> 8) & 0xFF) as u8,
            (self.0 & 0xFF) as u8,
        )
    }
}

#[derive(Debug, Clone)]
enum EntryKind {
    ProcKind(ProcGroup),
    Proc(ProcID, Option<DeviceKind>),
    MemKind(MemGroup),
    Mem(MemID),
    ChanKind(Option<NodeID>),
    Chan(ChanID),
    DepPartKind(Option<NodeID>),
    DepPart(ChanID),
}

#[derive(Debug, Clone)]
struct ItemInfo {
    point_interval: ts::Interval,
    expand: bool,
}

#[derive(Debug, Clone)]
pub struct Fields {
    chan_reqs: FieldID,
    expanded_for_visibility: FieldID,
    operation: FieldID,
    insts: FieldID,
    inst_fields: FieldID,
    inst_fspace: FieldID,
    inst_ispace: FieldID,
    inst_layout: FieldID,
    size: FieldID,
    interval: FieldID,
    num_items: FieldID,
    provenance: FieldID,
    status_ready: FieldID,
    status_running: FieldID,
    status_waiting: FieldID,
    deferred_time: FieldID,
    delayed_time: FieldID,
    creator: FieldID,
    caller: FieldID,
    mapper: FieldID,
    mapper_proc: FieldID,
}

#[derive(Debug)]
pub struct StateDataSource {
    state: State,
    field_schema: FieldSchema,
    fields: Fields,
    info: EntryInfo,
    entry_map: BTreeMap<EntryID, EntryKind>,
    proc_entries: BTreeMap<ProcID, EntryID>,
    proc_groups: BTreeMap<ProcGroup, Vec<ProcID>>,
    mem_entries: BTreeMap<MemID, EntryID>,
    mem_groups: BTreeMap<MemGroup, Vec<MemID>>,
    chan_groups: BTreeMap<Option<NodeID>, Vec<ChanID>>,
    deppart_groups: BTreeMap<Option<NodeID>, Vec<ChanID>>,
    step_utilization_cache: Mutex<BTreeMap<EntryID, Arc<Vec<(Timestamp, f64)>>>>,
}

impl StateDataSource {
    pub fn new(state: State) -> Self {
        let mut field_schema = FieldSchema::new();

        let fields = Fields {
            chan_reqs: field_schema.insert("Requirements".to_owned(), true),
            expanded_for_visibility: field_schema
                .insert("(Expanded for Visibility)".to_owned(), false),
            operation: field_schema.insert("Operation".to_owned(), true),
            insts: field_schema.insert("Instances".to_owned(), true),
            inst_fields: field_schema.insert("Fields".to_owned(), true),
            inst_fspace: field_schema.insert("Field Space".to_owned(), true),
            inst_ispace: field_schema.insert("Index Space".to_owned(), true),
            inst_layout: field_schema.insert("Layout".to_owned(), true),
            interval: field_schema.insert("Lifetime".to_owned(), false),
            num_items: field_schema.insert("Number of Items".to_owned(), false),
            provenance: field_schema.insert("Provenance".to_owned(), true),
            size: field_schema.insert("Size".to_owned(), true),
            status_ready: field_schema.insert("Ready".to_owned(), false),
            status_running: field_schema.insert("Running".to_owned(), false),
            status_waiting: field_schema.insert("Waiting".to_owned(), false),
            deferred_time: field_schema.insert("Deferred".to_owned(), false),
            delayed_time: field_schema.insert("Delayed".to_owned(), false),
            creator: field_schema.insert("Creator".to_owned(), false),
            caller: field_schema.insert("Caller".to_owned(), false),
            mapper: field_schema.insert("Mapper".to_owned(), true),
            mapper_proc: field_schema.insert("Mapper Processor".to_owned(), true),
        };

        let mut entry_map = BTreeMap::<EntryID, EntryKind>::new();
        let mut proc_entries = BTreeMap::new();
        let mut mem_entries = BTreeMap::new();

        let mut proc_groups = state.group_procs();
        let mem_groups = state.group_mems();
        let chan_groups = state.group_chans();
        let deppart_groups = state.group_depparts();

        let mut nodes: BTreeSet<_> = proc_groups.keys().map(|ProcGroup(n, _, _)| *n).collect();
        let proc_kinds: BTreeSet<_> = proc_groups
            .keys()
            .map(|ProcGroup(_, k, d)| (*k, *d))
            .collect();
        let mem_kinds: BTreeSet<_> = mem_groups.keys().map(|MemGroup(_, k)| *k).collect();

        if !state.has_multiple_nodes() {
            nodes.remove(&None);
        }

        let mut node_slots = Vec::new();
        let root_id = EntryID::root();
        for node in &nodes {
            let node_short_name;
            let node_long_name;
            match node {
                Some(node_id) => {
                    node_short_name = format!("n{}", node_id.0);
                    node_long_name = format!("Node {}", node_id.0);
                }
                None => {
                    node_short_name = "all".to_owned();
                    node_long_name = "All Nodes".to_owned();
                }
            }
            let node_index = node_slots.len() as u64;
            let node_id = root_id.child(node_index);

            let mut kind_slots = Vec::new();
            let mut kind_index = 0;
            let mut node_empty = node.is_some();
            // Processors
            for (kind, device) in &proc_kinds {
                let group = ProcGroup(*node, *kind, *device);

                let procs = proc_groups.get(&group).unwrap();
                if node.is_some() {
                    // Don't render kind if all processors of the kind are empty
                    let empty = procs.iter().all(|p| state.procs.get(p).unwrap().is_empty());
                    node_empty = node_empty && empty;
                    if empty {
                        continue;
                    }
                }

                let kind_name = format!("{:?}", kind);
                let kind_first_letter = kind_name.chars().next().unwrap().to_lowercase();

                let short_suffix = match device {
                    Some(DeviceKind::Device) => "d",
                    Some(DeviceKind::Host) => "h",
                    None => "",
                };

                let medium_suffix = match device {
                    Some(DeviceKind::Device) => " dev",
                    Some(DeviceKind::Host) => " host",
                    None => "",
                };

                let long_suffix = match device {
                    Some(DeviceKind::Device) => " Device",
                    Some(DeviceKind::Host) => " Host",
                    None => "",
                };

                let kind_id = node_id.child(kind_index);
                kind_index += 1;

                let color = match (kind, device) {
                    (ProcKind::GPU, Some(DeviceKind::Device)) => Color::OLIVEDRAB,
                    (ProcKind::GPU, Some(DeviceKind::Host)) => Color::ORANGERED,
                    (ProcKind::CPU, None) => Color::STEELBLUE,
                    (ProcKind::Utility, None) => Color::CRIMSON,
                    (ProcKind::IO, None) => Color::ORANGERED,
                    (ProcKind::ProcGroup, None) => Color::ORANGERED,
                    (ProcKind::ProcSet, None) => Color::ORANGERED,
                    (ProcKind::OpenMP, None) => Color::ORANGERED,
                    (ProcKind::Python, None) => Color::OLIVEDRAB,
                    _ => unreachable!(),
                };
                let color: Color32 = color.into();

                let mut proc_slots = Vec::new();
                if node.is_some() {
                    let mut proc_index = 0;
                    for proc in procs {
                        let proc_id = kind_id.child(proc_index as u64);
                        entry_map.insert(proc_id.clone(), EntryKind::Proc(*proc, *device));
                        proc_entries.insert(*proc, proc_id);

                        let short_name = format!(
                            "{}{}{}",
                            kind_first_letter,
                            proc.proc_in_node(),
                            short_suffix
                        );
                        let long_name = format!(
                            "{} {} {}{}",
                            node_long_name,
                            kind_name,
                            proc.proc_in_node(),
                            long_suffix
                        );

                        let max_rows =
                            state.procs.get(proc).unwrap().max_levels(*device) as u64 + 1;
                        proc_slots.push(EntryInfo::Slot {
                            short_name,
                            long_name,
                            max_rows,
                        });
                        proc_index += 1;
                    }
                }

                let summary_id = kind_id.summary();
                entry_map.insert(summary_id, EntryKind::ProcKind(group));

                kind_slots.push(EntryInfo::Panel {
                    short_name: format!("{}{}", kind_name.to_lowercase(), medium_suffix),
                    long_name: format!("{} {}{}", node_long_name, kind_name, long_suffix),
                    summary: Some(Box::new(EntryInfo::Summary { color })),
                    slots: proc_slots,
                });
            }

            // Don't render node if all processors of the node are empty
            if node_empty {
                // Remove this node's processors from the all nodes list to
                // avoid influencing global utilization
                for (kind, device) in &proc_kinds {
                    let group = ProcGroup(None, *kind, *device);
                    proc_groups
                        .get_mut(&group)
                        .unwrap()
                        .retain(|p| p.node_id() != node.unwrap());
                }
                continue;
            }

            // Memories
            for kind in &mem_kinds {
                let group = MemGroup(*node, *kind);

                let Some(mems) = mem_groups.get(&group) else {
                    continue;
                };

                let kind_name = format!("{:?}", kind);
                let kind_first_letter = kind_name.chars().next().unwrap().to_lowercase();

                let kind_id = node_id.child(kind_index);
                kind_index += 1;

                let color = match kind {
                    MemKind::NoMemKind => unreachable!(),
                    MemKind::Global => Color::CRIMSON,
                    MemKind::System => Color::OLIVEDRAB,
                    MemKind::Registered => Color::DARKMAGENTA,
                    MemKind::Socket => Color::ORANGERED,
                    MemKind::ZeroCopy => Color::CRIMSON,
                    MemKind::Framebuffer => Color::BLUE,
                    MemKind::Disk => Color::DARKGOLDENROD,
                    MemKind::HDF5 => Color::OLIVEDRAB,
                    MemKind::File => Color::ORANGERED,
                    MemKind::L3Cache => Color::CRIMSON,
                    MemKind::L2Cache => Color::DARKMAGENTA,
                    MemKind::L1Cache => Color::OLIVEDRAB,
                    MemKind::GPUManaged => Color::DARKMAGENTA,
                    MemKind::GPUDynamic => Color::ORANGERED,
                };
                let color: Color32 = color.into();

                let mut mem_slots = Vec::new();
                if node.is_some() {
                    for (mem_index, mem) in mems.iter().enumerate() {
                        let mem_id = kind_id.child(mem_index as u64);
                        entry_map.insert(mem_id.clone(), EntryKind::Mem(*mem));
                        mem_entries.insert(*mem, mem_id);

                        let rows = state.mems.get(mem).unwrap().max_levels(None) as u64 + 1;
                        mem_slots.push(EntryInfo::Slot {
                            short_name: format!("{}{}", kind_first_letter, mem.mem_in_node()),
                            long_name: format!(
                                "{} {} {}",
                                node_long_name,
                                kind_name,
                                mem.mem_in_node()
                            ),
                            max_rows: rows,
                        });
                    }
                }

                let summary_id = kind_id.summary();
                entry_map.insert(summary_id, EntryKind::MemKind(group));

                kind_slots.push(EntryInfo::Panel {
                    short_name: kind_name.to_lowercase(),
                    long_name: format!("{} {}", node_long_name, kind_name),
                    summary: Some(Box::new(EntryInfo::Summary { color })),
                    slots: mem_slots,
                });
            }

            // Channels (except for Dependent Partitioning)
            loop {
                let Some(chans) = chan_groups.get(node) else {
                    break;
                };

                let kind_id = node_id.child(kind_index);
                kind_index += 1;

                let color: Color32 = Color::ORANGERED.into();

                let mut chan_slots = Vec::new();
                if node.is_some() {
                    for (chan_index, chan) in chans.iter().enumerate() {
                        let chan_id = kind_id.child(chan_index as u64);
                        entry_map.insert(chan_id, EntryKind::Chan(*chan));

                        let (src_name, src_short) = match chan {
                            ChanID::Copy { src, .. } | ChanID::Scatter { src } => {
                                let kind = state.mems.get(&src).unwrap().kind;
                                let kind_first_letter =
                                    format!("{:?}", kind).chars().next().unwrap().to_lowercase();
                                let src_node = src.node_id().0;
                                (
                                    Some(format!(
                                        "Node {} {:?} {}",
                                        src_node,
                                        kind,
                                        src.mem_in_node()
                                    )),
                                    Some(format!(
                                        "n{}{}{}",
                                        src_node,
                                        kind_first_letter,
                                        src.mem_in_node()
                                    )),
                                )
                            }
                            _ => (None, None),
                        };

                        let (dst_name, dst_short) = match chan {
                            ChanID::Copy { dst, .. }
                            | ChanID::Fill { dst }
                            | ChanID::Gather { dst } => {
                                let kind = state.mems.get(&dst).unwrap().kind;
                                let kind_first_letter =
                                    format!("{:?}", kind).chars().next().unwrap().to_lowercase();
                                let dst_node = dst.node_id().0;
                                (
                                    Some(format!(
                                        "Node {} {:?} {}",
                                        dst_node,
                                        kind,
                                        dst.mem_in_node()
                                    )),
                                    Some(format!(
                                        "n{}{}{}",
                                        dst_node,
                                        kind_first_letter,
                                        dst.mem_in_node()
                                    )),
                                )
                            }
                            _ => (None, None),
                        };

                        let short_name = match chan {
                            ChanID::Copy { .. } => {
                                format!("{}-{}", src_short.unwrap(), dst_short.unwrap())
                            }
                            ChanID::Fill { .. } => format!("f {}", dst_short.unwrap()),
                            ChanID::Gather { .. } => format!("g {}", dst_short.unwrap()),
                            ChanID::Scatter { .. } => format!("s {}", src_short.unwrap()),
                            ChanID::DepPart { .. } => unreachable!(),
                        };

                        let long_name = match chan {
                            ChanID::Copy { .. } => {
                                format!("{} to {}", src_name.unwrap(), dst_name.unwrap())
                            }
                            ChanID::Fill { .. } => format!("Fill {}", dst_name.unwrap()),
                            ChanID::Gather { .. } => format!("Gather to {}", dst_name.unwrap()),
                            ChanID::Scatter { .. } => {
                                format!("Scatter from {}", src_name.unwrap())
                            }
                            ChanID::DepPart { .. } => unreachable!(),
                        };

                        let rows = state.chans.get(chan).unwrap().max_levels(None) as u64 + 1;
                        chan_slots.push(EntryInfo::Slot {
                            short_name,
                            long_name,
                            max_rows: rows,
                        });
                    }
                }

                let summary_id = kind_id.summary();
                entry_map.insert(summary_id, EntryKind::ChanKind(*node));

                kind_slots.push(EntryInfo::Panel {
                    short_name: "chan".to_owned(),
                    long_name: format!("{} Channel", node_long_name),
                    summary: Some(Box::new(EntryInfo::Summary { color })),
                    slots: chan_slots,
                });

                break;
            }

            // Dependent Partitioning Channels
            loop {
                let Some(chans) = deppart_groups.get(node) else {
                    break;
                };

                let kind_id = node_id.child(kind_index);

                let color: Color32 = Color::ORANGERED.into();

                let mut deppart_slots = Vec::new();
                if node.is_some() {
                    for (chan_index, chan) in chans.iter().enumerate() {
                        let chan_id = kind_id.child(chan_index as u64);
                        entry_map.insert(chan_id, EntryKind::DepPart(*chan));

                        let short_name = match chan {
                            ChanID::DepPart { node_id } => format!("dp{}", node_id.0),
                            _ => unreachable!(),
                        };

                        let long_name = match chan {
                            ChanID::DepPart { node_id } => {
                                format!("Dependent Partitioning {}", node_id.0)
                            }
                            _ => unreachable!(),
                        };

                        let rows = state.chans.get(chan).unwrap().max_levels(None) as u64 + 1;
                        deppart_slots.push(EntryInfo::Slot {
                            short_name,
                            long_name,
                            max_rows: rows,
                        });
                    }
                }

                let summary_id = kind_id.summary();
                entry_map.insert(summary_id, EntryKind::DepPartKind(*node));

                kind_slots.push(EntryInfo::Panel {
                    short_name: "dp".to_owned(),
                    long_name: format!("{} Dependent Partitioning", node_long_name),
                    summary: Some(Box::new(EntryInfo::Summary { color })),
                    slots: deppart_slots,
                });

                break;
            }
            node_slots.push(EntryInfo::Panel {
                short_name: node_short_name,
                long_name: node_long_name,
                summary: None,
                slots: kind_slots,
            });
        }

        let info = EntryInfo::Panel {
            short_name: "root".to_owned(),
            long_name: "root".to_owned(),
            summary: None,
            slots: node_slots,
        };

        Self {
            state,
            field_schema,
            fields,
            info,
            entry_map,
            proc_entries,
            proc_groups,
            mem_entries,
            mem_groups,
            chan_groups,
            deppart_groups,
            step_utilization_cache: Mutex::new(BTreeMap::new()),
        }
    }
}

impl StateDataSource {
    /// A step utilization is a series of step functions. At time T, the
    /// utilization takes value U. That value continues until the next
    /// step. This is a good match for Legion's discrete execution model (a
    /// task is either running, or it is not), but doesn't play so well with
    /// interpolation and level of detail. We compute this first because it's
    /// how the profiler internally represents utilization, but we convert it
    /// to a more useful format below.
    fn generate_step_utilization(&self, entry_id: &EntryID) -> Arc<Vec<(Timestamp, f64)>> {
        // This is an INTENTIONAL race; if two requests for the same entry
        // arrive simultaneously, we'll miss in the cache on both and compute
        // the utilization twice. The result should be the same, so this is
        // mostly wasted computation (in exchange for enabling parallelism).

        let cache = &self.step_utilization_cache;
        if let Some(util) = cache.lock().unwrap().get(entry_id) {
            return util.clone();
        }

        let group_kind = self.entry_map.get(entry_id).unwrap();
        let step_utilization = match group_kind {
            EntryKind::ProcKind(group) => {
                let ProcGroup(_, _, device) = *group;
                let procs = self.proc_groups.get(group).unwrap();
                let points = self.state.proc_group_timepoints(device, procs);
                let count = procs.len() as u64;
                let owners: BTreeSet<_> = procs
                    .iter()
                    .zip(points.iter())
                    .filter(|(_, tp)| !tp.is_empty())
                    .map(|(proc_id, _)| *proc_id)
                    .collect();

                if owners.is_empty() {
                    Vec::new()
                } else {
                    let mut utilizations = Vec::new();
                    for tp in points {
                        if !tp.is_empty() {
                            self.state
                                .convert_points_to_utilization(tp, &mut utilizations);
                        }
                    }
                    utilizations.sort_by_key(|point| point.time_key());
                    self.state
                        .calculate_proc_utilization_data(utilizations, owners, count)
                }
            }
            EntryKind::MemKind(group) => {
                let mems = self.mem_groups.get(group).unwrap();
                let points = self.state.mem_group_timepoints(mems);
                let owners: BTreeSet<_> = mems
                    .iter()
                    .zip(points.iter())
                    .filter(|(_, tp)| !tp.is_empty())
                    .map(|(mem_id, _)| *mem_id)
                    .collect();

                if owners.is_empty() {
                    Vec::new()
                } else {
                    let mut utilizations: Vec<_> = points
                        .iter()
                        .filter(|tp| !tp.is_empty())
                        .flat_map(|tp| *tp)
                        .collect();
                    utilizations.sort_by_key(|point| point.time_key());
                    self.state
                        .calculate_mem_utilization_data(utilizations, owners)
                }
            }
            EntryKind::ChanKind(node) | EntryKind::DepPartKind(node) => {
                let chans = match group_kind {
                    EntryKind::ChanKind(..) => self.chan_groups.get(node).unwrap(),
                    EntryKind::DepPartKind(..) => self.deppart_groups.get(node).unwrap(),
                    _ => unreachable!(),
                };
                let points = self.state.chan_group_timepoints(chans);
                let owners: BTreeSet<_> = chans
                    .iter()
                    .zip(points.iter())
                    .filter(|(_, tp)| !tp.is_empty())
                    .map(|(chan_id, _)| *chan_id)
                    .collect();

                if owners.is_empty() {
                    Vec::new()
                } else {
                    let mut utilizations = Vec::new();
                    for tp in points {
                        if !tp.is_empty() {
                            self.state
                                .convert_points_to_utilization(tp, &mut utilizations);
                        }
                    }
                    utilizations.sort_by_key(|point| point.time_key());
                    self.state
                        .calculate_chan_utilization_data(utilizations, owners)
                }
            }
            _ => unreachable!(),
        };
        let result = Arc::new(step_utilization);
        cache
            .lock()
            .unwrap()
            .insert(entry_id.clone(), result.clone());
        result
    }

    /// Converts the step utilization into a sample utilization, where each
    /// utilization point (sample) represents the average utilization over a
    /// certain time interval. The sample is located in the middle of the
    /// interval.
    fn compute_sample_utilization(
        step_utilization: &Vec<(Timestamp, f64)>,
        interval: ts::Interval,
        samples: u64,
    ) -> Vec<UtilPoint> {
        let start_time = interval.start.0 as u64;
        let duration = interval.duration_ns() as u64;

        let first_index = step_utilization
            .partition_point(|&(t, _)| {
                let t: ts::Timestamp = t.into();
                t < interval.start
            })
            .saturating_sub(1);

        let mut last_index = step_utilization[first_index..].partition_point(|&(t, _)| {
            let t: ts::Timestamp = t.into();
            t < interval.stop
        }) + first_index;
        if last_index + 1 < step_utilization.len() {
            last_index = last_index + 1;
        }

        let mut utilization = Vec::new();
        let mut last_t = 0u64;
        let mut last_u = 0.0;
        let mut step_it = step_utilization[first_index..last_index].iter().peekable();
        for sample in 0..samples {
            let sample_start = duration * sample / samples + start_time;
            let sample_stop = duration * (sample + 1) / samples + start_time;
            if sample_stop - sample_start == 0 {
                continue;
            }

            let mut sample_util = 0.0;
            while let Some((t, u)) = step_it.next_if(|(t, _)| t.to_ns() < sample_stop) {
                if t.to_ns() < sample_start {
                    (last_t, last_u) = (t.to_ns(), *u);
                    continue;
                }

                // This is a step utilization. So utilization u begins on time
                // t. That means the previous utilization stop at time t-1.
                let last_duration = (t.to_ns() - 1).saturating_sub(last_t.max(sample_start));
                sample_util += last_duration as f64 * last_u;

                (last_t, last_u) = (t.to_ns(), *u);
            }
            if last_t < sample_stop {
                let last_duration = sample_stop - last_t.max(sample_start);
                sample_util += last_duration as f64 * last_u;
            }

            sample_util = sample_util / (sample_stop - sample_start) as f64;
            assert!(sample_util <= 1.0);
            utilization.push(UtilPoint {
                time: Timestamp::from_ns((sample_start + sample_stop) / 2).into(),
                util: sample_util as f32,
            });
        }
        utilization
    }

    /// Items smaller than this should be expanded (and merged, if suitable
    /// nearby items are found)
    const MAX_RATIO: f64 = 2000.0;

    /// Items larger than this should NOT be merged, even if nearby an expanded
    /// item
    const MIN_RATIO: f64 = 1000.0;

    /// Expand small items to improve visibility
    fn expand_item(
        interval: &mut ts::Interval,
        tile_id: TileID,
        last: Option<&Item>,
        merged: u64,
    ) -> bool {
        let view_ratio = tile_id.0.duration_ns() as f64 / interval.duration_ns() as f64;

        let expand = view_ratio > Self::MAX_RATIO;
        if expand {
            let min_duration = tile_id.0.duration_ns() as f64 / Self::MAX_RATIO;
            let center = (interval.start.0 + interval.stop.0) as f64 / 2.0;
            let start = ts::Timestamp((center - min_duration / 2.0) as i64);
            let stop = ts::Timestamp(start.0 + min_duration as i64);
            *interval = ts::Interval::new(start, stop);

            // If the previous task is large (and overlaps), shrink to avoid overlapping it
            if let Some(last) = last {
                let last_ratio =
                    tile_id.0.duration_ns() as f64 / last.interval.duration_ns() as f64;
                if interval.overlaps(last.interval) && last_ratio < Self::MIN_RATIO {
                    if merged > 0 {
                        // It's already a merged task, ok to keep merging
                    } else {
                        interval.start = last.interval.stop;
                    }
                }
            }
        }
        expand
    }

    /// Merge small tasks to reduce load on renderer
    fn merge_items(
        interval: ts::Interval,
        tile_id: TileID,
        last: &mut Item,
        last_meta: Option<&mut ItemMeta>,
        num_items_field: FieldID,
        merged: &mut u64,
    ) -> bool {
        // Check for overlap with previous task. If so, either one or the
        // other task was expanded (since tasks don't normally overlap)
        // and this is a good opportunity to combine them.
        if last.interval.overlaps(interval) {
            // If the current task is large, don't merge. Instead,
            // just modify the previous task so it doesn't overlap
            let view_ratio = tile_id.0.duration_ns() as f64 / interval.duration_ns() as f64;
            if view_ratio < Self::MIN_RATIO {
                last.interval.stop = interval.start;
            } else {
                last.interval.stop = interval.stop;
                last.color = Color::GRAY.into();
                if let Some(last_meta) = last_meta {
                    if let Some((_, Field::U64(value))) = last_meta.fields.get_mut(0) {
                        *value += 1;
                    } else {
                        last_meta.title = "Merged Tasks".to_owned();
                        last_meta.fields = vec![(num_items_field, Field::U64(2))];
                    }
                }
                *merged += 1;
                return true;
            }
        }
        *merged = 0;
        false
    }

    fn build_items<C>(
        &self,
        cont: &C,
        device: Option<DeviceKind>,
        tile_id: TileID,
        full: bool,
        mut item_metas: Option<&mut Vec<Vec<ItemMeta>>>,
        get_meta: impl Fn(&C::Entry, ItemInfo) -> ItemMeta,
    ) -> Vec<Vec<Item>>
    where
        C: Container,
    {
        let mut items: Vec<Vec<Item>> = Vec::new();
        let mut merged = Vec::new();
        let levels = cont.max_levels(device) as usize + 1;
        items.resize_with(levels, Vec::new);
        if let Some(ref mut item_metas) = item_metas {
            item_metas.resize_with(levels, Vec::new);
        }
        merged.resize(levels, 0u64);
        let points_stacked = cont.time_points_stacked(device);

        for (level, points) in points_stacked.iter().enumerate() {
            let items = &mut items[level];
            let mut item_metas = item_metas.as_mut().map(|m| &mut m[level]);
            let merged = &mut merged[level];

            let first_index = points.partition_point(|p| {
                let stop: ts::Timestamp = cont.entry(p.entry).time_range().stop.unwrap().into();
                ts::Timestamp(stop.0.saturating_sub(1)) < tile_id.0.start
            });
            let last_index = points[first_index..].partition_point(|p| {
                let start: ts::Timestamp = cont.entry(p.entry).time_range().start.unwrap().into();
                start < tile_id.0.stop
            }) + first_index;

            #[cfg(debug_assertions)]
            {
                info!("Debug assertions enabled: checking point overlap. This can be expensive.");
                for point in &points[..first_index] {
                    let time_range = cont.entry(point.entry).time_range();
                    let point_interval: ts::Interval = time_range.into();
                    assert!(!point_interval.overlaps(tile_id.0));
                }
                for point in &points[last_index..] {
                    let time_range = cont.entry(point.entry).time_range();
                    let point_interval: ts::Interval = time_range.into();
                    assert!(!point_interval.overlaps(tile_id.0));
                }
            }

            for point in &points[first_index..last_index] {
                assert!(point.first);

                let entry = cont.entry(point.entry);
                let (base, time_range, waiters) =
                    (&entry.base(), entry.time_range(), &entry.waiters());

                let point_interval: ts::Interval = time_range.into();
                assert!(point_interval.overlaps(tile_id.0));
                let mut view_interval = point_interval.intersection(tile_id.0);

                assert_eq!(level, base.level.unwrap() as usize);

                let expand =
                    !full && Self::expand_item(&mut view_interval, tile_id, items.last(), *merged);

                if let Some(last) = items.last_mut() {
                    let last_meta = if let Some(ref mut item_metas) = item_metas {
                        item_metas.last_mut()
                    } else {
                        None
                    };
                    if Self::merge_items(
                        view_interval,
                        tile_id,
                        last,
                        last_meta,
                        self.fields.num_items,
                        merged,
                    ) {
                        continue;
                    }
                }

                let color = entry.color(&self.state);
                let color: Color32 = color.into();
                let color: Rgba = color.into();

                let item_meta = item_metas.as_ref().map(|_| {
                    get_meta(
                        entry,
                        ItemInfo {
                            point_interval,
                            expand,
                        },
                    )
                });

                let mut add_item =
                    |interval: ts::Interval, opacity: f32, status: Option<FieldID>| {
                        if !interval.overlaps(tile_id.0) {
                            return;
                        }
                        let view_interval = interval.intersection(tile_id.0);
                        let color =
                            (Rgba::WHITE.multiply(1.0 - opacity) + color.multiply(opacity)).into();
                        let item = Item {
                            item_uid: base.prof_uid.into(),
                            interval: view_interval,
                            color,
                        };
                        items.push(item);
                        if let Some(ref mut item_metas) = item_metas {
                            let mut item_meta = item_meta.clone().unwrap();
                            if let Some(status) = status {
                                item_meta
                                    .fields
                                    .insert(1, (status, Field::Interval(interval)));
                            }
                            item_metas.push(item_meta);
                        }
                    };
                if let Some(waiters) = waiters {
                    let mut start = time_range.start.unwrap();
                    for wait in &waiters.wait_intervals {
                        let running_interval = ts::Interval::new(start.into(), wait.start.into());
                        let waiting_interval =
                            ts::Interval::new(wait.start.into(), wait.ready.into());
                        let ready_interval = ts::Interval::new(wait.ready.into(), wait.end.into());
                        add_item(running_interval, 1.0, Some(self.fields.status_running));
                        add_item(waiting_interval, 0.15, Some(self.fields.status_waiting));
                        add_item(ready_interval, 0.45, Some(self.fields.status_ready));
                        start = max(start, wait.end);
                    }
                    let stop = time_range.stop.unwrap();
                    if start < stop {
                        let running_interval = ts::Interval::new(start.into(), stop.into());
                        add_item(running_interval, 1.0, Some(self.fields.status_running));
                    }
                } else {
                    add_item(view_interval, 1.0, None);
                }
            }
        }
        items
    }

    fn generate_proc_slot_tile(
        &self,
        entry_id: &EntryID,
        proc_id: ProcID,
        device: Option<DeviceKind>,
        tile_id: TileID,
        full: bool,
    ) -> SlotTile {
        let proc = self.state.procs.get(&proc_id).unwrap();
        let items = self.build_items(proc, device, tile_id, full, None, |_, _| unreachable!());
        SlotTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotTileData { items },
        }
    }

    fn generate_op_link(&self, op_id: OpID) -> Field {
        if let Some(proc_id) = self.state.tasks.get(&op_id) {
            if let Some(proc) = self.state.procs.get(proc_id) {
                let op = proc.find_task(op_id).unwrap();
                let op_name = op.name(&self.state);
                return Field::ItemLink(ItemLink {
                    item_uid: op.base().prof_uid.into(),
                    title: op_name,
                    interval: op.time_range().into(),
                    entry_id: self.proc_entries.get(proc_id).unwrap().clone(),
                });
            }
        }
        if let Some(task) = self.state.multi_tasks.get(&op_id) {
            if let Some(kind) = self.state.task_kinds.get(&task.task_id) {
                if let Some(name) = &kind.name {
                    return Field::String(format!("Task {}<{}>", name, op_id.0));
                }
            }
        }
        if let Some(op) = self.state.find_op(op_id) {
            if let Some(kind) = op.kind {
                return Field::String(format!(
                    "{} Operation<{}>",
                    self.state.op_kinds[&kind].name, op_id.0
                ));
            }
        }
        Field::U64(op_id.0.get())
    }

    fn generate_inst_link(&self, inst_uid: InstUID, prefix: &str) -> Option<Field> {
        let mem_id = self.state.insts.get(&inst_uid)?;
        let mem = self.state.mems.get(mem_id)?;
        let inst = mem.insts.get(&inst_uid)?;

        Some(Field::ItemLink(ItemLink {
            item_uid: inst.base().prof_uid.into(),
            title: format!("{}0x{:x}", prefix, inst.inst_id.unwrap().0),
            interval: inst.time_range().into(),
            entry_id: self.mem_entries.get(mem_id).unwrap().clone(),
        }))
    }

    fn generate_creator_link(&self, prof_uid: ProfUID, create_time: Timestamp) -> Field {
        let proc_id = self.state.prof_uid_proc.get(&prof_uid).unwrap();
        let proc = self.state.procs.get(&proc_id).unwrap();
        let mut entry = proc.find_entry(prof_uid).unwrap();
        // Check to see if we need to link one of the subcalls instead
        // of the main task that produced the this operation
        // Subcalls are sorted from smallest to largest so the first one we hit
        // is the one we know that that actually made this box
        for (call_uid, start_time, stop_time) in &entry.subcalls {
            if (*start_time <= create_time) && (create_time < *stop_time) {
                entry = proc.find_entry(*call_uid).unwrap();
                break;
            }
        }
        let op_name = entry.name(&self.state);
        Field::ItemLink(ItemLink {
            item_uid: entry.base().prof_uid.into(),
            title: op_name,
            interval: entry.time_range().into(),
            entry_id: self.proc_entries.get(proc_id).unwrap().clone(),
        })
    }

    fn generate_proc_slot_meta_tile(
        &self,
        entry_id: &EntryID,
        proc_id: ProcID,
        device: Option<DeviceKind>,
        tile_id: TileID,
        full: bool,
    ) -> SlotMetaTile {
        let proc = self.state.procs.get(&proc_id).unwrap();
        let mut m: Vec<Vec<ItemMeta>> = Vec::new();
        let items = self.build_items(proc, device, tile_id, full, Some(&mut m), |entry, info| {
            let ItemInfo {
                point_interval,
                expand,
            } = info;

            let name = entry.name(&self.state);
            let provenance = entry.provenance(&self.state);

            let mut fields = Vec::new();
            if expand {
                fields.push((self.fields.expanded_for_visibility, Field::Empty));
            }
            fields.push((self.fields.interval, Field::Interval(point_interval)));
            if let Some(initiation_op) = entry.initiation_op {
                // FIXME: You might think that initiation_op is None rather than
                // needing this check with zero, but backwards compatibility is hard
                // You can remove this check once we stop needing to be compatible with Python
                if initiation_op != OpID::ZERO {
                    fields.push((self.fields.operation, self.generate_op_link(initiation_op)));
                }
            }
            if let Some(op_id) = entry.op_id {
                let op = self.state.find_op(op_id).unwrap();
                let inst_set: BTreeSet<_> =
                    op.operation_inst_infos.iter().map(|i| i.inst_uid).collect();

                let insts: Vec<_> = inst_set
                    .iter()
                    .flat_map(|i| {
                        let result = self.generate_inst_link(*i, "");
                        conditional_assert!(
                            result.is_some(),
                            Config::all_logs(),
                            "Cannot find instance 0x{:x}",
                            i.0
                        );
                        result
                    })
                    .collect();
                fields.push((self.fields.insts, Field::Vec(insts)));
            }
            if let Some(provenance) = provenance {
                fields.push((
                    self.fields.provenance,
                    Field::String(provenance.to_string()),
                ));
            }
            if let Some(creator) = self.state.fevents.get(&entry.creator) {
                // Check to see if these are function calls or tasks
                match entry.kind {
                    ProcEntryKind::MapperCall(..)
                    | ProcEntryKind::RuntimeCall(_)
                    | ProcEntryKind::ApplicationCall(_)
                    | ProcEntryKind::GPUKernel(_, _) => {
                        if let Some(start_time) = entry.time_range.start {
                            fields.push((
                                self.fields.caller,
                                // Use the first tick before the start so it is outside
                                // of our box but hopefully in the caller's box
                                self.generate_creator_link(*creator, start_time - Timestamp::ONE),
                            ));
                        }
                    }
                    _ => {
                        // Everything else can use the create time to find the creator
                        if let Some(create_time) = entry.time_range.create {
                            fields.push((
                                self.fields.creator,
                                self.generate_creator_link(*creator, create_time),
                            ));
                        }
                    }
                }
            }
            match entry.kind {
                ProcEntryKind::MapperCall(mapper_id, mapper_proc, _) => {
                    let mapper = self.state.mappers.get(&(mapper_id, mapper_proc)).unwrap();
                    fields.push((self.fields.mapper, Field::String(mapper.name.to_owned())));
                    if let Some(proc) = self.state.procs.get(&mapper_proc) {
                        let proc_name = format!(
                            "Node {} {:?} {}",
                            mapper_proc.node_id().0,
                            proc.kind,
                            mapper_proc.proc_in_node()
                        );
                        fields.push((self.fields.mapper_proc, Field::String(proc_name)));
                    } else {
                        let proc_name = format!("Node {}", mapper_proc.node_id().0);
                        fields.push((self.fields.mapper_proc, Field::String(proc_name)));
                    }
                }
                _ => {}
            }
            if let Some(ready) = entry.time_range.ready {
                if let Some(create) = entry.time_range.create {
                    fields.push((
                        self.fields.deferred_time,
                        Field::Interval(ts::Interval::new(create.into(), ready.into())),
                    ));
                }
                if let Some(start) = entry.time_range.start {
                    fields.push((
                        self.fields.delayed_time,
                        Field::Interval(ts::Interval::new(ready.into(), start.into())),
                    ));
                }
            }
            ItemMeta {
                item_uid: entry.base().prof_uid.into(),
                title: name,
                original_interval: point_interval,
                fields,
            }
        });
        assert_eq!(items.len(), m.len());
        for (item_row, item_meta_row) in items.iter().zip(m.iter()) {
            assert_eq!(item_row.len(), item_meta_row.len());
        }
        SlotMetaTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotMetaTileData { items: m },
        }
    }

    fn generate_mem_slot_tile(
        &self,
        entry_id: &EntryID,
        mem_id: MemID,
        tile_id: TileID,
        full: bool,
    ) -> SlotTile {
        let mem = self.state.mems.get(&mem_id).unwrap();
        let items = self.build_items(mem, None, tile_id, full, None, |_, _| unreachable!());
        SlotTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotTileData { items },
        }
    }

    fn generate_inst_regions(&self, inst: &Inst, result: &mut Vec<(FieldID, Field)>) {
        for (ispace_id, fspace_id) in inst.ispace_ids.iter().zip(inst.fspace_ids.iter()) {
            let ispace = format!("{}", ISpacePretty(*ispace_id, &self.state),);
            result.push((self.fields.inst_ispace, Field::String(ispace)));

            let fspace = self.state.field_spaces.get(&fspace_id).unwrap();
            let fspace_name = format!("{}", FSpaceShort(&fspace));
            result.push((self.fields.inst_fspace, Field::String(fspace_name)));

            let fields = format!("{}", FieldsPretty(&fspace, inst));
            result.push((self.fields.inst_fields, Field::String(fields)));
        }
    }

    fn generate_inst_layout(&self, inst: &Inst, result: &mut Vec<(FieldID, Field)>) {
        let layout = format!("{}", DimOrderPretty(inst, false));
        result.push((self.fields.inst_layout, Field::String(layout)));
    }

    fn generate_inst_size(&self, inst: &Inst, result: &mut Vec<(FieldID, Field)>) {
        let size = format!("{}", SizePretty(inst.size.unwrap()));
        result.push((self.fields.size, Field::String(size)));
    }

    fn generate_mem_slot_meta_tile(
        &self,
        entry_id: &EntryID,
        mem_id: MemID,
        tile_id: TileID,
        full: bool,
    ) -> SlotMetaTile {
        let mem = self.state.mems.get(&mem_id).unwrap();
        let mut m: Vec<Vec<ItemMeta>> = Vec::new();
        let items = self.build_items(mem, None, tile_id, full, Some(&mut m), |entry, info| {
            let ItemInfo {
                point_interval,
                expand,
            } = info;

            let name = format!("Instance {}", InstShort(entry));
            let provenance = entry.provenance(&self.state);

            let mut fields = Vec::new();
            if expand {
                fields.push((self.fields.expanded_for_visibility, Field::Empty));
            }
            fields.push((self.fields.interval, Field::Interval(point_interval)));
            self.generate_inst_regions(entry, &mut fields);
            self.generate_inst_layout(entry, &mut fields);
            self.generate_inst_size(entry, &mut fields);
            if let Some(initiation_op) = entry.initiation() {
                // FIXME: You might think that initiation_op is None rather than
                // needing this check with zero, but backwards compatibility is hard
                // You can remove this check once we stop needing to be compatible with Python
                if initiation_op != OpID::ZERO {
                    fields.push((self.fields.operation, self.generate_op_link(initiation_op)));
                }
            }
            if let Some(provenance) = provenance {
                fields.push((
                    self.fields.provenance,
                    Field::String(provenance.to_string()),
                ));
            }
            if let Some(creator_event) = entry.creator {
                if let Some(creator) = self.state.fevents.get(&creator_event) {
                    if let Some(create_time) = entry.time_range.create {
                        fields.push((
                            self.fields.creator,
                            self.generate_creator_link(*creator, create_time),
                        ));
                    }
                }
            }
            ItemMeta {
                item_uid: entry.base().prof_uid.into(),
                title: name,
                original_interval: point_interval,
                fields,
            }
        });
        assert_eq!(items.len(), m.len());
        for (item_row, item_meta_row) in items.iter().zip(m.iter()) {
            assert_eq!(item_row.len(), item_meta_row.len());
        }
        SlotMetaTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotMetaTileData { items: m },
        }
    }

    fn generate_chan_slot_tile(
        &self,
        entry_id: &EntryID,
        chan_id: ChanID,
        tile_id: TileID,
        full: bool,
    ) -> SlotTile {
        let chan = self.state.chans.get(&chan_id).unwrap();
        let items = self.build_items(chan, None, tile_id, full, None, |_, _| unreachable!());
        SlotTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotTileData { items },
        }
    }

    fn generate_copy_reqs(&self, copy: &Copy, result_reqs: &mut Vec<Field>) {
        let groups = copy.copy_inst_infos.linear_group_by(|a, b| {
            a.src_inst_uid == b.src_inst_uid
                && a.dst_inst_uid == b.dst_inst_uid
                && a.num_hops == b.num_hops
        });
        let mut i = 0;
        for group in groups {
            let req_nums = if group.len() == 1 {
                format!("Requirement {}", i)
            } else {
                format!("Requirements {}-{}", i, i + group.len() - 1)
            };
            result_reqs.push(Field::String(req_nums));

            let CopyInstInfo {
                src_inst_uid,
                dst_inst_uid,
                num_hops,
                ..
            } = group[0];

            let src_inst = self.state.find_inst(src_inst_uid);
            let dst_inst = self.state.find_inst(dst_inst_uid);

            let src_fids = group.iter().map(|x| x.src_fid).collect();
            let src_fields = format!(
                "Fields: {}",
                ChanEntryFieldsPretty(src_inst, &src_fids, &self.state)
            );

            let dst_fids = group.iter().map(|x| x.dst_fid).collect();
            let dst_fields = format!(
                "Fields: {}",
                ChanEntryFieldsPretty(dst_inst, &dst_fids, &self.state)
            );

            match (src_inst_uid.0, dst_inst_uid.0) {
                (0, 0) => unreachable!(),
                (0, _) => {
                    let prefix = "Scatter: destination indirect instance ";
                    if let Some(dst) = self.generate_inst_link(dst_inst_uid, prefix) {
                        result_reqs.push(dst);
                    } else {
                        result_reqs.push(Field::String(format!("{}<unknown instance>", prefix)));
                    }
                    result_reqs.push(Field::String(dst_fields));
                }
                (_, 0) => {
                    let prefix = "Gather: source indirect instance ";
                    if let Some(src) = self.generate_inst_link(src_inst_uid, prefix) {
                        result_reqs.push(src);
                    } else {
                        result_reqs.push(Field::String(format!("{}<unknown instance>", prefix)));
                    }
                    result_reqs.push(Field::String(src_fields));
                }
                (_, _) => {
                    let prefix = "Source: ";
                    if let Some(src) = self.generate_inst_link(src_inst_uid, prefix) {
                        result_reqs.push(src);
                    } else {
                        result_reqs.push(Field::String(format!("{}<unknown instance>", prefix)));
                    }
                    result_reqs.push(Field::String(src_fields));

                    let prefix = "Destination: ";
                    if let Some(dst) = self.generate_inst_link(dst_inst_uid, prefix) {
                        result_reqs.push(dst);
                    } else {
                        result_reqs.push(Field::String(format!("{}<unknown instance>", prefix)));
                    }
                    result_reqs.push(Field::String(dst_fields));
                }
            }

            result_reqs.push(Field::String(format!("Number of Hops: {}", num_hops)));

            i += group.len();
        }
    }

    fn generate_fill_reqs(&self, fill: &Fill, result_reqs: &mut Vec<Field>) {
        let groups = fill
            .fill_inst_infos
            .linear_group_by(|a, b| a.dst_inst_uid == b.dst_inst_uid);
        let mut i = 0;
        for group in groups {
            let req_nums = if group.len() == 1 {
                format!("Requirement {}", i)
            } else {
                format!("Requirements {}-{}", i, i + group.len() - 1)
            };
            result_reqs.push(Field::String(req_nums));

            let FillInstInfo { dst_inst_uid, .. } = group[0];

            let dst_inst = self.state.find_inst(dst_inst_uid);

            let dst_fids = group.iter().map(|x| x.fid).collect();
            let dst_fields = format!(
                "Fields: {}",
                ChanEntryFieldsPretty(dst_inst, &dst_fids, &self.state)
            );

            let prefix = "Destination: ";
            if let Some(dst) = self.generate_inst_link(dst_inst_uid, prefix) {
                result_reqs.push(dst);
            } else {
                result_reqs.push(Field::String(format!("{}<unknown instance>", prefix)));
            }
            result_reqs.push(Field::String(dst_fields));

            i += group.len();
        }
    }

    fn generate_chan_reqs(&self, entry: &ChanEntry, result: &mut Vec<(FieldID, Field)>) {
        let mut result_reqs = Vec::new();
        match entry {
            ChanEntry::Copy(copy) => {
                self.generate_copy_reqs(copy, &mut result_reqs);
            }
            ChanEntry::Fill(fill) => {
                self.generate_fill_reqs(fill, &mut result_reqs);
            }
            ChanEntry::DepPart(_) => {}
        }
        result.push((self.fields.chan_reqs, Field::Vec(result_reqs)));
    }

    fn generate_chan_size(&self, entry: &ChanEntry, result: &mut Vec<(FieldID, Field)>) {
        let size = match entry {
            ChanEntry::Copy(copy) => copy.size,
            ChanEntry::Fill(fill) => fill.size,
            ChanEntry::DepPart(_) => return,
        };
        let size = format!("{}", SizePretty(size));
        result.push((self.fields.size, Field::String(size)));
    }

    fn generate_chan_slot_meta_tile(
        &self,
        entry_id: &EntryID,
        chan_id: ChanID,
        tile_id: TileID,
        full: bool,
    ) -> SlotMetaTile {
        let chan = self.state.chans.get(&chan_id).unwrap();
        let mut m: Vec<Vec<ItemMeta>> = Vec::new();
        let items = self.build_items(chan, None, tile_id, full, Some(&mut m), |entry, info| {
            let ItemInfo {
                point_interval,
                expand,
            } = info;

            let name = format!("{}", ChanEntryShort(entry));
            let provenance = entry.provenance(&self.state);

            let mut fields = Vec::new();
            if expand {
                fields.push((self.fields.expanded_for_visibility, Field::Empty));
            }
            fields.push((self.fields.interval, Field::Interval(point_interval)));
            self.generate_chan_reqs(entry, &mut fields);
            self.generate_chan_size(entry, &mut fields);
            if let Some(initiation_op) = entry.initiation() {
                // FIXME: You might think that initiation_op is None rather than
                // needing this check with zero, but backwards compatibility is hard
                // You can remove this check once we stop needing to be compatible with Python
                if initiation_op != OpID::ZERO {
                    fields.push((self.fields.operation, self.generate_op_link(initiation_op)));
                }
            }
            if let Some(provenance) = provenance {
                fields.push((
                    self.fields.provenance,
                    Field::String(provenance.to_string()),
                ));
            }
            if let Some(creator) = entry.creator() {
                if let Some(creator_uid) = self.state.fevents.get(&creator) {
                    if let Some(create_time) = entry.time_range().create {
                        fields.push((
                            self.fields.creator,
                            self.generate_creator_link(*creator_uid, create_time),
                        ));
                    }
                }
            }
            ItemMeta {
                item_uid: entry.base().prof_uid.into(),
                title: name,
                original_interval: point_interval,
                fields,
            }
        });
        assert_eq!(items.len(), m.len());
        for (item_row, item_meta_row) in items.iter().zip(m.iter()) {
            assert_eq!(item_row.len(), item_meta_row.len());
        }
        SlotMetaTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SlotMetaTileData { items: m },
        }
    }

    fn interval(&self) -> ts::Interval {
        let last_time = self.state.last_time;
        // Add a bit to the end of the timeline to make it more visible
        let last_time = last_time + Timestamp::from_ns(last_time.to_ns() / 200);
        ts::Interval::new(ts::Timestamp(0), last_time.into())
    }

    fn generate_warning_message(&self) -> Option<String> {
        if !self.state.runtime_config.any() {
            return None;
        }
        Some(format!(
            "This profile was generated with {}. Extreme performance degradation may occur.",
            self.state.runtime_config
        ))
    }
}

impl DataSource for StateDataSource {
    fn fetch_description(&self) -> DataSourceDescription {
        DataSourceDescription {
            source_locator: self.state.source_locator.clone(),
        }
    }

    fn fetch_info(&self) -> DataSourceInfo {
        DataSourceInfo {
            entry_info: self.info.clone(),
            interval: self.interval(),
            tile_set: TileSet::default(),
            field_schema: self.field_schema.clone(),
            warning_message: self.generate_warning_message(),
        }
    }

    fn fetch_summary_tile(&self, entry_id: &EntryID, tile_id: TileID, full: bool) -> SummaryTile {
        // Pick this number to be approximately the number of pixels we expect
        // the user to have on their screen. If this is a full tile, increase
        // this so that we get more resolution when zoomed in.
        let samples = if full { 4_000 } else { 800 };

        let step_utilization = self.generate_step_utilization(entry_id);

        let utilization = Self::compute_sample_utilization(&step_utilization, tile_id.0, samples);

        SummaryTile {
            entry_id: entry_id.clone(),
            tile_id,
            data: SummaryTileData { utilization },
        }
    }

    fn fetch_slot_tile(&self, entry_id: &EntryID, tile_id: TileID, full: bool) -> SlotTile {
        let entry = self.entry_map.get(entry_id).unwrap();
        match entry {
            EntryKind::Proc(proc_id, device) => {
                self.generate_proc_slot_tile(entry_id, *proc_id, *device, tile_id, full)
            }
            EntryKind::Mem(mem_id) => self.generate_mem_slot_tile(entry_id, *mem_id, tile_id, full),
            EntryKind::Chan(chan_id) | EntryKind::DepPart(chan_id) => {
                self.generate_chan_slot_tile(entry_id, *chan_id, tile_id, full)
            }
            _ => unreachable!(),
        }
    }

    fn fetch_slot_meta_tile(
        &self,
        entry_id: &EntryID,
        tile_id: TileID,
        full: bool,
    ) -> SlotMetaTile {
        let entry = self.entry_map.get(entry_id).unwrap();
        match entry {
            EntryKind::Proc(proc_id, device) => {
                self.generate_proc_slot_meta_tile(entry_id, *proc_id, *device, tile_id, full)
            }
            EntryKind::Mem(mem_id) => {
                self.generate_mem_slot_meta_tile(entry_id, *mem_id, tile_id, full)
            }
            EntryKind::Chan(chan_id) | EntryKind::DepPart(chan_id) => {
                self.generate_chan_slot_meta_tile(entry_id, *chan_id, tile_id, full)
            }
            _ => unreachable!(),
        }
    }
}