|
| 1 | +import {useEffect, useState} from "react"; |
| 2 | +import {Circle, Group, Layer, Line, Rect, Stage, Text} from "react-konva"; |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +const baseColors = ['hsl(4, 72%, 63.8%)', 'hsl(340, 65.6%, 57.2%)', 'hsl(291, 51.2%, 46.2%)', 'hsl(262, 41.6%, 51.7%)', 'hsl(231, 38.4%, 52.8%)', 'hsl(207, 72%, 59.4%)', 'hsl(199, 78.4%, 52.8%)', 'hsl(187, 80%, 46.2%)', 'hsl(174, 80%, 31.9%)', 'hsl(122, 31.2%, 53.9%)', 'hsl(88, 40%, 58.3%)', 'hsl(66, 56%, 59.4%)', 'hsl(45, 80%, 56.1%)', 'hsl(36, 80%, 55%)', 'hsl(14, 80%, 62.7%)', 'hsl(16, 20%, 41.8%)', 'hsl(200, 14.4%, 50.6%)', 'hsl(54, 80%, 68.2%)'] |
| 12 | +const variantColors = ['hsl(4, 57.6%, 82.94%)', 'hsl(340, 52.48%, 74.36%)', 'hsl(291, 40.96%, 60.06%)', 'hsl(262, 33.28%, 67.21000000000001%)', 'hsl(231, 30.72%, 68.64%)', 'hsl(207, 57.6%, 77.22%)', 'hsl(199, 62.720000000000006%, 68.64%)', 'hsl(187, 64%, 60.06%)', 'hsl(174, 64%, 41.47%)', 'hsl(122, 24.96%, 70.07%)', 'hsl(88, 32%, 75.78999999999999%)', 'hsl(66, 44.8%, 77.22%)', 'hsl(45, 64%, 72.93%)', 'hsl(36, 64%, 71.5%)', 'hsl(14, 64%, 81.51%)', 'hsl(16, 16%, 54.339999999999996%)', 'hsl(200, 11.52%, 65.78%)', 'hsl(54, 64%, 88.66%)'] |
| 13 | + |
| 14 | +export const TraceViewer = ({traceData,width, height, onSelect}) => { |
| 15 | + const [showExecutionOnly, setShowExecutionOnly] = useState(true) |
| 16 | + const [data, setData] = useState([]) |
| 17 | + let [scaleX, setScaleX] = useState(1); |
| 18 | + let [timeRange, setTimeRange] = useState([0, 1]); |
| 19 | + let [initialScaleX, setInitialScaleX] = useState(1) |
| 20 | + const [maxTime, setMaxTime] = useState(1) |
| 21 | + const [threads, setThreads] = useState([]) |
| 22 | + const [categoryColors, setCategoryColors] = useState({}) |
| 23 | + const [canvasWidth, setCanvasWidth] = useState(0) |
| 24 | + const [dataWidth,setDataWidth]= useState(0) |
| 25 | + useEffect(()=> { |
| 26 | + if (traceData) { |
| 27 | + let executionRecord = traceData.filter((d) => d.category === "Execution" && d.name === "run") |
| 28 | + let filteredData = showExecutionOnly ? traceData.filter((d) => d.start >= executionRecord[0].start && d.start <= executionRecord[0].start + executionRecord[0].duration) : traceData |
| 29 | + let analyzedData = filteredData.reduce((acc, d) => { |
| 30 | + let event = d |
| 31 | + if (!acc[d.tid]) { |
| 32 | + acc[d.tid] = [] |
| 33 | + } |
| 34 | + for (let i = 0; i < acc[d.tid].length; i++) { |
| 35 | + let lastElementInRow = acc[d.tid][i][acc[d.tid][i].length - 1] |
| 36 | + let end = lastElementInRow.start + lastElementInRow.duration |
| 37 | + if (d.start >= end) { |
| 38 | + acc[d.tid][i].push(event) |
| 39 | + return acc |
| 40 | + } |
| 41 | + } |
| 42 | + acc[d.tid].push([event]) |
| 43 | + return acc |
| 44 | + }, {}) |
| 45 | + let numRows = 0 |
| 46 | + let maxTime = 0 |
| 47 | + let minTime = filteredData.reduce((acc, d) => Math.min(acc, d.start), Infinity) |
| 48 | + let categoryColors = {} |
| 49 | + let data = [] |
| 50 | + let threads = [] |
| 51 | + for (let tid in analyzedData) { |
| 52 | + threads.push({"threadId": tid, "startRow": numRows}) |
| 53 | + for (let row in analyzedData[tid]) { |
| 54 | + for (let entryIdx in analyzedData[tid][row]) { |
| 55 | + let entry = analyzedData[tid][row][entryIdx] |
| 56 | + maxTime = Math.max(maxTime, entry.start + entry.duration - minTime) |
| 57 | + if (entry.category) { |
| 58 | + if (!categoryColors[entry.category + "::" + entry.name]) { |
| 59 | + let idx = Object.keys(categoryColors).length % baseColors.length |
| 60 | + categoryColors[entry.category + "::" + entry.name] = baseColors[idx] |
| 61 | + } |
| 62 | + } |
| 63 | + data.push({...entry, start: entry.start - minTime, duration: entry.duration, row: numRows,}) |
| 64 | + } |
| 65 | + numRows++ |
| 66 | + } |
| 67 | + } |
| 68 | + setData(data) |
| 69 | + const newScaleX = dataWidth / maxTime; |
| 70 | + setScaleX(newScaleX) |
| 71 | + setInitialScaleX(newScaleX) |
| 72 | + setTimeRange([0, maxTime]) |
| 73 | + setMaxTime(maxTime) |
| 74 | + setThreads(threads) |
| 75 | + setCategoryColors(categoryColors) |
| 76 | + } |
| 77 | + },[traceData,showExecutionOnly,dataWidth]) |
| 78 | + |
| 79 | + let offsetY = 20 |
| 80 | + let offsetX = 100 |
| 81 | + let rowOffset = 20; |
| 82 | + useEffect(() => { |
| 83 | + setCanvasWidth(width - 20) |
| 84 | + setDataWidth(width - 20 - offsetX) |
| 85 | + }, [width]); |
| 86 | + const [selectedEvent, setSelectedEvent] = useState(null) |
| 87 | + const selectEvent = (event) => { |
| 88 | + setSelectedEvent(event) |
| 89 | + onSelect(event) |
| 90 | + } |
| 91 | + const formatExtraText = (d) => { |
| 92 | + let extra = [] |
| 93 | + for (let prop in d.extra) { |
| 94 | + extra.push({key: prop, value: d.extra[prop]}) |
| 95 | + } |
| 96 | + let extraText = extra.map((d, i) => { |
| 97 | + return `${d.key}: ${d.value}` |
| 98 | + }).join(", ") |
| 99 | + return extraText |
| 100 | + } |
| 101 | + const handleScroll = (e) => { |
| 102 | + if (e.evt.ctrlKey) { |
| 103 | + e.evt.preventDefault(); |
| 104 | + let layerX = e.evt.layerX-offsetX |
| 105 | + let timePosition = layerX / scaleX + timeRange[0] |
| 106 | + let factor = e.evt.wheelDelta / 100; |
| 107 | + let newScaleX = factor > 0 ? scaleX * factor : scaleX / -factor; |
| 108 | + if (newScaleX < initialScaleX) { |
| 109 | + setScaleX(initialScaleX) |
| 110 | + setTimeRange([0, maxTime]) |
| 111 | + } else { |
| 112 | + let newPositionOfScrolledPoint = (timePosition - timeRange[0]) * newScaleX |
| 113 | + let diff = layerX - newPositionOfScrolledPoint |
| 114 | + var newTimeStart = timeRange[0] - diff / newScaleX |
| 115 | + var newTimeEnd = newTimeStart + dataWidth / newScaleX |
| 116 | + if (newTimeEnd > maxTime) { |
| 117 | + newTimeStart -= newTimeEnd - maxTime |
| 118 | + newTimeEnd = maxTime |
| 119 | + } |
| 120 | + let newTimeRange = [newTimeStart, newTimeEnd] |
| 121 | + setScaleX(newScaleX) |
| 122 | + setTimeRange(newTimeRange) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + const [mouseDownLocation, setMouseDownLocation] = useState(null) |
| 128 | + const handleMouseDown = (e) => { |
| 129 | + setMouseDownLocation(e.evt.layerX) |
| 130 | + } |
| 131 | + const handleMouseUp = (e) => { |
| 132 | + if (mouseDownLocation) { |
| 133 | + let diff = e.evt.layerX - mouseDownLocation; |
| 134 | + let newTimeStart = timeRange[0] - diff / scaleX |
| 135 | + let newTimeEnd = newTimeStart + dataWidth / scaleX |
| 136 | + if (newTimeStart < 0) { |
| 137 | + newTimeStart = 0 |
| 138 | + newTimeEnd = dataWidth / scaleX |
| 139 | + } |
| 140 | + if (newTimeEnd > maxTime) { |
| 141 | + newTimeStart -= newTimeEnd - maxTime |
| 142 | + newTimeEnd = maxTime |
| 143 | + } |
| 144 | + setTimeRange([newTimeStart, newTimeEnd]) |
| 145 | + setMouseDownLocation(null) |
| 146 | + } |
| 147 | + } |
| 148 | + const handleMouseMove = (e) => { |
| 149 | + if (mouseDownLocation) { |
| 150 | + let diff = e.evt.layerX - mouseDownLocation; |
| 151 | + let newTimeStart = timeRange[0] - diff / scaleX |
| 152 | + let newTimeEnd = newTimeStart + dataWidth / scaleX |
| 153 | + if (newTimeStart < 0) { |
| 154 | + newTimeStart = 0 |
| 155 | + newTimeEnd = dataWidth / scaleX |
| 156 | + } |
| 157 | + if (newTimeEnd > maxTime) { |
| 158 | + newTimeStart -= newTimeEnd - maxTime |
| 159 | + newTimeEnd = maxTime |
| 160 | + } |
| 161 | + setTimeRange([newTimeStart, newTimeEnd]) |
| 162 | + setMouseDownLocation(e.evt.layerX) |
| 163 | + } |
| 164 | + } |
| 165 | + const [rows, setRows] = useState([]); |
| 166 | + |
| 167 | + |
| 168 | + function roundUpToStepSize(value) { |
| 169 | + const magnitude = Math.pow(10, Math.floor(Math.log10(value))); // Find the magnitude of the value |
| 170 | + const normalized = value / magnitude; // Normalize the value to the range [1, 10) |
| 171 | + |
| 172 | + let step; |
| 173 | + if (normalized <= 1) { |
| 174 | + step = 1; |
| 175 | + } else if (normalized <= 2) { |
| 176 | + step = 2; |
| 177 | + } else if (normalized <= 5) { |
| 178 | + step = 5; |
| 179 | + } else { |
| 180 | + step = 10; |
| 181 | + } |
| 182 | + |
| 183 | + return step * magnitude; |
| 184 | + } |
| 185 | + |
| 186 | + const [splitters, setSplitters] = useState([]) |
| 187 | + const [splitterStep, setSplitterStep] = useState(0) |
| 188 | + useEffect(() => { |
| 189 | + let numSplitters = dataWidth / 200; |
| 190 | + let timeDuration = timeRange[1] - timeRange[0] |
| 191 | + let timeStep = roundUpToStepSize(timeDuration / numSplitters) |
| 192 | + let firstSplitter = Math.ceil(timeRange[0] / timeStep) * timeStep |
| 193 | + let newSplitters = [] |
| 194 | + for (let i = firstSplitter; i < timeRange[1]; i += timeStep) { |
| 195 | + newSplitters.push(i) |
| 196 | + } |
| 197 | + setSplitters(newSplitters) |
| 198 | + setSplitterStep(timeStep) |
| 199 | + |
| 200 | + }, [timeRange]); |
| 201 | + const formatTime = (time) => { |
| 202 | + if (time < 1000) { |
| 203 | + return `${time} us` |
| 204 | + } else if (time < 1000000) { |
| 205 | + return `${time / 1000} ms` |
| 206 | + } else { |
| 207 | + return `${time / 1000000} s` |
| 208 | + } |
| 209 | + } |
| 210 | + //useEffect(() => { |
| 211 | + // doTest(); |
| 212 | + //}, []); |
| 213 | + return ( |
| 214 | + <div> |
| 215 | + <div style={{maxHeight: height-60, overflowY: "auto"}}> |
| 216 | + <Stage width={canvasWidth} height={window.innerHeight} |
| 217 | + onWheel={(e) => handleScroll(e)} onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} |
| 218 | + onMouseMove={handleMouseMove}> |
| 219 | + <Layer> |
| 220 | + { |
| 221 | + threads.map((t, i) => <Group> |
| 222 | + <Rect x={0} y={offsetY + t.startRow * rowOffset - 2} width={canvasWidth} height={1} |
| 223 | + fill={"black"}></Rect> |
| 224 | + <Text x={0} y={offsetY + t.startRow * rowOffset} text={"Thread " + t.threadId} |
| 225 | + fontSize={14} fill={"black"}></Text> |
| 226 | + </Group>) |
| 227 | + } |
| 228 | + { |
| 229 | + splitters.map((d, i) => <Group> |
| 230 | + <Rect x={offsetX + (d - timeRange[0]) * scaleX} y={0} width={1} |
| 231 | + height={window.innerHeight} fill={"black"}></Rect> |
| 232 | + <Text x={offsetX + (d - timeRange[0]) * scaleX} y={0} |
| 233 | + text={`${formatTime(d)} step: ${formatTime(splitterStep)}`} fontSize={14} |
| 234 | + fill={"black"}></Text> |
| 235 | + </Group>) |
| 236 | + |
| 237 | + } |
| 238 | + {data.map((d, i) => { |
| 239 | + let duration = d.duration |
| 240 | + if (d.start + duration < timeRange[0] || d.start > timeRange[1]) { |
| 241 | + return null |
| 242 | + } else { |
| 243 | + let correctedStart = d.start < timeRange[0] ? 0 : d.start - timeRange[0] |
| 244 | + var correctedDuration = d.start < timeRange[0] ? duration - (timeRange[0] - d.start) : duration |
| 245 | + correctedDuration = d.start + duration > timeRange[1] ? duration - (d.start + duration - timeRange[1]) : correctedDuration |
| 246 | + if (duration == 0) { |
| 247 | + return <Circle x={correctedStart * scaleX+offsetX} y={d.row * rowOffset+offsetY} radius={2} fill={"black"} onClick={()=> selectEvent(d)}></Circle> |
| 248 | + } else { |
| 249 | + let extraText = formatExtraText(d) |
| 250 | + return (<Group onClick={() => selectEvent(d)}> |
| 251 | + <Rect x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset} height={15} |
| 252 | + width={correctedDuration * scaleX} |
| 253 | + fill={d.category=== "Ignore" ? "white":categoryColors[d.category + "::" + d.name]} stroke={"gray"} strokeWidth={0.4} |
| 254 | + onClick={() => console.log(d)}></Rect> |
| 255 | + |
| 256 | + <Text x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset} |
| 257 | + text={`${d.name} (${extraText})`} |
| 258 | + width={correctedDuration * scaleX} fontSize={14} fill={"white"} wrap={"none"} |
| 259 | + ellipsis={true}></Text> |
| 260 | + {d.category === "Ignore" && |
| 261 | + <Group> |
| 262 | + <Line points={[offsetX + correctedStart * scaleX,offsetY + d.row * rowOffset,offsetX + correctedStart * scaleX+correctedDuration*scaleX,offsetY + d.row * rowOffset+15]} stroke={"red"} strokeWidth={1}></Line> |
| 263 | + <Line points={[offsetX + correctedStart * scaleX,offsetY + d.row * rowOffset+15,offsetX + correctedStart * scaleX+correctedDuration*scaleX,offsetY + d.row * rowOffset]} stroke={"red"} strokeWidth={1}></Line> |
| 264 | + </Group> |
| 265 | + } |
| 266 | + </Group>) |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + )} |
| 271 | + </Layer> |
| 272 | + </Stage> |
| 273 | + </div> |
| 274 | + <div> |
| 275 | + <input type="checkbox" checked={showExecutionOnly} onChange={(e) => setShowExecutionOnly(e.target.checked)}/> Show Execution Only |
| 276 | + {selectedEvent && |
| 277 | + <div style={{display:"inline"}}><b>Selected Element:</b> Category: {selectedEvent.category} Name: {selectedEvent.name} Duration: {formatTime(selectedEvent.duration)} Metadata: {formatExtraText(selectedEvent)} |
| 278 | + </div> |
| 279 | + |
| 280 | + } |
| 281 | + </div> |
| 282 | + </div> |
| 283 | + ); |
| 284 | +} |
0 commit comments