Exemplo n.º 1
0
void
settimes()
{
#ifdef BSDTIMES
    struct sysrusage ruch;
#ifdef convex
    memset(&ru0, 0, sizeof(ru0));
    memset(&ruch, 0, sizeof(ruch));
#endif /* convex */

    (void) gettimeofday(&time0,	NULL);
    (void) getrusage(RUSAGE_SELF, (struct rusage *) &ru0);
    (void) getrusage(RUSAGE_CHILDREN, (struct rusage *) &ruch);
    ruadd(&ru0,	&ruch);
#else
# ifdef	_SEQUENT_
    struct process_stats ruch;

    (void) get_process_stats(&time0, PS_SELF, &ru0, &ruch);
    ruadd(&ru0,	&ruch);
# else	/* _SEQUENT_ */
#  ifndef COHERENT
    time0 = times(&times0);
#  else	/* !COHERENT */
    time0 = HZ * time(NULL);
    times(&times0);
#  endif /* !COHERENT */
    times0.tms_stime +=	times0.tms_cstime;
    times0.tms_utime +=	times0.tms_cutime;
    times0.tms_cstime =	0;
    times0.tms_cutime =	0;
# endif	/* _SEQUENT_ */
#endif /* BSDTIMES */
}
Exemplo n.º 2
0
int tee_status_handler(request_rec *r, int flags)
{
	global_snapshot *global_snap = get_global_stats(r->pool, r);
	if (flags & AP_STATUS_SHORT) {
		write_stats_auto(r, global_snap);
	} else {
		process_snapshot **process_snap_array = get_process_stats(r->pool);
		write_stats_full(r, global_snap, process_snap_array);
	}
	return OK;
}
Exemplo n.º 3
0
int
gettimeofday(
	struct timeval *tvp
	)
{
	/*
	 * hi, this is Sequents sneak path to get to a clock
	 * this is also the most logical syscall for such a function
	 */
	return (get_process_stats(tvp, PS_SELF, (struct procstats *) 0,
				  (struct procstats *) 0));
}
Exemplo n.º 4
0
void
log_process_stats(void)
{
	int       stat;
	timeval_t tv;
	struct process_stats myproc, mychildren;

	emit(ERRLOC, POLL_STATS_SET1);
	emit(ERRLOC, POLL_STATS_SET2);
	emit(ERRLOC, POLL_STATS_SET1);

	stat = get_process_stats( &tv, PS_SELF, &myproc, &mychildren);
	if ( stat == -1 )
	   return;

	emit(ERRLOC, POLL_STATS_SET3, 
	     myproc.ps_utime.tv_sec, myproc.ps_utime.tv_usec);
	emit(ERRLOC, POLL_STATS_SET4,
	     myproc.ps_stime.tv_sec, myproc.ps_stime.tv_usec);
	emit(ERRLOC, POLL_STATS_SET5, myproc.ps_maxrss);
	emit(ERRLOC, POLL_STATS_SET6, myproc.ps_pagein);
	emit(ERRLOC, POLL_STATS_SET7, myproc.ps_reclaim);
	emit(ERRLOC, POLL_STATS_SET8, myproc.ps_zerofill);
	emit(ERRLOC, POLL_STATS_SET9, myproc.ps_pffincr);
	emit(ERRLOC, POLL_STATS_SET10, myproc.ps_pffdecr);
	emit(ERRLOC, POLL_STATS_SET11, myproc.ps_swap);
	emit(ERRLOC, POLL_STATS_SET12, myproc.ps_syscall);
	emit(ERRLOC, POLL_STATS_SET13, myproc.ps_volcsw);
	emit(ERRLOC, POLL_STATS_SET14, myproc.ps_involcsw);
	emit(ERRLOC, POLL_STATS_SET15, myproc.ps_signal);
	emit(ERRLOC, POLL_STATS_SET16, myproc.ps_lread);
	emit(ERRLOC, POLL_STATS_SET17, myproc.ps_lwrite);
	emit(ERRLOC, POLL_STATS_SET18, myproc.ps_bread);
	emit(ERRLOC, POLL_STATS_SET19, myproc.ps_bwrite);
	emit(ERRLOC, POLL_STATS_SET20, myproc.ps_phread);
	emit(ERRLOC, POLL_STATS_SET21, myproc.ps_phwrite);
	emit(ERRLOC, POLL_STATS_SET1);
}
Exemplo n.º 5
0
void
f_rusage (void)
{
    struct process_stats ps;
    mapping_t *m;
    int utime, stime, maxrss;

    if (get_process_stats(NULL, PS_SELF, &ps, NULL) == -1)
        m = allocate_mapping(0);
    else {
        utime = ps.ps_utime.tv_sec * 1000 + ps.ps_utime.tv_usec / 1000;
        stime = ps.ps_stime.tv_sec * 1000 + ps.ps_stime.tv_usec / 1000;
        maxrss = ps.ps_maxrss * getpagesize() / 1024;

        m = allocate_mapping(19);
        add_mapping_pair(m, "utime", utime);
        add_mapping_pair(m, "stime", stime);
        add_mapping_pair(m, "maxrss", maxrss);
        add_mapping_pair(m, "pagein", ps.ps_pagein);
        add_mapping_pair(m, "reclaim", ps.ps_reclaim);
        add_mapping_pair(m, "zerofill", ps.ps_zerofill);
        add_mapping_pair(m, "pffincr", ps.ps_pffincr);
        add_mapping_pair(m, "pffdecr", ps.ps_pffdecr);
        add_mapping_pair(m, "swap", ps.ps_swap);
        add_mapping_pair(m, "syscall", ps.ps_syscall);
        add_mapping_pair(m, "volcsw", ps.ps_volcsw);
        add_mapping_pair(m, "involcsw", ps.ps_involcsw);
        add_mapping_pair(m, "signal", ps.ps_signal);
        add_mapping_pair(m, "lread", ps.ps_lread);
        add_mapping_pair(m, "lwrite", ps.ps_lwrite);
        add_mapping_pair(m, "bread", ps.ps_bread);
        add_mapping_pair(m, "bwrite", ps.ps_bwrite);
        add_mapping_pair(m, "phread", ps.ps_phread);
        add_mapping_pair(m, "phwrite", ps.ps_phwrite);
    }
    push_refed_mapping(m);
}
Exemplo n.º 6
0
int
gettimeofday (struct timeval *tp, void *tzp)
{
    return get_process_stats (tp, PS_SELF, 0, 0);
}
Exemplo n.º 7
0
/*
 * Find the process with the largest oom_score and kill it.
 * See trigger_kernel_oom() for the reason why this is done in userspace.
 */
