Skip to content

Commit

Permalink
cubestat: lint again
Browse files Browse the repository at this point in the history
  • Loading branch information
okuvshynov committed Aug 2, 2024
1 parent e1a99fc commit 0ad963c
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 25 deletions.
1 change: 1 addition & 0 deletions cubestat/cubestat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ def main():
args = parser.parse_args()
curses.wrapper(start, get_platform(args.refresh_ms), args)


if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion cubestat/metrics/base_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cubestat.common import SimpleMode


# TODO: make this one metric_set or metric_group.
# each individual metric would be able to pick the implementation
# for formatting, etc.
Expand Down Expand Up @@ -44,7 +45,7 @@ def hotkey(self):
def configure_argparse(cls, parser):
pass

# help message to be used for this metric.
# help message to be used for this metric.
@classmethod
def help(self):
return None
2 changes: 1 addition & 1 deletion cubestat/metrics/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def read(self, _context):

return res


@cubestat_metric('darwin')
class macos_cpu_metric(cpu_metric):
def read(self, context):
Expand All @@ -85,4 +86,3 @@ def read(self, context):
res[cluster_title] = 100.0 - 100.0 * idle_cluster / total_cluster

return res

13 changes: 8 additions & 5 deletions cubestat/metrics/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from cubestat.metrics.base_metric import base_metric
from cubestat.metrics.registry import cubestat_metric


class disk_metric(base_metric):
def pre(self, title):
if self.mode == SimpleMode.hide:
return False, ''
return True, ''

def format(self, title, values, idxs):
return label_bytes_per_sec(values, idxs)

Expand All @@ -29,19 +30,21 @@ def configure(self, conf):
self.rate_reader = RateReader(conf.refresh_ms)
return self


@cubestat_metric('darwin')
class macos_disc_metric(disk_metric):
def read(self, context):
res = {}
res['disk read'] = context['disk']['rbytes_per_s']
res['disk read'] = context['disk']['rbytes_per_s']
res['disk write'] = context['disk']['wbytes_per_s']
return res



@cubestat_metric('linux')
class linux_disc_metric(disk_metric):
def read(self, _context):
res = {}
disk_io = psutil.disk_io_counters()
res['disk read'] = self.rate_reader.next('disk read', disk_io.read_bytes)
res['disk write'] = self.rate_reader.next('disk write', disk_io.write_bytes)
res['disk read'] = self.rate_reader.next('disk read', disk_io.read_bytes)
res['disk write'] = self.rate_reader.next('disk write', disk_io.write_bytes)
return res
10 changes: 7 additions & 3 deletions cubestat/metrics/gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from cubestat.metrics.base_metric import base_metric
from cubestat.metrics.registry import cubestat_metric


class GPUMode(DisplayMode):
collapsed = 'collapsed'
load_only = 'load_only'
load_and_vram = 'load_and_vram'


class gpu_metric(base_metric):
def pre(self, title):
if self.n_gpus > 0 and self.mode == GPUMode.collapsed and "Total GPU" not in title:
Expand All @@ -19,7 +21,7 @@ def pre(self, title):
if self.n_gpus > 1 and "Total GPU" not in title:
return True, ' '
return True, ''

def format(self, title, values, idxs):
return 100.0, [f'{values[i]:3.0f}%' for i in idxs]

Expand All @@ -38,11 +40,12 @@ def hotkey(self):
def configure_argparse(cls, parser):
parser.add_argument('--gpu', type=GPUMode, default=GPUMode.load_only, choices=list(GPUMode), help='GPU mode - hidden, showing all GPUs load, or showing load and vram usage. Can be toggled by pressing g.')


@cubestat_metric('linux')
class nvidia_gpu_metric(gpu_metric):
def __init__(self) -> None:
self.has_nvidia = False
self.n_gpus = 0
self.n_gpus = 0
try:
subprocess.check_output('nvidia-smi')
nvspec = find_spec('pynvml')
Expand Down Expand Up @@ -71,7 +74,8 @@ def read(self, _context):
combined[k] = v
return combined
return res



@cubestat_metric('darwin')
class macos_gpu_metric(gpu_metric):
def read(self, context):
Expand Down
22 changes: 13 additions & 9 deletions cubestat/metrics/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from cubestat.common import label_bytes
from cubestat.common import DisplayMode


class RAMMode(DisplayMode):
percent = 'percent'
all = 'all'
all = 'all'


class ram_metric(base_metric):
def configure(self, _conf):
Expand All @@ -16,14 +18,14 @@ def configure(self, _conf):

def hotkey(self):
return 'm'

def pre(self, title):
if title == 'RAM used %':
return True, ''
if self.mode == RAMMode.all:
return True, ' '
return False, ''

def format(self, title, values, idxs):
if title == 'RAM used %':
return 100.0, [f'{values[i]:3.0f}%' for i in idxs]
Expand All @@ -33,24 +35,26 @@ def format(self, title, values, idxs):
def key(cls):
return 'ram'


@cubestat_metric('darwin')
class ram_metric_macos(ram_metric):
def read(self, _context):
vm = psutil.virtual_memory()
return {
'RAM used %' : vm.percent,
'RAM used' : vm.used,
'RAM wired' : vm.wired,
'RAM used %': vm.percent,
'RAM used': vm.used,
'RAM wired': vm.wired,
}


