diff --git a/utils/call_stacks_analysis/generate_call_stacks.py b/utils/call_stacks_analysis/generate_call_stacks.py index d721cecc106..958a9db8511 100755 --- a/utils/call_stacks_analysis/generate_call_stacks.py +++ b/utils/call_stacks_analysis/generate_call_stacks.py @@ -21,6 +21,7 @@ PARSER.add_argument('-e', '--extra-calls', default='extra_calls.json') PARSER.add_argument('-a', '--api-file', default='api.txt') PARSER.add_argument('-w', '--white-list') # XXX +PARSER.add_argument('-i', '--api-filter-file') PARSER.add_argument('-d', '--dump', action='store_true', help='Dump debug files') PARSER.add_argument('-t', '--skip-threshold', type=int, default=0, help='Ignore non-reachable function if its stack usage <= threshold') @@ -31,6 +32,9 @@ # WhiteList: TypeAlias = List[str] # for Python >= 3.10 WhiteList = List[str] # for Python < 3.8 +# APIFilter: TypeAlias = List[str] # for Python >= 3.10 +APIFilter = List[str] # for Python < 3.8 + # class StackUsageRecord(TypedDict): # for Python >= 3.8 # size: int # type: str @@ -64,7 +68,7 @@ def load_from_json(file_name: str) -> Any: return json.load(file) def txt_filter(line: str) -> bool: - if len(line) == 0: # drop empty lines + if line == '\n': # drop empty lines return False if line[0] == '#': # drop comment lines return False @@ -277,6 +281,17 @@ def generate_all_call_stacks(stack_usage: StackUsage, calls: Calls, rcalls: RCal call_stacks.sort(reverse=True, key=call_stack_key) return call_stacks +def filter_call_stacks(call_stacks: List[CallStack], api_filter_file: APIFilter) -> List[CallStack]: + api_filter = load_from_txt(api_filter_file) + dump(api_filter, 'api_filter') + print('Load APIFilter - done') + + call_stacks_filtered = [] + for call_stack in call_stacks: + if call_stack['stack'][0] in api_filter: + call_stacks_filtered.append(call_stack) + return call_stacks_filtered + def main(): args = PARSER.parse_args() global DUMP @@ -312,5 +327,11 @@ def main(): print('Number of found call stacks: {}'.format(len(call_stacks))) print('Call stack generation - done') + if args.api_filter_file is not None: + call_stacks_filtered = filter_call_stacks(call_stacks, args.api_filter_file) + dump(call_stacks_filtered, 'call_stacks_filtered', True) + print('Number of found call stacks (filtered): {}'.format(len(call_stacks_filtered))) + print('Call stacks filtering - done') + if __name__ == '__main__': main()