From patchwork Tue Mar 8 23:07:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 594380 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id E0407140783 for ; Wed, 9 Mar 2016 10:08:45 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b=Nxm5/vlv; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751925AbcCHXIl (ORCPT ); Tue, 8 Mar 2016 18:08:41 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:8705 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751712AbcCHXIA (ORCPT ); Tue, 8 Mar 2016 18:08:00 -0500 Received: from pps.filterd (m0001255.ppops.net [127.0.0.1]) by mx0b-00082601.pphosted.com (8.15.0.59/8.15.0.59) with SMTP id u28N2Nmx019398 for ; Tue, 8 Mar 2016 15:07:59 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=IU3QFgFUG8FbAll8dr8gatgD0ETLTW8t+hyzruRP8mE=; b=Nxm5/vlve01Tm1dIv8PaxRMazxsZ3vX4y23YXUy6OLR6lKIE8aBEr/PAkl1HpP0cHZ6A v7yEZa0K9ag6k+F7oR9pXVpZgrDiMbX6sXepKnoaVOmtsXlephgLVkj8bEDjVi7XPt4O 0QV5rQXxcbXA+s3k7gTrBADSiBSn9/SOTXU= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 21hdu6q56e-4 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Tue, 08 Mar 2016 15:07:59 -0800 Received: from mx-out.facebook.com (192.168.52.123) by PRN-CHUB06.TheFacebook.com (192.168.16.16) with Microsoft SMTP Server (TLS) id 14.3.248.2; Tue, 8 Mar 2016 15:07:57 -0800 Received: from facebook.com (2401:db00:11:d093:face:0:1b:0) by mx-out.facebook.com (10.223.100.97) with ESMTP id 97a03250e58211e5b16a24be0593f280-f53f5420 for ; Tue, 08 Mar 2016 15:07:55 -0800 Received: by devbig505.prn1.facebook.com (Postfix, from userid 572438) id 1E874222639E; Tue, 8 Mar 2016 15:07:55 -0800 (PST) From: Alexei Starovoitov To: "David S . Miller" CC: Daniel Borkmann , , , Subject: [PATCH net-next 3/3] samples/bpf: add map performance test Date: Tue, 8 Mar 2016 15:07:54 -0800 Message-ID: <1457478474-1614041-3-git-send-email-ast@fb.com> X-Mailer: git-send-email 2.8.0.rc1 In-Reply-To: <1457478474-1614041-1-git-send-email-ast@fb.com> References: <1457478474-1614041-1-git-send-email-ast@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-03-08_09:, , signatures=0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org performance tests for hash map and per-cpu hash map with and without pre-allocation Signed-off-by: Alexei Starovoitov --- samples/bpf/Makefile | 4 + samples/bpf/map_perf_test_kern.c | 100 +++++++++++++++++++++++++ samples/bpf/map_perf_test_user.c | 155 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 259 insertions(+) create mode 100644 samples/bpf/map_perf_test_kern.c create mode 100644 samples/bpf/map_perf_test_user.c diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 75a13e742ab4..502c9fc8db85 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -18,6 +18,7 @@ hostprogs-y += trace_output hostprogs-y += lathist hostprogs-y += offwaketime hostprogs-y += spintest +hostprogs-y += map_perf_test test_verifier-objs := test_verifier.o libbpf.o test_maps-objs := test_maps.o libbpf.o @@ -36,6 +37,7 @@ trace_output-objs := bpf_load.o libbpf.o trace_output_user.o lathist-objs := bpf_load.o libbpf.o lathist_user.o offwaketime-objs := bpf_load.o libbpf.o offwaketime_user.o spintest-objs := bpf_load.o libbpf.o spintest_user.o +map_perf_test-objs := bpf_load.o libbpf.o map_perf_test_user.o # Tell kbuild to always build the programs always := $(hostprogs-y) @@ -53,6 +55,7 @@ always += tcbpf1_kern.o always += lathist_kern.o always += offwaketime_kern.o always += spintest_kern.o +always += map_perf_test_kern.o HOSTCFLAGS += -I$(objtree)/usr/include @@ -71,6 +74,7 @@ HOSTLOADLIBES_trace_output += -lelf -lrt HOSTLOADLIBES_lathist += -lelf HOSTLOADLIBES_offwaketime += -lelf HOSTLOADLIBES_spintest += -lelf +HOSTLOADLIBES_map_perf_test += -lelf -lrt # point this to your LLVM backend with bpf support LLC=$(srctree)/tools/bpf/llvm/bld/Debug+Asserts/bin/llc diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test_kern.c new file mode 100644 index 000000000000..311538e5a701 --- /dev/null +++ b/samples/bpf/map_perf_test_kern.c @@ -0,0 +1,100 @@ +/* Copyright (c) 2016 Facebook + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ +#include +#include +#include +#include +#include "bpf_helpers.h" + +#define MAX_ENTRIES 1000 + +struct bpf_map_def SEC("maps") hash_map = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(u32), + .value_size = sizeof(long), + .max_entries = MAX_ENTRIES, +}; + +struct bpf_map_def SEC("maps") percpu_hash_map = { + .type = BPF_MAP_TYPE_PERCPU_HASH, + .key_size = sizeof(u32), + .value_size = sizeof(long), + .max_entries = MAX_ENTRIES, +}; + +struct bpf_map_def SEC("maps") hash_map_alloc = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(u32), + .value_size = sizeof(long), + .max_entries = MAX_ENTRIES, + .map_flags = BPF_F_NO_PREALLOC, +}; + +struct bpf_map_def SEC("maps") percpu_hash_map_alloc = { + .type = BPF_MAP_TYPE_PERCPU_HASH, + .key_size = sizeof(u32), + .value_size = sizeof(long), + .max_entries = MAX_ENTRIES, + .map_flags = BPF_F_NO_PREALLOC, +}; + +SEC("kprobe/sys_getuid") +int stress_hmap(struct pt_regs *ctx) +{ + u32 key = bpf_get_current_pid_tgid(); + long init_val = 1; + long *value; + + bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY); + value = bpf_map_lookup_elem(&hash_map, &key); + if (value) + bpf_map_delete_elem(&hash_map, &key); + return 0; +} + +SEC("kprobe/sys_geteuid") +int stress_percpu_hmap(struct pt_regs *ctx) +{ + u32 key = bpf_get_current_pid_tgid(); + long init_val = 1; + long *value; + + bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY); + value = bpf_map_lookup_elem(&percpu_hash_map, &key); + if (value) + bpf_map_delete_elem(&percpu_hash_map, &key); + return 0; +} +SEC("kprobe/sys_getgid") +int stress_hmap_alloc(struct pt_regs *ctx) +{ + u32 key = bpf_get_current_pid_tgid(); + long init_val = 1; + long *value; + + bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY); + value = bpf_map_lookup_elem(&hash_map_alloc, &key); + if (value) + bpf_map_delete_elem(&hash_map_alloc, &key); + return 0; +} + +SEC("kprobe/sys_getegid") +int stress_percpu_hmap_alloc(struct pt_regs *ctx) +{ + u32 key = bpf_get_current_pid_tgid(); + long init_val = 1; + long *value; + + bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY); + value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key); + if (value) + bpf_map_delete_elem(&percpu_hash_map_alloc, &key); + return 0; +} +char _license[] SEC("license") = "GPL"; +u32 _version SEC("version") = LINUX_VERSION_CODE; diff --git a/samples/bpf/map_perf_test_user.c b/samples/bpf/map_perf_test_user.c new file mode 100644 index 000000000000..95af56ec5739 --- /dev/null +++ b/samples/bpf/map_perf_test_user.c @@ -0,0 +1,155 @@ +/* Copyright (c) 2016 Facebook + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "libbpf.h" +#include "bpf_load.h" + +#define MAX_CNT 1000000 + +static __u64 time_get_ns(void) +{ + struct timespec ts; + + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000000000ull + ts.tv_nsec; +} + +#define HASH_PREALLOC (1 << 0) +#define PERCPU_HASH_PREALLOC (1 << 1) +#define HASH_KMALLOC (1 << 2) +#define PERCPU_HASH_KMALLOC (1 << 3) + +static int test_flags = ~0; + +static void test_hash_prealloc(int cpu) +{ + __u64 start_time; + int i; + + start_time = time_get_ns(); + for (i = 0; i < MAX_CNT; i++) + syscall(__NR_getuid); + printf("%d:hash_map_perf pre-alloc %lld events per sec\n", + cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); +} + +static void test_percpu_hash_prealloc(int cpu) +{ + __u64 start_time; + int i; + + start_time = time_get_ns(); + for (i = 0; i < MAX_CNT; i++) + syscall(__NR_geteuid); + printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n", + cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); +} + +static void test_hash_kmalloc(int cpu) +{ + __u64 start_time; + int i; + + start_time = time_get_ns(); + for (i = 0; i < MAX_CNT; i++) + syscall(__NR_getgid); + printf("%d:hash_map_perf kmalloc %lld events per sec\n", + cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); +} + +static void test_percpu_hash_kmalloc(int cpu) +{ + __u64 start_time; + int i; + + start_time = time_get_ns(); + for (i = 0; i < MAX_CNT; i++) + syscall(__NR_getegid); + printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n", + cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time)); +} + +static void loop(int cpu) +{ + cpu_set_t cpuset; + + CPU_ZERO(&cpuset); + CPU_SET(cpu, &cpuset); + sched_setaffinity(0, sizeof(cpuset), &cpuset); + + if (test_flags & HASH_PREALLOC) + test_hash_prealloc(cpu); + + if (test_flags & PERCPU_HASH_PREALLOC) + test_percpu_hash_prealloc(cpu); + + if (test_flags & HASH_KMALLOC) + test_hash_kmalloc(cpu); + + if (test_flags & PERCPU_HASH_KMALLOC) + test_percpu_hash_kmalloc(cpu); +} + +static void run_perf_test(int tasks) +{ + pid_t pid[tasks]; + int i; + + for (i = 0; i < tasks; i++) { + pid[i] = fork(); + if (pid[i] == 0) { + loop(i); + exit(0); + } else if (pid[i] == -1) { + printf("couldn't spawn #%d process\n", i); + exit(1); + } + } + for (i = 0; i < tasks; i++) { + int status; + + assert(waitpid(pid[i], &status, 0) == pid[i]); + assert(status == 0); + } +} + +int main(int argc, char **argv) +{ + struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; + char filename[256]; + int num_cpu = 8; + + snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + setrlimit(RLIMIT_MEMLOCK, &r); + + if (argc > 1) + test_flags = atoi(argv[1]) ? : test_flags; + + if (argc > 2) + num_cpu = atoi(argv[2]) ? : num_cpu; + + if (load_bpf_file(filename)) { + printf("%s", bpf_log_buf); + return 1; + } + + run_perf_test(num_cpu); + + return 0; +}