📄 mom_mach.c
字号:
/** OpenPBS (Portable Batch System) v2.3 Software License* * Copyright (c) 1999-2000 Veridian Information Solutions, Inc.* All rights reserved.* * ---------------------------------------------------------------------------* For a license to use or redistribute the OpenPBS software under conditions* other than those described below, or to purchase support for this software,* please contact Veridian Systems, PBS Products Department ("Licensor") at:* * www.OpenPBS.org +1 650 967-4675 sales@OpenPBS.org* 877 902-4PBS (US toll-free)* ---------------------------------------------------------------------------* * This license covers use of the OpenPBS v2.3 software (the "Software") at* your site or location, and, for certain users, redistribution of the* Software to other sites and locations. Use and redistribution of* OpenPBS v2.3 in source and binary forms, with or without modification,* are permitted provided that all of the following conditions are met.* After December 31, 2001, only conditions 3-6 must be met:* * 1. Commercial and/or non-commercial use of the Software is permitted* provided a current software registration is on file at www.OpenPBS.org.* If use of this software contributes to a publication, product, or* service, proper attribution must be given; see www.OpenPBS.org/credit.html* * 2. Redistribution in any form is only permitted for non-commercial,* non-profit purposes. There can be no charge for the Software or any* software incorporating the Software. Further, there can be no* expectation of revenue generated as a consequence of redistributing* the Software.* * 3. Any Redistribution of source code must retain the above copyright notice* and the acknowledgment contained in paragraph 6, this list of conditions* and the disclaimer contained in paragraph 7.* * 4. Any Redistribution in binary form must reproduce the above copyright* notice and the acknowledgment contained in paragraph 6, this list of* conditions and the disclaimer contained in paragraph 7 in the* documentation and/or other materials provided with the distribution.* * 5. Redistributions in any form must be accompanied by information on how to* obtain complete source code for the OpenPBS software and any* modifications and/or additions to the OpenPBS software. The source code* must either be included in the distribution or be available for no more* than the cost of distribution plus a nominal fee, and all modifications* and additions to the Software must be freely redistributable by any party* (including Licensor) without restriction.* * 6. All advertising materials mentioning features or use of the Software must* display the following acknowledgment:* * "This product includes software developed by NASA Ames Research Center,* Lawrence Livermore National Laboratory, and Veridian Information * Solutions, Inc.* Visit www.OpenPBS.org for OpenPBS software support,* products, and information."* * 7. DISCLAIMER OF WARRANTY* * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. ANY EXPRESS* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT* ARE EXPRESSLY DISCLAIMED.* * IN NO EVENT SHALL VERIDIAN CORPORATION, ITS AFFILIATED COMPANIES, OR THE* U.S. GOVERNMENT OR ANY OF ITS AGENCIES BE LIABLE FOR ANY DIRECT OR INDIRECT,* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.* * This license will be governed by the laws of the Commonwealth of Virginia,* without reference to its choice of law rules.*/#include <pbs_config.h> /* the master config generated by configure */#include <assert.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <dirent.h>#include <fcntl.h>#include <malloc.h>#include <errno.h>#include <string.h>#include <pwd.h>#include <kvm.h>#include <nlist.h>#include <sys/types.h>#include <sys/unistd.h>#include <sys/param.h>#include <sys/stat.h>#include <sys/statvfs.h>#include <sys/sysmacros.h>#include <sys/systeminfo.h>#include <sys/time.h>#include <procfs.h>#include <sys/resource.h>#include "pbs_error.h"#include "portability.h"#include "list_link.h"#include "server_limits.h"#include "attribute.h"#include "resource.h"#include "job.h"#include "log.h"#include "mom_mach.h"#include "resmon.h"#include "../rm_dep.h"#include "mom_func.h"/*** System dependent code to gather information for the resource** monitor for a Sun Solaris machine**** Resources known by this code:** cput cpu time for a pid or job** mem memory size for a pid or job in KB** resi resident memory size for a pid or job in KB** sessions list of sessions in the system** pids list of pids in a job** nsessions number of sessions in the system** nusers number of users in the system** ncpus number of cpus** physmem physical memory size in KB** size size of a file or filesystem in KB** idletime seconds of idle time** walltime wall clock time for a pid** loadave current load average*/static char ident[] = "@(#) solaris5/$RCSfile: mom_mach.c,v $ $Revision: 1.8.4.2.2.3 $";#ifndef TRUE#define FALSE 0#define TRUE 1#endif /* TRUE */#ifndef MAX#define MAX(a,b) (((a)>(b))?(a):(b))#endif /* MAX *//*** external functions and data*/extern struct config *search A_((struct config *, char *));extern struct rm_attribute *momgetattr A_((char *));extern int rm_errno;extern unsigned int reqnum;extern double cputfactor;extern double wallfactor;/*** local functions and data*/static char *resi A_((struct rm_attribute *attrib));static char *physmem A_((struct rm_attribute *attrib));static char *ncpus A_((struct rm_attribute *attrib));static char *walltime A_((struct rm_attribute *attrib));static char *platform A_((struct rm_attribute *attrib));extern char *nullproc A_((struct rm_attribute *attrib));extern char *loadave A_((struct rm_attribute *attrib));char procfs[] = "/proc";char procfmts[] = "/proc/%s/psinfo";DIR *pdir;extern char *ret_string;extern int ret_size;time_t wait_time = 10;int nproc = 0;int max_proc = 0;psinfo_t *proc_info = NULL;static long page_size;static int myproc_ct; /* count of processes in a session */static int myproc_max = 0;pbs_plinks *Proc_lnks = NULL; /* process links table head */#define TBL_INC 50extern struct pbs_err_to_txt pbs_err_to_txt[];extern time_t time_now;extern char extra_parm[];extern char no_parm[];char no_count[] = "count not found";/*** local resource array*/struct config dependent_config[] = { { "resi", resi }, { "physmem", physmem }, { "ncpus", ncpus }, { "loadave", loadave }, { "platform", platform }, { "walltime", walltime }, { NULL, nullproc },}; /*** Don't need any periodic procsessing.*/voidend_proc(){ return;}voiddep_initialize(){ char *id = "dep_initialize"; page_size = sysconf(_SC_PAGESIZE); if ((pdir = opendir(procfs)) == NULL) { log_err(errno, id, "opendir"); return; } return;}voiddep_cleanup(){ char *id = "dep_cleanup"; log_record(PBSEVENT_SYSTEM, 0, id, "dependent cleanup"); if (pdir) closedir(pdir);}/* * Internal size decoding routine. * * Accepts a resource pointer and a pointer to the unsigned long integer * to receive the decoded value. It returns a PBS error code, and the * decoded value in the unsigned long integer. * * sizeof(word) = sizeof(int) */static int local_getsize(pres, ret) resource *pres; unsigned long *ret;{ unsigned long value; if (pres->rs_value.at_type != ATR_TYPE_SIZE) return (PBSE_ATTRTYPE); value = pres->rs_value.at_val.at_size.atsv_num; if (pres->rs_value.at_val.at_size.atsv_units == ATR_SV_WORDSZ) { if (value > ULONG_MAX / sizeof(int)) return (PBSE_BADATVAL); value *= sizeof(int); } if (value > ULONG_MAX >> pres->rs_value.at_val.at_size.atsv_shift) return (PBSE_BADATVAL); *ret = value << pres->rs_value.at_val.at_size.atsv_shift; return (PBSE_NONE);}/* * Internal time decoding routine. * * Accepts a resource pointer and a pointer to the unsigned long integer * to receive the decoded value. It returns a PBS error code, and the * decoded value of time in seconds in the unsigned long integer. */static int local_gettime(pres, ret) resource *pres; unsigned long *ret;{ if (pres->rs_value.at_type != ATR_TYPE_LONG) return (PBSE_ATTRTYPE); if (pres->rs_value.at_val.at_long < 0) return (PBSE_BADATVAL); *ret = pres->rs_value.at_val.at_long; return (PBSE_NONE);}/* * Time decoding macro. Accepts a timestruc_t pointer. * Returns unsigned long time in seconds. */#define tv(val) (ulong)((val).tv_sec)staticintinjob(pjob, sid) job *pjob; pid_t sid;{ task *ptask; for (ptask = (task *)GET_NEXT(pjob->ji_tasks); ptask; ptask = (task *)GET_NEXT(ptask->ti_jobtask)) { if (ptask->ti_qs.ti_sid <= 1) continue; if (ptask->ti_qs.ti_sid == sid) return TRUE; } return FALSE;}/* * Internal session cpu time decoding routine. * * Accepts a job pointer. Returns the sum of all cpu time * consumed for all tasks executed by the job, in seconds. * The "real" time is adjusted by "cputfactor" */static unsigned long cput_sum(pjob) job *pjob;{ char *id = "cput_sum"; ulong cputime, addtime; int i; int nps = 0; psinfo_t *pi; cputime = 0; for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (!injob(pjob, pi->pr_sid)) continue; nps++; if (pi->pr_nlwp == 0) { /* zombie */ cputime += tv(pi->pr_time); DBPRT(("%s: ses %d pid %d (zombie) cputime %d\n", id, pi->pr_sid, pi->pr_pid, cputime)) continue; } pi = &proc_info[i]; addtime = tv(pi->pr_time) + tv(pi->pr_ctime); cputime += addtime; DBPRT(("%s: ses %d pid %d cputime %d\n", id, pi->pr_sid, pi->pr_pid, cputime)) } if (nps == 0) pjob->ji_flags |= MOM_NO_PROC; return ((unsigned)((double)cputime * cputfactor));}/* * Return TRUE if any process in the job is over limit for cputime usage * adjusted by cputfactor. */static int overcput_proc(pjob, limit) job *pjob; unsigned long limit;{ char *id = "overcput_proc"; ulong cputime; int i; psinfo_t *pi; for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (!injob(pjob, pi->pr_sid)) continue; cputime = (ulong)(cputfactor * (double)( tv(pi->pr_time) + tv(pi->pr_ctime))); if (cputime > limit) return (TRUE); } return (FALSE);}/* * Internal session memory usage function. * * Returns the total number of bytes of address * space consumed by all current tasks within the session. */static unsigned long mem_sum(pjob) job *pjob;{ char *id="mem_sum"; ulong memsize; int i; psinfo_t *pi; memsize = 0; for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (!injob(pjob, pi->pr_sid)) continue; memsize += pi->pr_size * 1024; DBPRT(("mem_sum: pid: %d pr_size: %ld total: %ld\n", pi->pr_pid,pi->pr_size * 1024, memsize)) } return (memsize);}/* * Internal session mem (workingset) size function. */static unsigned long resi_sum(pjob) job *pjob;{ char *id="resi_sum"; ulong resisize; int i; psinfo_t *pi; resisize = 0; for (i=0; i<nproc; i++) { pi = &proc_info[i]; if (!injob(pjob, pi->pr_sid)) continue; resisize += pi->pr_rssize * 1024; } return (resisize);}extern char *msg_momsetlim;/* * Internal error routine */int error(string, value) char *string; int value;{ int i = 0; char *message; assert(string != NULL); assert(*string != '\0'); assert(value > PBSE_); /* minimum PBS error number */ assert(value <= PBSE_NOSYNCMSTR); /* maximum PBS error number */ assert(pbs_err_to_txt[i].err_no != 0); do { if (pbs_err_to_txt[i].err_no == value) break; } while (pbs_err_to_txt[++i].err_no != 0); assert(pbs_err_to_txt[i].err_txt != NULL); message = *pbs_err_to_txt[i].err_txt; assert(message != NULL); assert(*message != '\0'); (void)fprintf(stderr, msg_momsetlim, string, message); (void)fflush(stderr); return (value);}/* * Establish system-enforced limits for the job. * * Run through the resource list, checking the values for all items * we recognize. * * If set_mode is SET_LIMIT_SET, then also set hard limits for the * system enforced limits (not-polled). * If anything goes wrong with the process, return a PBS error code * and print a message on standard error. A zero-length resource list * is not an error. * * If set_mode is SET_LIMIT_SET the entry conditions are: * 1. MOM has already forked, and we are called from the child. * 2. The child is still running as root. * 3. Standard error is open to the user's file. * * If set_mode is SET_LIMIT_ALTER, we are beening called to modify * existing limits. Cannot alter those set by setrlimit (kernel) * because we are the wrong process. */int mom_set_limits(pjob, set_mode) job *pjob; int set_mode; /* SET_LIMIT_SET or SET_LIMIT_ALTER */{ char *id = "mom_set_limits"; char *pname; int retval; unsigned long value; /* place in which to build resource value */ resource *pres; struct rlimit reslim; unsigned long mem_limit = 0; DBPRT(("%s: entered\n", id)) assert(pjob != NULL); assert(pjob->ji_wattr[(int)JOB_ATR_resource].at_type == ATR_TYPE_RESC); pres = (resource *) GET_NEXT(pjob->ji_wattr[(int)JOB_ATR_resource].at_val.at_list);/* * Cycle through all the resource specifications, * setting limits appropriately. */ while (pres != NULL) { assert(pres->rs_defin != NULL); pname = pres->rs_defin->rs_name; assert(pname != NULL); assert(*pname != '\0'); if (strcmp(pname, "cput") == 0) { /* cpu time - check */ retval = local_gettime(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); } else if (strcmp(pname, "pcput") == 0) { /* process cpu time - set */ retval = local_gettime(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); reslim.rlim_cur = reslim.rlim_max = (unsigned long)((double)value / cputfactor); if (setrlimit(RLIMIT_CPU, &reslim) < 0) return (error("RLIMIT_CPU", PBSE_SYSTEM)); } else if (strcmp(pname, "file") == 0) { /* set */ if (set_mode == SET_LIMIT_SET) { retval = local_getsize(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); if (value > LONG_MAX) return (error(pname, PBSE_BADATVAL)); reslim.rlim_cur = reslim.rlim_max = value; if (setrlimit(RLIMIT_FSIZE, &reslim) < 0) return (error(pname, PBSE_SYSTEM)); } } else if (strcmp(pname, "mem") == 0) { /* check */ retval = local_getsize(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); } else if (strcmp(pname, "pmem") == 0) { /* set */ if (set_mode == SET_LIMIT_SET) { retval = local_getsize(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); if (value > LONG_MAX) return (error(pname, PBSE_BADATVAL)); reslim.rlim_cur = reslim.rlim_max = value; if (setrlimit(RLIMIT_DATA, &reslim) < 0) return (error("RLIMIT_DATA", PBSE_SYSTEM)); if (setrlimit(RLIMIT_STACK, &reslim) < 0) return (error("RLIMIT_STACK", PBSE_SYSTEM)); } } else if (strcmp(pname, "vmem") == 0) { /* check */ retval = local_getsize(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); if ((mem_limit == 0) || (value < mem_limit)) mem_limit = value; } else if (strcmp(pname, "pvmem") == 0) { /* set */ retval = local_getsize(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); if (value > LONG_MAX) return (error(pname, PBSE_BADATVAL)); if ((mem_limit == 0) || (value < mem_limit)) mem_limit = value; } else if (strcmp(pname, "walltime") == 0) { /* Check */ retval = local_gettime(pres, &value); if (retval != PBSE_NONE) return (error(pname, retval)); } else if (strcmp(pname, "nice") == 0) { /* set nice */ if (set_mode == SET_LIMIT_SET) { errno = 0; if ((nice((int)pres->rs_value.at_val.at_long) == -1) && (errno != 0)) return (error(pname, PBSE_BADATVAL)); } } else if ((pres->rs_defin->rs_flags & ATR_DFLAG_RMOMIG) == 0) /* don't recognize and not marked as ignore by mom */ return (error(pname, PBSE_UNKRESC)); pres = (resource *)GET_NEXT(pres->rs_link); } if (set_mode == SET_LIMIT_SET) { /* if either of vmem or pvmem was given,set sys limit of lesser */ if (mem_limit != 0) { reslim.rlim_cur = reslim.rlim_max = mem_limit; if (setrlimit(RLIMIT_VMEM, &reslim) < 0) return (error("RLIMIT_VMEM", PBSE_SYSTEM)); } } return (PBSE_NONE);}/* * State whether MOM main loop has to poll this job to determine if some * limits are being exceeded. * * Sets flag TRUE if polling is necessary, FALSE otherwise. Actual * polling is done using the mom_over_limit machine-dependent function. */int mom_do_poll(pjob) job *pjob;{ char *id = "mom_do_poll"; char *pname; resource *pres; DBPRT(("%s: entered\n", id)) assert(pjob != NULL); assert(pjob->ji_wattr[(int)JOB_ATR_resource].at_type == ATR_TYPE_RESC); pres = (resource *)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -