@@ -125,6 +125,7 @@ libperf-$(CONFIG_DWARF) += genelf_debug.o
endif
libperf-y += perf-hooks.o
+libperf-y += jit-helpers.o
libperf-$(CONFIG_CXX) += c++/
@@ -38,6 +38,7 @@
#include "llvm-utils.h"
#include "util-cxx.h"
#include "perf-hooks.h"
+#include "jit-helpers.h"
namespace perf {
@@ -199,12 +200,18 @@ PerfModule::toBPFObject(void)
return std::move(Buffer);
}
+#define __stringify_1(x) #x
+#define __stringify(x) __stringify_1(x)
static std::map<const std::string, const void *> exported_funcs =
{
-#define EXPORT(f) {#f, (const void *)&f}
+#define EXPORT(f) {__stringify(f), (const void *)&f}
EXPORT(test__clang_callback),
EXPORT(printf),
EXPORT(puts),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_update_elem)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_lookup_elem)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_get_next_key)),
+ EXPORT(JIT_HELPER_FUNC_NAME(map_pin)),
#undef EXPORT
};
new file mode 100644
@@ -0,0 +1,57 @@
+/*
+ * jit-helper.c
+ *
+ * Copyright (C) 2016 Wang Nan <wangnan0@huawei.com>
+ * Copyright (C) 2016 Huawei Inc.
+ *
+ * Provide helpers which can be invoked by jit scripts attached to
+ * perf hooks.
+ */
+
+#include <util/jit-helpers.h>
+#include <util/bpf-loader.h>
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+
+#include "asm/bug.h"
+
+static int get_bpf_map_fd(struct bpf_object *obj, void *map)
+{
+ int fd;
+ char errbuf[BUFSIZ];
+
+ fd = bpf__map_fd(obj, map);
+ if (fd < 0) {
+ bpf__strerror_map_fd(obj, map, fd, errbuf, sizeof(errbuf));
+ WARN_ONCE(fd < 0, "Failed to get map fd: %s\n", errbuf);
+ }
+ return fd;
+}
+
+#define PARAMS(args...) args
+#define DEFINE_JIT_BPF_MAP_HELPER(name, proto, args) \
+ JIT_BPF_MAP_HELPER(name, proto) { \
+ int map_fd = get_bpf_map_fd(ctx, map); \
+ \
+ if (map_fd < 0) \
+ return map_fd; \
+ return bpf_map_##name(map_fd, args); \
+ }
+
+DEFINE_JIT_BPF_MAP_HELPER(update_elem,
+ PARAMS(void *key, void *value, u64 flags),
+ PARAMS(key, value, flags))
+
+DEFINE_JIT_BPF_MAP_HELPER(lookup_elem,
+ PARAMS(void *key, void *value),
+ PARAMS(key, value))
+
+DEFINE_JIT_BPF_MAP_HELPER(get_next_key,
+ PARAMS(void *key, void *next_key),
+ PARAMS(key, next_key))
+
+#define bpf_map_pin bpf_obj_pin
+DEFINE_JIT_BPF_MAP_HELPER(pin,
+ PARAMS(const char *pathname),
+ PARAMS(pathname));
+#undef bpf_map_pin
new file mode 100644
@@ -0,0 +1,28 @@
+#ifndef JIT_HELPERS_H
+#define JIT_HELPERS_H
+
+#include <stdint.h>
+#include <util/perf-hooks.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define JIT_HELPER_FUNC_NAME(name) perf_##name
+
+#define JIT_HELPER(type, name, ...) \
+type JIT_HELPER_FUNC_NAME(name)(__VA_ARGS__)
+
+#define JIT_BPF_MAP_HELPER(name, ...) \
+ JIT_HELPER(int, map_##name, void *ctx, void *map, ##__VA_ARGS__)
+
+extern JIT_BPF_MAP_HELPER(update_elem, void *key, void *value, uint64_t flags);
+extern JIT_BPF_MAP_HELPER(lookup_elem, void *key, void *value);
+extern JIT_BPF_MAP_HELPER(get_next_key, void *key, void *next_key);
+extern JIT_BPF_MAP_HELPER(pin, const char *pathname);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif