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