From 56ba289b23d8759c7bde4e1d451ae3451388ef3d Mon Sep 17 00:00:00 2001 From: Jan Vcelak Date: Sun, 29 Oct 2045 16:15:54 +0100 Subject: [PATCH] This patch adds detection of netcard supported speeds. --- Makefile | 3 +- monitorplugins/net.py | 18 +++- tuned_nettool.py | 206 ++++++++++++++++++++++++++++++++++++++++++ tuningplugins/net.py | 7 +- 4 files changed, 225 insertions(+), 9 deletions(-) create mode 100644 tuned_nettool.py diff --git a/Makefile b/Makefile index 2472f42..73049c6 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ MANDIR = /usr/share/man/ GITTAG = v$(VERSION) DIRS = doc contrib tuningplugins monitorplugins ktune -FILES = tuned tuned.spec Makefile tuned.py tuned.initscript tuned.conf tuned-adm tuned_adm.py tuned-adm.pam tuned-adm.consolehelper +FILES = tuned tuned.spec Makefile tuned.py tuned.initscript tuned.conf tuned-adm tuned_adm.py tuned-adm.pam tuned-adm.consolehelper tuned_nettool.py FILES_doc = doc/DESIGN.txt doc/README.utils doc/TIPS.txt doc/tuned.8 doc/tuned.conf.5 doc/tuned-adm.1 doc/README.scomes FILES_contrib = contrib/diskdevstat contrib/netdevstat contrib/scomes contrib/varnetload FILES_tuningplugins = tuningplugins/cpu.py tuningplugins/disk.py tuningplugins/net.py tuningplugins/__init__.py @@ -71,6 +71,7 @@ install: mkdir -p $(DESTDIR)/usr/share/$(NAME)/monitorplugins install -m 0644 tuned.py $(DESTDIR)/usr/share/$(NAME)/ install -m 0644 tuned_adm.py $(DESTDIR)/usr/share/$(NAME)/ + install -m 0644 tuned_nettool.py $(DESTDIR)/usr/share/$(NAME)/ for file in $(FILES_tuningplugins); do \ install -m 0644 $$file $(DESTDIR)/usr/share/$(NAME)/tuningplugins; \ done diff --git a/monitorplugins/net.py b/monitorplugins/net.py index 9186b4e..3c68e3d 100644 --- a/monitorplugins/net.py +++ b/monitorplugins/net.py @@ -17,6 +17,7 @@ # import os +from tuned_nettool import ethcard class NetMonitor: def __init__(self): @@ -32,10 +33,17 @@ class NetMonitor: continue self.devices[d] = {} self.devices[d]["new"] = ['0', '0', '0', '0'] - # Assume 1gbit interfaces for now. FIXME: Need clean way to figure out max interface speed - self.devices[d]["max"] = [70*1024*1024, 1, 70*1024*1024, 1] + max_speed = self.__calcspeed__( ethcard(d).get_max_speed() ); + self.devices[d]["max"] = [max_speed, 1, max_speed, 1] self.__updateStat__(d) - self.devices[d]["max"] = [70*1024*1024, 1, 70*1024*1024, 1] + self.devices[d]["max"] = [max_speed, 1, max_speed, 1] + + def __calcspeed__(self, speed): + # 0.6 is just a magical constant (empirical value): Typical workload on netcard won't exceed + # that and if it does, then the code is smart enough to adapt it. + # 1024 * 1024 as for MB -> B + # speed / 8 Mb -> MB + return (int) (0.6 * 1024 * 1024 * speed / 8) def __calcdiff__(self, dev): l = [] @@ -73,9 +81,9 @@ class NetMonitor: self.verbose = (self.config.get("NetMonitor", "verbose") == "True") except: pass - # Assume 1gbit interfaces for now. FIXME: Need clean way to figure out max interface speed for d in self.devices.keys(): - self.devices[d]["max"] = [70*1024*1024*interval, 1, 70*1024*1024*interval, 1] + max_data = self.__calcspeed__(ethcard(d).get_max_speed()) * interval; + self.devices[d]["max"] = [max_data, 1, max_data, 1] if self.verbose: print self.devices diff --git a/tuned_nettool.py b/tuned_nettool.py new file mode 100644 index 0000000..0aa4be4 --- /dev/null +++ b/tuned_nettool.py @@ -0,0 +1,206 @@ +# Copyright (C) 2008, 2009 Red Hat, Inc. +# Authors: Jan Vcelak +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +from subprocess import * +import re + +class Tuned_nettool: + + __advertise_values = { # [ half, full ] + 10 : [ 0x001, 0x002 ], + 100 : [ 0x004, 0x008 ], + 1000 : [ 0x010, 0x020 ], + 2500 : [ 0, 0x8000 ], + 10000 : [ 0, 0x1000 ], + "auto" : 0x03F + } + + def __init__(self, interface): + self.__interface = interface; + self.update() + +# print "speed:", self.speed +# print "full dupl:", self.full_duplex +# print "autoneg:", self.autoneg +# print "link:", self.link +# print "sup modes:", self.supported_modes +# print "sup autoneg:", self.supported_autoneg +# print "adv modes:", self.advertised_modes +# print "adv autoneg:", self.advertised_autoneg + +# def __del__(self): +# if self.supported_autoneg: +# self.__set_advertise(self.__advertise_values["auto"]) + + def __clean_status(self): + self.speed = 0 + self.full_duplex = False + self.autoneg = False + self.link = False + + self.supported_modes = [] + self.supported_autoneg = False + + self.advertised_modes = [] + self.advertised_autoneg = False + + def __calculate_mode(self, modes): + mode = 0; + for m in modes: + mode += self.__advertise_values[m[0]][ 1 if m[1] else 0 ] + + return mode + + def __set_autonegotiation(self, enable): + if self.autoneg == enable: + return True + + if not self.supported_autoneg: + return False + + return 0 == call(["ethtool", "-s", self.__interface, "autoneg", "on" if enable else "off"]) + + def __set_advertise(self, value): + if not self.__set_autonegotiation(True): + return False + + return 0 == call(["ethtool", "-s", self.__interface, "advertise", "0x%03x" % value]) + + def get_max_speed(self): + max = 0 + for mode in self.supported_modes: + if mode[0] > max: max = mode[0] + + if max > 0: + return max + else: + return 1000 + + def set_max_speed(self): + if not self.supported_autoneg: + return False + + #if self.__set_advertise(self.__calculateMode(self.supported_modes)): + if self.__set_advertise(self.__advertise_values["auto"]): + self.update() + return True + else: + return False + + def set_speed(self, speed): + if not self.supported_autoneg: + return False + + mode = 0 + for am in self.__advertise_values: + if am == "auto": continue + if am <= speed: + mode += self.__advertise_values[am][0]; + mode += self.__advertise_values[am][1]; + + effective_mode = mode & self.__calculate_mode(self.supported_modes) + + if self.__set_advertise(effective_mode): + self.update() + return True + else: + return False + + def update(self): + + # run ethtool and preprocess output + + p_ethtool = Popen(["ethtool", self.__interface], stdout=PIPE, stderr=PIPE) + p_filter = Popen(["sed", "s/^\s*//;s/:\s*/:\\n/g"], stdin=p_ethtool.stdout, stdout=PIPE) + + output = p_filter.communicate()[0] + errors = p_ethtool.communicate()[1] + + if errors != "": + raise Exception("Some errors were reported by 'ethtool'.", errors) + + # parses output - kind of FSM + + self.__clean_status() + + re_speed = re.compile(r"(\d+)") + re_mode = re.compile(r"(\d+)baseT/(Half|Full)") + + state = "wait" + + for line in output.split("\n"): + + if line.endswith(":"): + section = line[:-1] + if section == "Speed": state = "speed" + elif section == "Duplex": state = "duplex" + elif section == "Auto-negotiation": state = "autoneg" + elif section == "Link detected": state = "link" + elif section == "Supported link modes": state = "supported_modes" + elif section == "Supports auto-negotiation": state = "supported_autoneg" + elif section == "Advertised link modes": state = "advertised_modes" + elif section == "Advertised auto-negotiation": state = "advertised_autoneg" + else: state = "wait" + del section + + elif state == "speed": + self.speed = re_speed.match(line).group(1) + state = "wait" + + elif state == "duplex": + self.full_duplex = line == "Full" + state = "wait" + + elif state == "autoneg": + self.autoneg = line == "yes" + state = "wait" + + elif state == "link": + self.link = line == "yes" + state = "wait" + + elif state == "supported_modes": + for m in line.split(): + (s, d) = re_mode.match(m).group(1,2) + self.supported_modes.append( (int(s), d == "Full") ) + del m,s,d + + elif state == "supported_autoneg": + self.supported_autoneg = line == "Yes" + state = "wait" + + elif state == "advertised_modes": + if line != "Not reported": + for m in line.split(): + (s, d) = re_mode.match(m).group(1,2) + self.advertised_modes.append( (int(s), d == "Full") ) + del m,s,d + + elif state == "advertised_autoneg": + self.advertised_autoneg = line == "Yes" + state = "wait" + +def ethcard(interface): + if not interface in ethcard.list: + #print "ethcard -> Tuned_nettool(%s)" % interface + ethcard.list[interface] = Tuned_nettool(interface) + + return ethcard.list[interface] + +ethcard.list = {} + diff --git a/tuningplugins/net.py b/tuningplugins/net.py index 8efe1b1..d53171b 100644 --- a/tuningplugins/net.py +++ b/tuningplugins/net.py @@ -17,6 +17,7 @@ # import os, copy +from tuned_nettool import ethcard class NetTuning: def __init__(self): @@ -48,7 +49,7 @@ class NetTuning: def cleanup(self): for dev in self.devidle.keys(): if self.enabled and self.devidle[dev]["LEVEL"] > 0: - os.system("ethtool -s "+dev+" advertise 0x03F") + ethcard(dev).set_max_speed() def setTuning(self, load): if not self.enabled: @@ -59,10 +60,10 @@ class NetTuning: self.__updateIdle__(dev, devload) if self.devidle[dev]["LEVEL"] == 0 and self.devidle[dev]["READ"] >= 6 and self.devidle[dev]["WRITE"] >= 6: self.devidle[dev]["LEVEL"] = 1 - os.system("ethtool -s "+dev+" advertise 0x00F") + ethcard(dev).set_speed(100) # FIXME: what about making this more dynamic? if self.devidle[dev]["LEVEL"] > 0 and (self.devidle[dev]["READ"] == 0 or self.devidle[dev]["WRITE"] == 0): self.devidle[dev]["LEVEL"] = 0 - os.system("ethtool -s "+dev+" advertise 0x03F") + ethcard(dev).set_max_speed() if self.verbose: print(load, self.devidle) _plugin = NetTuning()