Commit 04c27ca1 authored by Tomáš Peterka's avatar Tomáš Peterka Committed by Rafael Monnerat

[recipe.wrapper] Add option to reserve CPU core

Add functionality to wrapper to ask for an exclusive CPU core.

/reviewed-on !184
parent ad71146a
...@@ -131,7 +131,7 @@ class GenericBaseRecipe(object): ...@@ -131,7 +131,7 @@ class GenericBaseRecipe(object):
def createWrapper(self, name, command, parameters, comments=[], def createWrapper(self, name, command, parameters, comments=[],
parameters_extra=False, environment=None, parameters_extra=False, environment=None,
pidfile=None pidfile=None, reserve_cpu=False
): ):
""" """
Creates a shell script for process replacement. Creates a shell script for process replacement.
...@@ -139,6 +139,8 @@ class GenericBaseRecipe(object): ...@@ -139,6 +139,8 @@ class GenericBaseRecipe(object):
Takes care of #! line limitation when the wrapped command is a script. Takes care of #! line limitation when the wrapped command is a script.
if pidfile parameter is specified, then it will make the wrapper a singleton, if pidfile parameter is specified, then it will make the wrapper a singleton,
accepting to run only if no other instance is running. accepting to run only if no other instance is running.
:param reserve_cpu: bool, try to reserve one core for the `command`
""" """
lines = [ '#!/bin/sh' ] lines = [ '#!/bin/sh' ]
...@@ -163,6 +165,14 @@ class GenericBaseRecipe(object): ...@@ -163,6 +165,14 @@ class GenericBaseRecipe(object):
fi fi
echo $$ > $pidfile""" % shlex.quote(pidfile))) echo $$ > $pidfile""" % shlex.quote(pidfile)))
if reserve_cpu:
# if the CGROUPS cpuset is available (and prepared by slap format)
# request an exclusive CPU core for this process
lines.append(dedent("""
# put own PID into waiting list for exclusive CPU-core access
echo $$ >> ~/.slapos-cpu-exclusive
"""))
lines.append(dedent(''' lines.append(dedent('''
# If the wrapped command uses a shebang, execute the referenced # If the wrapped command uses a shebang, execute the referenced
# executable passing the script path as first argument. # executable passing the script path as first argument.
......
...@@ -31,6 +31,16 @@ import os ...@@ -31,6 +31,16 @@ import os
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
class Recipe(GenericBaseRecipe): class Recipe(GenericBaseRecipe):
"""Recipe to create a script from given command and options.
:param str command-line: shell command which launches the intended process
:param str wrapper-path: absolute path to file's destination
:param lines wait-for-files: list of files to wait for
:param str pidfile: path to pidfile ensure exclusivity for the process
:param bool parameters-extra: whether wrapper parameters are passed onto command
:param bool reserve-cpu: command will ask for an exclusive CPU core
"""
def install(self): def install(self):
command_line = shlex.split(self.options['command-line']) command_line = shlex.split(self.options['command-line'])
wrapper_path = self.options['wrapper-path'] wrapper_path = self.options['wrapper-path']
...@@ -38,6 +48,7 @@ class Recipe(GenericBaseRecipe): ...@@ -38,6 +48,7 @@ class Recipe(GenericBaseRecipe):
environment = self.options.get('environment') environment = self.options.get('environment')
parameters_extra = self.options.get('parameters-extra') parameters_extra = self.options.get('parameters-extra')
pidfile = self.options.get('pidfile') pidfile = self.options.get('pidfile')
reserve_cpu = self.options.get('reserve-cpu', False)
if not wait_files and not environment: if not wait_files and not environment:
# Create a simple wrapper as shell script # Create a simple wrapper as shell script
...@@ -47,6 +58,7 @@ class Recipe(GenericBaseRecipe): ...@@ -47,6 +58,7 @@ class Recipe(GenericBaseRecipe):
parameters=command_line[1:], parameters=command_line[1:],
parameters_extra=parameters_extra, parameters_extra=parameters_extra,
pidfile=pidfile, pidfile=pidfile,
reserve_cpu=reserve_cpu
)] )]
# More complex needs: create a Python script as wrapper # More complex needs: create a Python script as wrapper
...@@ -76,5 +88,6 @@ class Recipe(GenericBaseRecipe): ...@@ -76,5 +88,6 @@ class Recipe(GenericBaseRecipe):
parameters=[], parameters=[],
parameters_extra=parameters_extra, parameters_extra=parameters_extra,
pidfile=pidfile, pidfile=pidfile,
reserve_cpu=reserve_cpu
)] )]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment