| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
from argparse import ArgumentParser |
| 5 |
|
| 6 |
|
| 7 |
def psi_path_to_metrics(psi_path): |
| 8 |
""" |
| 9 |
""" |
| 10 |
with open(psi_path) as f: |
| 11 |
psi_list = f.readlines() |
| 12 |
|
| 13 |
some_list, full_list = psi_list[0].split(' '), psi_list[1].split(' ') |
| 14 |
|
| 15 |
some_avg10 = some_list[1].split('=')[1] |
| 16 |
some_avg60 = some_list[2].split('=')[1] |
| 17 |
some_avg300 = some_list[3].split('=')[1] |
| 18 |
|
| 19 |
full_avg10 = full_list[1].split('=')[1] |
| 20 |
full_avg60 = full_list[2].split('=')[1] |
| 21 |
full_avg300 = full_list[3].split('=')[1] |
| 22 |
|
| 23 |
return (some_avg10, some_avg60, some_avg300, |
| 24 |
full_avg10, full_avg60, full_avg300) |
| 25 |
|
| 26 |
|
| 27 |
def psi_path_to_metrics_cpu(psi_path): |
| 28 |
""" |
| 29 |
""" |
| 30 |
with open(psi_path) as f: |
| 31 |
psi_list = f.readlines() |
| 32 |
|
| 33 |
some_list = psi_list[0].rstrip().split(' ') |
| 34 |
|
| 35 |
some_avg10 = some_list[1].split('=')[1] |
| 36 |
some_avg60 = some_list[2].split('=')[1] |
| 37 |
some_avg300 = some_list[3].split('=')[1] |
| 38 |
|
| 39 |
return (some_avg10, some_avg60, some_avg300) |
| 40 |
|
| 41 |
|
| 42 |
def cgroup2_root(): |
| 43 |
""" |
| 44 |
""" |
| 45 |
with open(mounts) as f: |
| 46 |
for line in f: |
| 47 |
if cgroup2_separator in line: |
| 48 |
return line.partition(cgroup2_separator)[0].partition(' ')[2] |
| 49 |
|
| 50 |
|
| 51 |
def get_psi_mem_files(cgroup2_path, met): |
| 52 |
""" |
| 53 |
""" |
| 54 |
path_list = [] |
| 55 |
|
| 56 |
for root, dirs, files in os.walk(cgroup2_path): |
| 57 |
for file in files: |
| 58 |
path = os.path.join(root, file) |
| 59 |
if path.endswith('/{}.pressure'.format(met)): |
| 60 |
path_list.append(path) |
| 61 |
|
| 62 |
return path_list |
| 63 |
|
| 64 |
|
| 65 |
def psi_path_to_cgroup2(path): |
| 66 |
""" |
| 67 |
""" |
| 68 |
|
| 69 |
if path.endswith('/cpu.pressure'): |
| 70 |
return path.partition(cgroup2_mountpoint)[ |
| 71 |
2].partition('/cpu.pressure')[0] |
| 72 |
|
| 73 |
if path.endswith('/io.pressure'): |
| 74 |
return path.partition(cgroup2_mountpoint)[ |
| 75 |
2].partition('/io.pressure')[0] |
| 76 |
|
| 77 |
if path.endswith('/memory.pressure'): |
| 78 |
return path.partition(cgroup2_mountpoint)[ |
| 79 |
2].partition('/memory.pressure')[0] |
| 80 |
|
| 81 |
|
| 82 |
parser = ArgumentParser() |
| 83 |
|
| 84 |
parser.add_argument( |
| 85 |
'-m', |
| 86 |
'--metrics', |
| 87 |
help="""metrics (memory, io or cpu)""", |
| 88 |
default='memory', |
| 89 |
type=str |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
args = parser.parse_args() |
| 94 |
|
| 95 |
|
| 96 |
met = args.metrics |
| 97 |
|
| 98 |
|
| 99 |
if not (met == 'memory' or met == 'io' or met == 'cpu'): |
| 100 |
print('ERROR: invalid metrics:', met) |
| 101 |
exit(1) |
| 102 |
|
| 103 |
|
| 104 |
psi_path = '/proc/pressure/{}'.format(met) |
| 105 |
mounts = '/proc/mounts' |
| 106 |
cgroup2_separator = ' cgroup2 rw,' |
| 107 |
|
| 108 |
cgroup2_mountpoint = cgroup2_root() |
| 109 |
|
| 110 |
|
| 111 |
if cgroup2_mountpoint is None: |
| 112 |
print('ERROR: cgroup_v2 hierarchy is not mounted') |
| 113 |
exit(1) |
| 114 |
|
| 115 |
|
| 116 |
try: |
| 117 |
psi_path_to_metrics('/proc/pressure/memory') |
| 118 |
except Exception as e: |
| 119 |
print('ERROR: {}'.format(e)) |
| 120 |
print('PSI metrics are not provided by the kernel. Exit.') |
| 121 |
exit(1) |
| 122 |
|
| 123 |
|
| 124 |
if cgroup2_mountpoint is not None: |
| 125 |
y = get_psi_mem_files(cgroup2_mountpoint, met) |
| 126 |
|
| 127 |
|
| 128 |
path_list = get_psi_mem_files(cgroup2_mountpoint, met) |
| 129 |
|
| 130 |
|
| 131 |
head_mem_io = '''PSI metrics: {} |
| 132 |
cgroup_v2 mountpoint: {} |
| 133 |
=====================|======================| |
| 134 |
some | full | |
| 135 |
-------------------- | -------------------- | |
| 136 |
avg10 avg60 avg300 | avg10 avg60 avg300 | cgroup_v2 |
| 137 |
------ ------ ------ | ------ ------ ------ | -----------'''.format( |
| 138 |
met, cgroup2_mountpoint) |
| 139 |
|
| 140 |
|
| 141 |
head_cpu = '''PSI metrics: {} |
| 142 |
cgroup_v2 mountpoint: {} |
| 143 |
=====================| |
| 144 |
some | |
| 145 |
-------------------- | |
| 146 |
avg10 avg60 avg300 | cgroup_v2 |
| 147 |
------ ------ ------ | -----------'''.format( |
| 148 |
met, cgroup2_mountpoint) |
| 149 |
|
| 150 |
|
| 151 |
if met == 'cpu': |
| 152 |
print(head_cpu) |
| 153 |
else: |
| 154 |
print(head_mem_io) |
| 155 |
|
| 156 |
|
| 157 |
if met == 'cpu': |
| 158 |
|
| 159 |
some_avg10, some_avg60, some_avg300 = psi_path_to_metrics_cpu(psi_path) |
| 160 |
|
| 161 |
print('{} {} {} | {}'.format( |
| 162 |
some_avg10.rjust(6), |
| 163 |
some_avg60.rjust(6), |
| 164 |
some_avg300.rjust(6), |
| 165 |
'SYSTEM_WIDE')) |
| 166 |
|
| 167 |
else: |
| 168 |
|
| 169 |
(some_avg10, some_avg60, some_avg300, full_avg10, full_avg60, full_avg300 |
| 170 |
) = psi_path_to_metrics(psi_path) |
| 171 |
|
| 172 |
print('{} {} {} | {} {} {} | {}'.format( |
| 173 |
some_avg10.rjust(6), |
| 174 |
some_avg60.rjust(6), |
| 175 |
some_avg300.rjust(6), |
| 176 |
full_avg10.rjust(6), |
| 177 |
full_avg60.rjust(6), |
| 178 |
full_avg300.rjust(6), 'SYSTEM_WIDE')) |
| 179 |
|
| 180 |
|
| 181 |
for psi_path in path_list: |
| 182 |
|
| 183 |
if met == 'cpu': |
| 184 |
some_avg10, some_avg60, some_avg300 = psi_path_to_metrics_cpu(psi_path) |
| 185 |
|
| 186 |
print('{} {} {} | {}'.format( |
| 187 |
some_avg10.rjust(6), |
| 188 |
some_avg60.rjust(6), |
| 189 |
some_avg300.rjust(6), |
| 190 |
psi_path_to_cgroup2(psi_path))) |
| 191 |
|
| 192 |
else: |
| 193 |
(some_avg10, some_avg60, some_avg300, |
| 194 |
full_avg10, full_avg60, full_avg300) = psi_path_to_metrics(psi_path) |
| 195 |
|
| 196 |
print('{} {} {} | {} {} {} | {}'.format( |
| 197 |
some_avg10.rjust(6), |
| 198 |
some_avg60.rjust(6), |
| 199 |
some_avg300.rjust(6), |
| 200 |
full_avg10.rjust(6), |
| 201 |
full_avg60.rjust(6), |
| 202 |
full_avg300.rjust(6), psi_path_to_cgroup2(psi_path))) |