#define _XOPEN_SOURCE 500 /* needed for nftw() */
#define _GNU_SOURCE /* needed for asprintf() */
/* Parse event JSON files */
/*
* Copyright (c) 2014, Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <stdarg.h>
#include <libgen.h>
#include <dirent.h>
#include <sys/time.h> /* getrlimit */
#include <sys/resource.h> /* getrlimit */
#include <ftw.h>
#include <sys/stat.h>
#include "jsmn.h"
#include "json.h"
#include "jevents.h"
#ifndef __maybe_unused
#define __maybe_unused __attribute__((unused))
#endif
int verbose;
char *prog;
int eprintf(int level, int var, const char *fmt, ...)
{
int ret;
va_list args;
if (var < level)
return 0;
va_start(args, fmt);
ret = vfprintf(stderr, fmt, args);
va_end(args);
return ret;
}
__attribute__((weak)) char *get_cpu_str(void)
{
return NULL;
}
static void addfield(char *map, char **dst, const char *sep,
const char *a, jsmntok_t *bt)
{
unsigned int len = strlen(a) + 1 + strlen(sep);
int olen = *dst ? strlen(*dst) : 0;
int blen = bt ? json_len(bt) : 0;
char *out;
out = realloc(*dst, len + olen + blen);
if (!out) {
/* Don't add field in this case */
return;
}
*dst = out;
if (!olen)
*(*dst) = 0;
else
strcat(*dst, sep);
strcat(*dst, a);
if (bt)
strncat(*dst, map + bt->start, blen);
}
static void fixname(char *s)
{
for (; *s; s++)
*s = tolower(*s);
}
static void fixdesc(char *s)
{
char *e = s + strlen(s);
/* Remove trailing dots that look ugly in perf list */
--e;
while (e >= s && isspace(*e))
--e;
if (*e == '.')
*e = 0;
}
static struct msrmap {
const char *num;
const char *pname;
} msrmap[] = {
{ "0x3F6", "ldlat=" },
{ "0x1A6", "offcore_rsp=" },
{ "0x1A7", "offcore_rsp=" },
{ "0x3F7", "frontend=" },
{ NULL, NULL }
};
static struct field {
const char *field;
const char *kernel;
} fields[] = {
{ "UMask", "umask=" },
{ "CounterMask", "cmask=" },
{ "Invert", "inv=" },
{ "AnyThread", "any=" },
{ "EdgeDetect", "edge=" },
{ "SampleAfterValue", "period=" },
{ NULL, NULL }
};
static void cut_comma(char *map, jsmntok_t *newval)
{
int i;
/* Cut off everything after comma */
for (i = newval->start; i < newval->end; i++) {
if (map[i] == ',')
newval->end = i;
}
}
static int match_field(char *map, jsmntok_t *field, int nz,
char **event, jsmntok_t *val)
{
struct field *f;
jsmntok_t newval = *val;
for (f = fields; f->field; f++)
if (json_streq(map, field, f->field) && nz) {
cut_comma(map, &newval);
addfield(map, event, ",", f->kernel, &newval);
return 1;
}
return 0;
}
static struct msrmap *lookup_msr(char *map, jsmntok_t *val)
{
jsmntok_t newval = *val;
static bool warned;
int i;
cut