Skip to content

Commit 20e4513

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 f217a2b commit 20e4513

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
@@ -86,16 +86,24 @@ if sys.platform.startswith('freebsd'):
8686

8787
VDEV_CACHE_SIZE = 'vdev.cache_size'
8888

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

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

100108
def get_tunable_params():
101109
return get_params('vfs.zfs')
@@ -112,25 +120,8 @@ if sys.platform.startswith('freebsd'):
112120
return '{} version {}'.format(name, version)
113121

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

135126

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

cmd/arcstat/arcstat.in

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

129129

130130
if sys.platform.startswith('freebsd'):
131-
# Requires py27-sysctl on FreeBSD
131+
# Requires py-sysctl on FreeBSD
132132
import sysctl
133133

134134
def kstat_update():
135135
global kstat
136136

137-
k = sysctl.filter('kstat.zfs.misc.arcstats')
137+
k = [ctl for ctl in sysctl.filter('kstat.zfs.misc.arcstats')
138+
if ctl.type != sysctl.CTLTYPE_NODE]
138139

139140
if not k:
140141
sys.exit(1)

0 commit comments

Comments
 (0)