mom_mach.c

来自「OpenPBS」· C语言 代码 · 共 2,003 行 · 第 1/3 页

C
2,003
字号
				sprintf(log_buffer,					"%s: get_proc_stat", dent->d_name);				log_err(errno, id, log_buffer);			}			continue;		}		if (jobid != ps->session)			continue;		found = 1;		resisize += ps->rss;	}	if (found) {		/* in KB */		sprintf(ret_string, "%lukb",(resisize * (ulong)pagesize) >> 10);		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}static char	*resi_proc(pid)pid_t	pid;{	char			*id = "resi_proc";	proc_stat_t		*ps;	if ((ps = get_proc_stat(pid)) == NULL) {		if (errno != ENOENT) {			sprintf(log_buffer,				"%d: get_proc_stat(PIOCPSINFO)", pid);			log_err(errno, id, log_buffer);		}		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	/* in KB */	sprintf(ret_string, "%lukb", ((ulong)ps->rss * (ulong)pagesize) >> 10);	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			j;	struct dirent		*dent;	proc_stat_t		*ps;	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;	}	/*	** Search for members of session	*/	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 (ps->uid == 0)			continue;		if ((jobid = ps->session) == 0)			continue;		DBPRT(("%s[%d]: pid %d sid %d\n",		       id, njids, ps->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;	struct	dirent	*dent;	proc_stat_t	*ps;	int		i;	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;	}	/*	** Search for members of session	*/	rewinddir(pdir);	fmt = ret_string;	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;		}		DBPRT(("%s[%d]: pid: %s sid %d\n",		       id, num_pids, dent->d_name, ps->session))		if (jobid != ps->session)			continue;		sprintf(fmt, "%s ", dent->d_name);		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			j;	struct dirent		*dent;	proc_stat_t		*ps;	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;	}	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 ((uid = ps->uid) == 0)			continue;		DBPRT(("%s[%d]: pid %d uid %d\n",		       id, nuids, ps->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	*totmem(attrib)struct	rm_attribute	*attrib;{	char	*id = "totmem";	proc_mem_t	*mm;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((mm = get_proc_mem()) == NULL) {		log_err(errno, id, "get_proc_mem");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	DBPRT(("%s: total mem=%u\n", id, mm->total))	sprintf(ret_string, "%lukb", (ulong)mm->total >> 10); /* KB */	return ret_string;}static char	*availmem(attrib)struct	rm_attribute	*attrib;{	char	*id = "availmem";	proc_mem_t	*mm;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((mm = get_proc_mem()) == NULL) {		log_err(errno, id, "get_proc_mem");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	DBPRT(("%s: free mem=%u\n", id, mm->free))	sprintf(ret_string, "%lukb", (ulong)mm->free >> 10); /* KB */	return ret_string;}static char	*ncpus(attrib)struct	rm_attribute	*attrib;{	char		*id = "ncpus", label[128];	FILE		*fp;	int		procs;	unsigned int	pmem;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)		return(NULL);	procs = 0;	while (!feof(fp))  {		fscanf(fp, "%s %*[^\n]%*c", label);		if (strcmp("processor", label) == 0)			procs++;	}				sprintf(ret_string, "%d", procs);	fclose(fp);	return ret_string;}static char	*physmem(attrib)struct	rm_attribute	*attrib;{	char		*id = "physmem";	struct	stat	buf;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (stat("/proc/kcore", &buf))  {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_SYSTEM;		return NULL;	}  else  {		/* length of /proc/kcore == physical memory + 4K */		sprintf(ret_string, "%lukb",			((ulong)buf.st_size - (ulong)4096) >> 10); /* KB */		return ret_string;	}}char	*size_fs(param)char	*param;{	char		*id = "size_fs";	FILE		*mf;	struct	mntent	*mp;	struct	statfs	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 (statfs(param, &fsbuf) == -1) {		log_err(errno, id, "statfs");		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	sprintf(ret_string, "%lukb",		(ulong)(((double)fsbuf.f_bsize * (double)fsbuf.f_bfree) / 1024.0)); /* KB */	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")) == 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) {		if (maxtm >= curtm)			break;		if (strncmp(de->d_name, "tty", 3))			continue;		sprintf(ttyname, "/dev/%s", de->d_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			value, job, found = 0;	time_t			now, start;	proc_stat_t		*ps;	struct dirent		*dent;	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;	}	start = now;	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 (job) {			if (value != ps->session)				continue;		}		else {			if (value != ps->pid)				continue;		}		found = 1;		start = MIN(start, ps->start_time);	}	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;{	FILE	*fp;	char	*id = "get_la";	float	load;        if ((fp = fopen("/proc/loadavg", "r")) == NULL)                return(rm_errno = RM_ERR_SYSTEM);	if (fscanf(fp, "%f", &load) != 1) {		log_err(errno, id, "fscanf of load in /proc/loadavg");		(void) fclose(fp);		return (rm_errno = RM_ERR_SYSTEM);	}	*rv = (double)load;	(void) fclose(fp);	return 0;}u_longgracetime(secs)u_long	secs;{	time_t	now = time((time_t *)NULL);	if (secs > now)		/* time is in the future */		return (secs - now);	else		return 0;}

⌨️ 快捷键说明

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