mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-11-09 02:51:11 +01:00
Use a hashmap between a char* string and a double* value. While bpf's hashmap entries are size_t in size, we can't guarantee sizeof(size_t) >= sizeof(double). Avoid a memory allocation when gathering ids by making 0.0 a special value encoded as NULL. Original map suggestion by Andi Kleen: https://lore.kernel.org/lkml/[email protected]/ and seconded by Jiri Olsa: https://lore.kernel.org/lkml/20200423112915.GH1136647@krava/ Committer notes: There are fixes that need to land upstream before we can use libbpf's headers, for now use our copy unconditionally, since the data structures at this point are exactly the same, no problem. When the fixes for libbpf's hashmap land upstream, we can fix this up. Testing it: Building with LIBBPF=1, i.e. the default: $ perf -vv | grep -i bpf bpf: [ on ] # HAVE_LIBBPF_SUPPORT $ nm ~/bin/perf | grep -i libbpf_ | wc -l 39 $ nm ~/bin/perf | grep -i hashmap_ | wc -l 17 $ Explicitely building without LIBBPF: $ perf -vv | grep -i bpf bpf: [ OFF ] # HAVE_LIBBPF_SUPPORT $ $ nm ~/bin/perf | grep -i libbpf_ | wc -l 0 $ nm ~/bin/perf | grep -i hashmap_ | wc -l 9 $ Signed-off-by: Ian Rogers <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Cong Wang <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Jin Yao <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: John Fastabend <[email protected]> Cc: John Garry <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Kan Liang <[email protected]> Cc: Kim Phillips <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Song Liu <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Yonghong Song <[email protected]> Cc: [email protected] Cc: kp singh <[email protected]> Cc: [email protected] Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
111 lines
2.1 KiB
Plaintext
111 lines
2.1 KiB
Plaintext
/* Simple expression parser */
|
|
%{
|
|
#define YYDEBUG 1
|
|
#include <stdio.h>
|
|
#include "util.h"
|
|
#include "util/debug.h"
|
|
#include <stdlib.h> // strtod()
|
|
#define IN_EXPR_Y 1
|
|
#include "expr.h"
|
|
#include "smt.h"
|
|
#include <string.h>
|
|
|
|
%}
|
|
|
|
%define api.pure full
|
|
|
|
%parse-param { double *final_val }
|
|
%parse-param { struct expr_parse_ctx *ctx }
|
|
%parse-param {void *scanner}
|
|
%lex-param {void* scanner}
|
|
|
|
%union {
|
|
double num;
|
|
char *str;
|
|
}
|
|
|
|
%token EXPR_PARSE EXPR_OTHER EXPR_ERROR
|
|
%token <num> NUMBER
|
|
%token <str> ID
|
|
%destructor { free ($$); } <str>
|
|
%token MIN MAX IF ELSE SMT_ON
|
|
%left MIN MAX IF
|
|
%left '|'
|
|
%left '^'
|
|
%left '&'
|
|
%left '-' '+'
|
|
%left '*' '/' '%'
|
|
%left NEG NOT
|
|
%type <num> expr if_expr
|
|
|
|
%{
|
|
static void expr_error(double *final_val __maybe_unused,
|
|
struct expr_parse_ctx *ctx __maybe_unused,
|
|
void *scanner,
|
|
const char *s)
|
|
{
|
|
pr_debug("%s\n", s);
|
|
}
|
|
|
|
%}
|
|
%%
|
|
|
|
start:
|
|
EXPR_PARSE all_expr
|
|
|
|
|
EXPR_OTHER all_other
|
|
|
|
all_other: all_other other
|
|
|
|
|
|
|
other: ID
|
|
{
|
|
expr__add_id(ctx, $1, 0.0);
|
|
}
|
|
|
|
|
MIN | MAX | IF | ELSE | SMT_ON | NUMBER | '|' | '^' | '&' | '-' | '+' | '*' | '/' | '%' | '(' | ')' | ','
|
|
|
|
|
|
all_expr: if_expr { *final_val = $1; }
|
|
;
|
|
|
|
if_expr:
|
|
expr IF expr ELSE expr { $$ = $3 ? $1 : $5; }
|
|
| expr
|
|
;
|
|
|
|
expr: NUMBER
|
|
| ID { if (expr__get_id(ctx, $1, &$$)) {
|
|
pr_debug("%s not found\n", $1);
|
|
free($1);
|
|
YYABORT;
|
|
}
|
|
free($1);
|
|
}
|
|
| expr '|' expr { $$ = (long)$1 | (long)$3; }
|
|
| expr '&' expr { $$ = (long)$1 & (long)$3; }
|
|
| expr '^' expr { $$ = (long)$1 ^ (long)$3; }
|
|
| expr '+' expr { $$ = $1 + $3; }
|
|
| expr '-' expr { $$ = $1 - $3; }
|
|
| expr '*' expr { $$ = $1 * $3; }
|
|
| expr '/' expr { if ($3 == 0) {
|
|
pr_debug("division by zero\n");
|
|
YYABORT;
|
|
}
|
|
$$ = $1 / $3;
|
|
}
|
|
| expr '%' expr { if ((long)$3 == 0) {
|
|
pr_debug("division by zero\n");
|
|
YYABORT;
|
|
}
|
|
$$ = (long)$1 % (long)$3;
|
|
}
|
|
| '-' expr %prec NEG { $$ = -$2; }
|
|
| '(' if_expr ')' { $$ = $2; }
|
|
| MIN '(' expr ',' expr ')' { $$ = $3 < $5 ? $3 : $5; }
|
|
| MAX '(' expr ',' expr ')' { $$ = $3 > $5 ? $3 : $5; }
|
|
| SMT_ON { $$ = smt_on() > 0; }
|
|
;
|
|
|
|
%%
|