📄 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 <errno.h>#include <strings.h>#include <pwd.h>#include <mntent.h>#include <asm/types.h>#include <linux/quota.h>#include <sys/time.h>#include <sys/types.h>#include <sys/procfs.h>#include <sys/param.h>#include <sys/stat.h>#include <sys/vfs.h>#include <sys/sysmacros.h>#include <sys/resource.h>#include <syscall.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"/*** System dependent code to gather information for the resource** monitor for a Linux i386 machine.**** Resources known by this code:** cput cpu time for a pid or session** mem memory size for a pid or session in KB** resi resident memory size for a pid or session in KB** sessions list of sessions in the system** pids list of pids in a session** nsessions number of sessions in the system** nusers number of users in the system** totmem total memory size in KB** availmem available memory size in KB** ncpus number of cpus** physmem physical memory size in KB** size size of a file or filesystem** idletime seconds of idle time** walltime wall clock time for a pid** loadave current load average** quota quota information (sizes in kb)*/static char ident[] = "@(#) $RCSfile: mom_mach.c,v $ $Revision: 2.12.4.7 $";#ifndef TRUE#define FALSE 0#define TRUE 1#endif /* TRUE */static char procfs[] = "/proc";static DIR *pdir = NULL;static int pagesize;extern char *ret_string;extern char extra_parm[];extern char no_parm[];extern struct pbs_err_to_txt pbs_err_to_txt[];extern time_t time_now;/*** external functions and data*/extern struct config *search A_((struct config *, char *));extern struct rm_attribute *momgetattr A_((char *));extern int rm_errno;extern double cputfactor;extern double wallfactor;/*** local functions and data*/static char *resi A_((struct rm_attribute *attrib));static char *totmem A_((struct rm_attribute *attrib));static char *availmem 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 *quota A_((struct rm_attribute *attrib));extern char *loadave A_((struct rm_attribute *attrib));extern char *nullproc A_((struct rm_attribute *attrib));time_t wait_time = 10;typedef struct proc_mem { unsigned total; unsigned used; unsigned free;} proc_mem_t;/*** local resource array*/struct config dependent_config[] = { { "resi", resi }, { "totmem", totmem }, { "availmem", availmem }, { "physmem", physmem }, { "ncpus", ncpus }, { "loadave", loadave }, { "walltime", walltime }, { "quota", quota }, { NULL, nullproc },};unsigned linux_time = 0;/* * support routine for getting system time -- sets linux_time */void proc_get_btime(){ FILE *fp; char label[256]; if ((fp = fopen("/proc/stat", "r")) == NULL) return; while (!feof(fp)) { fscanf(fp, "%s", label); if (strcmp(label, "btime")) { fscanf(fp, "%*[^\n]%*c"); } else { fscanf(fp, "%u", &linux_time); fclose(fp); return; } } fclose(fp); return;}static char stat_str[] = "%d (%[^)]) %c %*d %*d %d %*d %*d %u %*u \%*u %*u %*u %d %d %d %d %*d %*d %*u %*u %u %u %u %*u %*u \%*u %*u %*u %*u %*u %*u %*u %*u %*u %*u %*u";/*** Convert jiffies to seconds.*/#define JTOS(x) x = (x + CLK_TCK/2) / CLK_TCK;/* * Linux /proc status routine. * * Returns a pointer to a malloc'd proc_stat_t structure given * a process number, or NULL if there is an error. Takes the * place of the ioctl call PIOCSTATUS in the irix imp of mom_mach.c * */proc_stat_t *get_proc_stat(pid) int pid;{ static proc_stat_t ps; static char path[1024]; FILE *fd; unsigned jiffies; struct stat sb; sprintf(path, "/proc/%d/stat", pid); if ((fd = fopen(path, "r")) == NULL) return(NULL); if (fscanf(fd, stat_str, &ps.pid, path, &ps.state, &ps.session, &ps.flags, &ps.utime, &ps.stime, &ps.cutime, &ps.cstime, &jiffies, &ps.vsize, &ps.rss) != 12) { fclose(fd); return(NULL); } if (fstat(fileno(fd), &sb) == -1) { fclose(fd); return(NULL); } else { ps.uid = sb.st_uid; } ps.start_time = linux_time + (jiffies / 100); ps.name = path; JTOS(ps.utime) JTOS(ps.stime) JTOS(ps.cutime) JTOS(ps.cstime) fclose(fd); return(&ps);}proc_mem_t *get_proc_mem(){ static proc_mem_t mm; FILE *fp; unsigned m_tot, m_use, m_free; unsigned s_tot, s_use, s_free; if ((fp = fopen("/proc/meminfo", "r")) == NULL) return(NULL); fscanf(fp, "%*[^\n]%*c"); /* lose the text header */; fscanf(fp, "%*s %u %u %u %*[^\n]%*c", &m_tot, &m_use, &m_free); fscanf(fp, "%*s %u %u %u %*[^\n]%*c", &s_tot, &s_use, &s_free); mm.total = m_tot + s_tot; mm.used = m_use + s_use; mm.free = m_free + s_free; fclose(fp); return(&mm);}voiddep_initialize(){ char *id = "dep_initialize"; pagesize = getpagesize(); if ((pdir = opendir(procfs)) == NULL) { log_err(errno, id, "opendir"); return; } proc_get_btime(); return;}voiddep_cleanup(){ char *id = "dep_cleanup"; log_record(PBSEVENT_SYSTEM, 0, id, "dependent cleanup"); if (pdir) { closedir(pdir); pdir = NULL; }}/*** Don't need any periodic procsessing.*/voidend_proc(){ return;}/* * 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 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 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);}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, * adjusted by cputfactor. */static unsigned long cput_sum(pjob) job *pjob;{ char *id = "cput_ses"; int fd; struct dirent *dent; ulong cputime; int nps = 0; proc_stat_t *ps; cputime = 0.0; rewinddir(pdir); while ((dent = readdir(pdir)) != NULL) { if (!isdigit(dent->d_name[0])) continue; if ((ps = get_proc_stat(atoi(dent->d_name))) == NULL) { if (errno != ENOENT) { sprintf(log_buffer, "%s: get_proc_stat", dent->d_name); log_err(errno, id, log_buffer); } continue; } if (!injob(pjob, ps->session)) continue; nps++; cputime += (ps->utime + ps->stime + ps->cutime + ps->cstime); DBPRT(("%s: ses %d pid %d cputime %d\n", id, ps->session, ps->pid, cputime)); } if (nps == 0) pjob->ji_flags |= MOM_NO_PROC; return ((unsigned long)((double)cputime * cputfactor));}/* * Return TRUE if any process in the job is over limit for cputime usage. */static int overcpu_proc(pjob, limit) job *pjob; unsigned long limit;{ char *id = "overcpu_proc"; ulong memsize; int fd; struct dirent *dent; ulong cputime; proc_stat_t *ps; memsize = 0; rewinddir(pdir); while ((dent = readdir(pdir)) != NULL) { if (!isdigit(dent->d_name[0])) continue; if ((ps = get_proc_stat(atoi(dent->d_name))) == NULL) { if (errno != ENOENT) { sprintf(log_buffer, "%s: get_proc_stat", dent->d_name); log_err(errno, id, log_buffer); } continue; } if (!injob(pjob, ps->session)) continue; cputime = (ulong)((double)(ps->cutime + ps->cstime)*cputfactor); 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 processes within the job. */static unsigned long mem_sum(pjob) job *pjob;{ char *id="mem_sum"; struct dirent *dent; char procname[100]; int num, i; unsigned long segadd; proc_stat_t *ps; segadd = 0; rewinddir(pdir); while ((dent = readdir(pdir)) != NULL) { if (!isdigit(dent->d_name[0])) continue; if ((ps = get_proc_stat(atoi(dent->d_name))) == NULL) { if (errno != ENOENT) { sprintf(log_buffer, "%s: get_proc_stat", dent->d_name); log_err(errno, id, log_buffer); } continue; } if (!injob(pjob, ps->session)) continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -