Example #1
0
/*
 ***************************************************************************
 * Read CPU frequency statistics.
 *
 * IN:
 * @a	Activity structure.
 *
 * OUT:
 * @a	Activity structure with statistics.
 ***************************************************************************
 */
__read_funct_t wrap_read_cpuinfo(struct activity *a)
{
	struct stats_pwr_cpufreq *st_pwr_cpufreq
		= (struct stats_pwr_cpufreq *) a->_buf0;

	/* Read CPU frequency stats */
	read_cpuinfo(st_pwr_cpufreq, a->nr);

	return;
}
int copy_cpu_stats(perf_stats* stats){
  // For now we only handle 1 cpu. This needs to be fixed in the future.
  uint64_t numCPUs = 1;
  cpu_info* cpuInfo = NULL;
  if ((cpuInfo = read_cpuinfo(&numCPUs)) == 0) {
    printf("Error reading cpu information.\n");
    return -1;
  }
  /* TODO, copy cpuInfo to the stats variable. */
  stats->numCPUs = numCPUs;
  stats->cpuStructs = (void*)cpuInfo;
  return 0;
}
Example #3
0
static void * st_cb(void *arg)
{
	int num = 0;
	st_ctx_p ctx = (st_ctx_p)arg;
	pkt_cap_ctx_p p_snap = NULL;
	transfer *sender = ctx->sender;
	int log_size = 10240, log_len = 0;
	char *log;

	struct timeval now;
	cpu_set_t mask;
	int cpu_fd = 0; // read from /proc/stat
	int mem_fd = 0; // read from /proc/meminfo

	MEM_ALLOC(log, char*, log_size, NULL);

	if (ctx->id >= 0) {
		CPU_ZERO(&mask);
		CPU_SET(ctx->id, &mask);
		if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
			fprintf(stderr, "%s pthread_setaffinity_np on %d failed!\n", __FUNCTION__, ctx->id);
			return NULL;
		}
	}

	p_snap = pkt_cap_ctx_snap(ctx->p_cap_ctx, ctx->num, NULL);

	sleep(5);

	while (1) {
		int all_stop = 1;
		for (int i = 0; i < ctx->num; i++) {
			if (pkt_st_vary(&p_snap[i].p_pkt_info[PKT_PKT], &ctx->p_cap_ctx[i].p_pkt_info[PKT_PKT])) {
				all_stop = 0;
				break;
			}
		}
		cpu_fd = open("/proc/stat", O_RDONLY, S_IRUSR);
		if (cpu_fd < 0) {
			fprintf(stderr, "read [%s] failed! [%d,%s]\n", "proc/stat", errno, strerror(errno));
			return NULL;
		}
		mem_fd = open("/proc/meminfo", O_RDONLY, S_IRUSR);
		if (cpu_fd < 0) {
			fprintf(stderr, "read [%s] failed! [%d,%s]\n", "proc/meminfo", errno, strerror(errno));
			return NULL;
		}

		// send log to log-server
		gettimeofday(&now, NULL);
		log_len = pkt_cap_ctx_log(ctx->p_cap_ctx, ctx->num, log, log_size, now);
		if (log_len > 0) sender->nsend(log, log_len, NULL, -1);
		log_len = read_cpuinfo(cpu_fd, log, log_size, now);
		if (log_len > 0) sender->nsend(log, log_len, NULL, -1);
		else fprintf(stderr, "read_cpuinfo %d\n", log_len);
		log_len = read_meminfo(mem_fd, log, log_size, now);
		if (log_len > 0) sender->nsend(log, log_len, NULL, -1);
		else fprintf(stderr, "read_meminfo %d\n", log_len);

		if (all_stop) {
			fprintf(stdout, "pkt_cap_ctx_clear: clear pkt statis info\n");
			pkt_cap_ctx_clear(p_snap, p_snap->num);	
			pkt_cap_ctx_clear(ctx->p_cap_ctx, ctx->num);
		} else {
			pkt_cap_ctx_snap(ctx->p_cap_ctx, ctx->num, p_snap);
		}
		close(mem_fd);
		close(cpu_fd);

		sleep(ctx->chk_interval);
	}

	return NULL;
}