static void userspace_kill(DIR *procdir, int sig, int ignore_oom_score_adj)
{
	struct dirent * d;
	char buf[256];
	int pid;
	int victim_pid = 0;
	int victim_badness = 0;
	unsigned long victim_vm_rss = 0;
	char name[PATH_MAX];
	struct procinfo p;
	int badness;

	rewinddir(procdir);
	while(1)
	{
		errno = 0;
		d = readdir(procdir);
		if(d == NULL)
		{
			if(errno != 0)
				perror("readdir returned error");

			break;
		}

		// proc contains lots of directories not related to processes,
		// skip them
		if(!isnumeric(d->d_name))
			continue;

		pid = strtoul(d->d_name, NULL, 10);

		if(pid == 1)
			// Let's not kill init.
			continue;

		get_process_stats(pid, &p);

		if(p.exited == 1)
			// Process may have died in the meantime
			continue;

		badness = p.oom_score;
		if(ignore_oom_score_adj && p.oom_score_adj > 0)
			badness -= p.oom_score_adj;

		if(enable_debug)
			printf("pid %5d: badness %3d vm_rss %6lu\n", pid, badness, p.vm_rss);

		if(badness > victim_badness)
		{
			victim_pid = pid;
			victim_badness = badness;
			if(enable_debug)
				printf("    ^ new victim (higher badness)\n");
		} else if(badness == victim_badness && p.vm_rss > victim_vm_rss) {
			victim_pid = pid;
			victim_vm_rss = p.vm_rss;
			if(enable_debug)
				printf("    ^ new victim (higher vm_rss)\n");
		}
	}

	if(victim_pid == 0)
	{
		fprintf(stderr, "Error: Could not find a process to kill. Sleeping 10 seconds.\n");
		sleep(10);
		return;
	}

	name[0]=0;
	snprintf(buf, sizeof(buf), "%d/stat", victim_pid);
	FILE * stat = fopen(buf, "r");
	fscanf(stat, "%*d %s", name);
	fclose(stat);

	if(sig != 0)
		fprintf(stderr, "Killing process %d %s\n", victim_pid, name);

	if(kill(victim_pid, sig) != 0)
	{
		perror("Could not kill process");
		// Killing the process may have failed because we are not running as root.
		// In that case, trying again in 100ms will just yield the same error.
		// Throttle ourselves to not spam the log.
		fprintf(stderr, "Sleeping 10 seconds\n");
		sleep(10);
	}
}
Exemplo n.º 8
0
/*
 * Find the process with the largest oom_score and kill it.
 * See trigger_kernel_oom() for the reason why this is done in userspace.
 */
