From patchwork Wed Apr 28 19:50:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Dekker X-Patchwork-Id: 27466 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:4023:0:0:0:0 with SMTP id ky35csp756008pxb; Wed, 28 Apr 2021 12:51:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQV3FEDUOV8tDNl4wytOuExN6VYQ8O2Ah/WepQv+LZL1N0ug3/3i7AU4e9t7z4jeZSaZZx X-Received: by 2002:a05:6402:1711:: with SMTP id y17mr13371517edu.384.1619639476839; Wed, 28 Apr 2021 12:51:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619639476; cv=none; d=google.com; s=arc-20160816; b=GOlp4mp8tT0puJizymgzUczmx7EMKZxoMkMm1dz4vgCe9f0LpbF89LWgFaFW8sMJhJ M5ShhWsHAyUeRSBQbsFyQdBej9Rs4G7xsb2gUf8AqonJ4S6Fo/zMcqHGRf13/u1HD8Ix mSnh2+L5fuJeAI3fckM9gcP6UFewr9T8+oLGY4GsvCzsUL3KRx9uqhtDAxcTsMNOYUoO qDF/2caVq58u6/lD0pPBb5Sityw4xzmmkpW/f19AgmHfPS9bVVBqiuDpSlWO0I+yYf4U NsWJ3dc/DoCaREXWFVg4CXabqugllecCxl0JqcVMVJwitfcFQlyRvUMuVoGSvo/t+uZJ E3kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:dkim-signature:delivered-to; bh=k8mJVus02UcXET3UeF87KsdTdcZr7AkCbrckduo0/y8=; b=VlGA4sGIxiSBnLD/EQ9Xx4AESbzIJKvoe2belx3Swy7RUKNH+S8nPhVRbR84bHBx13 8g7/c2/BOHph5eJXq+I7NyIgQ6Ulp85E8hxN/FO+Ki3+MvwcRcBoXug+Gy3RQ4mxhOti gGs8PlsObAT/AX6/tP0BoxuoplFt9zbrt3kDcGaxXO0TTuWyFv/ZO9PRFk3Npg7DQL9K 4E359/JcVxFVT1b6NSfC4+Z5p/RsKOrHksvo2xUZ/jGgNJk4IQlShtaVDjQxy4lz9sFs 9At2PliaYkminjiqwniseqPogD0n7jWUgCJKJkUuar3oY+nHYd5j36QU/nbJrGgywsQo /Qgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@itanimul.li header.s=fm1 header.b="tXdzQ/iV"; dkim=neutral (body hash did not verify) header.i=@messagingengine.com header.s=fm2 header.b=mrlgMhfy; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id c95si783461edf.253.2021.04.28.12.51.15; Wed, 28 Apr 2021 12:51:16 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@itanimul.li header.s=fm1 header.b="tXdzQ/iV"; dkim=neutral (body hash did not verify) header.i=@messagingengine.com header.s=fm2 header.b=mrlgMhfy; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2A2DE68A154; Wed, 28 Apr 2021 22:51:02 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 48809689903 for ; Wed, 28 Apr 2021 22:50:54 +0300 (EEST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id C1851EB1 for ; Wed, 28 Apr 2021 15:50:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 28 Apr 2021 15:50:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=itanimul.li; h= from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm1; bh=btrGwFolf6A1L zv1KVHp+jRP/bJyVxnC55Zx/09vqw4=; b=tXdzQ/iV5Za9tGLNrJDYCT+UeRNhq 4XQdlvc2Zwl98ftV5jwWbEV3YMN38+TXjnSDnwsO7E6hdn7Kh/D6DbIrV90WD73b X36gXLHaT7f8XeFZlUVLN3C80wWRFoOjGCfaIw0/uZWF+rtHPkK9B5mkXqGIwAci k70uE7G8/EbUybumQxMnZHAgXVUfn6NY+gJOBRLH8Q7V54MZldGmLi62V4JnHxWR I4VhihXI86RzXreq+srZStaEOz+srIhsao/34P4xBT8al078PXLGvRvML/bP1/OH 8KVir5znm4eOi3XALEPy3eEL4XP0/WooYeK00mYTeB1x+DVl6/y1++9eA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=btrGwFolf6A1Lzv1KVHp+jRP/bJyVxnC55Zx/09vqw4=; b=mrlgMhfy xFyFdqmRrHZRRAJ9UKZZWcdDXugFKqBqhl8A7XAmkdxl8Q5YV+l3qN8VvtYGLhG7 Vg9cEZBDscifytSl44rxT9NnlM9wD/5E5yRqT73voMJC4Q6Gp6t9D76jfqjp1V+T 1LVPg4BMs6z8gFzHP+VQdyuamRg99brEb1INBfOMBcItVq96eMY1p/iJGhCCassI vNWKbM9KDXGGszYUN50M3yoTL6XI8qrQqz77OWVQ73Tj9Asb+6godgEStlzx8eog qqNjVkxFJUkT1JaRxo3vjTQ141LxycqMtLMVk4k8nmvw+LfT10DcMlr98fUfKeQS 54YO4EgyeUKG9Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrvddvvddguddulecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkffojghfggfgsedtke ertdertddtnecuhfhrohhmpeflohhshhcuffgvkhhkvghruceojhhoshhhsehithgrnhhi mhhulhdrlhhiqeenucggtffrrghtthgvrhhnpeefvddvuedtfeeutdfhffduledukeejhe duvdehgeehtdelvdeglefhgfetledvfeenucfkphepkeekrddufedtrdegkedrudejtden ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehjohhshh esihhtrghnihhmuhhlrdhlih X-ME-Proxy: Received: from computer.fritz.box (mue-88-130-48-170.dsl.tropolys.de [88.130.48.170]) by mail.messagingengine.com (Postfix) with ESMTPA for ; Wed, 28 Apr 2021 15:50:51 -0400 (EDT) From: Josh Dekker To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Apr 2021 21:50:25 +0200 Message-Id: <20210428195028.80000-2-josh@itanimul.li> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20210428195028.80000-1-josh@itanimul.li> References: <20210428195028.80000-1-josh@itanimul.li> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] lavu/checkasm: add (private) kperf timing for macOS X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 1SfdpVJcl5dP Signed-off-by: Josh Dekker --- configure | 2 + libavutil/Makefile | 1 + libavutil/macos_kperf.c | 140 ++++++++++++++++++++++++++++++++++++++ libavutil/macos_kperf.h | 23 +++++++ libavutil/timer.h | 17 ++++- tests/checkasm/checkasm.c | 14 +++- tests/checkasm/checkasm.h | 7 +- 7 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 libavutil/macos_kperf.c create mode 100644 libavutil/macos_kperf.h diff --git a/configure b/configure index 820f719a32..a79052ad28 100755 --- a/configure +++ b/configure @@ -489,6 +489,7 @@ Developer options (useful when working on FFmpeg itself): --ignore-tests=TESTS comma-separated list (without "fate-" prefix in the name) of tests whose result is ignored --enable-linux-perf enable Linux Performance Monitor API + --enable-macos-kperf enable macOS kperf (private) API --disable-large-tests disable tests that use a large amount of memory NOTE: Object files are built at the place where configure is launched. @@ -1947,6 +1948,7 @@ CONFIG_LIST=" fontconfig large_tests linux_perf + macos_kperf memory_poisoning neon_clobber_test ossfuzz diff --git a/libavutil/Makefile b/libavutil/Makefile index 47efb718d2..18dc5f22d9 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -181,6 +181,7 @@ OBJS-$(CONFIG_D3D11VA) += hwcontext_d3d11va.o OBJS-$(CONFIG_DXVA2) += hwcontext_dxva2.o OBJS-$(CONFIG_LIBDRM) += hwcontext_drm.o OBJS-$(CONFIG_LZO) += lzo.o +OBJS-$(CONFIG_MACOS_KPERF) += macos_kperf.o OBJS-$(CONFIG_MEDIACODEC) += hwcontext_mediacodec.o OBJS-$(CONFIG_OPENCL) += hwcontext_opencl.o OBJS-$(CONFIG_QSV) += hwcontext_qsv.o diff --git a/libavutil/macos_kperf.c b/libavutil/macos_kperf.c new file mode 100644 index 0000000000..d5de491e12 --- /dev/null +++ b/libavutil/macos_kperf.c @@ -0,0 +1,140 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg 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; either version 2 of the License, or + * (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "macos_kperf.h" +#include +#include +#include + +#define KPERF_LIST \ + F(int, kpc_get_counting, void) \ + F(int, kpc_force_all_ctrs_set, int) \ + F(int, kpc_set_counting, uint32_t) \ + F(int, kpc_set_thread_counting, uint32_t) \ + F(int, kpc_set_config, uint32_t, void *) \ + F(int, kpc_get_config, uint32_t, void *) \ + F(int, kpc_set_period, uint32_t, void *) \ + F(int, kpc_get_period, uint32_t, void *) \ + F(uint32_t, kpc_get_counter_count, uint32_t) \ + F(uint32_t, kpc_get_config_count, uint32_t) \ + F(int, kperf_sample_get, int *) \ + F(int, kpc_get_thread_counters, int, unsigned int, void *) + +#define F(ret, name, ...) \ + typedef ret name##proc(__VA_ARGS__); \ + static name##proc *name = NULL; +KPERF_LIST +#undef F + +#define CFGWORD_EL0A32EN_MASK (0x10000) +#define CFGWORD_EL0A64EN_MASK (0x20000) +#define CFGWORD_EL1EN_MASK (0x40000) +#define CFGWORD_EL3EN_MASK (0x80000) +#define CFGWORD_ALLMODES_MASK (0xf0000) + +#define CPMU_NONE 0 +#define CPMU_CORE_CYCLE 0x02 +#define CPMU_INST_A64 0x8c +#define CPMU_INST_BRANCH 0x8d +#define CPMU_SYNC_DC_LOAD_MISS 0xbf +#define CPMU_SYNC_DC_STORE_MISS 0xc0 +#define CPMU_SYNC_DTLB_MISS 0xc1 +#define CPMU_SYNC_ST_HIT_YNGR_LD 0xc4 +#define CPMU_SYNC_BR_ANY_MISP 0xcb +#define CPMU_FED_IC_MISS_DEM 0xd3 +#define CPMU_FED_ITLB_MISS 0xd4 + +#define KPC_CLASS_FIXED_MASK (1 << 0) +#define KPC_CLASS_CONFIGURABLE_MASK (1 << 1) +#define KPC_CLASS_POWER_MASK (1 << 2) +#define KPC_CLASS_RAWPMU_MASK (1 << 3) + +#define COUNTERS_COUNT 10 +#define CONFIG_COUNT 8 +#define KPC_MASK (KPC_CLASS_CONFIGURABLE_MASK | KPC_CLASS_FIXED_MASK) + +int ff_kperf_setup() +{ + uint64_t config[COUNTERS_COUNT] = {0}; + config[0] = CPMU_CORE_CYCLE | CFGWORD_EL0A64EN_MASK; + // config[3] = CPMU_INST_BRANCH | CFGWORD_EL0A64EN_MASK; + // config[4] = CPMU_SYNC_BR_ANY_MISP | CFGWORD_EL0A64EN_MASK; + // config[5] = CPMU_INST_A64 | CFGWORD_EL0A64EN_MASK; + + if (kpc_set_config(KPC_MASK, config)) { + fprintf(stderr, "kperf: kpc_set_config failed\n"); + return -1; + } + + if (kpc_force_all_ctrs_set(1)) { + fprintf(stderr, "kperf: kpc_force_all_ctrs_set failed\n"); + return -1; + } + + if (kpc_set_counting(KPC_MASK)) { + fprintf(stderr, "kperf: kpc_set_counting failed\n"); + return -1; + } + + if (kpc_set_thread_counting(KPC_MASK)) { + fprintf(stderr, "kperf: kpc_set_thread_counting failed\n"); + return -1; + } + + return 0; +} + +int ff_kperf_init() +{ + void *kperf = dlopen("/System/Library/PrivateFrameworks/kperf.framework/Versions/A/kperf", RTLD_LAZY); + if (!kperf) { + fprintf(stderr, "kperf: kperf = %p\n", kperf); + return -1; + } + +#define F(ret, name, ...) \ + name = (name##proc *)(dlsym(kperf, #name)); \ + if (!name) { \ + fprintf(stderr, "kperf: %s = %p\n", #name, (void *)name); \ + return -1; \ + } + KPERF_LIST +#undef F + + if (kpc_get_counter_count(KPC_MASK) != COUNTERS_COUNT) { + fprintf(stderr, "kperf: wrong fixed counters count\n"); + return -1; + } + + if (kpc_get_config_count(KPC_MASK) != CONFIG_COUNT) { + fprintf(stderr, "kperf: wrong fixed config count\n"); + return -1; + } + + return 0; +} + +uint64_t ff_kperf_cycles() +{ + uint64_t counters[COUNTERS_COUNT]; + if (kpc_get_thread_counters(0, COUNTERS_COUNT, counters)) { + return -1; + } + + return counters[0]; +} diff --git a/libavutil/macos_kperf.h b/libavutil/macos_kperf.h new file mode 100644 index 0000000000..e9fe37b3f8 --- /dev/null +++ b/libavutil/macos_kperf.h @@ -0,0 +1,23 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg 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; either version 2 of the License, or + * (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +int ff_kperf_setup(void); +int ff_kperf_init(void); +uint64_t ff_kperf_cycles(void); diff --git a/libavutil/timer.h b/libavutil/timer.h index 36f920e96a..10198b20e3 100644 --- a/libavutil/timer.h +++ b/libavutil/timer.h @@ -42,7 +42,9 @@ #include #include -#if HAVE_MACH_ABSOLUTE_TIME +#if CONFIG_MACOS_KPERF +#include "macos_kperf.h" +#elif HAVE_MACH_ABSOLUTE_TIME #include #endif @@ -125,6 +127,19 @@ read(linux_perf_fd, &tperf, sizeof(tperf)); \ TIMER_REPORT(id, tperf) +#elif CONFIG_MACOS_KPERF + +#define START_TIMER \ + uint64_t tperf; \ + if (ff_kperf_init()) \ + av_log(NULL, AV_LOG_ERROR, "ff_kperf_init() failed\n"); \ + if (ff_kperf_setup()) \ + av_log(NULL, AV_LOG_ERROR, "ff_kperf_setup() failed\n"); \ + tperf = kperf_cycles(); + +#define STOP_TIMER(id) \ + TIMER_REPORT(id, kperf_cycles() - tperf); + #elif defined(AV_READ_TIME) #define START_TIMER \ uint64_t tend; \ diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c index e2e17d2b11..6b1abe1df6 100644 --- a/tests/checkasm/checkasm.c +++ b/tests/checkasm/checkasm.c @@ -638,9 +638,17 @@ static int bench_init_linux(void) } return 0; } -#endif +#elif CONFIG_MACOS_KPERF +static int bench_init_kperf(void) +{ + if (ff_kperf_init() || ff_kperf_setup()) { + fprintf(stderr, "checkasm must be run as root to use kperf on macOS\n"); + return -1; + } -#if !CONFIG_LINUX_PERF + return 0; +} +#else static int bench_init_ffmpeg(void) { #ifdef AV_READ_TIME @@ -657,6 +665,8 @@ static int bench_init(void) { #if CONFIG_LINUX_PERF int ret = bench_init_linux(); +#elif CONFIG_MACOS_KPERF + int ret = bench_init_kperf(); #else int ret = bench_init_ffmpeg(); #endif diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h index 0593d0edac..b747ed1986 100644 --- a/tests/checkasm/checkasm.h +++ b/tests/checkasm/checkasm.h @@ -31,6 +31,8 @@ #include #include #include +#elif CONFIG_MACOS_KPERF +#include "libavutil/macos_kperf.h" #endif #include "libavutil/avstring.h" @@ -225,7 +227,7 @@ typedef struct CheckasmPerf { int iterations; } CheckasmPerf; -#if defined(AV_READ_TIME) || CONFIG_LINUX_PERF +#if defined(AV_READ_TIME) || CONFIG_LINUX_PERF || CONFIG_MACOS_KPERF #if CONFIG_LINUX_PERF #define PERF_START(t) do { \ @@ -236,6 +238,9 @@ typedef struct CheckasmPerf { ioctl(sysfd, PERF_EVENT_IOC_DISABLE, 0); \ read(sysfd, &t, sizeof(t)); \ } while (0) +#elif CONFIG_MACOS_KPERF +#define PERF_START(t) t = ff_kperf_cycles() +#define PERF_STOP(t) t = ff_kperf_cycles() - t #else #define PERF_START(t) t = AV_READ_TIME() #define PERF_STOP(t) t = AV_READ_TIME() - t