Skip to content

Commit

Permalink
Merge pull request #5906 from osalyk/api_filer_func
Browse files Browse the repository at this point in the history
common: add api_filter parameter + small fix
  • Loading branch information
janekmi authored Nov 9, 2023
2 parents c77b47a + f622b33 commit eb16412
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion utils/call_stacks_analysis/generate_call_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit eb16412

Please sign in to comment.