Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 01981dd0 authored by Jim Shargo's avatar Jim Shargo Committed by Android (Google) Code Review
Browse files

Merge changes from topic "bs-bench" into main

* changes:
  nativewindow: Add more benchmarks to evaluate FFI costs
  nativewindow: Add C++/Rust benchmarks
parents 6d2bd6a1 2dbcb1fe
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ impl AHardwareBuffer {
    /// program termination.
    ///
    /// Available since API level 26.
    #[inline]
    pub fn new(
        width: u32,
        height: u32,
+50 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

cc_defaults {
    name: "nativewindow_benchmark_defaults_cc",
    shared_libs: ["libnativewindow"],
    static_libs: [
        "libbase",
        "libgoogle-benchmark-main",
    ],
    test_suites: [
        "device-tests",
        "NativeWindowBenchmarks",
    ],
}

cc_benchmark {
    name: "nativewindow_buffer_benchmarks_cc",
    srcs: ["buffer_benchmarks.cc"],
    defaults: ["nativewindow_benchmark_defaults_cc"],
}

rust_defaults {
    name: "nativewindow_benchmark_defaults_rs",
    rustlibs: [
        "libnativewindow_rs",
        "libcriterion",
    ],
    test_suites: [
        "device-tests",
        "NativeWindowBenchmarks",
    ],
}

rust_benchmark {
    name: "nativewindow_buffer_benchmarks_rs",
    srcs: ["buffer_benchmarks.rs"],
    defaults: ["nativewindow_benchmark_defaults_rs"],
}
+22 −0
Original line number Diff line number Diff line
# libnativewindow Benchmarks

This directory contains benchmarks for the C++ and Rust variants of
libnativewindow.

## Running

It is currently a little tricky to get statistics from Rust benchmarks directly
from tradefed. But we can hack it by using atest to build/push, then running
the benchmarks by hand to get stats.

```
  $ atest nativewindow_buffer_benchmarks_rs nativewindow_buffer_benchmarks_cc -d
  $ adb shell /data/local/tmp/nativewindow_buffer_benchmarks_cc/x86_64/nativewindow_buffer_benchmarks_cc
  $ adb shell /data/local/tmp/nativewindow_buffer_benchmarks_rs/x86_64/nativewindow_buffer_benchmarks_rs --bench
```

## Results

On a remote emulator, the results we see from the benchmarks from Rust and C++
seem to be roughly equivalent! Allocating/deallocating a 720p buffer takes
~2.3ms on each.
+74 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <android-base/macros.h>
#include <android/hardware_buffer.h>
#include <benchmark/benchmark.h>

constexpr AHardwareBuffer_Desc k720pDesc = {.width = 1280,
                                            .height = 720,
                                            .layers = 1,
                                            .format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
                                            .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
                                            .stride = 0};

static void BM_BufferAllocationDeallocation(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    for (auto _ : state) {
        int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
        if (UNLIKELY(status != 0)) {
            state.SkipWithError("Unable to allocate buffer.");
        }
        AHardwareBuffer_release(buffer);
        buffer = nullptr;
    }
}
BENCHMARK(BM_BufferAllocationDeallocation);

static void BM_AHardwareBuffer_Id(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
    if (UNLIKELY(status != 0)) {
        state.SkipWithError("Unable to allocate buffer.");
    }

    for (auto _ : state) {
        uint64_t id = 0;
        int status = AHardwareBuffer_getId(buffer, &id);
        if (UNLIKELY(status != 0)) {
            state.SkipWithError("Unable to get ID.");
        }
    }

    AHardwareBuffer_release(buffer);
}
BENCHMARK(BM_AHardwareBuffer_Id);

static void BM_AHardwareBuffer_Desc(benchmark::State& state) {
    AHardwareBuffer* buffer = nullptr;
    int status = AHardwareBuffer_allocate(&k720pDesc, &buffer);
    if (UNLIKELY(status != 0)) {
        state.SkipWithError("Unable to allocate buffer.");
    }

    for (auto _ : state) {
        AHardwareBuffer_Desc desc = {};
        AHardwareBuffer_describe(buffer, &desc);
    }

    AHardwareBuffer_release(buffer);
}
BENCHMARK(BM_AHardwareBuffer_Desc);

BENCHMARK_MAIN();
+60 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmark for libnativewindow AHardwareBuffer bindings

#![allow(dead_code)]
#![allow(missing_docs)]

use criterion::*;
use nativewindow::*;

#[inline]
fn create_720p_buffer() -> AHardwareBuffer {
    AHardwareBuffer::new(
        1280,
        720,
        1,
        AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
        AHardwareBuffer_UsageFlags::AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN,
    )
    .unwrap()
}

fn criterion_benchmark(c: &mut Criterion) {
    c.bench_function("allocate_deallocate", |b| {
        b.iter(|| {
            let buffer = create_720p_buffer();
            drop(buffer);
        })
    });

    let buffer = create_720p_buffer();
    c.bench_with_input(BenchmarkId::new("id", "buffer"), &buffer, |b, buffer| {
        b.iter(|| {
            buffer.id();
        })
    });

    // This benchmark exercises getters that need to fetch data via an
    // underlying call to AHardwareBuffer_describe.
    c.bench_with_input(BenchmarkId::new("desc", "buffer"), &buffer, |b, buffer| {
        b.iter(|| {
            buffer.width();
        })
    });
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);