xref: /xnu-10002.41.9/tools/lldbmacros/taskinfo.py (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributionsfrom core.cvalue import sizeof, value
2*699cd480SApple OSS Distributionsfrom process import GetTaskSummary
3*699cd480SApple OSS Distributionsfrom scheduler import GetRecentTimestamp
4*699cd480SApple OSS Distributionsfrom xnu import lldb_command, kern
5*699cd480SApple OSS Distributions
6*699cd480SApple OSS Distributions# Macro: showtasksuspendstats
7*699cd480SApple OSS Distributions
8*699cd480SApple OSS Distributionsdef ShowTaskSuspendStats(task: value):
9*699cd480SApple OSS Distributions    """
10*699cd480SApple OSS Distributions    Routine to print out a summary of suspension statistics for a given task
11*699cd480SApple OSS Distributions        params:
12*699cd480SApple OSS Distributions            task - core.value : a object of type 'task *'
13*699cd480SApple OSS Distributions        returns:
14*699cd480SApple OSS Distributions            None
15*699cd480SApple OSS Distributions    """
16*699cd480SApple OSS Distributions    stats = task.t_suspend_stats
17*699cd480SApple OSS Distributions    count = stats.tss_count
18*699cd480SApple OSS Distributions    suspended = bool(task.suspend_count > 0)
19*699cd480SApple OSS Distributions    recent_time = GetRecentTimestamp()
20*699cd480SApple OSS Distributions    duration_sec = kern.GetNanotimeFromAbstime(stats.tss_duration) / 1e9
21*699cd480SApple OSS Distributions    last_start_sec = kern.GetNanotimeFromAbstime(stats.tss_last_start - recent_time) / 1e9
22*699cd480SApple OSS Distributions    last_end_sec = kern.GetNanotimeFromAbstime(stats.tss_last_end - recent_time) / 1e9
23*699cd480SApple OSS Distributions    header_fmt = '{:<20s} {:<20s} {:<20s} {:<20s} {:<20s} {:<20s}'
24*699cd480SApple OSS Distributions    header = header_fmt.format('task', 'suspended', 'total_suspensions', 'total_duration(s)', 'last_start_ago(s)', 'last_end_ago(s)')
25*699cd480SApple OSS Distributions    print(header)
26*699cd480SApple OSS Distributions    print(f'{task: <#020x} {str(suspended).lower():<20s} {count:<20d} {duration_sec:<20f} {last_start_sec:<20f} {last_end_sec:<20f}')
27*699cd480SApple OSS Distributions
28*699cd480SApple OSS Distributions@lldb_command('showtasksuspendstats')
29*699cd480SApple OSS Distributionsdef ShowTaskSuspendStatsMacro(cmd_args=None, cmd_options={}):
30*699cd480SApple OSS Distributions    """
31*699cd480SApple OSS Distributions    Display suspension statistics for a given task
32*699cd480SApple OSS Distributions        Usage: showtasksuspendstats <task addr>  (ex. showtasksuspendstats 0x00ataskptr00 )
33*699cd480SApple OSS Distributions    """
34*699cd480SApple OSS Distributions    if not cmd_args or len(cmd_args) != 1:
35*699cd480SApple OSS Distributions        raise ArgumentError("Invalid argument")
36*699cd480SApple OSS Distributions    task = kern.GetValueFromAddress(cmd_args[0], 'task *')
37*699cd480SApple OSS Distributions    ShowTaskSuspendStats(task)
38*699cd480SApple OSS Distributions
39*699cd480SApple OSS Distributions# EndMacro
40*699cd480SApple OSS Distributions# Macro: showtasksuspenders
41*699cd480SApple OSS Distributions
42*699cd480SApple OSS Distributionsdef ShowTaskSuspendSources(task: value):
43*699cd480SApple OSS Distributions    '''
44*699cd480SApple OSS Distributions    Print task suspension events for a given task
45*699cd480SApple OSS Distributions        params:
46*699cd480SApple OSS Distributions            task - core.value : an object of type `task_t`
47*699cd480SApple OSS Distributions    '''
48*699cd480SApple OSS Distributions    sources = task.t_suspend_sources
49*699cd480SApple OSS Distributions    header_fmt = '{:<20s} {:<20s} {:<20s} {:<20s}'
50*699cd480SApple OSS Distributions    header = header_fmt.format('procname', 'pid', 'tid', 'time_ago(s)')
51*699cd480SApple OSS Distributions    print(header)
52*699cd480SApple OSS Distributions    source_count = sizeof(sources) // sizeof(sources[0])
53*699cd480SApple OSS Distributions    for i in range(source_count):
54*699cd480SApple OSS Distributions        source = sources[i]
55*699cd480SApple OSS Distributions        recent_time = GetRecentTimestamp()
56*699cd480SApple OSS Distributions        time_ago_sec = kern.GetNanotimeFromAbstime(source.tss_time - recent_time) / 1e9 if source.tss_time != 0 else -1.0
57*699cd480SApple OSS Distributions        procname = str(source.tss_procname) if str(source.tss_procname) != '' else 'nil'
58*699cd480SApple OSS Distributions        print(f'{procname:<20s} {source.tss_pid:<20d} {source.tss_tid:<20d} {time_ago_sec:<20.3f}')
59*699cd480SApple OSS Distributions
60*699cd480SApple OSS Distributions
61*699cd480SApple OSS Distributions@lldb_command('showtasksuspendsources')
62*699cd480SApple OSS Distributionsdef ShowTaskSuspendSourcesMacro(cmd_args=None, cmd_options={}):
63*699cd480SApple OSS Distributions    '''
64*699cd480SApple OSS Distributions    Show info on the most recent suspenders for a given task
65*699cd480SApple OSS Distributions        Usage showtasksuspenders <task addr> (ex. showtasksuspenders 0x00ataskptr00 )
66*699cd480SApple OSS Distributions    '''
67*699cd480SApple OSS Distributions    if not cmd_args or len(cmd_args) != 1:
68*699cd480SApple OSS Distributions        raise ArgumentError("Invalid argument")
69*699cd480SApple OSS Distributions    task = kern.GetValueFromAddress(cmd_args[0], 'task *')
70*699cd480SApple OSS Distributions    ShowTaskSuspendSources(task)
71*699cd480SApple OSS Distributions
72*699cd480SApple OSS Distributions# EndMacro
73