@cubestat_metric('linux')
class ram_metric_linux(ram_metric):
def __init__(self):
# how to get metric from meminfo data
self.rows = {
'RAM used %' : lambda mi: 100.0 * (mi['MemTotal'] - mi['MemAvailable']) / mi['MemTotal'],
'RAM used' : lambda mi: mi['MemTotal'] - mi['MemAvailable'],
'RAM mapped' : lambda mi: mi['Mapped'],
'RAM used %': lambda mi: 100.0 * (mi['MemTotal'] - mi['MemAvailable']) / mi['MemTotal'],
'RAM used': lambda mi: mi['MemTotal'] - mi['MemAvailable'],
'RAM mapped': lambda mi: mi['Mapped'],
}

def read(self, _context):
Expand Down
5 changes: 4 additions & 1 deletion cubestat/metrics/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from cubestat.metrics.base_metric import base_metric
from cubestat.metrics.registry import cubestat_metric


class network_metric(base_metric):
def pre(self, title):
if self.mode == SimpleMode.hide:
return False, ''
return True, ''

def format(self, title, values, idxs):
return label_bytes_per_sec(values, idxs)

Expand All @@ -29,6 +30,7 @@ def configure(self, conf):
def configure_argparse(cls, parser):
parser.add_argument('--network', type=SimpleMode, default=SimpleMode.show, choices=list(SimpleMode), help="Show network io. Can be toggled by pressing n.")


@cubestat_metric('darwin')
class macos_network_metric(network_metric):
def read(self, context):
Expand All @@ -37,6 +39,7 @@ def read(self, context):
res['network tx'] = context['network']['obyte_rate']
return res


@cubestat_metric('linux')
class linux_network_metric(network_metric):
def read(self, _context):
Expand Down
1 change: 1 addition & 0 deletions cubestat/metrics/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def get_metrics(args):
for key, cls in _metrics
}


# to run the annotations and register metrics
from cubestat.metrics import cpu, gpu, memory, accel, swap, network, disk, power
2 changes: 2 additions & 0 deletions cubestat/platforms/factory.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
import sys

from cubestat.platforms.linux import LinuxPlatform
from cubestat.platforms.macos import MacOSPlatform


def get_platform(refresh_ms):
if sys.platform == "darwin":
return MacOSPlatform(refresh_ms)
Expand Down
3 changes: 2 additions & 1 deletion cubestat/platforms/linux.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time


class LinuxPlatform:
def __init__(self, interval_ms):
self.interval_ms = interval_ms
self.platform = 'linux'
self.platform = 'linux'

def loop(self, do_read_cb):
# TODO: should this be monotonic?
Expand Down
4 changes: 2 additions & 2 deletions cubestat/platforms/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class MacOSPlatform:
def __init__(self, interval_ms) -> None:
cmd = ['sudo', 'powermetrics', '-f', 'plist', '-i', str(interval_ms), '-s', 'cpu_power,gpu_power,ane_power,network,disk']
self.powermetrics = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# getting first line here to allow user to enter sudo credentials before
# getting first line here to allow user to enter sudo credentials before
# curses initialization.
self.firstline = self.powermetrics.stdout.readline()
self.platform = 'macos'

def loop(self, do_read_cb):
buf = bytearray()
buf.extend(self.firstline)
Expand Down
2 changes: 2 additions & 0 deletions cubestat/tests/test_prepare_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from unittest.mock import patch
import unittest


class TestPrepareCells(unittest.TestCase):
@patch('curses.init_pair')
def test_prepare_cells(self, mock_init_pair):
cells = prepare_cells()
self.assertEqual(len(cells['green']), 27)
self.assertEqual(mock_init_pair.call_count, 18)


if __name__ == '__main__':
unittest.main()
6 changes: 4 additions & 2 deletions scripts/apple_loadgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
n = 8
layers = 30


class Convs(nn.Module):
def __init__(self, channels, layers):
super(Convs, self).__init__()
Expand All @@ -19,7 +20,8 @@ def __init__(self, channels, layers):

def forward(self, x):
return self.blocks(x)



class TestModel(nn.Module):
def __init__(self):
super(TestModel, self).__init__()
Expand All @@ -30,6 +32,7 @@ def __init__(self):
nn.Flatten(),
nn.LogSoftmax(dim=1)
)

def forward(self, x):
return self.action(x)

Expand Down Expand Up @@ -76,4 +79,3 @@ def to_coreml(torch_model, batch_size, compute_units):
ms_per_sample = 1000.0 * duration / total_ranked

print(f'{batch_size},{duration:.3f},{total_ranked},{ms_per_sample:.3f}')

2 changes: 2 additions & 0 deletions scripts/cuda_loadgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
print("This script requires at least two GPUs")
exit()


# Set up a simple neural network model
class SimpleModel(nn.Module):
def __init__(self):
Expand All @@ -17,6 +18,7 @@ def __init__(self):
def forward(self, x):
return self.fc(x)


# Create model and wrap it with DataParallel to utilize multiple GPUs
model = SimpleModel().cuda()
model = DataParallel(model)
Expand Down

0 comments on commit 0ad963c

Please sign in to comment.