staticvoid cpu_stat_print(void)
{ int i, j; char state_str[sizeof("cstate-9")]; struct cpu_stat_data *data;
/* Clear screen */
printf("\033[2J");
/* Header */
printf("\nCPU states statistics:\n");
printf("%-10s ", "state(ms)");
for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
sprintf(state_str, "cstate-%d", i);
printf("%-11s ", state_str);
}
for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
sprintf(state_str, "pstate-%d", i);
printf("%-11s ", state_str);
}
printf("\n");
for (j = 0; j < MAX_CPU; j++) {
data = &stat_data[j];
printf("CPU-%-6d ", j); for (i = 0; i < MAX_CSTATE_ENTRIES; i++)
printf("%-11lu ", data->cstate[i] / 1000000);
for (i = 0; i < MAX_PSTATE_ENTRIES; i++)
printf("%-11lu ", data->pstate[i] / 1000000);
printf("\n");
}
}
staticvoid cpu_stat_update(int cstate_fd, int pstate_fd)
{ unsignedlong key, value; int c, i;
for (c = 0; c < MAX_CPU; c++) { for (i = 0; i < MAX_CSTATE_ENTRIES; i++) {
key = c * MAX_CSTATE_ENTRIES + i;
bpf_map_lookup_elem(cstate_fd, &key, &value);
stat_data[c].cstate[i] = value;
}
for (i = 0; i < MAX_PSTATE_ENTRIES; i++) {
key = c * MAX_PSTATE_ENTRIES + i;
bpf_map_lookup_elem(pstate_fd, &key, &value);
stat_data[c].pstate[i] = value;
}
}
}
/* * This function is copied from 'idlestat' tool function * idlestat_wake_all() in idlestate.c. * * It sets the self running task affinity to cpus one by one so can wake up * the specific CPU to handle scheduling; this results in all cpus can be * waken up once and produce ftrace event 'trace_cpu_idle'.
*/ staticint cpu_stat_inject_cpu_idle_event(void)
{ int rcpu, i, ret;
cpu_set_t cpumask;
cpu_set_t original_cpumask;
ret = sysconf(_SC_NPROCESSORS_CONF); if (ret < 0) return -1;
rcpu = sched_getcpu(); if (rcpu < 0) return -1;
/* Keep track of the CPUs we will run on */
sched_getaffinity(0, sizeof(original_cpumask), &original_cpumask);
for (i = 0; i < ret; i++) {
/* Pointless to wake up ourself */ if (i == rcpu) continue;
/* Pointless to wake CPUs we will not run on */ if (!CPU_ISSET(i, &original_cpumask)) continue;
/* Enable all the CPUs of the original mask */
sched_setaffinity(0, sizeof(original_cpumask), &original_cpumask); return 0;
}
/* * It's possible to have no any frequency change for long time and cannot * get ftrace event 'trace_cpu_frequency' for long period, this introduces * big deviation for pstate statistics. * * To solve this issue, below code forces to set 'scaling_max_freq' to 208MHz * for triggering ftrace event 'trace_cpu_frequency' and then recovery back to * the maximum frequency value 1.2GHz.
*/ staticint cpu_stat_inject_cpu_frequency_event(void)
{ int len, fd;
fd = open(CPUFREQ_MAX_SYSFS_PATH, O_WRONLY); if (fd < 0) {
printf("failed to open scaling_max_freq, errno=%d\n", errno); return fd;
}
len = write(fd, CPUFREQ_LOWEST_FREQ, strlen(CPUFREQ_LOWEST_FREQ)); if (len < 0) {
printf("failed to open scaling_max_freq, errno=%d\n", errno); goto err;
}
len = write(fd, CPUFREQ_HIGHEST_FREQ, strlen(CPUFREQ_HIGHEST_FREQ)); if (len < 0) {
printf("failed to open scaling_max_freq, errno=%d\n", errno); goto err;
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.5Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.