Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Flamegraph data trimming #2299

Merged
merged 10 commits into from
Dec 22, 2022
Prev Previous commit
Next Next commit
Introduced a user preference hook that supports types other than bool…
…ean and hooked in NodeCutOffThreshold prefrence for the query request
  • Loading branch information
manojVivek committed Dec 20, 2022
commit 5e1820bcaa2cbcca7be81e8faf9f6fa5794a5a5c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import useUserPreference, {USER_PREFERENCES} from '@parca/functions/useUserPreference';
import UserPreferenceItem from './UserPreferenceItem';

const GraphTrimming = () => {
const [disabledTrimming] = useUserPreference<boolean>(USER_PREFERENCES.DISABLE_TRIMMING.key);

return (
<div>
<UserPreferenceItem userPreferenceDetails={USER_PREFERENCES.DISABLE_TRIMMING} />
<div className="">
<UserPreferenceItem
userPreferenceDetails={USER_PREFERENCES.NODE_TRIM_THRESHOLD}
className="flex-row-reverse w-80"
disabled={disabledTrimming}
/>
<p className="text-xs mt-1 ml-6">
Nodes that have value less than this percentage will be removed.
</p>
</div>
</div>
);
};

export default GraphTrimming;
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import cx from 'classnames';
import useUserPreference, {UserPreferenceDetails} from '@parca/functions/useUserPreference';

interface GenericInputProps {
id: string;
type: string;
value: any;
onChange: (value: any) => void;
disabled?: boolean;
}

