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