mom_mach.c

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

C
2,037
字号
		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	*ncpus(attrib)struct rm_attribute	*attrib;{	if (attrib) {		log_err(-1, "ncpus", extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	sprintf(ret_string, "%d", nncpus);	return ret_string;}static char	*resi_job(jobid)pid_t	jobid;{	char			*id = "resi_job";	int			i, nproc;	int			resisize;	int			found = 0;	if ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	resisize = 0;	for (i=0; i<nproc; i++) {		struct	procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if (jobid != pp->pi_sid)			continue;		found = 1;		if (pp->pi_state == SZOMB)			continue;		resisize += pp->pi_drss + pp->pi_trss;		DBPRT(("%s: pid=%d size=%d\n", id, pp->pi_pid, resisize))	}	if (found) {		sprintf(ret_string, "%ukb", ctob(resisize) >> 10);  /* KB */		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}static char	*resi_proc(pid)pid_t	pid;{	char			*id = "resi_proc";	int			i, nproc;	int			resisize;	int			found = 0;	if ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	resisize = 0;	for (i=0; i<nproc; i++) {		struct	procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if (pid != pp->pi_pid)			continue;		found = 1;		if (pp->pi_state == SZOMB)			break;		resisize = pp->pi_drss + pp->pi_trss;		DBPRT(("%s: pid=%d size=%d\n", id, pp->pi_pid, resisize))		break;	}	if (found) {		sprintf(ret_string, "%ukb", ctob(resisize) >> 10); /* KB */		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}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			nproc;	int			i, j;	char			*fmt;	int			njids = 0;	pid_t			*jids, jobid;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if ((jids = calloc(nproc, sizeof(pid_t))) == NULL) {		log_err(errno, id, "no memory");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	/*	** Search for session	*/	for (i=0; i<nproc; i++) {		register struct procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if (pp->pi_suid == 0)			continue;		jobid = pp->pi_sid;		if (jobid == 0)			continue;		DBPRT(("%s: pid %d sid %u\n", id, pp->pi_pid, jobid))		for (j=0; j<njids; j++) {			if (jids[j] == jobid)				break;		}		if (j == njids)			/* not found */			jids[njids++] = jobid;	/* so add it to list */	}	fmt = ret_string;	for (j=0; j<njids; j++) {		checkret(&fmt, 100);		sprintf(fmt, " %d", 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			nproc;	int			i;	char			*fmt;	int			num_pids = 0;	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 ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	/*	** Search for members of session	*/	fmt = ret_string;	for (i=0; i<nproc; i++) {		register struct procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if (jobid != pp->pi_sid)			continue;		checkret(&fmt, 100);		sprintf(fmt, " %d", pp->pi_pid);		fmt += strlen(fmt);		num_pids++;		DBPRT(("%s[%d]: pid %d sid %u\n",		       id, num_pids, pp->pi_pid, pp->pi_sid))	}	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			nproc;	int			i, j;	int			nuids = 0;	uid_t			*uids, uid;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if ((uids = calloc(nproc, sizeof(uid_t))) == NULL) {		log_err(errno, id, "no memory");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	for (i=0; i<nproc; i++) {		register struct procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if ((uid = pp->pi_suid) == 0)			continue;		DBPRT(("%s: pid %d uid %u\n", id, pp->pi_pid, uid))		for (j=0; j<nuids; j++) {			if (uids[j] == uid)				break;		}		if (j == nuids)			/* not found */			uids[nuids++] = uid;	/* so add it to list */	}	sprintf(ret_string, "%d", nuids);	free(uids);	return ret_string;}uint	swap_free;uint	swap_size;intgetswap(id)char	*id;{	static	unsigned	int	lastai = 0;	int	i;	struct	pginfo	pginfo;	if (lastai == reqnum)	/* already have anoninfo */		return 0;	swap_free = swap_size = 0;	for (i=0; swap_dev[i]; i++) {		if (swapqry(swap_dev[i], &pginfo) == -1) {			log_err(errno, id, swap_dev[i]);			continue;		}		swap_free += pginfo.free;		swap_size += pginfo.size;	}	lastai = reqnum;	return 0;}static char	*totmem(attrib)struct	rm_attribute	*attrib;{	char	*id = "totmem";	ulong	tmem;	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (getswap(id))		return NULL;	sprintf(ret_string, "%lukb", (swap_size * page_size) >> 10); /* KB */	return ret_string;}static char	*availmem(attrib)struct	rm_attribute	*attrib;{	char	*id = "availmem";	if (attrib) {		log_err(-1, id, extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if (getswap(id))		return NULL;	sprintf(ret_string, "%lukb", (swap_free * page_size) >> 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",			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;	}	/* in KB */	sprintf(ret_string, "%lukb", (unsigned long)(((double)fsbuf.f_bsize * (double)fsbuf.f_bavail) / 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",			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) {		DBPRT(("%s: prev %d curr %d\n", dev, maxtm, sb.st_atime))		maxtm = sb.st_atime;	}	return;}intinteresting(name)    char	*name;{	static	char	*list[] = {		"kbd",		"mouse",		"pty",		"tty",		NULL	};	char	**el;	if (name == NULL || *name == '.')		return 0;	for (el=list; *el; el++) {		if (strncmp(name, *el, strlen(*el)) == 0)			return 1;	}	return 0;}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);	while ((de=readdir(dp)) != NULL) {		if (maxtm >= curtm)			break;		if (!interesting(de->d_name))			continue;		sprintf(ttyname, "/dev/%s", de->d_name);		setmax(ttyname);	}	closedir(dp);	if ((maxtm < curtm) &&	    ((dp=opendir("/dev/pts")) != NULL)) {		while ((de=readdir(dp)) != NULL) {			if (maxtm >= curtm)				break;			if (de->d_name[0] == '.')				continue;			sprintf(ttyname, "/dev/pts/%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";	pid_t			value;	int			i, nproc, job, found = 0;	time_t			now, start;	if (attrib == NULL) {		log_err(-1, id, no_parm);		rm_errno = RM_ERR_NOPARAM;		return NULL;	}	if ((value = (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, "proc") == 0)		job = 0;	else if (strcmp(attrib->a_qualifier, "session") == 0)		job = 1;	else {		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	if ((nproc = getproctab()) == 0) {		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	if ((now = time(NULL)) <= 0) {		log_err(errno, id, "time");		rm_errno = RM_ERR_SYSTEM;		return NULL;	}	start = now;	for (i=0; i<nproc; i++) {		struct	procsinfo	*pp = &proc_tbl[i];		if (pp->pi_state == SNONE)			continue;		if (job) {			if (value != pp->pi_sid)				continue;		}		else {			if (value != pp->pi_pid)				continue;		}		found = 1;		DBPRT(("%s: pid %d start %d\n", id, pp->pi_pid, pp->pi_start))		start = MIN(start, pp->pi_start);	}	if (found) {		sprintf(ret_string, "%ld", (long)((double)(now - start) * wallfactor));		return ret_string;	}	rm_errno = RM_ERR_EXIST;	return NULL;}static char *physmem(attrib)struct  rm_attribute    *attrib;{	if (attrib) {		log_err(-1, "physmem", extra_parm);		rm_errno = RM_ERR_BADPARAM;		return NULL;	}	sprintf(ret_string, "%lukb", realmem);	return (ret_string);}#define	FSCALE	(1<<16)intget_la(rv)	double	*rv;{	char	*id = "get_la";	long	load;	if (kd == NULL) {		log_err(-1, id, nokernel);		return (rm_errno = RM_ERR_SYSTEM);	}	if (kvm_read(kd, nl[KSYM_LOAD].n_value, (char *)&load,		     sizeof(load)) != sizeof(load)) {		log_err(errno, id, "kvm_read");		return (rm_errno = RM_ERR_SYSTEM);	}	*rv = (double)load/FSCALE;	return 0;}

⌨️ 快捷键说明

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