const GenericInput = ({id, type, value, onChange, disabled = false}: GenericInputProps) => {
switch (type) {
case 'boolean':
return (
<input
className="bg-inherit border disabled:cursor-not-allowed"
id={id}
type="checkbox"
checked={value}
onChange={e => onChange(e.target.checked)}
disabled={disabled}
/>
);
case 'number':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting idea, but I think this would break shareability, if you have a different configuration from me and you share a link with me I will see different data, if we expose this value (which I don't think we should today), then we should make it part of exploring the data (and therefore part of the URL)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ok, and we can make this a value added to the URL whenever the user is in the ProfileExplorer and the query formed with the value from the URL taking higher precedence over the value from the preference so that the shareability is maintained.

But I would prefer moving this into a separate issue and implement it once #2115 is merged. As it has some nice utilities for dealing with the URL params state. Any concerns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest then let's just use a hard-coded value for a start. (I'm not even sure it ever doesn't need to be)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to see if I can implement the URL state without that PR, but it looks like a lot of duplicate work between these two. So, I'll hold it off.

So, if I understand it correctly, meanwhile, you would like this option removed from the user preference and work with just the hardcoded 0.005cut-off value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done.

return (
<input
className="bg-inherit border p-1 w-20 disabled:cursor-not-allowed rounded-sm"
id={id}
type="number"
value={value}
onChange={e => onChange(Number(e.target.value))}
disabled={disabled}
/>
);
}
};

interface UserPreferenceItemProps {
userPreferenceDetails: UserPreferenceDetails;
className?: string;
disabled?: boolean;
}

function UserPreferenceItem<T>({
userPreferenceDetails,
className = '',
disabled = false,
}: UserPreferenceItemProps) {
const [enabledTrimming, setEnabledTrimming] = useUserPreference<T>(userPreferenceDetails.key);

return (
<div
className={cx('flex gap-2 items-center', {[className]: className, 'opacity-50': disabled})}
>
<GenericInput
id={userPreferenceDetails.key}
value={enabledTrimming}
onChange={setEnabledTrimming}
type={userPreferenceDetails.type}
disabled={disabled}
/>
<label htmlFor={userPreferenceDetails.key}>{userPreferenceDetails.name}</label>
</div>
);
}

export default UserPreferenceItem;
Original file line number Diff line number Diff line change
@@ -14,7 +14,10 @@
import {Icon} from '@iconify/react';
import {Button, IconButton, Modal} from '@parca/components';
import useUIFeatureFlag from '@parca/functions/useUIFeatureFlag';
import {USER_PREFERENCES} from '@parca/functions/useUserPreference';
import {useState} from 'react';
import GraphTrimming from './GraphTrimming';
import UserPreferenceItem from './UserPreferenceItem';

interface FlagToggleProps {
name: string;
@@ -50,16 +53,11 @@ const UserPreferences = () => {
setIsOpen(false);
}}
title="Preferences"
className="max-w-[500px]"
>
<div className="min-h-40 min-w-96 mt-8">
<FlagToggle
name="Highlight matching nodes after filtering"
id="highlightAfterFiltering"
/>
<FlagToggle
name="Disable trimming of insignificant nodes"
id="flamegraphDisableTrimming"
/>
<div className="min-h-40 w-[500px] mt-8">
<UserPreferenceItem userPreferenceDetails={USER_PREFERENCES.HIGHTLIGHT_AFTER_FILTERING} />
<GraphTrimming />
<div className=" min-w-96 mt-8">
<h4 className="font-medium mb-2">Experimental Features</h4>
<FlagToggle name="Enable Callgraph" id="callgraph" />
59 changes: 59 additions & 0 deletions ui/packages/shared/functions/src/useUserPreference/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {useLocalStorage} from '@rehooks/local-storage';

const USER_PREFERENCES_KEY = 'user-preferences';

export interface UserPreferenceDetails {
name: string;
key: string;
type: string;
default: any;
}

export const USER_PREFERENCES: {[key: string]: UserPreferenceDetails} = {
HIGHTLIGHT_AFTER_FILTERING: {
name: 'Highlight matching nodes after filtering',
key: 'HIGHTLIGHT_AFTER_FILTERING',
type: 'boolean',
default: true,
},
DISABLE_TRIMMING: {
name: 'Disable trimming of insignificant nodes',
key: 'DISABLE_TRIMMING',
type: 'boolean',
default: false,
},
NODE_TRIM_THRESHOLD: {
name: 'Node trim threshold',
key: 'NODE_TRIM_THRESHOLD',
type: 'number',
default: 0.5,
},
} as const;

export type UserPreference = keyof typeof USER_PREFERENCES;

const useUserPreference = <T>(preferenceName: UserPreference): [T, (flag: T) => void] => {
const [flags, setFlags] = useLocalStorage<{[flag: string]: any}>(USER_PREFERENCES_KEY, {});

const value: T = flags[preferenceName] ?? USER_PREFERENCES[preferenceName].default;
const setFlag = (flag: T): void => {
setFlags({...flags, [preferenceName]: flag});
};

return [value, setFlag];
};

export default useUserPreference;
15 changes: 15 additions & 0 deletions ui/packages/shared/functions/useUserPreference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

export {default} from './src/useUserPreference';
export * from './src/useUserPreference';
5 changes: 4 additions & 1 deletion ui/packages/shared/profile/src/ProfileView/index.tsx
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ import TopTable from '../TopTable';
import useDelayedLoader from '../useDelayedLoader';

import '../ProfileView.styles.css';
import useUserPreference, {USER_PREFERENCES} from '@parca/functions/useUserPreference';

type NavigateFunction = (path: string, queryParams: any) => void;

@@ -123,7 +124,9 @@ export const ProfileView = ({
const filterByFunctionString = useAppSelector(selectFilterByFunction);

const [callgraphEnabled] = useUIFeatureFlag('callgraph');
const [highlightAfterFilteringEnabled] = useUIFeatureFlag('highlightAfterFiltering');
const [highlightAfterFilteringEnabled] = useUserPreference<boolean>(
USER_PREFERENCES.HIGHTLIGHT_AFTER_FILTERING.key
);

const {loader, perf} = useParcaContext();

6 changes: 4 additions & 2 deletions ui/packages/shared/profile/src/ProfileViewWithData.tsx
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ import {downloadPprof} from './utils';
import {useGrpcMetadata, useParcaContext} from '@parca/components';
import {saveAsBlob} from '@parca/functions';
import {useEffect} from 'react';
import useUIFeatureFlag from '@parca/functions/useUIFeatureFlag';
import useUserPreference, {USER_PREFERENCES} from '@parca/functions/useUserPreference';

export type NavigateFunction = (path: string, queryParams: any) => void;

@@ -39,14 +39,16 @@ export const ProfileViewWithData = ({
const profileVisState = useProfileVisState();
const metadata = useGrpcMetadata();
const {currentView} = profileVisState;
const [disableTrimming] = useUIFeatureFlag('flamegraphDisableTrimming', false);
const [disableTrimming] = useUserPreference<boolean>(USER_PREFERENCES.DISABLE_TRIMMING.key);
const [nodeTrimThreshold] = useUserPreference<number>(USER_PREFERENCES.NODE_TRIM_THRESHOLD.key);
const {
isLoading: flamegraphLoading,
response: flamegraphResponse,
error: flamegraphError,
} = useQuery(queryClient, profileSource, QueryRequest_ReportType.FLAMEGRAPH_TABLE, {
skip: currentView !== 'icicle' && currentView !== 'both',
disableTrimming,
nodeTrimThreshold,
});
const {perf} = useParcaContext();

4 changes: 3 additions & 1 deletion ui/packages/shared/profile/src/useQuery.tsx
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ export interface IQueryResult {
interface UseQueryOptions {
skip?: boolean;
disableTrimming?: boolean;
nodeTrimThreshold?: number;
}

export const useQuery = (
@@ -38,11 +39,12 @@ export const useQuery = (
const {skip = false, disableTrimming = false} = options ?? {};
const metadata = useGrpcMetadata();
const {data, isLoading, error} = useGrpcQuery<QueryResponse | undefined>({
key: ['query', profileSource, reportType, options?.disableTrimming],
key: ['query', profileSource, reportType, options?.disableTrimming, options?.nodeTrimThreshold],
queryFn: async () => {
const req = profileSource.QueryRequest();
req.reportType = reportType;
req.disableTrimming = disableTrimming;
req.nodeTrimThreshold = options?.nodeTrimThreshold;

const {response} = await client.query(req, {meta: metadata});
return response;