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