def run(self, once=False): if not self.rules: usage(sys.argv[0], "Error: no rules specified") for idx, (irq_ids, cpumask) in enumerate(self.rules): if (cpumask is None) or (irq_ids is None): usage(sys.argv[0], "Error: cpumask: %s, irq_ids: %s" % (cpumask, irq_ids)) msg("Rule %d:" % (idx, )) msg("Interrupt numbers: %s" % (str([irq_id for irq_id in irq_ids]), )) msg("Hex interrupt cores mask: %x" % (cpumask, )) msg("IRQ switch interval: %f" % (self.interval, )) sprinkler = IRQSprinkler(self.rules) # if we are on the "once" mode of operation then run once and exit if once: sprinkler.move_next() return while True: sleep(self.interval) # msg("***** step *****") sprinkler.move_next()
def daemonize(self): """Deamonize class. UNIX double fork mechanism.""" try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError as err: msg('fork #1 failed: {0}\n'.format(err)) sys.exit(1) # decouple from parent environment os.chdir('/') os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent sys.exit(0) except OSError as err: msg('fork #2 failed: {0}\n'.format(err)) sys.exit(1) # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() si = open(os.devnull, 'r') so = open(os.devnull, 'a+') se = open(os.devnull, 'a+') os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) # write pidfile atexit.register(self.del_pid) pid = str(os.getpid()) with open(self.pidfile, 'w+') as f: f.write(pid + '\n')
def start(self): """Start the daemon.""" # Check for a pidfile to see if the daemon already runs try: with open(self.pidfile, 'r') as pf: pid = int(pf.read().strip()) except IOError: pid = None if pid: message = "pidfile {0} already exist. Daemon already running?\n" msg(message.format(self.pidfile)) sys.exit(1) # Start the daemon self.daemonize() self.run()
def main(argv): euid = os.geteuid() if euid != 0: print("Script did not started as root, running sudo..") args = ['sudo', sys.executable] + argv + [os.environ] # the next row replaces the currently-running process with the sudo os.execlpe('sudo', *args) opts = None try: opts, args = getopt.getopt(argv[1:], "c:hp", ["config=", "process", "help"]) except getopt.GetoptError: usage(argv[0], "Illegal Argument!") config_filename = "/tmp/io_manager_configuration.json" daemon = "" for opt, arg in opts: if opt in ("-h", "--help"): usage(argv[0], "Help") elif opt in ("-c", "--config"): config_filename = arg elif opt in ("-p", "--process"): daemon = "-p" msg("configuration file: %s" % config_filename) if not os.path.exists(config_filename): usage(argv[0], "configuration file %s not found " % (config_filename, )) with open(config_filename, "r") as f: conf = json.load(f) scripts_path = os.path.dirname(os.path.abspath(__file__)) interval = conf["interval"] out = __live_execute([ os.path.join(scripts_path, 'reconfigure.py'), config_filename, "-p", "interval:%s" % interval ]) out = __live_execute([ os.path.join(scripts_path, 'io_manager.py'), "-s", config_filename, daemon ])
def stop(self): """Stop the daemon.""" # Get the pid from the pidfile try: with open(self.pidfile, 'r') as pf: pid = int(pf.read().strip()) except IOError: pid = None if not pid: message = "pidfile {0} does not exist. Daemon not running?\n" msg(message.format(self.pidfile)) return # not an error in a restart # Try killing the daemon process try: i = 10 while 1: i -= 1 if i == 0: msg("cannot kill process %d" % (pid, )) break # sys.exit(1) os.kill(pid, signal.SIGTERM) time.sleep(0.1) except OSError as err: e = str(err.args) if e.find("No such process") > 0: if os.path.exists(self.pidfile): os.remove(self.pidfile) else: msg(str(err.args)) sys.exit(1)
def main(argv): parser = argparse.ArgumentParser() parser.add_argument( "num_of_cores", help= "is the number of cores on your system; use less if you want to confine the virtual machines and iocores to use a subset of the available cores", type=int) parser.add_argument( "if_name", help= "is the interface name (from the host perspective) in which all traffic of the virtual machines goes through. Currently, supporting up to 2 NICs" ) parser.add_argument("--config", help="configuration file path") parser.add_argument("--min", help="minimum number of iocores allowed") parser.add_argument("--max", help="maximum number of iocores allowed") args = parser.parse_args() configuration_file = "/tmp/io_manager_configuration.json" if args.config: configuration_file = args.config num_of_cores = args.num_of_cores if num_of_cores <= 4: err("num_of_cores must be greater than 4") if_names = args.if_name.split(',') if len(if_names) > 2: err("too many network interfaces") io_manager = IOManager(num_of_cores, if_names) conf = io_manager.get_config_file(args.min, args.max) with open(configuration_file, "w") as f: json.dump(conf, f, indent=2) msg("Configuration file written to %s" % configuration_file)
def main(argv): if len(argv) < 2: usage( argv[0], "Wrong number of arguments, expected at least 1 got %d" % (len(argv) - 1, )) opts = None try: opts, args = getopt.getopt(argv[1:], "s:kph", ["start=", "kill", "process", "help"]) except getopt.GetoptError: usage(argv[0], "Illegal Argument!") configuration_filename = None no_daemon = False for opt, arg in opts: if opt in ("-h", "--help"): usage(argv[0], "Help") elif opt in ("-s", "--start"): msg("configuration file: %s" % (arg, )) configuration_filename = arg elif opt in ("-k", "--kill"): msg("kills an mover that runs in a daemon.") Daemon(MOVER_PID).stop() sys.exit() elif opt in ("-p", "--process"): msg("run io manager with all output to stdout and stderr.") no_daemon = True if not os.path.exists(configuration_filename): usage(argv[0], "Configuration file doesn't exists!") with open(configuration_filename) as f: config = json.load(f) # a way to run the manager not as daemon regardless of configuration file if no_daemon: config["daemon"] = "no" # start the log file if exists timestamp = time.strftime("%Y-%m-%d_%H-%M-%S") log_format = "[%(filename)s:%(lineno)s] %(message)s" if "log" in config and config["log"] and not no_daemon: log_file = os.path.expanduser(config["log"]) formatted_log_file = log_file if "tag" in config: formatted_log_file += ".%s" % (config["tag"], ) formatted_log_file += ".%s.txt" % (timestamp, ) logging.basicConfig(filename=formatted_log_file, format=log_format, level=logging.INFO) if os.path.exists(log_file): os.unlink(log_file) os.symlink(formatted_log_file, log_file) else: logging.basicConfig(stream=sys.stdout, format=log_format, level=logging.INFO) sys.stdout = LoggerWriter(logging.INFO) sys.stderr = LoggerWriter(logging.ERROR) logging.info("****** start of a new run: %s ******" % (timestamp, )) # set the interval in which the IO manager works interval = float(config["interval"]) if "interval" in config \ else MOVER_INTERVAL logging.info(interval) # initialize vhost Vhost.initialize() Vhost.INSTANCE.update(light_update=False, update_epoch=True, rescan_files=False) # get backing devices info backing_devices_policy = \ BackingDevicesPreConfiguredBalancePolicy( config["backing_devices_balance_policy"]) bdm = BackingDeviceManager(config["backing_devices"], backing_devices_policy) # start the vm manager vm_policy = LastAddedPolicy.create_vm_policy(config["vms"]) vm_balance_policy = \ VmsPreConfiguredBalancePolicy(config["vms_balance_policy"], vm_policy.cpus) # vm_balance_policy = VmsRunEverywhereBalancePolicy() vm_core_addition_policy = \ VMCoreAdditionPolicy(config["vms"], config["vm_core_addition_policy"]) vm_manager = VMManager(config["vms"], bdm.backing_devices, vm_policy, vm_core_addition_policy, vm_balance_policy) # get devices devices = [dev for vm in vm_manager.vms for dev in vm.devices] # set up manager policies vq_classifier = VirtualQueueClassifier(config["virtual_queue_classifier"]) # poll_policy = PollPolicy(config["poll_policy"]) poll_policy = NullPollPolicy() io_core_policy = LastAddedPolicy.create_io_cores_policy(config["workers"]) io_core_balance_policy = \ IOCoresPreConfiguredBalancePolicy(config["io_cores_balance_policy"], devices) # io_core_balance_policy = BalanceByDeviceNumberPolicy() throughput_policy = IOWorkerThroughputPolicy(config["throughput_policy"]) latency_policy = LatencyPolicy(config["latency_policy"]) regret_policy = ThroughputRegretPolicy(config["throughput_regret_policy"], bdm) # log the resolution of the timer Timer.check_resolution() # setup the io core controller io_workers_manager = IOWorkersManager(devices, vm_manager, bdm, config["workers"], vq_classifier, poll_policy, throughput_policy, latency_policy, io_core_policy, io_core_balance_policy, regret_policy) daemon = MoverDaemon(io_workers_manager, vm_manager, bdm, interval) if "daemon" in config: if 'start' == config["daemon"]: daemon.start() elif 'stop' == config["daemon"]: daemon.stop() elif 'restart' == config["daemon"]: daemon.restart() elif 'no' == config["daemon"]: logging.info("running without daemon") daemon.run() else: usage(argv[0], "Unknown daemon command!") else: logging.info("running without daemon") daemon.run()
def parse_rule(_irq_prefix, _cpu_list): msg("irq_prefix: %s" % (_irq_prefix, )) irq_ids = [irq_id for irq_id in get_irq_numbers(_irq_prefix)] msg("cpu_list: %s" % (_cpu_list, )) cpu_mask = parse_cpu_mask(_cpu_list) return irq_ids, cpu_mask
return irq_ids, cpu_mask if __name__ == '__main__': daemon_command = None interval = 0.1 # msg("Number of arguments: %d arguments." % (len(sys.argv),)) # msg("Argument List: %s" % (str(sys.argv), )) opts = None try: opts, args = getopt.getopt( sys.argv[1:], "r:d:c:t:h", ["rule=", "daemon=", "config=", "interval="]) except getopt.GetoptError: msg("Error") usage(sys.argv[0], "Illegal arguments!") rules = [] for opt, arg in opts: if opt == '-h': usage(sys.argv[0], "Help") elif opt in ("-r", "--rule"): irq_prefix, cpu_list = arg.split() rules.append(parse_rule(irq_prefix, cpu_list)) elif opt in ("-d", "--daemon"): msg("daemon: %s" % (arg, )) daemon_command = arg elif opt in ("-t", "--interval"): msg("interval: %s" % (arg, ))
config = None with open(config_filename, "r") as f: config = json.load(f) opts = None try: opts, args = getopt.getopt(sys.argv[2:], "p:h", ["param=", "help"]) except getopt.GetoptError: usage(sys.argv[0], "Illegal Argument!") configuration_filename = None for opt, arg in opts: if opt in ("-h", "--help"): usage(sys.argv[0], "Help") elif opt in ("-p", "--param"): msg("parameter: %s" % (arg, )) key, value = arg.split(":") keys = [k.strip() for k in key.split(".")] entry = config for key in keys[:-1]: entry = entry[key] entry[keys[-1]] = value # initialize vhost Vhost.initialize() Vhost.INSTANCE.update(light_update=False, update_epoch=False, rescan_files=True) vms_conf = config["vms"]