+bucketStr = ["", "FIXPRI (>UI)", "TIMESHARE_FG", "TIMESHARE_IN", "TIMESHARE_DF", "TIMESHARE_UT", "TIMESHARE_BG"]
+
+@header(" {:>18s} | {:>20s} | {:>20s} | {:>10s} | {:>10s}".format('Thread Group', 'Interactivity Score', 'Last Timeshare Tick', 'pri_shift', 'highq'))
+def GetSchedClutchBucketSummary(clutch_bucket):
+ return " 0x{:>16x} | {:>20d} | {:>20d} | {:>10d} | {:>10d}".format(clutch_bucket.scb_clutch.sc_tg, clutch_bucket.scb_interactivity_score, clutch_bucket.scb_timeshare_tick, clutch_bucket.scb_pri_shift, clutch_bucket.scb_runq.highq)
+
+def ShowSchedClutchForPset(pset):
+ root_clutch = pset.pset_clutch_root
+ print "\n{:s} : {:d}\n\n".format("Current Timestamp", GetRecentTimestamp())
+ print "{:>10s} | {:>20s} | {:>30s} | {:>18s} | {:>10s} | {:>10s} | {:>30s} | {:>30s} | {:>15s} | ".format("Root", "Root Buckets", "Clutch Buckets", "Address", "Priority", "Count", "CPU Usage (MATUs)", "CPU Blocked (MATUs)", "Deadline (abs)") + GetSchedClutchBucketSummary.header
+ print "=" * 300
+ print "{:>10s} | {:>20s} | {:>30s} | 0x{:16x} | {:>10d} | {:>10d} | {:>30s} | {:>30s} | {:>15s} | ".format("Root", "*", "*", addressof(root_clutch), root_clutch.scr_priority, root_clutch.scr_thr_count, "*", "*", "*")
+ print "-" * 300
+
+ for i in range(1, 7):
+ root_bucket = root_clutch.scr_buckets[i]
+ print "{:>10s} | {:>20s} | {:>30s} | 0x{:16x} | {:>10s} | {:>10s} | {:>30s} | {:>30s} | {:>15d} | ".format("*", bucketStr[i], "*", addressof(root_bucket), "*", "*", "*", "*", root_bucket.scrb_deadline)
+ prioq = root_bucket.scrb_clutch_buckets
+ clutch_bucket_list = []
+ for clutch_bucket in IteratePriorityQueue(prioq, 'struct sched_clutch_bucket', 'scb_pqlink'):
+ clutch_bucket_list.append(clutch_bucket)
+ if len(clutch_bucket_list) > 0:
+ clutch_bucket_list.sort(key=lambda x: x.scb_priority, reverse=True)
+ for clutch_bucket in clutch_bucket_list:
+ cpu_used = clutch_bucket.scb_cpu_data.cpu_data.scbcd_cpu_used
+ cpu_blocked = clutch_bucket.scb_cpu_data.cpu_data.scbcd_cpu_blocked
+ print "{:>10s} | {:>20s} | {:>30s} | 0x{:16x} | {:>10d} | {:>10d} | {:>30d} | {:>30d} | {:>15s} | ".format("*", "*", clutch_bucket.scb_clutch.sc_tg.tg_name, clutch_bucket, clutch_bucket.scb_priority, clutch_bucket.scb_thr_count, cpu_used, cpu_blocked, "*") + GetSchedClutchBucketSummary(clutch_bucket)
+ print "-" * 300
+
+@lldb_command('showschedclutch')
+def ShowSchedClutch(cmd_args=[]):
+ """ Routine to print the clutch scheduler hierarchy.
+ Usage: showschedclutch <pset>
+ """
+ if not cmd_args:
+ raise ArgumentError("Invalid argument")
+ pset = kern.GetValueFromAddress(cmd_args[0], "processor_set_t")
+ ShowSchedClutchForPset(pset)
+
+@lldb_command('showschedclutchroot')
+def ShowSchedClutchRoot(cmd_args=[]):
+ """ show information about the root of the sched clutch hierarchy
+ Usage: showschedclutchroot <root>
+ """
+ if not cmd_args:
+ raise ArgumentError("Invalid argument")
+ root = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_root *")
+ if not root:
+ print "unknown arguments:", str(cmd_args)
+ return False
+ print "{:>30s} : 0x{:16x}".format("Root", root)
+ print "{:>30s} : 0x{:16x}".format("Pset", root.scr_pset)
+ print "{:>30s} : {:d}".format("Priority", root.scr_priority)
+ print "{:>30s} : {:d}".format("Urgency", root.scr_urgency)
+ print "{:>30s} : {:d}".format("Threads", root.scr_thr_count)
+ print "{:>30s} : {:d}".format("Current Timestamp", GetRecentTimestamp())
+ print "{:>30s} : {:b} (BG/UT/DF/IN/FG/FIX/NULL)".format("Runnable Root Buckets Bitmap", int(root.scr_runnable_bitmap[0]))
+
+@lldb_command('showschedclutchrootbucket')
+def ShowSchedClutchRootBucket(cmd_args=[]):
+ """ show information about a root bucket in the sched clutch hierarchy
+ Usage: showschedclutchrootbucket <root_bucket>
+ """
+ if not cmd_args:
+ raise ArgumentError("Invalid argument")
+ root_bucket = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_root_bucket *")
+ if not root_bucket:
+ print "unknown arguments:", str(cmd_args)
+ return False
+ print "{:<30s} : 0x{:16x}".format("Root Bucket", root_bucket)
+ print "{:<30s} : {:s}".format("Bucket Name", bucketStr[int(root_bucket.scrb_bucket)])
+ print "{:<30s} : {:d}".format("Deadline", root_bucket.scrb_deadline)
+ print "{:<30s} : {:d}".format("Current Timestamp", GetRecentTimestamp())
+ print "\n"
+ prioq = root_bucket.scrb_clutch_buckets
+ clutch_bucket_list = []
+ for clutch_bucket in IteratePriorityQueue(prioq, 'struct sched_clutch_bucket', 'scb_pqlink'):
+ clutch_bucket_list.append(clutch_bucket)
+ if len(clutch_bucket_list) > 0:
+ print "=" * 240
+ print "{:>30s} | {:>18s} | {:>20s} | {:>20s} | ".format("Name", "Clutch Bucket", "Priority", "Count") + GetSchedClutchBucketSummary.header
+ print "=" * 240
+ clutch_bucket_list.sort(key=lambda x: x.scb_priority, reverse=True)
+ for clutch_bucket in clutch_bucket_list:
+ print "{:>30s} | 0x{:16x} | {:>20d} | {:>20d} | ".format(clutch_bucket.scb_clutch.sc_tg.tg_name, clutch_bucket, clutch_bucket.scb_priority, clutch_bucket.scb_thr_count) + GetSchedClutchBucketSummary(clutch_bucket)
+
+@lldb_command('showschedclutchbucket')
+def ShowSchedClutchBucket(cmd_args=[]):
+ """ show information about a clutch bucket in the sched clutch hierarchy
+ Usage: showschedclutchbucket <clutch_bucket>
+ """
+ if not cmd_args:
+ raise ArgumentError("Invalid argument")
+ clutch_bucket = kern.GetValueFromAddress(cmd_args[0], "struct sched_clutch_bucket *")
+ if not clutch_bucket:
+ print "unknown arguments:", str(cmd_args)
+ return False
+ print "{:<30s} : 0x{:16x}".format("Clutch Bucket", clutch_bucket)
+ print "{:<30s} : {:s}".format("TG Name", clutch_bucket.scb_clutch.sc_tg.tg_name)
+ print "{:<30s} : {:d}".format("Priority", clutch_bucket.scb_priority)
+ print "{:<30s} : {:d}".format("Thread Count", clutch_bucket.scb_thr_count)
+ print "{:<30s} : 0x{:16x}".format("Thread Group", clutch_bucket.scb_clutch.sc_tg)
+ cpu_used = clutch_bucket.scb_cpu_data.cpu_data.scbcd_cpu_used
+ cpu_blocked = clutch_bucket.scb_cpu_data.cpu_data.scbcd_cpu_blocked
+ print "{:<30s} : {:d}".format("CPU Used (MATUs)", cpu_used)
+ print "{:<30s} : {:d}".format("CPU Blocked (MATUs)", cpu_blocked)
+ print "{:<30s} : {:d}".format("Interactivity Score", clutch_bucket.scb_interactivity_score)
+ print "{:<30s} : {:d}".format("Last Timeshare Update Tick", clutch_bucket.scb_timeshare_tick)
+ print "{:<30s} : {:d}".format("Priority Shift", clutch_bucket.scb_pri_shift)
+ print "\n"
+ runq = clutch_bucket.scb_clutchpri_prioq
+ thread_list = []
+ for thread in IteratePriorityQueue(runq, 'struct thread', 'sched_clutchpri_link'):
+ thread_list.append(thread)
+ if len(thread_list) > 0:
+ print "=" * 240
+ print GetThreadSummary.header + "{:s}".format("Process Name")
+ print "=" * 240
+ for thread in thread_list:
+ proc = Cast(thread.task.bsd_info, 'proc *')
+ print GetThreadSummary(thread) + "{:s}".format(str(proc.p_comm))