Skip to content

Instantly share code, notes, and snippets.

@squelart
Forked from cpearce/filter-stacks.py
Last active June 1, 2017 23:45
Show Gist options
  • Save squelart/230627ff2fa8d85a1c371906619a8082 to your computer and use it in GitHub Desktop.
Save squelart/230627ff2fa8d85a1c371906619a8082 to your computer and use it in GitHub Desktop.
Filter for BHR json logs, keeping counts of all buckets with each stack, 2nd arg is regex to only keep stacks of interest
#!/bin/env python
import json
import re
import sys
skipList = [
"BaseThreadInitThunk",
"CharPrevA",
"CsrAllocateMessagePointer",
"DispatchMessageW",
"DispatchMessageWorker",
"EtwEventEnabled",
"GetCurrentThread",
"GetTickCount",
"KiFastSystemCallRet",
"MessageLoop::Run",
"MessageLoop::RunHandler",
"MsgWaitForMultipleObjects",
"MsgWaitForMultipleObjectsEx",
"NS_ProcessNextEvent",
"NS_internal_main",
"NtUserValidateHandleSecure",
"NtWaitForAlertByThreadId",
"NtWaitForMultipleObjects",
"NtWaitForSingleObject",
"PR_Lock",
"PeekMessageW",
"RealMsgWaitForMultipleObjectsEx",
"RtlAnsiStringToUnicodeString",
"RtlDeNormalizeProcessParams",
"RtlEnterCriticalSection",
"RtlLeaveCriticalSection",
"RtlUserThreadStart",
"RtlpAllocateListLookup",
"RtlpDeCommitFreeBlock",
"RtlpEnterCriticalSectionContended",
"RtlpUnWaitCriticalSection",
"RtlpWaitOnAddress",
"RtlpWaitOnAddressWithTimeout",
"RtlpWaitOnCriticalSection",
"RtlpWakeByAddress",
"UserCallWinProcCheckWow"
"ValidateHwnd",
"WaitForMultipleObjectsEx",
"WaitForMultipleObjectsExImplementation",
"WaitForSingleObjectEx",
"XRE_InitChildProcess",
"XRE_RunAppShell",
"ZwWaitForMultipleObjects",
"ZwWaitForSingleObject",
"_RtlUserThreadStart",
"__RtlUserThreadStart",
"__scrt_common_main_seh",
"content_process_main",
"mozilla::BackgroundHangMonitor::NotifyActivity",
"mozilla::BackgroundHangThread::NotifyActivity",
"mozilla::BackgroundHangThread::NotifyWait",
"mozilla::BootstrapImpl::XRE_InitChildProcess",
"mozilla::HangMonitor::NotifyActivity",
"mozilla::HangMonitor::Suspend",
"mozilla::ValidatingDispatcher::Runnable::Run",
"mozilla::detail::MutexImpl::lock",
"mozilla::ipc::MessageChannel::MessageTask::Run",
"mozilla::ipc::MessagePump::Run",
"mozilla::ipc::MessagePumpForChildProcess::Run",
"mozilla::widget::WinUtils::PeekMessageW",
"mozilla::widget::WinUtils::WaitForMessage",
"nsAppShell::ProcessNextNativeEvent",
"nsAppShell::Run",
"nsBaseAppShell::DoProcessNextNativeEvent",
"nsBaseAppShell::OnProcessNextEvent",
"nsBaseAppShell::Run",
"nsThread::DoMainThreadSpecificProcessing",
"nsThread::ProcessNextEvent",
"wmain",
"UserCallWinProcCheckWow",
"ValidateHwnd",
"0x1bc3d",
"0x1905a",
"RtlSleepConditionVariableCS",
"SleepConditionVariableCS",
"NDXGI::CDevice::GetKernelDeviceExecutionState",
"NtGdiDdDDIGetDeviceState",
"_PR_",
"_MD_",
"WaitForSingleObject",
"0x",
]
def shouldSkip(frame):
for item in skipList:
if frame.startswith(item):
return True
return False
def filterStacks(stacks, filter):
newstacks = []
accept = filter is None
for frame in stacks:
if shouldSkip(frame):
continue
if filter is not None and filter.search(frame) is not None:
accept = True
newstacks.append(frame)
if accept:
return newstacks
return []
if __name__ == "__main__":
input = sys.argv[1]
filter = None
if len(sys.argv) >= 3:
filter = re.compile(sys.argv[2])
print "we have a filter!"
data = json.load(open(input))
ranks = {}
for record in data:
stacks = filterStacks(record['nativeStack']['symbolicatedStacks'][0], filter)
key = '`'.join(stacks)
if stacks:
if not ranks.has_key(key):
ranks[key] = {'stacks':stacks, 'count':1, 'buckets':{}, 'sum':0 }
else:
ranks[key]['count'] += 1
for bucket in record['histogram']['values']:
if record['histogram']['values'][bucket] > 0:
if not ranks[key]['buckets'].has_key(bucket):
ranks[key]['buckets'][bucket] = record['histogram']['values'][bucket]
else:
ranks[key]['buckets'][bucket] += record['histogram']['values'][bucket]
ranks[key]['sum'] += int(bucket) * record['histogram']['values'][bucket]
for r in sorted(ranks.itervalues(), key=lambda r: r['sum'], reverse=True):
print("{} - {}: {}".format(r['count'],
r['sum'],
', '.join(['x'.join([bucket, str(r['buckets'][bucket])])
for bucket in
sorted(r['buckets'], key=lambda x: int(x), reverse=True)])))
for frame in r['stacks']:
print(" {}".format(frame))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment