Slurm Workload Manager -

    Architecture for Node and job Kontrol (\fBSPANK\fR) as well as the \fBSPANK\fR
    configuration file: (By default: \fBplugstack.conf\fP.)
    .LP
    \fBSPANK\fR provides a very generic interface for stackable plug\-ins
    which may be used to dynamically modify the job launch code in
    Slurm. \fBSPANK\fR plugins may be built without access to Slurm source
    code. They need only be compiled against Slurm's \fBspank.h\fR header file,
    added to the \fBSPANK\fR config file \fBplugstack.conf\fR,
    and they will be loaded at runtime during the next job launch. Thus,
    the \fBSPANK\fR infrastructure provides administrators and other developers
    a low cost, low effort ability to dynamically modify the runtime
    behavior of Slurm job launch.
    .LP
    \fBNote\fR: \fBSPANK\fR plugins using the Slurm APIs need to be recompiled when
    upgrading Slurm to a new major release.
    .LP

    .SH "SPANK PLUGINS"
    \fBSPANK\fR plugins are loaded in up to five separate contexts during a
    \fBSlurm\fR job. Briefly, the five contexts are:
    .TP 8
    \fBlocal\fR
    In \fBlocal\fR context, the plugin is loaded by \fBsrun\fR. (i.e. the "local"
    part of a parallel job).
    .TP
    \fBremote\fR
    In \fBremote\fR context, the plugin is loaded by \fBslurmstepd\fR. (i.e. the "remote"
    part of a parallel job).
    .TP
    \fBallocator\fR
    In \fBallocator\fR context, the plugin is loaded in one of the job allocation
    utilities \fBsbatch\fR or \fBsalloc\fR.
    .TP
    \fBslurmd\fR
    In \fBslurmd\fR context, the plugin is loaded in the
    \fBslurmd\fR daemon itself. \fBNote\fR: Plugins loaded in slurmd context
    persist for the entire time slurmd is running, so if configuration is
    changed or plugins are updated, slurmd must be restarted for the changes
    to take effect.
    .TP
    \fBjob_script\fR
    In the \fBjob_script\fR context, plugins are loaded in the context of the
    job prolog or epilog. \fBNote\fR: Plugins are loaded in \fBjob_script\fR
    context on each run on the job prolog or epilog, in a separate address
    space from plugins in \fBslurmd\fR context. This means there is no
    state shared between this context and other contexts, or even between
    one call to \fBslurm_spank_job_prolog\fR or \fBslurm_spank_job_epilog\fR
    and subsequent calls.
    .LP
    In local context, only the \fBinit\fR, \fBexit\fR, \fBinit_post_opt\fR, and
    \fBlocal_user_init\fR functions are called. In allocator context, only the
    \fBinit\fR, \fBexit\fR, and \fBinit_post_opt\fR functions are called.
    .TP
    \fBslurm_spank_job_prolog\fR
    Called at the same time as the job prolog. If this function returns a
    negative value and the \fBSPANK\fR plugin that contains it is required in the
    \fBplugstack.conf\fR, the node that this is run on will be drained.

    .TP
    \fBslurm_spank_init_post_opt\fR
    Called at the same point as \fBslurm_spank_init\fR, but after all
    user options to the plugin have been processed. The reason that the
    \fBinit\fR and \fBinit_post_opt\fR callbacks are separated is so that
    plugins can process system-wide options specified in plugstack.conf in
    the \fBinit\fR callback, then process user options, and finally take some
    action in \fBslurm_spank_init_post_opt\fR if necessary.
    In the case of a heterogeneous job, \fBslurm_spank_init\fR is invoked once
    per job component.
    .TP
    \fBslurm_spank_local_user_init\fR
    Called in local (\fBsrun\fR) context only after all
    options have been processed.
    This is called after the job ID and step IDs are available.
    This happens in \fBsrun\fR after the allocation is made, but before
    tasks are launched.
    .TP
    \fBslurm_spank_user_init\fR
    Called after privileges are temporarily dropped. (remote context only)
    .TP
    \fBslurm_spank_task_init_privileged\fR
    Called for each task just after fork, but before all elevated privileges
    are dropped. (remote context only)
    .TP
    \fBslurm_spank_task_init\fR
    Called for each task just before execve (2). If you are restricing memory
    with cgroups, memory allocated here will be in the job's cgroup. (remote
    context only)
    .TP
    \fBslurm_spank_task_post_fork\fR
    Called for each task from parent process after fork (2) is complete.
    Due to the fact that \fBslurmd\fR does not exec any tasks until all
    tasks have completed fork (2), this call is guaranteed to run before
    the user task is executed. (remote context only)
    .TP
    \fBslurm_spank_task_exit\fR
    Called for each task as its exit status is collected by Slurm.
    (remote context only)
    .TP
    \fBslurm_spank_exit\fR
    Called once just before \fBslurmstepd\fR exits in remote context.
    In local context, called before \fBsrun\fR exits.
    .TP
    \fBslurm_spank_job_epilog\fR
    Called at the same time as the job epilog. If this function returns a
    Slurm when the plugin calls functions like \fBspank_get_item\fR and
    \fBspank_getenv\fR. Configured arguments (See \fBCONFIGURATION\fR
    below) are passed in the argument vector \fBargv\fR with argument
    count \fBac\fR.
    .LP
    \fBSPANK\fR plugins can query the current list of supported slurm_spank
    symbols to determine if the current version supports a given plugin hook.
    This may be useful because the list of plugin symbols may grow in the
    future. The query is done using the \fBspank_symbol_supported\fR function,
    which has the following prototype:
    .nf

        int \fBspank_symbol_supported\fR (const char *sym);

    .fi
    .LP
    The return value is 1 if the symbol is supported, 0 if not.
    .LP
    \fBSPANK\fR plugins do not have direct access to internally defined Slurm
    data structures. Instead, information about the currently executing
    job is obtained via the \fBspank_get_item\fR function call.
    .nf

      spank_err_t \fBspank_get_item\fR (spank_t spank, spank_item_t item, ...);

    .fi
    The \fBspank_get_item\fR call must be passed the current \fBSPANK\fR
    handle as well as the item requested, which is defined by the
    passed \fBspank_item_t\fR. A variable number of pointer arguments are also
    passed, depending on which item was requested by the plugin. A
    list of the valid values for \fBitem\fR is kept in the \fBspank.h\fR header
    file. Some examples are:
    .TP 2
    \fBS_JOB_UID\fR
    User id for running job. (uid_t *) is third arg of \fBspank_get_item\fR
    .TP
    \fBS_JOB_STEPID\fR
    Job step id for running job. (uint32_t *) is third arg of \fBspank_get_item\fR.
    .TP
    \fBS_TASK_EXIT_STATUS\fR
    Exit status for exited task. Only valid from \fBslurm_spank_task_exit\fR.
    (int *) is third arg of \fBspank_get_item\fR.
    .TP
    \fBS_JOB_ARGV\fR
    Complete job command line. Third and fourth args to \fBspank_get_item\fR
    are (int *, char ***).
    .LP
    See \fBspank.h\fR for more details, and \fBEXAMPLES\fR below for an example
    of \fBspank_get_item\fR usage.
    .LP
    \fBSPANK\fR functions in the \fBlocal\fB and \fBallocator\fR environment should
    use the \fBgetenv\fR, \fBsetenv\fR, and \fBunsetenv\fR functions to view and
     spank_err_t \fBspank_setenv\fR (spank_t spank, const char *var,
    		           const char *val, int overwrite);
     spank_err_t \fBspank_unsetenv\fR (spank_t spank, const char *var);
    .fi
    .LP
    These are only necessary in remote context since modifications of
    the standard process environment using \fBsetenv\fR (3), \fBgetenv\fR (3),
    and \fBunsetenv\fR (3) may be used in local context.
    .LP
    Functions are also available from within the \fBSPANK\fR plugins to
    establish environment variables to be exported to the Slurm
    \fBPrologSlurmctld\fR, \fBProlog\fR, \fBEpilog\fR and \fBEpilogSlurmctld\fR
    programs (the so-called \fBjob control\fR environment).
    The name of environment variables established by these calls will be prepended
    with the string \fISPANK_\fR in order to avoid any security implications
    of arbitrary environment variable control. (After all, the job control
    scripts do run as root or the Slurm user.).
    .LP
    These functions are available from \fBlocal\fR context only.
    .nf

      spank_err_t \fBspank_job_control_getenv\fR(spank_t spank, const char *var,
    		             char *buf, int len);
      spank_err_t \fBspank_job_control_setenv\fR(spank_t spank, const char *var,
    		             const char *val, int overwrite);
      spank_err_t \fBspank_job_control_unsetenv\fR(spank_t spank, const char *var);
    .fi
    .LP
    See \fBspank.h\fR for more information, and \fBEXAMPLES\fR below for an example
    for \fBspank_getenv\fR usage.
    .LP
    Many of the described \fBSPANK\fR functions available to plugins return
    errors via the \fBspank_err_t\fR error type. On success, the return value
    will be set to \fBESPANK_SUCCESS\fR, while on failure, the return value
    will be set to one of many error values defined in slurm/spank.h. The
    \fBSPANK\fR interface provides a simple function
    .nf

      const char * \fBspank_strerror\fR(spank_err_t err);

    .fi
    which may be used to translate a \fBspank_err_t\fR value into its
    string representation.

    .LP
    The \fBslurm_spank_log\fR function can be used to print messages back to the
    user at an error level.  This is to keep users from having to rely on the
    \fBslurm_error\fR function, which can be confusing because it prepends
    "\fBerror:\fR" to every message.

    .SH "SPANK OPTIONS"
    .LP
          char *         arginfo;
          char *         usage;
          int            has_arg;
          int            val;
          spank_opt_cb_f cb;
       };

    .fi

    Where
    .TP
    .I name
    is the name of the option. Its length is limited to \fBSPANK_OPTION_MAXLEN\fR
    defined in \fB<slurm/spank.h>\fR.
    .TP
    .I arginfo
    is a description of the argument to the option, if the option does take
    an argument.
    .TP
    .I usage
    is a short description of the option suitable for \-\-help output.
    .TP
    .I has_arg
    0 if option takes no argument, 1 if option takes an argument, and
    2 if the option takes an optional argument. (See \fBgetopt_long\fR (3)).
    .TP
    .I val
    A plugin\-local value to return to the option callback function.
    .TP
    .I cb
    A callback function that is invoked when the plugin option is
    registered with Slurm. \fBspank_opt_cb_f\fR is typedef'd in
    \fB<slurm/spank.h>\fR as
    .nf

      typedef int (*spank_opt_cb_f) (int val, const char *optarg,
    		                 int remote);

    .fi
    Where \fIval\fR is the value of the \fIval\fR field in the \fBspank_option\fR
    struct, \fIoptarg\fR is the supplied argument if applicable, and \fIremote\fR
    is 0 if the function is being called from the "local" host (e.g. host where
    \fBsrun\fR or \fBsbatch/salloc\fR are invoked) or 1 from the "remote" host
    (host where slurmd/slurmstepd run) but only executed by \fBslurmstepd\fR
    (remote context) if the option was registered for such context.
    .LP
    Plugin options may be registered with Slurm using
    the \fBspank_option_register\fR function. This function is only valid
    when called from the plugin's \fBslurm_spank_init\fR handler, and
    registers one option at a time. The prototype is
    .nf

          spank_option_register (sp, opt);

    .fi
    If, however, the option is used in all contexts, the \fBspank_option_register\fR
    needs to be called everywhere.
    .LP
    In addition to \fBspank_option_register\fR, plugins may also export options
    to Slurm by defining a table of \fBstruct spank_option\fR with the
    symbol name \fBspank_options\fR. This method, however, is not supported
    for use with \fBsbatch\fR and \fBsalloc\fR (allocator context), thus
    the use of \fBspank_option_register\fR is preferred. When using the
    \fBspank_options\fR table, the final element in the array must be
    filled with zeros. A \fBSPANK_OPTIONS_TABLE_END\fR macro is provided
    in \fB<slurm/spank.h>\fR for this purpose.
    .LP
    When an option is provided by the user on the local side, either by command line
    options or by environment variables, \fBSlurm\fR will immediately invoke the
    option's callback with \fIremote\fR=0. This is meant for the plugin to do local
    sanity checking of the option before the value is sent to the remote side during
    job launch. If the argument the user specified is invalid, the plugin should
    issue an error and issue a non\-zero return code from the callback. The plugin
    should be able to handle cases where the spank option is set multiple times
    through environment variables and command line options. Environment variables
    are processed before command line options.
    .LP
    On the remote side, options and their arguments are registered just
    after \fBSPANK\fR plugins are loaded and before the \fBspank_init\fR
    handler is called. This allows plugins to modify behavior of all plugin
    functionality based on the value of user\-provided options.
    (See EXAMPLES below for a plugin that registers an option with \fBSlurm\fR).
    .LP
    As an alternative to use of an option callback and global variable,
    plugins can use the \fBspank_option_getopt\fR option to check for
    supplied options after option processing. This function has the prototype:
    .nf

       spank_err_t spank_option_getopt(spank_t sp,
           struct spank_option *opt, char **optargp);

    .nf
    This function returns \fBESPANK_SUCCESS\fR if the option defined in the
    struct spank_option \fIopt\fR has been used by the user. If \fIoptargp\fR
    is non-NULL then it is set to any option argument passed (if the option
    takes an argument). The use of this method is \fIrequired\fR to process
    options in \fBjob_script\fR context (\fBslurm_spank_job_prolog\fR and
    \fBslurm_spank_job_epilog\fR). This function is valid in the following contexts:
    slurm_spank_job_prolog, slurm_spank_local_user_init, slurm_spank_user_init,
    slurm_spank_task_init_privileged, slurm_spank_task_init, slurm_spank_task_exit,
    and slurm_spank_job_epilog.

    .SH "CONFIGURATION"
    .LP
    .nf

      required/optional   plugin   arguments

    .fi
    \fR For example:
    .nf

      optional /usr/lib/slurm/test.so

    .fi
    Tells \fBslurmd\fR to load the plugin \fBtest.so\fR passing no arguments.
    If a \fBSPANK\fR plugin is \fIrequired\fR, then failure of any of the
    plugin's functions will cause \fBslurmd\fR to terminate the job, while
    \fIoptional\fR plugins only cause a warning.
    .LP
    If a fully\-qualified path is not specified for a plugin, then the
    currently configured \fIPluginDir\fR in \fBslurm.conf\fR(5) is searched.
    .LP
    \fBSPANK\fR plugins are stackable, meaning that more than one plugin may
    be placed into the config file. The plugins will simply be called
    in order, one after the other, and appropriate action taken on
    failure given that state of the plugin's \fIoptional\fR flag.
    .LP
    Additional config files or directories of config files may be included
    in \fBplugstack.conf\fR with the \fBinclude\fR keyword. The \fBinclude\fR
    keyword must appear on its own line, and takes a glob as its parameter,
    so multiple files may be included from one \fBinclude\fR line. For
    example, the following syntax will load all config files in the
    /etc/slurm/plugstack.conf.d directory, in local collation order:
    .nf

      include /etc/slurm/plugstack.conf.d/*

    .fi
    which might be considered a more flexible method for building up
    a spank plugin stack.
    .LP
    The \fBSPANK\fR config file is re\-read on each job launch, so editing
    the config file will not affect running jobs. However care should
    be taken so that a partially edited config file is not read by a
    launching job.

    .SH "EXAMPLE: renice.so"
    .TP
    \fB/etc/slurm/plugstack.conf\fR:
    This example plugstack.conf file shows a configuration that activates the
    renice.so \fBSPANK\fR plugin.
    .nf
    #
    # SPANK config file
    #
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/resource.h>

    #include <slurm/spank.h>

    /*
     * All spank plugins must define this macro for the
     * Slurm plugin loader.
     */
    SPANK_PLUGIN(renice, 1);

    #define PRIO_ENV_VAR "SLURM_RENICE"
    #define PRIO_NOT_SET -1

    /*
     * Minimum allowable value for priority. May be
     * set globally via plugin option min_prio=<prio>
     */
    static int min_prio = -20;

    static int prio = PRIO_NOT_SET;

    static int _renice_opt_process(int val, const char *optarg, int remote);
    static int _str2prio(const char *str, int *p2int);

    /*
     *  Provide a --renice=[prio] option to srun:
     */
    struct spank_option spank_options[] =
    {
        {
            "renice",
            "[prio]",
            "Re-nice job tasks to priority [prio].",
            2,
            0,
            _renice_opt_process
        },
        SPANK_OPTIONS_TABLE_END
    };

    /*
     *  Called from both srun and slurmd.
     */
    int slurm_spank_init(spank_t sp, int ac, char **av)
    {
        int i;

        /* Don't do anything in sbatch/salloc */
        if (spank_context () == S_CTX_ALLOCATOR)
            slurm_verbose("renice: min_prio = %d", min_prio);

        return ESPANK_SUCCESS;
    }

    int slurm_spank_task_post_fork(spank_t sp, int ac, char **av)
    {
        int rc;
        pid_t pid;
        int taskid;

        if (prio == PRIO_NOT_SET) {
            /* See if SLURM_RENICE env var is set by user */
            char val[1024];

            rc = spank_getenv(sp, PRIO_ENV_VAR, val, sizeof(val));

            if (rc)
                return rc;

            rc = _str2prio(val, &prio);

            if (rc) {
                slurm_error("Bad value for %s: %s", PRIO_ENV_VAR, optarg);
                return rc;
            }

            if (prio < min_prio) {
                slurm_error("%s=%d not allowed, using min=%d",
                            PRIO_ENV_VAR, prio, min_prio);
            }
        }

        if (prio < min_prio)
            prio = min_prio;

        spank_get_item(sp, S_TASK_GLOBAL_ID, &taskid);
        spank_get_item(sp, S_TASK_PID, &pid);

        slurm_info("re-nicing task%d pid %d to %d", taskid, (int) pid, prio);

        if (setpriority(PRIO_PROCESS, (int) pid, (int) prio)) {
            slurm_error("setpriority: %m");
            return -ESPANK_ERROR;
        }

        return ESPANK_SUCCESS;
    }

    static int _str2prio(const char *str, int *p2int)
    {
        long l;

        *p2int = (int) l;

        return ESPANK_SUCCESS;
    }

    static int _renice_opt_process(int val, const char *optarg, int remote)
    {
        int rc;

        if (optarg == NULL) {
            slurm_error("renice: invalid NULL argument!");
            return -ESPANK_BAD_ARG;
        }

        if ((rc = _str2prio(optarg, &prio))) {
            slurm_error("Bad value for --renice: %s", optarg);
            return rc;
        }

        if (prio < min_prio) {
            slurm_error("--renice=%d not allowed, will use min=%d",
                        prio, min_prio);
        }

        return ESPANK_SUCCESS;
    }
    .fi
    .TP
    \fBCompile command\fR:
    .nf
    # gcc -ggdb3 -I${SLURM_PATH}/include/ -fPIC -shared -o /usr/lib/SPANK_renice.so /usr/local/src/renice.c
    .fi

    .SH "COPYING"
    Portions copyright (C) 2010-2018 SchedMD LLC.
    Copyright (C) 2006 The Regents of the University of California.
    Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
    CODE\-OCEC\-09\-009. All rights reserved.
    .LP
    This file is part of Slurm, a resource management program.
    For details, see <https://slurm.schedmd.com/>.
    .LP
    Slurm 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.
    .LP
    Slurm 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.