Exemplo n.º 1
0
static void server_loop(int fd)
{
	struct timeval snaptime = { 0 };
	struct pollfd p;

	p.fd = fd;
	p.events = p.revents = POLLIN;

	sprintf(info_source, "%d.%lu sampling_interval=%d time_const=%d",
		getpid(), (unsigned long)random(), scan_interval/1000, time_constant/1000);

	load_info();

	for (;;) {
		int status;
		time_t tdiff;
		struct timeval now;

		gettimeofday(&now, NULL);
		tdiff = T_DIFF(now, snaptime);
		if (tdiff >= scan_interval) {
			update_db(tdiff);
			snaptime = now;
			tdiff = 0;
		}

		if (poll(&p, 1, scan_interval - tdiff) > 0
		    && (p.revents&POLLIN)) {
			int clnt = accept(fd, NULL, NULL);

			if (clnt >= 0) {
				pid_t pid;

				if (children >= 5) {
					close(clnt);
				} else if ((pid = fork()) != 0) {
					if (pid > 0)
						children++;
					close(clnt);
				} else {
					FILE *fp = fdopen(clnt, "w");

					if (fp)
						dump_raw_db(fp, 0);
					exit(0);
				}
			}
		}
		while (children && waitpid(-1, &status, WNOHANG) > 0)
			children--;
	}
}
Exemplo n.º 2
0
static void server_loop(int fd)
{
	struct timeval snaptime;
	struct pollfd p;
	
	memset(&snaptime, 0, sizeof(snaptime));
	
	p.fd = fd;
	p.events = p.revents = POLLIN;

	load_info();

	for (;;) {
		int status;
		int tdiff;
		struct timeval now;

		gettimeofday(&now, NULL);
		tdiff = T_DIFF(now, snaptime);

//		if (tdiff >= 0) { 
			update_db(tdiff);
			snaptime = now;
			tdiff = 0;
//		}
		if (poll(&p, 1, conf.scan_interval-tdiff) > 0
		    && (p.revents&POLLIN)) {
			int clnt = accept(fd, NULL, NULL);

			if (clnt >= 0) {
				pid_t pid;

				/*
				  We assume forking will be ok
				  so update database here not
				  have races with forked process
				*/

				gettimeofday(&now, NULL);
				tdiff = T_DIFF(now, snaptime);
//				if (tdiff >= min_interval) {
					update_db(tdiff);
					snaptime = now;
					tdiff = 0;
//				}
				poll_client(clnt);
				
				sprintf(info_source,
					"pid=%d sampling_interval=%d "
					"time_const=%d",
					getpid(),
					conf.scan_interval/1000,
					conf.time_constant/1000);

				if (children >= 5) {
					close(clnt);
				} else if ((pid = fork()) != 0) {

					if (pid>0) 
						children++;
					close(clnt);
				} else {
					FILE *fp = fdopen(clnt, "w");
					if (fp) {
						/* Write on clients socket */
						dump_raw_db(fp);
					}
					exit(0);
				}
			}
		}
		while (children && waitpid(-1, &status, WNOHANG) > 0)
			children--;
	}
}
Exemplo n.º 3
0
void diff_taskstats(struct taskstats* res, struct taskstats* t1, struct taskstats* t2)
{
#define T_DIFF(name)    res->name = (t1->name - t2->name)
#define T_ASSIGN(name)  res->name = t1->name
    T_DIFF(cpu_delay_total);
    T_DIFF(cpu_run_real_total);
    T_DIFF(ac_utime);
    T_DIFF(ac_stime);
    T_DIFF(read_char);
    T_DIFF(write_char);
    T_DIFF(read_syscalls);
    T_DIFF(write_syscalls);
    T_DIFF(read_bytes);
    T_DIFF(write_bytes);
    T_DIFF(cancelled_write_bytes);
    T_DIFF(cpu_delay_total);
    T_DIFF(blkio_delay_total);
    T_DIFF(swapin_delay_total);
    T_DIFF(freepages_delay_total);
    T_DIFF(nvcsw);
    T_ASSIGN(ac_pid);
    T_ASSIGN(ac_ppid);
    T_ASSIGN(ac_btime);
    res->ac_etime = t1->ac_etime;
    memcpy(&res->ac_comm, &t1->ac_comm, sizeof(t1->ac_comm));
}