Skip to content

Commit 513c196

Browse files
Ryan Moellerbehlendorf
authored andcommitted
FreeBSD: Update usage of py-sysctl
py-sysctl now includes the CTLTYPE_NODE type nodes in the list returned by sysctl.filter() on FreeBSD head. It also provides descriptions now. Eliminate the subprocess call to get descriptions, and filter out the nodes so we only deal with values. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ryan Moeller <[email protected]> Closes #11318
1 parent e5f732e commit 513c196

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

cmd/arc_summary/arc_summary2

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ if sys.platform.startswith('freebsd'):
5959
# Requires py27-sysctl on FreeBSD
6060
import sysctl
6161

62+
def is_value(ctl):
63+
return ctl.type != sysctl.CTLTYPE_NODE
64+
6265
def load_kstats(namespace):
6366
"""Collect information on a specific subsystem of the ARC"""
6467

6568
base = 'kstat.zfs.misc.%s.' % namespace
66-
return [(kstat.name, D(kstat.value)) for kstat in sysctl.filter(base)]
69+
fmt = lambda kstat: (kstat.name, D(kstat.value))
70+
kstats = sysctl.filter(base)
71+
return [fmt(kstat) for kstat in kstats if is_value(kstat)]
6772

6873
def load_tunables():
69-
return dict((ctl.name, ctl.value) for ctl in sysctl.filter('vfs.zfs'))
74+
ctls = sysctl.filter('vfs.zfs')
75+
return dict((ctl.name, ctl.value) for ctl in ctls if is_value(ctl))
7076

7177
elif sys.platform.startswith('linux'):
7278

cmd/arc_summary/arc_summary3

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,24 @@ if sys.platform.startswith('freebsd'):
8585

8686
VDEV_CACHE_SIZE = 'vdev.cache_size'
8787

88+
def is_value(ctl):
89+
return ctl.type != sysctl.CTLTYPE_NODE
90+
91+
def namefmt(ctl, base='vfs.zfs.'):
92+
# base is removed from the name
93+
cut = len(base)
94+
return ctl.name[cut:]
95+
8896
def load_kstats(section):
8997
base = 'kstat.zfs.misc.{section}.'.format(section=section)
90-
# base is removed from the name
91-
fmt = lambda kstat: '{name} : {value}'.format(name=kstat.name[len(base):],
98+
fmt = lambda kstat: '{name} : {value}'.format(name=namefmt(kstat, base),
9299
value=kstat.value)
93-
return [fmt(kstat) for kstat in sysctl.filter(base)]
100+
kstats = sysctl.filter(base)
101+
return [fmt(kstat) for kstat in kstats if is_value(kstat)]
94102

95103
def get_params(base):
96-
cut = 8 # = len('vfs.zfs.')
97-
return {ctl.name[cut:]: str(ctl.value) for ctl in sysctl.filter(base)}
104+
ctls = sysctl.filter(base)
105+
return {namefmt(ctl): str(ctl.value) for ctl in ctls if is_value(ctl)}
98106

99107
def get_tunable_params():
100108
return get_params('vfs.zfs')
@@ -111,25 +119,8 @@ if sys.platform.startswith('freebsd'):
111119
return '{} version {}'.format(name, version)
112120

113121
def get_descriptions(_request):
114-
# py-sysctl doesn't give descriptions, so we have to shell out.
115-
command = ['sysctl', '-d', 'vfs.zfs']
116-
117-
# The recommended way to do this is with subprocess.run(). However,
118-
# some installed versions of Python are < 3.5, so we offer them
119-
# the option of doing it the old way (for now)
120-
if 'run' in dir(subprocess):
121-
info = subprocess.run(command, stdout=subprocess.PIPE,
122-
universal_newlines=True)
123-
lines = info.stdout.split('\n')
124-
else:
125-
info = subprocess.check_output(command, universal_newlines=True)
126-
lines = info.split('\n')
127-
128-
def fmt(line):
129-
name, desc = line.split(':', 1)
130-
return (name.strip(), desc.strip())
131-
132-
return dict([fmt(line) for line in lines if len(line) > 0])
122+
ctls = sysctl.filter('vfs.zfs')
123+
return {namefmt(ctl): ctl.description for ctl in ctls if is_value(ctl)}
133124

134125

135126
elif sys.platform.startswith('linux'):

cmd/arcstat/arcstat.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ pretty_print = True
144144

145145

146146
if sys.platform.startswith('freebsd'):
147-
# Requires py27-sysctl on FreeBSD
147+
# Requires py-sysctl on FreeBSD
148148
import sysctl
149149

150150
def kstat_update():
151151
global kstat
152152

153-
k = sysctl.filter('kstat.zfs.misc.arcstats')
153+
k = [ctl for ctl in sysctl.filter('kstat.zfs.misc.arcstats')
154+
if ctl.type != sysctl.CTLTYPE_NODE]
154155

155156
if not k:
156157
sys.exit(1)

0 commit comments

Comments
 (0)