1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* tracefile_idlestat.c
*
* Copyright (C) 2014, Linaro Limited.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Based on code migrated from idlestat.c
*
* Contributors:
* Tuukka Tikkanen <tuukka.tikkanen@linaro.org>
*/
#include "topology.h"
#include "trace_ops.h"
#include "utils.h"
#include "idlestat.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include <float.h>
#define TRACE_FORMAT "%*[^]]] %*s %lf:%*[^=]=%u%*[^=]=%d"
/**
* load_and_build_cstate_info - load c-state info written to idlestat
* trace file.
*
* @f: the file handle of the idlestat trace file
* @nrcpus: number of CPUs
*
* @return: per-CPU array of structs (success) or ptrerror() (error)
*/
static struct cpuidle_cstates *load_and_build_cstate_info(FILE* f, char *buffer, int nrcpus, struct cpu_topology * topo)
{
int cpu;
struct cpuidle_cstates *cstates;
assert(f != NULL);
assert(buffer != NULL);
assert(nrcpus > 0);
cstates = calloc(nrcpus, sizeof(*cstates));
if (!cstates)
return ptrerror(__func__);
for (cpu = 0; cpu < nrcpus; cpu++) {
int i, read_cpu;
struct cpuidle_cstate *c;
cstates[cpu].cstate_max = -1;
cstates[cpu].current_cstate = -1;
if (!cpu_is_online(topo, cpu))
continue;
if (sscanf(buffer, "cpuid %d:\n", &read_cpu) != 1 ||
read_cpu != cpu) {
release_cstate_info(cstates, cpu);
fprintf(stderr,
"%s: Error reading trace file\n"
"Expected: cpuid %d:\n"
"Read: %s",
__func__, cpu, buffer);
return ptrerror(NULL);
}
for (i = 0; i < MAXCSTATE; i++) {
int residency = 0;
char *name = calloc(sizeof(*name), 128);
if (!name) {
release_cstate_info(cstates, cpu);
return ptrerror(__func__);
}
if (fgets(buffer, BUFSIZE, f) != NULL)
sscanf(buffer, "\t%s\n", name);
if (fgets(buffer, BUFSIZE, f) != NULL)
sscanf(buffer, "\t%d\n", &residency);
c = &(cstates[cpu].cstate[i]);
if ((*name = '\0') || !strcmp(name, "(null)")) {
free(name);
c->name = NULL;
} else {
c->name = name;
}
c->data = NULL;
c->nrdata = 0;
c->early_wakings = 0;
c->late_wakings = 0;
c->avg_time = 0.;
c->max_time = 0.;
c->min_time = DBL_MAX;
c->duration = 0.;
c->target_residency = residency;
}
(void)fgets(buffer, BUFSIZE, f);
}
return cstates;
}
int load_text_data_line(char *buffer, struct cpuidle_datas *datas, char *format, double *begin, double *end, size_t *start)
{
unsigned int state, freq, cpu;
double time;
if (strstr(buffer, "cpu_idle")) {
if (sscanf(buffer, format, &time, &state, &cpu)
!= 3) {
fprintf(stderr, "warning: Unrecognized cpuidle "
"record. The result of analysis might "
"be wrong.\n");
return -1;
}
if (*start) {
*begin = time;
*start = 0;
}
*end = time;
return store_data(time, state, cpu, datas);
}
if (strstr(buffer, "cpu_frequency")) {
if (sscanf(buffer, format, &time, &freq, &cpu) != 3) {
fprintf(stderr, "warning: Unrecognized cpufreq "
"record. The result of analysis might "
"be wrong.\n");
return -1;
}
return cpu_change_pstate(datas, cpu, freq, time);
}
return get_wakeup_irq(datas, buffer);
}
void load_text_data_lines(FILE *f, char *buffer, struct cpuidle_datas *datas)
{
double begin = 0, end = 0;
size_t count = 0, start = 1;
setup_topo_states(datas);
do {
if (load_text_data_line(buffer, datas, TRACE_FORMAT,
&begin, &end, &start) != -1) {
count++;
}
} while (fgets(buffer, BUFSIZE, f));
fprintf(stderr, "Log is %lf secs long with %zu events\n",
end - begin, count);
}
static int idlestat_magic(const char *filename)
{
FILE *f;
char *line;
char buffer[BUFSIZE];
f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "%s: failed to open '%s': %m\n", __func__,
filename);
return -1;
}
line = fgets(buffer, BUFSIZE, f);
fclose(f);
return (line != NULL) && !strncmp(buffer, "idlestat version", 16);
}
static struct cpuidle_datas * idlestat_native_load(const char *filename)
{
FILE *f;
unsigned int nrcpus;
struct cpuidle_datas *datas;
char *line;
char buffer[BUFSIZE];
f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "%s: failed to open '%s': %m\n", __func__,
filename);
return ptrerror(NULL);
}
/* Version line */
line = fgets(buffer, BUFSIZE, f);
if (!line)
goto error_close;
/* Number of CPUs */
line = fgets(buffer, BUFSIZE, f);
if (!line)
goto error_close;
if (sscanf(buffer, "cpus=%u", &nrcpus) != 1 || nrcpus == 0) {
fclose(f);
return ptrerror("Cannot load trace file (nrcpus == 0)");
}
line = fgets(buffer, BUFSIZE, f);
if (!line)
goto error_close;
datas = calloc(sizeof(*datas), 1);
if (!datas) {
fclose(f);
return ptrerror(__func__);
}
datas->nrcpus = nrcpus;
datas->pstates = build_pstate_info(nrcpus);
if (!datas->pstates)
goto propagate_error_free_datas;
/* Read topology information */
datas->topo = read_cpu_topo_info(f, buffer);
if (is_err(datas->topo))
goto propagate_error_free_datas;
/* Read C-state information */
datas->cstates = load_and_build_cstate_info(f, buffer,
nrcpus, datas->topo);
if (is_err(datas->cstates))
goto propagate_error_free_datas;
load_text_data_lines(f, buffer, datas);
fclose(f);
return datas;
propagate_error_free_datas:
fclose(f);
if (!is_err(datas->topo))
release_cpu_topo_info(datas->topo);
if (!is_err(datas->cstates))
release_cstate_info(datas->cstates, nrcpus);
free(datas);
return ptrerror(NULL);
error_close:
fclose(f);
fprintf(stderr, "%s: error or EOF while reading '%s': %m",
__func__, filename);
return ptrerror(NULL);
}
static const struct trace_ops idlestat_trace_ops = {
.name = "Idlestat native",
.check_magic = idlestat_magic,
.load = idlestat_native_load
};
EXPORT_TRACE_OPS(idlestat);
|