static void userspace_kill(DIR *procdir, int sig, int ignore_oom_score_adj)
{
	struct dirent * d;
	char buf[256];
	int pid;
	int victim_pid = 0;
	int victim_points = 0;
	char name[PATH_MAX];
	struct procinfo p;
	int badness;
	#define CMDLINE_MAX 250
	char cmdline[CMDLINE_MAX];
	int len;

	rewinddir(procdir);
	while(1)
	{
		d = readdir(procdir);
		if(d == NULL)
			break;

		if(!isnumeric(d->d_name))
			continue;

		pid = strtoul(d->d_name, NULL, 10);

		p = get_process_stats(pid);

		if(p.exited == 1)
			// Process may have died in the meantime
			continue;

		badness = p.oom_score;
		if(ignore_oom_score_adj && p.oom_score_adj > 0)
			badness -= p.oom_score_adj;

		if(enable_debug)
			printf("pid %5d: badness %3d\n", pid, badness);

		// We don't need to check this, but it is a good optimization, reducing the number of files that will be read.
		if(badness > victim_points)
		{
			// If the process is marked as excluded, then reduce its score.
			snprintf(buf, PATH_MAX, "%d/cmdline", pid);
			len = read_contents_of_file(buf, cmdline, CMDLINE_MAX-1);
			convert_nulls_to_spaces(cmdline, len);
			if (regexec(&excluded_cmdlines_regexp, cmdline, (size_t)0, NULL, 0) == 0)
			{
				//fprintf(stderr, "Process is EXCLUDED!  %i %s\n", pid, cmdline);
				badness /= 32;
			}
		}

		if(badness > victim_points)
		{
			victim_pid = pid;
			victim_points = badness;
			if(enable_debug)
				printf("    ^ new victim\n");
		}
	}

	if(victim_pid == 0)
	{
		fprintf(stderr, "Error: Could not find a process to kill\n");
		exit(9);
	}

	if (!enable_debug)
	{
		name[0]=0;
		snprintf(buf, sizeof(buf), "%d/stat", victim_pid);
		FILE * stat = fopen(buf, "r");
		fscanf(stat, "%*d %s", name);
		fclose(stat);
	} else {
		// In debug mode, instead of the process name, we display the whole cmdline (i.e. the name and the arguments passed).
		snprintf(buf, PATH_MAX, "%d/cmdline", victim_pid);
		len = read_contents_of_file(buf, name, PATH_MAX-1);
		convert_nulls_to_spaces(name, len);
	}

	if(sig != 0)
		fprintf(stderr, "Killing process %d %s\n", victim_pid, name);

	if(kill(victim_pid, sig) != 0)
		perror("Could not kill process");
}