⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mom_mach.c

📁 openPBS的开放源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
struct	rm_attribute	*attrib;{	char			*id = "mem";	int			value;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if ((value = atoi(attrib->a_value)) == 0) {		sprintf(log_buffer, "bad param: %s", attrib->a_value);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (momgetattr(NULL)) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (strcmp(attrib->a_qualifier, "session") == 0)		return (mem_job((pid_t)value));	else if (strcmp(attrib->a_qualifier, "proc") == 0)		return (mem_proc((pid_t)value));	else {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}}static char	*resi_job(jobid)pid_t	jobid;{	char			*id = "resi_job";	int			resisize;	int			i;	int			found = 0;	psinfo_t		*pi;	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	resisize = 0;	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		if (jobid != pi->pr_sid)			continue;		found = 1;		resisize += pi->pr_rssize;	}	if (found) {		sprintf(ret_string, "%ukb",resisize); /* KB */		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}static char	*resi_proc(pid)pid_t	pid;{	char			*id = "resi_proc";	psinfo_t		*pi;	int			i;	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		if (pi->pr_pid == pid)			break;	}	if (i == nproc) {		rm_errno = RM_ERR_EXIST;		return NULL;	}	sprintf(ret_string, "%ukb", pi->pr_rssize); /* KB */	return ret_string;}static char	*resi(attrib)struct	rm_attribute	*attrib;{	char			*id = "resi";	int			value;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if ((value = atoi(attrib->a_value)) == 0) {		sprintf(log_buffer, "bad param: %s", attrib->a_value);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (momgetattr(NULL)) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (strcmp(attrib->a_qualifier, "session") == 0)		return (resi_job((pid_t)value));	else if (strcmp(attrib->a_qualifier, "proc") == 0)		return (resi_proc((pid_t)value));	else {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}}char	*sessions(attrib)struct	rm_attribute	*attrib;{	char			*id = "sessions";	int			i, j;	psinfo_t		*pi;	char			*fmt;	int			njids = 0;	pid_t			*jids, *hold;	static		int	maxjid = 200;	register	pid_t	jobid;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((jids = (pid_t *)calloc(maxjid, sizeof(pid_t))) == NULL) {		log_err(errno, id, "no memory");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	/*	** Search for members of job	*/	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		if (pi->pr_uid == 0)			continue;		if ((jobid = pi->pr_sid) == 0)			continue;		DBPRT(("%s[%d]: pid %d sid %d\n",		       id, njids, pi->pr_pid, jobid))		for (j=0; j<njids; j++) {			if (jids[j] == jobid)				break;		}		if (j == njids) {		/* not found */			if (njids == maxjid) {	/* need more space */				maxjid += 100;				hold = (pid_t *)realloc(jids, maxjid);				if (hold == NULL) {					log_err(errno, id, "realloc");					rm_errno = RM_ERR_SYSTEM;					free(jids);					return NULL;				}				jids = hold;			}			jids[njids++] = jobid;	/* add jobid to list */		}	}	fmt = ret_string;	for (j=0; j<njids; j++) {		checkret(&fmt, 100);		sprintf(fmt, " %d", (int)jids[j]);		fmt += strlen(fmt);	}	free(jids);	return ret_string;}char	*nsessions(attrib)struct	rm_attribute	*attrib;{	char	*result, *ch;	int	num = 0;	if ((result = sessions(attrib)) == NULL)		return result;	for (ch=result; *ch; ch++) {		if (*ch == ' ')		/* count blanks */			num++;	}	sprintf(ret_string, "%d", num);	return ret_string;}char	*pids(attrib)struct	rm_attribute	*attrib;{	char		*id = "pids";	pid_t		jobid;	int		i, j;	psinfo_t	*pi;	char		*fmt;	int		num_pids;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if ((jobid = (pid_t)atoi(attrib->a_value)) == 0) {		sprintf(log_buffer, "bad param: %s", attrib->a_value);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (momgetattr(NULL)) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (strcmp(attrib->a_qualifier, "session") != 0) {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	/*	** Search for members of session	*/	fmt = ret_string;	num_pids = 0;	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		DBPRT(("%s[%d]: pid: %d sid %d\n",		       id, num_pids, pi->pr_pid, pi->pr_sid))		if (jobid != pi->pr_sid)			continue;		sprintf(fmt, "%d ", pi->pr_pid);		fmt += strlen(fmt);		num_pids++;	}	if (num_pids == 0) {		rm_errno = RM_ERR_EXIST;		return NULL;	}	return ret_string;}char	*nusers(attrib)struct	rm_attribute	*attrib;{	char			*id = "nusers";	int			i, j;	psinfo_t		*pi;	int			nuids = 0;	uid_t			*uids, *hold;	static		int	maxuid = 200;	register	uid_t	uid;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((uids = (uid_t *)calloc(maxuid, sizeof(uid_t))) == NULL) {		log_err(errno, id, "no memory");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		if ((uid = pi->pr_uid) == 0)			continue;		DBPRT(("%s[%d]: pid %d uid %d\n",		       id, nuids, pi->pr_pid, uid))		for (j=0; j<nuids; j++) {			if (uids[j] == uid)				break;		}		if (j == nuids) {		/* not found */			if (nuids == maxuid) {	/* need more space */				maxuid += 100;				hold = (uid_t *)realloc(uids, maxuid);				if (hold == NULL) {					log_err(errno, id, "realloc");					rm_errno = RM_ERR_SYSTEM;					free(uids);					return NULL;				}				uids = hold;			}			uids[nuids++] = uid;	/* add uid to list */		}	}	sprintf(ret_string, "%d", nuids);	free(uids);	return ret_string;}static char	*ncpus(attrib)struct	rm_attribute	*attrib;{	char		*id = "ncpus";	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	sprintf(ret_string, "%d", sysconf(_SC_NPROCESSORS_ONLN));	return ret_string;}static char	*physmem(attrib)struct	rm_attribute	*attrib;{	char	*id = "physmem";	size_t	 pmem;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	pmem = sysconf(_SC_PHYS_PAGES) * page_size;	sprintf(ret_string, "%lukb", pmem >> 10); /* KB */	return ret_string;}char	*size_fs(param)char	*param;{	char		*id = "size_fs";	FILE		*mf;	struct	mntent	*mp;	struct	statvfs	fsbuf;	if (param[0] != '/') {		sprintf(log_buffer, "%s: not full path filesystem name: %s\n",			id, param);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (statvfs(param, &fsbuf) == -1) {		log_err(errno, id, "statvfs");		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	/* in KB */	sprintf(ret_string, "%lukb", (unsigned long)(((double)fsbuf.f_frsize * (double)fsbuf.f_bfree) / 1024.0));	return ret_string;}char	*size_file(param)char	*param;{	char		*id = "size_file";	struct	stat	sbuf;	if (param[0] != '/') {		sprintf(log_buffer, "%s: not full path filesystem name: %s\n",			id, param);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (stat(param, &sbuf) == -1) {		log_err(errno, id, "stat");		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	sprintf(ret_string, "%ukb", sbuf.st_size >> 10); /* KB */	return ret_string;}char	*size(attrib)struct	rm_attribute	*attrib;{	char	*id = "size";	char	*param;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if (momgetattr(NULL)) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	param = attrib->a_value;	if (strcmp(attrib->a_qualifier, "file") == 0)		return (size_file(param));	else if (strcmp(attrib->a_qualifier, "fs") == 0)		return (size_fs(param));	else {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}}time_t	maxtm;voidsetmax(dev)char	*dev;{	struct	stat	sb;	if (stat(dev, &sb) == -1)		return;	if (maxtm < sb.st_atime)		maxtm = sb.st_atime;	return;}char	*idletime(attrib)struct	rm_attribute	*attrib;{	char	*id = "idletime";	DIR	*dp;	struct	dirent	*de;	char	ttyname[50];	time_t	curtm;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((dp=opendir("/dev/pts")) == NULL) {		log_err(errno, id, "opendir /dev");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	maxtm = 0;	curtm = time(NULL);	setmax("/dev/mouse");	while ((de=readdir(dp)) != NULL) {		char	*name = de->d_name;		if (maxtm >= curtm)			break;		if (*name == '.')			continue;		sprintf(ttyname, "/dev/pts/%s", name);		setmax(ttyname);	}	closedir(dp);	sprintf(ret_string, "%d", MAX(0, curtm - maxtm));	return ret_string;}static char	*walltime(attrib)struct	rm_attribute	*attrib;{	char			*id = "walltime";	int			i;	int			value, job, found = 0;	time_t			now, start;	psinfo_t		*pi;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if ((value = atoi(attrib->a_value)) == 0) {		sprintf(log_buffer, "bad param: %s", attrib->a_value);		log_err(-1, id, log_buffer);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (momgetattr(NULL)) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (strcmp(attrib->a_qualifier, "proc") == 0)		job = 0;	else if (strcmp(attrib->a_qualifier, "session") == 0)		job = 1;	else {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((now = time(NULL)) <= 0) {		log_err(errno, id, "time");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if (getprocs() == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	start = now;	for (i=0; i<nproc; i++) {		pi = &proc_info[i];		if (job) {			if (value != pi->pr_sid)				continue;		}		else {			if ((pid_t)value != pi->pr_pid)				continue;		}		found = 1;		start = MIN(start, pi->pr_start.tv_sec);	}	if (found) {		sprintf(ret_string, "%ld", (long)((double)(now - start) * wallfactor));		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}intget_la(rv)	double	*rv;{	char	*id = "get_la";	double la[3];	if (getloadavg(la, 3) == -1) {		log_err(errno, id, "getloadavg");		return (rm_errno = RM_ERR_SYSTEM);	}	*rv = la[0];	return 0;}static char *platform(attrib)struct	rm_attribute	*attrib;{  char	*id = "platform";  int    err;  if (attrib) {    log_err(-1, id, extra_parm);    rm_errno = RM_ERR_BADPARAM;    return NULL;  }  err = sysinfo(SI_PLATFORM, ret_string, ret_size-1);  if (err < 0) {    return "unknown [error]";  } else {    return ret_string;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -