differentialFlamegraph.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import {useCallback, useLayoutEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import type {mat3} from 'gl-matrix';
  5. import {vec2} from 'gl-matrix';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import Feature from 'sentry/components/acl/feature';
  8. import {DifferentialFlamegraphLayout} from 'sentry/components/profiling/flamegraph/differentialFlamegraphLayout';
  9. import {FlamegraphContextMenu} from 'sentry/components/profiling/flamegraph/flamegraphContextMenu';
  10. import {DifferentialFlamegraphDrawer} from 'sentry/components/profiling/flamegraph/flamegraphDrawer/differentialFlamegraphDrawer';
  11. import {DifferentialFlamegraphToolbar} from 'sentry/components/profiling/flamegraph/flamegraphToolbar/differentialFlamegraphToolbar';
  12. import {FlamegraphZoomView} from 'sentry/components/profiling/flamegraph/flamegraphZoomView';
  13. import {FlamegraphZoomViewMinimap} from 'sentry/components/profiling/flamegraph/flamegraphZoomViewMinimap';
  14. import {
  15. CanvasPoolManager,
  16. useCanvasScheduler,
  17. } from 'sentry/utils/profiling/canvasScheduler';
  18. import {CanvasView} from 'sentry/utils/profiling/canvasView';
  19. import type {DifferentialFlamegraph as DifferentialFlamegraphModel} from 'sentry/utils/profiling/differentialFlamegraph';
  20. import {FlamegraphStateProvider} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphContextProvider';
  21. import {FlamegraphThemeProvider} from 'sentry/utils/profiling/flamegraph/flamegraphThemeProvider';
  22. import {useFlamegraphPreferences} from 'sentry/utils/profiling/flamegraph/hooks/useFlamegraphPreferences';
  23. import {useFlamegraphProfiles} from 'sentry/utils/profiling/flamegraph/hooks/useFlamegraphProfiles';
  24. import {useFlamegraphTheme} from 'sentry/utils/profiling/flamegraph/useFlamegraphTheme';
  25. import {FlamegraphCanvas} from 'sentry/utils/profiling/flamegraphCanvas';
  26. import type {FlamegraphFrame} from 'sentry/utils/profiling/flamegraphFrame';
  27. import type {Frame} from 'sentry/utils/profiling/frame';
  28. import {
  29. computeConfigViewWithStrategy,
  30. formatColorForFrame,
  31. initializeFlamegraphRenderer,
  32. useResizeCanvasObserver,
  33. } from 'sentry/utils/profiling/gl/utils';
  34. import {useCurrentProjectFromRouteParam} from 'sentry/utils/profiling/hooks/useCurrentProjectFromRouteParam';
  35. import {useDifferentialFlamegraphModel} from 'sentry/utils/profiling/hooks/useDifferentialFlamegraphModel';
  36. import {useDifferentialFlamegraphQuery} from 'sentry/utils/profiling/hooks/useDifferentialFlamegraphQuery';
  37. import {FlamegraphRenderer2D} from 'sentry/utils/profiling/renderers/flamegraphRenderer2D';
  38. import {FlamegraphRendererWebGL} from 'sentry/utils/profiling/renderers/flamegraphRendererWebGL';
  39. import {Rect} from 'sentry/utils/profiling/speedscope';
  40. import {useLocation} from 'sentry/utils/useLocation';
  41. import usePageFilters from 'sentry/utils/usePageFilters';
  42. import {LOADING_PROFILE_GROUP} from 'sentry/views/profiling/profileGroupProvider';
  43. const noopFormatDuration = () => '';
  44. function applicationFrameOnly(frame: Frame): boolean {
  45. return frame.is_application;
  46. }
  47. function systemFrameOnly(frame: Frame): boolean {
  48. return !frame.is_application;
  49. }
  50. function DifferentialFlamegraphView() {
  51. const location = useLocation();
  52. const selection = usePageFilters();
  53. const flamegraphTheme = useFlamegraphTheme();
  54. const {colorCoding} = useFlamegraphPreferences();
  55. const {selectedRoot} = useFlamegraphProfiles();
  56. const [frameFilterSetting, setFrameFilterSetting] = useState<
  57. 'application' | 'system' | 'all'
  58. >('all');
  59. const frameFilter =
  60. frameFilterSetting === 'application'
  61. ? applicationFrameOnly
  62. : frameFilterSetting === 'system'
  63. ? systemFrameOnly
  64. : undefined;
  65. const project = useCurrentProjectFromRouteParam();
  66. const [negated, setNegated] = useState<boolean>(false);
  67. const canvasPoolManager = useMemo(() => new CanvasPoolManager(), []);
  68. const scheduler = useCanvasScheduler(canvasPoolManager);
  69. const {before, after} = useDifferentialFlamegraphQuery({
  70. projectID: parseInt((project?.id as string) ?? 0, 10),
  71. breakpoint: location.query.breakpoint as unknown as number,
  72. environments: selection.selection.environments,
  73. fingerprint: location.query.fingerprint as unknown as string,
  74. });
  75. const differentialFlamegraph = useDifferentialFlamegraphModel({
  76. before,
  77. after,
  78. frameFilter,
  79. negated,
  80. });
  81. const [flamegraphCanvasRef, setFlamegraphCanvasRef] =
  82. useState<HTMLCanvasElement | null>(null);
  83. const [flamegraphOverlayCanvasRef, setFlamegraphOverlayCanvasRef] =
  84. useState<HTMLCanvasElement | null>(null);
  85. const [flamegraphMiniMapCanvasRef, setFlamegraphMiniMapCanvasRef] =
  86. useState<HTMLCanvasElement | null>(null);
  87. const [flamegraphMiniMapOverlayCanvasRef, setFlamegraphMiniMapOverlayCanvasRef] =
  88. useState<HTMLCanvasElement | null>(null);
  89. const flamegraphCanvas = useMemo(() => {
  90. if (!flamegraphCanvasRef) {
  91. return null;
  92. }
  93. return new FlamegraphCanvas(flamegraphCanvasRef, vec2.fromValues(0, 0));
  94. }, [flamegraphCanvasRef]);
  95. const flamegraphView = useMemo<CanvasView<DifferentialFlamegraphModel> | null>(
  96. () => {
  97. if (!flamegraphCanvas || !differentialFlamegraph.differentialFlamegraph) {
  98. return null;
  99. }
  100. const newView = new CanvasView({
  101. canvas: flamegraphCanvas,
  102. model: differentialFlamegraph.differentialFlamegraph,
  103. options: {
  104. inverted: differentialFlamegraph.differentialFlamegraph.inverted,
  105. minWidth:
  106. differentialFlamegraph.differentialFlamegraph.profile.minFrameDuration,
  107. barHeight: flamegraphTheme.SIZES.BAR_HEIGHT,
  108. depthOffset: flamegraphTheme.SIZES.AGGREGATE_FLAMEGRAPH_DEPTH_OFFSET,
  109. configSpaceTransform: undefined,
  110. },
  111. });
  112. return newView;
  113. },
  114. // We skip position.view dependency because it will go into an infinite loop
  115. // eslint-disable-next-line react-hooks/exhaustive-deps
  116. [differentialFlamegraph.differentialFlamegraph, flamegraphCanvas, flamegraphTheme]
  117. );
  118. // Uses a useLayoutEffect to ensure that these top level/global listeners are added before
  119. // any of the children components effects actually run. This way we do not lose events
  120. // when we register/unregister these top level listeners.
  121. useLayoutEffect(() => {
  122. if (!flamegraphCanvas || !flamegraphView) {
  123. return undefined;
  124. }
  125. // This code below manages the synchronization of the config views between spans and flamegraph
  126. // We do so by listening to the config view change event and then updating the other views accordingly which
  127. // allows us to keep the X axis in sync between the two views but keep the Y axis independent
  128. const onConfigViewChange = (rect: Rect, sourceConfigViewChange: CanvasView<any>) => {
  129. if (sourceConfigViewChange === flamegraphView) {
  130. flamegraphView.setConfigView(rect.withHeight(flamegraphView.configView.height));
  131. }
  132. canvasPoolManager.draw();
  133. };
  134. const onTransformConfigView = (
  135. mat: mat3,
  136. sourceTransformConfigView: CanvasView<any>
  137. ) => {
  138. if (sourceTransformConfigView === flamegraphView) {
  139. flamegraphView.transformConfigView(mat);
  140. }
  141. canvasPoolManager.draw();
  142. };
  143. const onResetZoom = () => {
  144. flamegraphView.resetConfigView(flamegraphCanvas);
  145. canvasPoolManager.draw();
  146. };
  147. const onZoomIntoFrame = (frame: FlamegraphFrame, strategy: 'min' | 'exact') => {
  148. const newConfigView = computeConfigViewWithStrategy(
  149. strategy,
  150. flamegraphView.configView,
  151. new Rect(frame.start, frame.depth, frame.end - frame.start, 1)
  152. ).transformRect(flamegraphView.configSpaceTransform);
  153. flamegraphView.setConfigView(newConfigView);
  154. canvasPoolManager.draw();
  155. };
  156. scheduler.on('set config view', onConfigViewChange);
  157. scheduler.on('transform config view', onTransformConfigView);
  158. scheduler.on('reset zoom', onResetZoom);
  159. scheduler.on('zoom at frame', onZoomIntoFrame);
  160. return () => {
  161. scheduler.off('set config view', onConfigViewChange);
  162. scheduler.off('transform config view', onTransformConfigView);
  163. scheduler.off('reset zoom', onResetZoom);
  164. scheduler.off('zoom at frame', onZoomIntoFrame);
  165. };
  166. }, [canvasPoolManager, flamegraphCanvas, flamegraphView, scheduler]);
  167. const flamegraphCanvases = useMemo(() => {
  168. return [flamegraphCanvasRef, flamegraphOverlayCanvasRef];
  169. }, [flamegraphCanvasRef, flamegraphOverlayCanvasRef]);
  170. const flamegraphCanvasBounds = useResizeCanvasObserver(
  171. flamegraphCanvases,
  172. canvasPoolManager,
  173. flamegraphCanvas,
  174. flamegraphView
  175. );
  176. const minimapCanvases = useMemo(() => {
  177. return [flamegraphMiniMapCanvasRef, flamegraphMiniMapOverlayCanvasRef];
  178. }, [flamegraphMiniMapCanvasRef, flamegraphMiniMapOverlayCanvasRef]);
  179. const flamegraphMiniMapCanvas = useMemo(() => {
  180. if (!flamegraphMiniMapCanvasRef) {
  181. return null;
  182. }
  183. return new FlamegraphCanvas(flamegraphMiniMapCanvasRef, vec2.fromValues(0, 0));
  184. }, [flamegraphMiniMapCanvasRef]);
  185. useResizeCanvasObserver(
  186. minimapCanvases,
  187. canvasPoolManager,
  188. flamegraphMiniMapCanvas,
  189. null
  190. );
  191. const flamegraphRenderer = useMemo(() => {
  192. if (!flamegraphCanvasRef || !differentialFlamegraph) {
  193. return null;
  194. }
  195. const renderer = initializeFlamegraphRenderer(
  196. [FlamegraphRendererWebGL, FlamegraphRenderer2D],
  197. [
  198. flamegraphCanvasRef,
  199. differentialFlamegraph.differentialFlamegraph,
  200. flamegraphTheme,
  201. {
  202. colorCoding,
  203. draw_border: true,
  204. },
  205. ]
  206. );
  207. if (renderer === null) {
  208. Sentry.captureException('Failed to initialize a flamegraph renderer');
  209. addErrorMessage('Failed to initialize renderer');
  210. return null;
  211. }
  212. return renderer;
  213. }, [colorCoding, differentialFlamegraph, flamegraphCanvasRef, flamegraphTheme]);
  214. const getFrameColor = useCallback(
  215. (frame: FlamegraphFrame) => {
  216. if (!flamegraphRenderer) {
  217. return '';
  218. }
  219. return formatColorForFrame(frame, flamegraphRenderer);
  220. },
  221. [flamegraphRenderer]
  222. );
  223. const rootNodes = useMemo(() => {
  224. return selectedRoot
  225. ? [selectedRoot]
  226. : differentialFlamegraph.differentialFlamegraph.root.children;
  227. }, [selectedRoot, differentialFlamegraph.differentialFlamegraph.root]);
  228. const referenceNode = useMemo(
  229. () =>
  230. selectedRoot ? selectedRoot : differentialFlamegraph.differentialFlamegraph.root,
  231. [selectedRoot, differentialFlamegraph.differentialFlamegraph.root]
  232. );
  233. return (
  234. <Feature features={['profiling-differential-flamegraph-page']}>
  235. <DifferentialFlamegraphContainer>
  236. <DifferentialFlamegraphToolbar
  237. frameFilter={frameFilterSetting}
  238. onFrameFilterChange={setFrameFilterSetting}
  239. negated={negated}
  240. onNegatedChange={setNegated}
  241. flamegraph={differentialFlamegraph.differentialFlamegraph}
  242. canvasPoolManager={canvasPoolManager}
  243. />
  244. <DifferentialFlamegraphLayout
  245. minimap={
  246. <FlamegraphZoomViewMinimap
  247. canvasPoolManager={canvasPoolManager}
  248. flamegraph={differentialFlamegraph.differentialFlamegraph}
  249. flamegraphMiniMapCanvas={flamegraphMiniMapCanvas}
  250. flamegraphMiniMapCanvasRef={flamegraphMiniMapCanvasRef}
  251. flamegraphMiniMapOverlayCanvasRef={flamegraphMiniMapOverlayCanvasRef}
  252. flamegraphMiniMapView={flamegraphView}
  253. setFlamegraphMiniMapCanvasRef={setFlamegraphMiniMapCanvasRef}
  254. setFlamegraphMiniMapOverlayCanvasRef={setFlamegraphMiniMapOverlayCanvasRef}
  255. />
  256. }
  257. flamegraph={
  258. <FlamegraphZoomView
  259. scheduler={scheduler}
  260. profileGroup={
  261. differentialFlamegraph.afterProfileGroup ?? LOADING_PROFILE_GROUP
  262. }
  263. disableGrid
  264. disableCallOrderSort
  265. disableColorCoding
  266. canvasBounds={flamegraphCanvasBounds}
  267. canvasPoolManager={canvasPoolManager}
  268. flamegraph={differentialFlamegraph.differentialFlamegraph}
  269. flamegraphRenderer={flamegraphRenderer}
  270. flamegraphCanvas={flamegraphCanvas}
  271. flamegraphCanvasRef={flamegraphCanvasRef}
  272. flamegraphOverlayCanvasRef={flamegraphOverlayCanvasRef}
  273. flamegraphView={flamegraphView}
  274. setFlamegraphCanvasRef={setFlamegraphCanvasRef}
  275. setFlamegraphOverlayCanvasRef={setFlamegraphOverlayCanvasRef}
  276. contextMenu={FlamegraphContextMenu}
  277. />
  278. }
  279. flamegraphDrawer={
  280. <DifferentialFlamegraphDrawer
  281. profileGroup={
  282. differentialFlamegraph.afterProfileGroup ?? LOADING_PROFILE_GROUP
  283. }
  284. getFrameColor={getFrameColor}
  285. referenceNode={referenceNode}
  286. rootNodes={rootNodes}
  287. flamegraph={differentialFlamegraph.differentialFlamegraph}
  288. formatDuration={
  289. differentialFlamegraph.differentialFlamegraph
  290. ? differentialFlamegraph.differentialFlamegraph.formatter
  291. : noopFormatDuration
  292. }
  293. canvasPoolManager={canvasPoolManager}
  294. canvasScheduler={scheduler}
  295. />
  296. }
  297. />
  298. </DifferentialFlamegraphContainer>
  299. </Feature>
  300. );
  301. }
  302. const DifferentialFlamegraphContainer = styled('div')`
  303. display: flex;
  304. flex-direction: column;
  305. flex: 1;
  306. ~ footer {
  307. display: none;
  308. }
  309. `;
  310. function DifferentialFlamegraphWithProviders() {
  311. return (
  312. <FlamegraphThemeProvider>
  313. <FlamegraphStateProvider
  314. initialState={{
  315. preferences: {
  316. sorting: 'alphabetical',
  317. view: 'top down',
  318. },
  319. }}
  320. >
  321. <DifferentialFlamegraphView />
  322. </FlamegraphStateProvider>
  323. </FlamegraphThemeProvider>
  324. );
  325. }
  326. export default DifferentialFlamegraphWithProviders;