Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using dataclasses #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 52 additions & 28 deletions ioztat
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@

import argparse
import copy
import dataclasses
commonism marked this conversation as resolved.
Show resolved Hide resolved
import math
import os
import re
import shutil
import signal
import sys
import time
import typing
commonism marked this conversation as resolved.
Show resolved Hide resolved

PROGRAM_VERSION = '2.0.2-dev'

Expand Down Expand Up @@ -93,6 +95,15 @@ if sys.platform.startswith("linux"):
@staticmethod
def parse_dataset(file):
"""Parse a single ZFS objset file"""
# header format is defined in
#
# https://github.com/openzfs/zfs/blob/9681de4657686d0ed19ca18d578513e74395f00f/module/os/linux/spl/spl-kstat.c#L53
#
# seq_printf(f, "%d %d 0x%02x %d %d %lld %lld\n",
# ksp->ks_kid, ksp->ks_type, ksp->ks_flags,
# ksp->ks_ndata, (int)ksp->ks_data_size,
# ksp->ks_crtime, ksp->ks_snaptime);
#
# Shortened example of these files:
#############################################
# 31 1 0x01 7 2160 6165792836 1634992995579
Expand Down Expand Up @@ -196,18 +207,23 @@ else:
class Dataset:
"""
ZFS dataset statistics over a timespan in seconds

The Dataset fields are "documented" here:
https://github.com/openzfs/zfs/blob/9681de4657686d0ed19ca18d578513e74395f00f/module/zfs/dataset_kstats.c#L32

"""
name = ''
reads = 0
nread = 0
writes = 0
nwritten = 0
nunlinks = 0
nunlinked = 0
timespan = 0

def __init__(self, name=''):
__slots__ = ("name", "reads", "nread", "writes", "nwritten", "nunlinks", "nunlinked", "timespan")

def __init__(self, name='', reads=0, nread=0, writes=0, nwritten=0, nunlinks=0, nunlinked=0, timespan=0):
self.name = name
self.reads = reads
self.nread = nread
self.writes = writes
self.nwritten = nwritten
self.nunlinks = nunlinks
self.nunlinked = nunlinked
self.timespan = timespan


@classmethod
def from_dict(cls, data):
Expand All @@ -221,6 +237,13 @@ class Dataset:
d.timespan = int(data['timestamp']) / 1e9
return d



# @property
commonism marked this conversation as resolved.
Show resolved Hide resolved
# def name(self):
# return self.name


@property
def rareq_sz(self):
return self.nread / self.reads if self.reads else 0
Expand All @@ -246,7 +269,7 @@ class Dataset:
Return a copy of Dataset with values normalized to per-second rates
"""
if self.timespan:
d = copy.copy(self)
d = Dataset(self.name)
commonism marked this conversation as resolved.
Show resolved Hide resolved
d.reads /= self.timespan
d.nread /= self.timespan
d.writes /= self.timespan
Expand All @@ -261,31 +284,32 @@ class Dataset:
"""
Return a new Dataset with this one's values subtracted from other
"""
d = copy.copy(self)
d.reads -= other.reads
d.nread -= other.nread
d.writes -= other.writes
d.nwritten -= other.nwritten
d.nunlinks -= other.nunlinks
d.nunlinked -= other.nunlinked
d.timespan -= other.timespan
d = Dataset(self.name)
d.reads = self.reads - other.reads
d.nread = self.nread - other.nread
d.writes = self.writes - other.writes
d.nwritten = self.nwritten - other.nwritten
d.nunlinks = self.nunlinks - other.nunlinks
d.nunlinked = self.nunlinked - other.nunlinked
d.timespan = self.timespan - other.timespan
return d


def __add__(self, other):
"""
Return a new Dataset with this one's values added to other

Note timespan is set to the maximum of the two values instead of being
added.
"""
d = copy.copy(self)
d.reads += other.reads
d.nread += other.nread
d.writes += other.writes
d.nwritten += other.nwritten
d.nunlinks += other.nunlinks
d.nunlinked += other.nunlinked
d.timespan = max(d.timespan, other.timespan)
d = Dataset(self.name)
d.reads = self.reads + other.reads
d.nread = self.nread + other.nread
d.writes = self.writes + other.writes
d.nwritten = self.nwritten + other.nwritten
d.nunlinks = self.nunlinks + other.nunlinks
d.nunlinked = self.nunlinked + other.nunlinked
d.timespan = max(self.timespan, other.timespan)
return d


Expand Down Expand Up @@ -820,7 +844,7 @@ class DatasetDiffIter:
if path in sums:
sums[path] += diff
else:
sums[path] = Dataset(path) + diff
sums[path] = Dataset(dataset_name=path) + diff
commonism marked this conversation as resolved.
Show resolved Hide resolved

return sums.values()

Expand Down