Last active
July 24, 2018 13:49
-
-
Save siddontang/010e7f5af80bd18c0168d00182328ce9 to your computer and use it in GitHub Desktop.
A tool to show how the thread runs on different CPUs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import re | |
# perf record -F 99 -p $1 -e "sched:sched_stat_runtime" -a -- sleep 20 | |
# perf script -F comm,pid,tid,cpu,time | python cpu_tenancy.py | |
# | |
# time-monitor-wo 79618/79634 [007] 11994704.695317: | |
# grpc-server-1 79618/79954 [006] 11994704.717969: | |
# apply worker 79618/79969 [010] 11994704.718048: | |
class CpuCount: | |
def __init__(self, cpu): | |
self.cpu = cpu | |
self.count = 1 | |
def __str__(self): | |
return "CPU-%d/%d" % (self.cpu, self.count) | |
class TidCount: | |
def __init__(self, tid, name, cpu): | |
self.name = name | |
self.tid = tid | |
self.total_count = 1 | |
self.cpu_counts = [CpuCount(cpu)] | |
def add(self, cpu): | |
if self.cpu_counts[-1].cpu != cpu: | |
self.cpu_counts.append(CpuCount(cpu)) | |
else: | |
self.cpu_counts[-1].count += 1 | |
self.total_count += 1 | |
def __str__(self): | |
return "| %s | %s %d" % (" | ".join([str(t) for t in self.cpu_counts]), self.name, self.tid) | |
def get_cpu_tenancy_count_by_tid(): | |
tid_map = dict() | |
for line in sys.stdin: | |
try: | |
tokens = re.split("\s+", line.strip()) | |
process_id = re.search("([0-9]+)/([0-9]+)", tokens[-3].strip()) | |
if process_id is not None: | |
_pid = int(process_id.group(1)) | |
tid = int(process_id.group(2)) | |
cpu_id = int(re.search("\[([0-9]+)\]", tokens[-2]).group(1)) | |
if tid not in tid_map: | |
name = " ".join(tokens[0:len(tokens) - 3]) | |
tid_map[tid] = TidCount(tid, name, cpu_id) | |
else: | |
tid_map[tid].add(cpu_id) | |
except AttributeError: | |
print("failed to parse line: " + line) | |
return tid_map | |
if __name__ == "__main__": | |
tid_map = get_cpu_tenancy_count_by_tid() | |
m = sorted(tid_map.items(), key = lambda kv: kv[1].total_count, reverse=True) | |
for v in m: | |
print(str(v[1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perf record -F 99 -p $1 -e "sched:sched_stat_runtime" -a -- sleep 60 | |
perf script -F comm,pid,tid,cpu,time > tenancy.out | |
cat tenancy.out | python cpu_tenancy.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CPU-6/34 | CPU-8/45 | CPU-30/34 | CPU-34/35 | CPU-6/1 | CPU-14/32 | CPU-32/32 | CPU-16/32 | CPU-38/32 | CPU-2/32 | CPU-14/1 | CPU-18/33 | CPU-38/3 | grpc-server-1 79954 | |
| CPU-32/32 | CPU-30/33 | CPU-38/16 | CPU-6/35 | CPU-8/36 | CPU-32/2 | CPU-30/1 | CPU-18/34 | CPU-38/20 | CPU-16/16 | CPU-12/36 | CPU-14/32 | CPU-34/37 | CPU-14/17 | grpc-server-2 79955 | |
| CPU-34/49 | CPU-18/33 | CPU-32/16 | CPU-6/34 | CPU-8/41 | CPU-18/1 | CPU-32/25 | CPU-14/35 | CPU-32/1 | CPU-6/1 | CPU-30/32 | CPU-1/39 | CPU-10/32 | grpc-server-3 79956 | |
| CPU-30/35 | CPU-6/34 | CPU-2/33 | CPU-32/34 | CPU-2/1 | CPU-14/33 | CPU-38/16 | CPU-14/1 | CPU-8/32 | CPU-18/36 | CPU-38/16 | grpc-server-0 79953 | |
| CPU-16/37 | CPU-1/34 | CPU-34/34 | raftstore-4 79958 | |
| CPU-10/49 | pd worker 79966 | |
| CPU-10/20 | CPU-12/22 | apply worker 79969 | |
| CPU-8/40 | promepusher 79960 | |
| CPU-17/36 | cop-high29 79886 | |
| CPU-5/36 | cop-high20 79877 | |
| CPU-2/35 | pd-0 79628 | |
| CPU-18/34 | tikv-server 79627 | |
| CPU-7/34 | time-monitor-wo 79634 | |
| CPU-13/34 | rocksb-metrics- 79982 | |
| CPU-0/32 | cop-high15 79872 | |
| CPU-21/32 | cop-high16 79873 | |
| CPU-29/32 | cop-high17 79874 | |
| CPU-19/32 | cop-high18 79875 | |
| CPU-12/32 | tikv-server 80005 | |
| CPU-31/32 | cop-high21 79878 | |
| CPU-11/32 | cop-high31 79888 | |
| CPU-9/32 | cop-high0 79857 | |
| CPU-33/32 | cop-high4 79861 | |
| CPU-15/32 | cop-high5 79862 | |
| CPU-15/32 | cop-high6 79863 | |
| CPU-13/32 | cop-high7 79864 | |
| CPU-25/32 | cop-high12 79869 | |
| CPU-9/32 | cop-high13 79870 | |
| CPU-1/20 | end-point-worke 79983 | |
| CPU-24/19 | local-reader 79970 | |
| CPU-9/17 | snapshot worker 79963 | |
| CPU-3/16 | cop-high11 79868 | |
| CPU-13/16 | cop-high14 79871 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment