未验证 提交 e266a204 编写于 作者: O openharmony_ci 提交者: Gitee

!934 Add musl benchmark

Merge pull request !934 from yinchuang/add_musl_benchmark
...@@ -430,4 +430,8 @@ if (is_lite_system && current_os == "ohos") { ...@@ -430,4 +430,8 @@ if (is_lite_system && current_os == "ohos") {
testonly = true testonly = true
deps = [ "libc-test:musl_libc_test" ] deps = [ "libc-test:musl_libc_test" ]
} }
group("benchmark_musl") {
deps = [ "Benchmark:benchmark_musl_group" ]
}
} }
# Copyright (c) 2023 Huawei Device Co., Ltd.
# 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.
import("//build/ohos.gni")
import("//third_party/musl/musl_config.gni")
group("benchmark_musl_group") {
deps = [
":musl_benchmark",
":musl_dlopen",
]
}
ohos_executable("musl_benchmark") {
sources = [
"benchmark_framework.cpp",
"libc_ctype.cpp",
"libc_dirent.cpp",
"libc_epoll.cpp",
"libc_errno.cpp",
"libc_fcntl.cpp",
"libc_fenv.cpp",
"libc_linker.cpp",
"libc_locale.cpp",
"libc_malloc.cpp",
"libc_math.cpp",
"libc_mman.cpp",
"libc_network.cpp",
"libc_pthread.cpp",
"libc_sched.cpp",
"libc_select.cpp",
"libc_stat.cpp",
"libc_stdio.cpp",
"libc_stdlib.cpp",
"libc_string.cpp",
"libc_syscall.cpp",
"libc_time.cpp",
"libc_unistd.cpp",
"util.cpp",
]
deps = [ "//third_party/benchmark:benchmark" ]
defines = []
if (defined(non_current_interface) && non_current_interface) {
defines += [ "ONO_CURRENT_INTERFACE" ]
}
part_name = "musl"
subsystem_name = "thirdparty"
}
ohos_executable("musl_dlopen") {
sources = [ "libc_dlopen.cpp" ]
part_name = "musl"
subsystem_name = "thirdparty"
}
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include <err.h>
#include <getopt.h>
#include <inttypes.h>
#include <math.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "util.h"
static const std::vector<int> commonArgs {
8,
16,
32,
64,
512,
1 * K,
8 * K,
16 * K,
32 * K,
64 * K,
128 * K,
};
static const std::vector<int> limitSizes{
1,
2,
3,
4,
5,
6,
7,
};
std::map<std::string, std::pair<benchmark_func, std::string>> g_allBenchmarks;
std::mutex g_benchmarkLock;
typedef std::vector<std::vector<int64_t>> args_vector;
static struct option g_benchmarkLongOptions[] = {
{"musl_cpu", required_argument, nullptr, 'c'},
{"musl_iterations", required_argument, nullptr, 'i'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0},
};
void PrintUsageAndExit()
{
printf("Usage:\n");
printf("musl_benchmarks [--musl_cpu=<cpu_to_isolate>]\n");
printf(" [--musl_iterations=<num_iter>]\n");
printf(" [<original benchmark flags>]\n");
printf("benchmark flags:\n");
int argc = 2;
char argv0[] = "musl_benchmark";
char argv1[] = "--help";
char *argv[3]{argv0, argv1, nullptr};
benchmark::Initialize(&argc, argv);
exit(1);
}
void ShiftOptions(int argc, char **argv, std::vector<char *> *argvAfterShift)
{
(*argvAfterShift)[0] = argv[0];
for (int i = 1; i < argc; ++i) {
char *optarg = argv[i];
size_t index = 0;
// Find if musl defined this arg.
while (g_benchmarkLongOptions[index].name && strncmp(g_benchmarkLongOptions[index].name, optarg + 2,
strlen(g_benchmarkLongOptions[index].name))) {
++index;
}
// Not defined.
if (!g_benchmarkLongOptions[index].name) {
argvAfterShift->push_back(optarg);
} else if ((g_benchmarkLongOptions[index].has_arg == required_argument) && !strchr(optarg, '=')) {
i++;
}
}
argvAfterShift->push_back(nullptr);
}
bench_opts_t ParseOptions(int argc, char **argv)
{
bench_opts_t opts;
int opt;
extern int opterr;
opterr = 0; // Don't show unrecognized option error.
while ((opt = getopt_long(argc, argv, "c:i:a:h", g_benchmarkLongOptions, 0)) != -1) {
switch (opt) {
case 'c':
if (*optarg) {
char *errorCheck;
opts.cpuNum = strtol(optarg, &errorCheck, 10);
if (*errorCheck) {
errx(1, "ERROR: Args %s is not a valid integer.", optarg);
}
} else {
printf("ERROR: no argument specified for musl_cpu.\n");
PrintUsageAndExit();
}
break;
case 'i':
if (*optarg) {
char *errorCheck;
opts.iterNum = strtol(optarg, &errorCheck, 10);
if (*errorCheck != '\0' or opts.iterNum < 0) {
errx(1, "ERROR: Args %s is not a valid number of iterations.", optarg);
}
} else {
printf("ERROR: no argument specified for musl_iterations.\n");
PrintUsageAndExit();
}
break;
case 'h':
PrintUsageAndExit();
break;
case '?':
break;
default:
exit(1);
}
}
return opts;
}
void LockAndRun(benchmark::State &state, benchmark_func func, int cpuNum)
{
if (cpuNum >= 0) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpuNum, &cpuset);
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
printf("lock CPU failed, ERROR:%s\n", strerror(errno));
}
}
reinterpret_cast<void (*)(benchmark::State &)>(func)(state);
}
args_vector *ResolveArgs(args_vector *argsVector, std::string args,
std::map<std::string, args_vector> &presetArgs)
{
// Get it from preset args.
if (presetArgs.count(args)) {
return &presetArgs[args];
}
// Convert string to int.
argsVector->push_back(std::vector<int64_t>());
std::stringstream sstream(args);
std::string argstr;
while (sstream >> argstr) {
char *errorCheck;
int converted = static_cast<int>(strtol(argstr.c_str(), &errorCheck, 10));
if (*errorCheck) {
errx(1, "ERROR: Args str %s contains an invalid macro or int.", args.c_str());
}
(*argsVector)[0].push_back(converted);
}
return argsVector;
}
static args_vector GetArgs(const std::vector<int> &sizes)
{
args_vector args;
for (int size : sizes) {
args.push_back({size});
}
return args;
}
static args_vector GetArgs(const std::vector<int> &sizes, int value)
{
args_vector args;
for (int size : sizes) {
args.push_back({size, value});
}
return args;
}
static args_vector GetArgs(const std::vector<int> &sizes, int value1, int value2)
{
args_vector args;
for (int size : sizes) {
args.push_back({size, value1, value2});
}
return args;
}
static args_vector GetArgs(const std::vector<int> &sizes, const std::vector<int> &limits, int value)
{
args_vector args;
for (int size : sizes) {
for (int limit : limits) {
args.push_back({size, limit, value});
}
}
return args;
}
std::map<std::string, args_vector> GetPresetArgs()
{
std::map<std::string, args_vector> presetArgs {
{"COMMON_ARGS", GetArgs(commonArgs)},
{"ALIGNED_ONEBUF", GetArgs(commonArgs, 0)},
{"ALIGNED_TWOBUF", GetArgs(commonArgs, 0, 0)},
{"STRING_LIMIT", GetArgs(commonArgs, limitSizes, 0)},
{"MATH_COMMON", args_vector{{0}, {1}, {2}, {3}, {4}, {5}}},
{"BENCHMARK_VARIABLE", args_vector{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}}},
{"REALPATH_VARIABLE", args_vector{{0}, {1}, {2}, {3}, {4}}},
{"MMAP_SIZE", args_vector{{8}, {16}, {32}, {64}, {128}, {512}}},
};
return presetArgs;
}
void RegisterSingleBenchmark(bench_opts_t opts, const std::string &funcName, args_vector *runArgs)
{
if (g_allBenchmarks.find(funcName) == g_allBenchmarks.end()) {
errx(1, "ERROR: No benchmark for function %s", funcName.c_str());
}
benchmark_func func = g_allBenchmarks.at(funcName).first;
for (const std::vector<int64_t> &args : (*runArgs)) {
// It will call LockAndRun(func, opts.cpuNum).
auto registration = benchmark::RegisterBenchmark(funcName.c_str(), LockAndRun, func, opts.cpuNum)->Args(args);
if (opts.iterNum > 0) {
registration->Iterations(opts.iterNum);
}
}
}
void RegisterAllBenchmarks(const bench_opts_t &opts, std::map<std::string, args_vector> &presetArgs)
{
for (auto &entry : g_allBenchmarks) {
auto &funcInfo = entry.second;
args_vector arg_vector;
args_vector *runArgs = ResolveArgs(&arg_vector, funcInfo.second, presetArgs);
RegisterSingleBenchmark(opts, entry.first, runArgs);
}
}
int main(int argc, char **argv)
{
std::map<std::string, args_vector> presetArgs = GetPresetArgs();
bench_opts_t opts = ParseOptions(argc, argv);
std::vector<char *> argvAfterShift(argc);
ShiftOptions(argc, argv, &argvAfterShift);
RegisterAllBenchmarks(opts, presetArgs);
if (setpriority(PRIO_PROCESS, 0, -20)) {
perror("Set priority of process failed.\n");
}
int argcAfterShift = argvAfterShift.size();
benchmark::Initialize(&argcAfterShift, argvAfterShift.data());
benchmark::RunSpecifiedBenchmarks();
}
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "ctype.h"
#include "util.h"
using namespace std;
#define TOTAL_COMMON_CHARACTERS 127
static void Bm_function_Tolower_a(benchmark::State &state)
{
int c = 97;
for (auto _ : state) {
benchmark::DoNotOptimize(tolower(c));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Tolower_A(benchmark::State &state)
{
int c = 65;
for (auto _ : state) {
benchmark::DoNotOptimize(tolower(c));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Tolower_all_ascii(benchmark::State &state)
{
for (auto _ : state) {
for (int i = 0; i < TOTAL_COMMON_CHARACTERS; i++) {
benchmark::DoNotOptimize(tolower(i));
}
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Tolower_a);
MUSL_BENCHMARK(Bm_function_Tolower_A);
MUSL_BENCHMARK(Bm_function_Tolower_all_ascii);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sys/types.h"
#include "dirent.h"
#include "util.h"
using namespace std;
static void Bm_function_Opendir(benchmark::State &state)
{
for (auto _ : state) {
DIR *dir = opendir("/dev/block");
if (dir == nullptr) {
perror("opendir proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(dir);
state.PauseTiming();
closedir(dir);
state.ResumeTiming();
}
}
MUSL_BENCHMARK(Bm_function_Opendir);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <dlfcn.h>
#include <sys/time.h>
#include <stdio.h>
#include <iostream>
#define LIBACE_PATH "system/lib64/libace.z.so"
class ScopeTime {
public:
explicit ScopeTime() : cost(0)
{
gettimeofday(&timeStart, nullptr);
}
void CurrentTime()
{
struct timeval timeCurrent;
gettimeofday(&timeCurrent, nullptr);
cost = (timeCurrent.tv_sec - timeStart.tv_sec) * 1000.0 +
(double)(timeCurrent.tv_usec - timeStart.tv_usec) / 1000.0;
printf("current cost %f ms.\n", cost);
}
~ScopeTime()
{
gettimeofday(&timeEnd, nullptr);
cost = (timeEnd.tv_sec - timeStart.tv_sec) * 1000.0 + (double)(timeEnd.tv_usec - timeStart.tv_usec) / 1000.0;
printf("dlopen cost %f ms.\n", cost);
}
private:
struct timeval timeStart, timeEnd;
double cost;
};
static void DoDlopen(const char *fileName, int flags)
{
ScopeTime st;
void *handle = dlopen(fileName, flags);
if (handle == nullptr) {
printf("dlopen error: %s", dlerror());
exit(-1);
}
}
int main()
{
DoDlopen(LIBACE_PATH, RTLD_LAZY);
return 0;
}
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include <sys/epoll.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "util.h"
using namespace std;
#define EVENTSIZE 10
struct epoll_event events[EVENTSIZE], event;
// Used to create an epoll object to record events for files to be listened to
static void Bm_function_Epoll_createl(benchmark::State &state)
{
for (auto _ : state) {
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("epoll_createl");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(epoll_fd);
close(epoll_fd);
}
state.SetBytesProcessed(state.iterations());
}
// Used to add, modify, or delete events to an epoll object
static void Bm_function_Epoll_ctl(benchmark::State &state)
{
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("epoll_createl");
exit(EXIT_FAILURE);
}
int fd = open("/dev/zero", O_RDONLY);
if (fd == -1) {
perror("open epoll_ctl");
exit(EXIT_FAILURE);
}
event.events = EPOLLIN | EPOLLET;
event.data.fd = fd;
for (auto _ : state) {
benchmark::DoNotOptimize(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event));
}
close(epoll_fd);
close(fd);
state.SetBytesProcessed(state.iterations());
}
// Used to wait for the file descriptor in the epoll object to appear for the event
static void Bm_function_Epoll_wait(benchmark::State &state)
{
int epoll_fd = epoll_create1(0);
if (epoll_fd == -1) {
perror("epoll_createl");
exit(EXIT_FAILURE);
}
int fd = open("/dev/zero", O_RDONLY);
if (fd == -1) {
perror("open epoll_wait");
exit(EXIT_FAILURE);
}
event.events = EPOLLIN | EPOLLET;
event.data.fd = fd;
for (auto _ : state) {
epoll_wait(epoll_fd, events, EVENTSIZE, 0);
}
close(epoll_fd);
close(fd);
state.SetBytesProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Epoll_createl);
MUSL_BENCHMARK(Bm_function_Epoll_ctl);
MUSL_BENCHMARK(Bm_function_Epoll_wait);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "errno.h"
#include "util.h"
using namespace std;
static void Bm_function_Strerror_noerror(benchmark::State &state)
{
int e = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_enoent(benchmark::State &state)
{
int e = 2;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_enomem(benchmark::State &state)
{
int e = 12;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_eacces(benchmark::State &state)
{
int e = 13;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_eexist(benchmark::State &state)
{
int e = 17;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_einval(benchmark::State &state)
{
int e = 22;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_erofs(benchmark::State &state)
{
int e = 30;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Strerror_etimeout(benchmark::State &state)
{
int e = 110;
for (auto _ : state) {
benchmark::DoNotOptimize(strerror(e));
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Strerror_noerror);
MUSL_BENCHMARK(Bm_function_Strerror_enoent);
MUSL_BENCHMARK(Bm_function_Strerror_enomem);
MUSL_BENCHMARK(Bm_function_Strerror_eacces);
MUSL_BENCHMARK(Bm_function_Strerror_eexist);
MUSL_BENCHMARK(Bm_function_Strerror_einval);
MUSL_BENCHMARK(Bm_function_Strerror_erofs);
MUSL_BENCHMARK(Bm_function_Strerror_etimeout);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "unistd.h"
#include "stdio.h"
#include "util.h"
using namespace std;
#ifdef ONO_CURRENT_INTERFACE
static void Bm_function_Fcntl_getfl(benchmark::State &state)
{
int fd = open("/etc/passwd", O_RDONLY);
if (fd == -1) {
perror("open fcntl_getfl");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
int ret = fcntl(fd, F_GETFL);
if (ret < 0) {
perror("fcntl_getfl proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Fcntl_setfl(benchmark::State &state)
{
int flags = fcntl(STDIN_FILENO, F_GETFL);
if (flags < 0) {
perror("fcntl_setfl F_GETFL");
exit(EXIT_FAILURE);
}
flags |= O_NONBLOCK;
for (auto _ : state) {
int ret = fcntl(STDIN_FILENO, F_SETFL, flags);
if (ret < 0) {
perror("fcntl_setfl proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Fcntl_setlkw(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open fcntl_setlkw");
exit(EXIT_FAILURE);
}
struct flock f_lock;
for (auto _ : state) {
state.PauseTiming();
f_lock.l_type = F_WRLCK;
f_lock.l_whence = SEEK_SET;
f_lock.l_start = 0;
f_lock.l_len = 0;
if (fcntl(fd, F_SETLKW, &f_lock) < 0) {
perror("fcntl_setlkw F_WRLCK");
exit(EXIT_FAILURE);
}
f_lock.l_type = F_UNLCK;
state.ResumeTiming();
int ret = fcntl(fd, F_SETLKW, &f_lock);
if (ret < 0) {
perror("fcntl_setlkw proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Fcntl_dupfd(benchmark::State &state)
{
int fd = open("/etc/passwd", O_RDONLY);
if (fd == -1) {
perror("open fcntl_dupfd");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
int dupfd = fcntl(fd, F_DUPFD, 0);
if (dupfd < 0) {
perror("fcntl_dupfd proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(dupfd);
state.PauseTiming();
close(dupfd);
state.ResumeTiming();
}
close(fd);
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Fcntl_setlk(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open fcntl_setlk");
exit(EXIT_FAILURE);
}
struct flock f_lock;
for (auto _ : state) {
state.PauseTiming();
f_lock.l_type = F_WRLCK;
f_lock.l_whence = SEEK_SET;
f_lock.l_start = 0;
f_lock.l_len = 0;
if (fcntl(fd, F_SETLK, &f_lock) < 0) {
perror("fcntl_setlk F_WRLCK");
exit(EXIT_FAILURE);
}
f_lock.l_type = F_UNLCK;
state.ResumeTiming();
int ret = fcntl(fd, F_SETLK, &f_lock);
if (ret < 0) {
perror("fcntl_setlk proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fcntl_getlk(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open fcntl_getlk");
exit(EXIT_FAILURE);
}
struct flock f_lock;
for (auto _ : state) {
state.PauseTiming();
f_lock.l_type = F_RDLCK;
f_lock.l_whence = SEEK_SET;
f_lock.l_start = 0;
f_lock.l_len = 0;
state.ResumeTiming();
int ret = fcntl(fd, F_GETLK, &f_lock);
if (ret < 0) {
perror("fcntl_getlk proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fcntl_getfd(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open fcntl_getfd");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
int ret = fcntl(fd, F_GETFD);
if (ret < 0) {
perror("fcntl_getfd proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fcntl_setfd(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open fcntl_setfd");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
state.PauseTiming();
int flags = fcntl(fd, F_GETFD);
if (flags < 0) {
perror("fcntl_setfd F_GETFD");
exit(EXIT_FAILURE);
}
flags |= FD_CLOEXEC;
state.ResumeTiming();
int ret = fcntl(fd, F_SETFD, flags);
if (ret < 0) {
perror("fcntl_setfd proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
state.SetBytesProcessed(state.iterations());
}
#endif
static void Bm_function_Open_rdonly(benchmark::State &state)
{
const char *filename = "/proc/self/cmdline";
for (auto _ : state) {
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("open_rdonly proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(fd);
state.PauseTiming();
close(fd);
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Open_rdwr(benchmark::State &state)
{
const char *filename = "/dev/zero";
for (auto _ : state) {
int fd = open(filename, O_RDWR);
if (fd == -1) {
perror("open_rdwr proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(fd);
state.PauseTiming();
close(fd);
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Open_creat_rdwr(benchmark::State &state)
{
const char *filename = "/data/log/hiview/sys_event_logger/event.db";
for (auto _ : state) {
int fd = open(filename, O_RDWR | O_CREAT);
if (fd == -1) {
perror("open_creat_rdwr proc");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(fd);
state.PauseTiming();
close(fd);
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
}
#ifdef ONO_CURRENT_INTERFACE
MUSL_BENCHMARK(Bm_function_Fcntl_getfl);
MUSL_BENCHMARK(Bm_function_Fcntl_setfl);
MUSL_BENCHMARK(Bm_function_Fcntl_setlkw);
MUSL_BENCHMARK(Bm_function_Fcntl_dupfd);
MUSL_BENCHMARK(Bm_function_Fcntl_setlk);
MUSL_BENCHMARK(Bm_function_Fcntl_getlk);
MUSL_BENCHMARK(Bm_function_Fcntl_getfd);
MUSL_BENCHMARK(Bm_function_Fcntl_setfd);
#endif
MUSL_BENCHMARK(Bm_function_Open_rdonly);
MUSL_BENCHMARK(Bm_function_Open_rdwr);
MUSL_BENCHMARK(Bm_function_Open_creat_rdwr);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "fenv.h"
#include "util.h"
// Obtain rounding direction mode
static void Bm_function_Fegetround(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(fegetround());
}
state.SetBytesProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Fegetround);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 "util.h"
#include <benchmark/benchmark.h>
#include <dlfcn.h>
#include <iostream>
using namespace std;
#define LIBGLES_MALI_PATH "/vendor/lib64/chipsetsdk/libGLES_mali.so"
static vector<string> syms = {
"eglGetProcAddress",
"eglChooseConfig",
"eglCopyBuffers",
"eglCreateContext",
"eglCreatePbufferSurface",
"eglDestroySurface",
"eglGetConfigs",
"eglGetCurrentSurface",
"eglGetError",
"eglGetProcAddress",
"eglMakeCurrent",
"eglQueryString",
"eglSwapBuffers",
"glGetFloatv",
"glGetLightfv",
"glGetTexEnvfv",
"glLightModelf",
"glLightModelfv",
"glLightf",
"glLightfv",
"glLineWidth",
"glOrthof",
"glPointParameterfv",
"glPolygonOffset",
"glColor4x",
"glCompressedTexImage2D",
"glEnableClientState",
"glFlush",
"glFogxv",
"glGetLightxv",
"glGetTexEnviv",
"glShadeModel",
"glStencilOp",
"glTexEnvi",
"glTexEnvx",
"glTexImage2D",
"glTexParameterx",
"glPointSizePointerOES",
"glEGLImageTargetTexture2DOES",
"glEGLImageTargetRenderbufferStorageOES",
"glBlendEquationSeparateOES",
"glBlendFuncSeparateOES",
"glDrawTexxOES",
"glDrawTexivOES",
"glDrawTexxvOES",
"glDrawTexfvOES",
"glClearColorxOES",
"glFogxvOES",
"glGetClipPlanexOES",
"glMaterialxOES",
"glMultMatrixxOES",
"glNormal3xOES",
"glPointParameterxvOES",
"glTexEnvxOES",
"glTexParameterxOES",
"glTranslatexOES",
"glGetMaterialxvOES",
"glSampleCoveragexOES",
"glBindFramebufferOES",
"glGenFramebuffersOES",
"glFramebufferRenderbufferOES",
"glGetFramebufferAttachmentParameterivOES",
"glGenerateMipmapOES",
"glUnmapBufferOES",
"glCurrentPaletteMatrixOES",
"glLoadPaletteFromModelViewMatrixOES",
"glWeightPointerOES",
"glClearDepthfOES",
"glClipPlanefOES",
"glFrustumfOES",
"glGetClipPlanefOES",
"glTexGenfOES",
"glTexGenfvOES",
"glTexGenivOES",
"glGetTexGenivOES",
"glBindVertexArrayOES",
"glGenVertexArraysOES",
"glDiscardFramebufferEXT",
"glRenderbufferStorageMultisampleEXT",
"glGetnUniformfvEXT",
"glGetnUniformivEXT",
"glTexStorage2DEXT",
"glTexStorage3DEXT",
"glColorMask",
"glCompressedTexImage2D",
"glCompressedTexSubImage2D",
"glCopyTexImage2D",
"glCreateProgram",
"glDrawElements",
"glGenBuffers",
"glGenFramebuffers",
"glGenRenderbuffers",
"glGenTextures",
"glObjectLabelKHR",
"glGetObjectLabelKHR",
"glGetnUniformivKHR",
"glDrawElementsBaseVertexOES",
"glDrawElementsInstancedBaseVertexOES",
"glPrimitiveBoundingBoxOES",
"glMinSampleShadingOES",
"glTexImage3DOES",
"glTexSubImage3DOES",
"glCompressedTexImage3DOES",
"glCompressedTexSubImage3DOES",
"glTexParameterIivOES",
"glGetTexParameterIivOES",
"glGetTexParameterIuivOES",
"glBindVertexArrayOES",
"glEGLImageTargetTexStorageEXT",
"glBufferStorageEXT",
"glCopyImageSubDataEXT",
"glDiscardFramebufferEXT",
"glGenQueriesEXT",
"glDeleteQueriesEXT",
"glQueryCounterEXT",
"glGetQueryObjectivEXT",
"glGetQueryObjectuivEXT",
"glGetQueryObjecti64vEXT",
"glGetQueryObjectui64vEXT",
"glEnableiEXT",
"glDisableiEXT",
"glBlendEquationiEXT",
"glColorMaskiEXT",
"glIsEnablediEXT",
"glRenderbufferStorageMultisampleEXT",
"glGetGraphicsResetStatusEXT",
"glGetnUniformfvEXT",
"glGetnUniformivEXT",
"glPatchParameteriEXT",
"glTexParameterIuivEXT",
"glGetSamplerParameterIivEXT",
"glTexBufferEXT",
"glTexBufferRangeEXT",
"glTexStorage2DEXT",
"glTexStorage3DEXT",
"glFramebufferTextureMultiviewOVR",
"glFramebufferTextureMultisampleMultiviewOVR",
"glBindBuffer",
"glBufferData",
"glCheckFramebufferStatus",
"glClear",
"glClearColor",
"glClearDepthf",
"glClearStencil",
"glCompileShader",
"glCompressedTexImage2D",
"glCompressedTexSubImage2D",
"glCopyTexImage2D",
"glCopyTexSubImage2D",
"glCreateProgram",
"glCreateShader",
"glDeleteBuffers",
"glDeleteFramebuffers",
"glDeleteProgram",
"glDeleteShader",
"glDeleteTextures",
"glDepthFunc",
"glDepthRangef",
"glDetachShader",
"glDisable",
"glDrawArrays",
"glDrawElements",
"glEnable",
"glEnableVertexAttribArray",
"glFlush",
"glFramebufferRenderbuffer",
"glFrontFace",
"glGenBuffers",
"glGenTextures",
"glGetActiveUniform",
"glGetAttachedShaders",
"glGetError",
"glGetFramebufferAttachmentParameteriv",
"glGetIntegerv",
"glGetProgramiv",
"glGetRenderbufferParameteriv",
"glGetShaderiv",
"glGetShaderPrecisionFormat",
"glGetShaderSource",
"glGetString",
"glGetTexParameterfv",
"glGetUniformfv",
"glGetUniformiv",
"glGetUniformLocation",
"glGetVertexAttribiv",
"glGetVertexAttribPointerv",
"glVertexAttribI4iv",
"glGetUniformuiv",
"glClearBufferfv",
"glGetStringi",
"glCopyBufferSubData",
"glGetUniformIndices",
"glGetActiveUniformsiv",
"glGetUniformBlockIndex",
"glIsSync",
"glClientWaitSync",
"glWaitSync",
"glGetInteger64v",
"glGetInteger64i_v",
"glGetBufferParameteri64v",
"glGetSamplerParameteriv",
"glInvalidateSubFramebuffer",
"glTexStorage3D",
"glDispatchCompute",
"glDispatchComputeIndirect",
"glDrawElementsIndirect",
"glFramebufferParameteri",
"glGetProgramInterfaceiv",
"glGetProgramResourceIndex",
"glGetProgramResourceiv",
"glUseProgramStages",
"glActiveShaderProgram",
"glBindProgramPipeline",
"glGetPointerv",
"glBlendEquationi",
"glBlendFuncSeparatei",
"glDrawElementsBaseVertex",
"glDrawElementsInstancedBaseVertex",
"glPrimitiveBoundingBox",
"glGetGraphicsResetStatus",
"glGetnUniformiv",
"glGetnUniformuiv",
"glPatchParameteri",
"glTexParameterIuiv",
"glGetTexParameterIiv",
"glSamplerParameterIiv",
"glBlendBarrierKHR",
"glDebugMessageControlKHR",
"glDebugMessageCallbackKHR",
"glGetDebugMessageLogKHR",
"glPopDebugGroupKHR",
"glGetObjectLabelKHR",
"glGetObjectPtrLabelKHR",
"glGetPointervKHR",
"glGetGraphicsResetStatusKHR",
"glReadnPixelsKHR",
"glGetnUniformfvKHR",
"glCopyImageSubDataOES",
"glDisableiOES",
"glBlendEquationiOES",
"glIsQueryEXT",
"glGetQueryivEXT",
"glEnableiEXT",
"glBlendEquationSeparateiEXT",
"glBlendFunciEXT",
"glTexStorage2DEXT",
"eglChooseConfig",
"eglCopyBuffers",
"eglCreateContext",
"eglCreatePbufferSurface",
"eglCreatePixmapSurface",
"eglCreateWindowSurface",
"eglDestroyContext",
"eglDestroySurface",
"eglGetConfigAttrib",
"eglGetConfigs",
"eglGetCurrentDisplay",
"eglGetCurrentSurface",
"eglGetDisplay",
"eglGetError",
"eglInitialize",
"eglMakeCurrent",
"eglQueryContext",
"eglQueryString",
"eglQuerySurface",
"eglSwapBuffers",
"eglTerminate",
"eglWaitGL",
"eglWaitNative",
"eglBindTexImage",
"eglReleaseTexImage",
"eglSurfaceAttrib",
"eglSwapInterval",
"eglBindAPI",
"eglCreatePbufferFromClientBuffer",
"eglReleaseThread",
"eglGetCurrentContext",
"glClearColor",
"glClearStencil",
"glColorMask",
"glCompileShader",
"glCompressedTexImage2D",
"glCompressedTexSubImage2D",
"glDepthFunc",
"glDepthRangef",
"glDetachShader",
"glDisableVertexAttribArray",
"glDrawArrays",
"glDrawElements",
"glEnable",
"glEnableVertexAttribArray",
"glFinish",
"glFlush",
"glGetBufferParameteriv",
"glGetFramebufferAttachmentParameteriv",
"glIsBuffer",
"glIsFramebuffer",
"glIsRenderbuffer",
"glIsShader",
"glIsTexture",
"glLineWidth",
"glLinkProgram",
"glPolygonOffset",
"glReadPixels",
"glReleaseShaderCompiler",
"glRenderbufferStorage",
"glSampleCoverage",
"glScissor",
"glShaderSource",
"glStencilFunc",
"glStencilMask",
"glStencilMaskSeparate",
"glStencilOp",
"glStencilOpSeparate",
"glTexImage2D",
"glTexParameterf",
"glUniform2f",
"glUniform2i",
"glUniform3f",
"glUniform3i",
"glUniform4i",
"glUniformMatrix2fv",
"glUniformMatrix4fv",
"glUseProgram",
"glValidateProgram",
"glVertexAttrib1f",
"glVertexAttrib1fv",
"glVertexAttrib2f",
"glVertexAttrib2fv",
"glVertexAttrib3fv",
"glVertexAttrib4f",
"glVertexAttrib4fv",
"glViewport",
"glBufferStorageEXT",
"glDiscardFramebufferEXT",
"glGenQueriesEXT",
"glDeleteQueriesEXT",
"glIsQueryEXT",
"glBeginQueryEXT",
"glEndQueryEXT",
"glQueryCounterEXT",
"glGetQueryivEXT",
"glGetQueryObjectivEXT",
"glGetQueryObjectuivEXT",
"glGetQueryObjecti64vEXT",
"glGetQueryObjectui64vEXT",
"glEnableiEXT",
"glDisableiEXT",
"glBlendEquationiEXT",
"glBlendEquationSeparateiEXT",
"glBlendFunciEXT",
"glBlendFuncSeparateiEXT",
"glColorMaskiEXT",
"glIsEnablediEXT",
"glDrawElementsBaseVertexEXT",
"glDrawRangeElementsBaseVertexEXT",
"glDrawElementsInstancedBaseVertexEXT",
"glBufferStorageExternalEXT",
"glFramebufferTextureEXT",
"glRenderbufferStorageMultisampleEXT",
"glPrimitiveBoundingBoxEXT",
"glGetSamplerParameterIivEXT",
"glGetSamplerParameterIuivEXT",
"glTexBufferEXT",
"glTexBufferRangeEXT",
"glTexStorage2DEXT",
"glTexStorage3DEXT",
"glDebugMessageControlKHR",
"glDebugMessageInsertKHR",
"glGetObjectLabelKHR",
"glObjectPtrLabelKHR",
"glEGLImageTargetTexture2DOES",
"glEGLImageTargetRenderbufferStorageOES",
"glCopyImageSubDataOES",
"glEnableiOES",
"glDisableiOES",
"glBlendEquationiOES",
"glBlendEquationSeparateiOES",
"glBlendFunciOES",
"glBlendFuncSeparateiOES",
"glColorMaskiOES",
"glIsEnablediOES",
"glDrawElementsBaseVertexOES",
"glDrawRangeElementsBaseVertexOES",
"glDrawElementsInstancedBaseVertexOES",
"glFramebufferTextureOES",
"glGetProgramBinaryOES",
"glProgramBinaryOES",
"glUnmapBufferOES",
"glGetBufferPointervOES",
"glPrimitiveBoundingBoxOES",
"glCopyTexSubImage3DOES",
"glCompressedTexSubImage3DOES",
"glTexParameterIuivOES",
"glGetTexParameterIivOES",
"glGetTexParameterIuivOES",
"glSamplerParameterIivOES",
"glSamplerParameterIuivOES",
"glGetSamplerParameterIivOES",
"glGetSamplerParameterIuivOES",
"glTexBufferOES",
"glTexBufferRangeOES",
"glTexStorage3DMultisampleOES",
"glBindVertexArrayOES",
"glDeleteVertexArraysOES",
"glGenVertexArraysOES",
"glIsVertexArrayOES",
"glFramebufferTextureMultiviewOVR",
"glDrawRangeElements",
"glTexSubImage3D",
"glCopyTexSubImage3D",
"glCompressedTexSubImage3D",
"glGenQueries",
"glGetBufferPointerv",
"glBlitFramebuffer",
"glGenVertexArrays",
"glIsVertexArray",
"glGetIntegeri_v",
"glVertexAttribI4i",
"glVertexAttribI4iv",
"glClearBufferiv",
"glClearBufferfv",
"glClearBufferfi",
"glGetStringi",
"glGetUniformIndices",
"glGetActiveUniformsiv",
"glWaitSync",
"glGetInteger64v",
"glDeleteTransformFeedbacks",
"glIsTransformFeedback",
"glInvalidateSubFramebuffer",
"glGetInternalformativ",
"glGetFramebufferParameteriv",
"glProgramUniform4f",
"glProgramUniform2iv",
"glProgramUniform4iv",
"glProgramUniform2uiv",
"glProgramUniform3uiv",
"glProgramUniform4uiv",
"glProgramUniform2fv",
"glProgramUniform4fv",
"glProgramUniformMatrix2fv",
"glProgramUniformMatrix3fv",
"glProgramUniformMatrix2x3fv",
"glProgramUniformMatrix3x2fv",
"glProgramUniformMatrix2x4fv",
"glProgramUniformMatrix4x2fv",
"glProgramUniformMatrix3x4fv",
"glProgramUniformMatrix4x3fv",
"glValidateProgramPipeline",
"glGetProgramPipelineInfoLog",
"glBindImageTexture",
"glVertexAttribBinding",
"glBlendBarrier",
"glDebugMessageControl",
"glDebugMessageInsert",
"glDebugMessageCallback",
"glGetDebugMessageLog",
"glPushDebugGroup",
"glPopDebugGroup",
"glObjectLabel",
"glGetObjectPtrLabel",
"glEnablei",
"glBlendEquationi",
"glBlendFunci",
"glGetnUniformuiv",
"glPatchParameteri",
"glTexParameterIiv",
"glGetTexParameterIiv",
"glGetTexParameterIuiv",
"glSamplerParameterIiv",
};
static void DoDlsym(void *handle)
{
for (auto &s :syms) {
void *res = dlsym(handle, s.c_str());
if (res == nullptr) {
printf("dlsym failed: %s.n", dlerror());
exit(-1);
}
}
}
static void Bm_function_dlsym_libGLES_mali(benchmark::State &state)
{
void *handle = dlopen(LIBGLES_MALI_PATH, RTLD_LAZY);
if (handle == nullptr) {
printf("dlopen %s failed: %s.n", LIBGLES_MALI_PATH, dlerror());
exit(-1);
}
for (auto _ : state) {
DoDlsym(handle);
}
}
MUSL_BENCHMARK(Bm_function_dlsym_libGLES_mali);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include <locale.h>
#include "sys/types.h"
#include "sys/epoll.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdarg.h"
#include "wchar.h"
#include "time.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"
#include "pthread.h"
#include "stdio.h"
#include "unistd.h"
#include "util.h"
#include "sys/syscall.h"
using namespace std;
static void Bm_function_Setlocale_All(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_ALL, NULL));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Setlocale_All1(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_ALL, "C"));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Setlocale_All2(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_ALL, ""));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Setlocale_Collate(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_COLLATE, NULL));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Setlocale_Ctype(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_CTYPE, NULL));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Setlocale_Time(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(setlocale(LC_TIME, NULL));
}
state.SetBytesProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Setlocale_All);
MUSL_BENCHMARK(Bm_function_Setlocale_All1);
MUSL_BENCHMARK(Bm_function_Setlocale_All2);
MUSL_BENCHMARK(Bm_function_Setlocale_Collate);
MUSL_BENCHMARK(Bm_function_Setlocale_Ctype);
MUSL_BENCHMARK(Bm_function_Setlocale_Time);
#endif
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "malloc.h"
#include "stdlib.h"
#include "util.h"
using namespace std;
#define MALLOC_SIZE 2
static void Bm_function_realloc_twice(benchmark::State &state)
{
size_t size = MALLOC_SIZE;
char *p = (char *)malloc(size);
if (p == nullptr) {
perror("malloc Je_realloc");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(realloc(p, size * 2));
}
if (p != nullptr) {
free(p);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_realloc_half(benchmark::State &state)
{
size_t size = MALLOC_SIZE;
char *p = (char *)malloc(size);
if (p == nullptr) {
perror("malloc Je_realloc");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(realloc(p, size / 2));
}
if (p != nullptr) {
free(p);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_realloc_equal(benchmark::State &state)
{
size_t size = MALLOC_SIZE;
char *p = (char *)malloc(size);
if (p == nullptr) {
perror("malloc Je_realloc");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(realloc(p, size));
}
if (p != nullptr) {
free(p);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_malloc_usable_size(benchmark::State &state)
{
char *p = (char *)malloc(MALLOC_SIZE);
if (p == nullptr) {
perror("malloc Je_malloc_usable_size");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(malloc_usable_size(p));
}
if (p != nullptr) {
free(p);
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_realloc_twice);
MUSL_BENCHMARK(Bm_function_realloc_half);
MUSL_BENCHMARK(Bm_function_realloc_equal);
MUSL_BENCHMARK(Bm_function_malloc_usable_size);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "math.h"
#include "util.h"
using namespace std;
#ifdef ONO_CURRENT_INTERFACE
#define PI 3.14159265
#define FLT_MIN 1.175494351e-38F
// The function generates a value that has the size of the parameter x and the symbol of the parameter y
static void Bm_function_Copysignl(benchmark::State &state)
{
long double x = 2417851639229258349412352.000000;
long double y = 6051711999279104.000000;
for (auto _ : state) {
// x is the result, y is the symbol
benchmark::DoNotOptimize(copysignl(x, y));
}
state.SetItemsProcessed(state.iterations());
}
// remainder
static void Bm_function_Fmodl(benchmark::State &state)
{
long double x = 6051711999279104.000000;
long double y = 536870912.000000;
for (auto _ : state) {
benchmark::DoNotOptimize(fmodl(x, y));
}
state.SetItemsProcessed(state.iterations());
}
#endif
// The 3.14th power of e
static void Bm_function_Exp(benchmark::State &state)
{
double x = 3.14;
for (auto _ : state) {
benchmark::DoNotOptimize(exp(x));
}
state.SetItemsProcessed(state.iterations());
}
#ifdef ONO_CURRENT_INTERFACE
// The logarithm of base e and x
static void Bm_function_Log(benchmark::State &state)
{
double x = 3.14;
for (auto _ : state) {
benchmark::DoNotOptimize(log(x));
}
state.SetItemsProcessed(state.iterations());
}
// Returns the cosine of the radian angle x
static void Bm_function_Cos(benchmark::State &state)
{
double x = 6.302892;
double val = PI / 180.0;
for (auto _ : state) {
benchmark::DoNotOptimize(cos(x * val));
}
state.SetItemsProcessed(state.iterations());
}
// Break floating point number into mantissa and exponent
static void Bm_function_Frexpl(benchmark::State &state)
{
long double x = 1024.0020;
int e;
for (auto _ : state) {
benchmark::DoNotOptimize(frexpl(x, &e));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbn1(benchmark::State &state)
{
double x = 3.8;
int n = 1024;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbn(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbn2(benchmark::State &state)
{
double x = 10.9;
int n = -1023;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbn(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbn3(benchmark::State &state)
{
double x = -100.9;
int n = -100;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbn(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbn4(benchmark::State &state)
{
double x = -100.9;
int n = 100;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbn(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbnl1(benchmark::State &state)
{
long double x = (long double)2e-10;
int n = -16384;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbnl(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbnl2(benchmark::State &state)
{
long double x = (long double)2e-10;
int n = 16384;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbnl(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbnl3(benchmark::State &state)
{
long double x = -PI;
int n = 1000;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbnl(x, n));
}
state.SetItemsProcessed(state.iterations());
}
// x * 2 ^ n
static void Bm_function_Scalbnl4(benchmark::State &state)
{
long double x = -PI;
int n = -1000;
for (auto _ : state) {
benchmark::DoNotOptimize(scalbnl(x, n));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Fmod(benchmark::State &state)
{
double x = 10.5;
double y = 3.2;
for (auto _ : state) {
benchmark::DoNotOptimize(fmod(x, y));
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_sin(benchmark::State &state)
{
double x = 45.0;
double val = PI / 180.0;
for (auto _ : state) {
benchmark::DoNotOptimize(sin(x*val));
}
state.SetItemsProcessed(state.iterations());
}
// FP_INFINITE The specified value is positive or negative infinity
static void Bm_function_fpclassifyl(benchmark::State &state)
{
long double x = log(0.0);
for (auto _ : state) {
benchmark::DoNotOptimize(fpclassify(x));
}
state.SetItemsProcessed(state.iterations());
}
// FP_SUBNORMAL The specified value is a positive or negative normalization value
static void Bm_function_fpclassifyl1(benchmark::State &state)
{
long double x = (FLT_MIN / 2.0);
for (auto _ : state) {
benchmark::DoNotOptimize(fpclassify(x));
}
state.SetItemsProcessed(state.iterations());
}
// FP_NAN The specified value is not a number
static void Bm_function_fpclassifyl2(benchmark::State &state)
{
long double x = sqrt(-1.0);
for (auto _ : state) {
benchmark::DoNotOptimize(fpclassify(x));
}
state.SetItemsProcessed(state.iterations());
}
// FP_ZERO Specify a value of zero
static void Bm_function_fpclassifyl3(benchmark::State &state)
{
long double x = -0.0;
for (auto _ : state) {
benchmark::DoNotOptimize(fpclassify(x));
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Copysignl);
MUSL_BENCHMARK(Bm_function_Fmodl);
MUSL_BENCHMARK(Bm_function_Log);
MUSL_BENCHMARK(Bm_function_Cos);
MUSL_BENCHMARK(Bm_function_Frexpl);
MUSL_BENCHMARK(Bm_function_Scalbn1);
MUSL_BENCHMARK(Bm_function_Scalbn2);
MUSL_BENCHMARK(Bm_function_Scalbn3);
MUSL_BENCHMARK(Bm_function_Scalbn4);
MUSL_BENCHMARK(Bm_function_Scalbnl1);
MUSL_BENCHMARK(Bm_function_Scalbnl2);
MUSL_BENCHMARK(Bm_function_Scalbnl3);
MUSL_BENCHMARK(Bm_function_Scalbnl4);
MUSL_BENCHMARK(Bm_function_Fmod);
MUSL_BENCHMARK(Bm_function_sin);
MUSL_BENCHMARK(Bm_function_fpclassifyl);
MUSL_BENCHMARK(Bm_function_fpclassifyl1);
MUSL_BENCHMARK(Bm_function_fpclassifyl2);
MUSL_BENCHMARK(Bm_function_fpclassifyl3);
#endif
MUSL_BENCHMARK(Bm_function_Exp);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "sys/mman.h"
#include "unistd.h"
#include "stdio.h"
#include "util.h"
using namespace std;
static void Bm_function_Mmap_1(benchmark::State &state)
{
int fd = open("/data/data/my_mmap1.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("open mmap1");
exit(EXIT_FAILURE);
}
size_t length = state.range(0);
for (auto _ : state) {
char* mem = (char *)mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
if (mem != MAP_FAILED) {
benchmark::DoNotOptimize(mem);
state.PauseTiming();
munmap(mem, length);
state.ResumeTiming();
}
}
close(fd);
remove("/data/data/my_mmap1.txt");
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Mmap_2(benchmark::State &state)
{
int fd = open("/data/data/my_mmap2.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("open mmap2");
exit(EXIT_FAILURE);
}
size_t length = state.range(0);
for (auto _ : state) {
char* mem = (char *)mmap(NULL, length, PROT_READ | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd, 0);
if (mem != MAP_FAILED) {
benchmark::DoNotOptimize(mem);
state.PauseTiming();
munmap(mem, length);
state.ResumeTiming();
}
}
close(fd);
remove("/data/data/my_mmap2.txt");
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Mmap_3(benchmark::State &state)
{
int fd = open("/data/data/my_mmap3.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("open mmap3");
exit(EXIT_FAILURE);
}
size_t length = state.range(0);
for (auto _ : state) {
char* mem = (char *)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, fd, 0);
if (mem != MAP_FAILED) {
benchmark::DoNotOptimize(mem);
state.PauseTiming();
munmap(mem, length);
state.ResumeTiming();
}
}
close(fd);
remove("/data/data/my_mmap3.txt");
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Munmap(benchmark::State &state)
{
int fd = open("/data/data/my_munmap.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("open munmap");
exit(EXIT_FAILURE);
}
size_t length = state.range(0);
for (auto _ : state) {
state.PauseTiming();
char* mem = (char*)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mem != MAP_FAILED) {
state.ResumeTiming();
benchmark::DoNotOptimize(munmap(mem, length));
}
}
close(fd);
remove("/data/data/my_munmap.txt");
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Mmap_anonymous(benchmark::State &state)
{
int fd = open("/data/data/my_mmap_anonymous.txt", O_RDWR | O_CREAT);
if (fd == -1) {
perror("open anonymous mmap");
exit(EXIT_FAILURE);
}
size_t length = state.range(0);
for (auto _ : state) {
int *addr = (int*)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (addr != MAP_FAILED) {
benchmark::DoNotOptimize(addr);
state.PauseTiming();
munmap(addr, length);
state.ResumeTiming();
}
}
close(fd);
remove("/data/data/my_mmap_anonymous.txt");
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK_WITH_ARG(Bm_function_Mmap_1, "MMAP_SIZE");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Mmap_2, "MMAP_SIZE");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Mmap_3, "MMAP_SIZE");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Munmap, "MMAP_SIZE");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Mmap_anonymous, "MMAP_SIZE");
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sys/types.h"
#include "sys/socket.h"
#include "netdb.h"
#include "util.h"
using namespace std;
static void Bm_function_Getaddrinfo_external_network(benchmark::State &state)
{
struct addrinfo *res;
for (auto _ : state) {
int n = getaddrinfo("www.baidu.com", nullptr, nullptr, &res);
if (n != 0) {
perror("getaddrinfo external_network");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(n);
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Getaddrinfo_intranet1(benchmark::State &state)
{
struct addrinfo hint;
struct addrinfo *res;
bzero(&hint, sizeof(struct addrinfo));
hint.ai_flags = AI_PASSIVE;
hint.ai_family = AF_UNSPEC;
for (auto _ : state) {
int n = getaddrinfo("127.0.0.1", nullptr, &hint, &res);
if (n != 0) {
perror("getaddrinfo intranet1");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(n);
}
state.SetBytesProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Getaddrinfo_external_network);
MUSL_BENCHMARK(Bm_function_Getaddrinfo_intranet1);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "pthread.h"
#include "threads.h"
#include "unistd.h"
#include "util.h"
static void Bm_function_pthread_mutex_trylock_fast(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_NORMAL);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_trylock_errchk(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_trylock_rec(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_trylock_pi_fast(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_NORMAL);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_trylock_pi_errorcheck(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_trylock_pi_rec(benchmark::State &state)
{
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_trylock(&mutex);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_fast(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_NORMAL);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_errchk(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_rec(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_pi_fast(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_NORMAL);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_pi_errchk(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_mutex_timedlock_pi_rec(benchmark::State &state)
{
struct timespec ts = {.tv_sec = 1, .tv_nsec = 0};
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_INHERIT);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
while (state.KeepRunning()) {
pthread_mutex_timedlock(&mutex, &ts);
pthread_mutex_unlock(&mutex);
}
}
static void Bm_function_pthread_rwlock_tryrdlock(benchmark::State &state)
{
pthread_rwlock_t rwlock;
pthread_rwlock_init(&rwlock, nullptr);
while (state.KeepRunning()) {
pthread_rwlock_tryrdlock(&rwlock);
pthread_rwlock_unlock(&rwlock);
}
pthread_rwlock_destroy(&rwlock);
}
static void Bm_function_pthread_mutex_init(benchmark::State &state)
{
pthread_mutex_t mutex;
for (auto _ : state) {
pthread_mutex_init(&mutex, nullptr);
}
state.SetBytesProcessed(state.iterations());
}
static void BM_pthread_rwlock_tryread(benchmark::State& state)
{
pthread_rwlock_t lock;
pthread_rwlock_init(&lock, nullptr);
while (state.KeepRunning()) {
pthread_rwlock_tryrdlock(&lock);
pthread_rwlock_unlock(&lock);
}
pthread_rwlock_destroy(&lock);
state.SetBytesProcessed(state.iterations());
}
static void BM_pthread_rwlock_trywrite(benchmark::State& state)
{
pthread_rwlock_t lock;
pthread_rwlock_init(&lock, nullptr);
while (state.KeepRunning()) {
pthread_rwlock_trywrlock(&lock);
pthread_rwlock_unlock(&lock);
}
pthread_rwlock_destroy(&lock);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_tss_get(benchmark::State &state)
{
tss_t key;
tss_create(&key, nullptr);
while (state.KeepRunning()) {
tss_get(key);
}
tss_delete(key);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_pthread_rwlock_timedrdlock(benchmark::State &state)
{
struct timespec tout;
time(&tout.tv_sec);
tout.tv_nsec = 0;
tout.tv_sec += 1;
pthread_rwlock_t lock;
pthread_rwlock_init(&lock, nullptr);
while (state.KeepRunning()) {
pthread_rwlock_timedrdlock(&lock, &tout);
pthread_rwlock_unlock(&lock);
}
pthread_rwlock_destroy(&lock);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_pthread_rwlock_timedwrlock(benchmark::State &state)
{
struct timespec tout;
time(&tout.tv_sec);
tout.tv_nsec = 0;
tout.tv_sec += 1;
pthread_rwlock_t lock;
pthread_rwlock_init(&lock, nullptr);
while (state.KeepRunning()) {
pthread_rwlock_timedwrlock(&lock, &tout);
pthread_rwlock_unlock(&lock);
}
pthread_rwlock_destroy(&lock);
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_pthread_cond_timedwait(benchmark::State &state)
{
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 1;
pthread_mutex_lock(&mutex);
while (state.KeepRunning()) {
pthread_cond_timedwait(&cond, &mutex, &ts);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
state.SetBytesProcessed(state.iterations());
}
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
void* thread_task1(void* arg)
{
pthread_mutex_lock(&mutex1);
pthread_cond_wait(&cond, &mutex1);
sleep(1);
pthread_mutex_unlock(&mutex1);
return nullptr;
}
void* thread_task2(void* arg)
{
pthread_mutex_lock(&mutex2);
pthread_cond_wait(&cond, &mutex2);
sleep(1);
pthread_mutex_unlock(&mutex2);
return nullptr;
}
void* broadcastNotifyMutex(void* arg)
{
benchmark::State *statePtr = (benchmark::State *)arg;
statePtr->ResumeTiming();
benchmark::DoNotOptimize(pthread_cond_broadcast(&cond));
statePtr->PauseTiming();
return nullptr;
}
static void Bm_function_pthread_cond_broadcast(benchmark::State &state)
{
while (state.KeepRunning()) {
state.PauseTiming();
pthread_t thread_1, thread_2, thread_3;
pthread_create(&thread_1, nullptr, thread_task1, nullptr);
pthread_create(&thread_2, nullptr, thread_task2, nullptr);
sleep(3);
pthread_create(&thread_3, nullptr, broadcastNotifyMutex, &state);
pthread_join(thread_1, nullptr);
pthread_join(thread_2, nullptr);
pthread_join(thread_3, nullptr);
}
state.SetBytesProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_fast);
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_errchk);
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_rec);
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_pi_fast);
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_pi_errorcheck);
MUSL_BENCHMARK(Bm_function_pthread_mutex_trylock_pi_rec);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_fast);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_errchk);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_rec);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_pi_fast);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_pi_errchk);
MUSL_BENCHMARK(Bm_function_pthread_mutex_timedlock_pi_rec);
MUSL_BENCHMARK(Bm_function_pthread_rwlock_tryrdlock);
MUSL_BENCHMARK(Bm_function_pthread_mutex_init);
MUSL_BENCHMARK(BM_pthread_rwlock_tryread);
MUSL_BENCHMARK(BM_pthread_rwlock_trywrite);
MUSL_BENCHMARK(Bm_function_tss_get);
MUSL_BENCHMARK(Bm_function_pthread_rwlock_timedrdlock);
MUSL_BENCHMARK(Bm_function_pthread_rwlock_timedwrlock);
MUSL_BENCHMARK(Bm_function_pthread_cond_timedwait);
MUSL_BENCHMARK(Bm_function_pthread_cond_broadcast);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sched.h"
#include "util.h"
static void Bm_function_sched_yield(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(sched_yield());
}
}
MUSL_BENCHMARK(Bm_function_sched_yield);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sys/select.h"
#include "sys/time.h"
#include "sys/types.h"
#include "unistd.h"
#include "util.h"
using namespace std;
#define MAX_MONITOR_FDS 2
static void Bm_function_Select(benchmark::State &state)
{
fd_set readfds, writefds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(0, &readfds);
FD_SET(1, &writefds);
for (auto _ : state) {
benchmark::DoNotOptimize(select(MAX_MONITOR_FDS, &readfds, &writefds, 0, 0));
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Select);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "sys/stat.h"
#include "sys/types.h"
#include "fcntl.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "util.h"
using namespace std;
static void Bm_function_Fstatat_relativepath(benchmark::State &state)
{
struct stat st;
int fd = -1;
for (auto _ : state) {
fd = fstatat(AT_FDCWD, "/dev/zero", &st, 0);
if (fd == -1) {
perror("fstatat relativepath");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(fd);
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fstatat_symbollink(benchmark::State &state)
{
symlink("/etc/passwd", "/data/local/tmp/passwd_link");
struct stat st;
int fd = -1;
for (auto _ : state) {
fd = fstatat(AT_FDCWD, "/data/local/tmp/passwd_link", &st, AT_SYMLINK_NOFOLLOW);
if (fd == -1) {
perror("fstatat symbollink");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(fd);
}
remove("/data/local/tmp/passwd_link");
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fstatat_absolutepath(benchmark::State &state)
{
struct stat st;
int ret = -1;
for (auto _ : state) {
ret = fstatat(0, "/dev/zero", &st, 0);
if (ret == -1) {
perror("fstatat absolutepath");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Fstat64(benchmark::State &state)
{
struct stat buf;
int fd = open("/etc/passwd", O_RDONLY);
if (fd == -1) {
perror("open fstat64");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(fstat(fd, &buf));
}
close(fd);
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Mkdir(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(mkdir("/data/data/test_mkdir", S_IRWXU | S_IRWXG | S_IXGRP | S_IROTH | S_IXOTH));
state.PauseTiming();
rmdir("/data/data/test_mkdir");
state.ResumeTiming();
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Fstatat_relativepath);
MUSL_BENCHMARK(Bm_function_Fstatat_symbollink);
MUSL_BENCHMARK(Bm_function_Fstatat_absolutepath);
MUSL_BENCHMARK(Bm_function_Fstat64);
MUSL_BENCHMARK(Bm_function_Mkdir);
#endif
\ No newline at end of file
此差异已折叠。
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "stdlib.h"
#include "stdio.h"
#include "util.h"
using namespace std;
#define BUFFER 100
#ifdef ONO_CURRENT_INTERFACE
int compare(const void *a, const void *b)
{
int num1 = *(int *)a;
int num2 = *(int *)b;
if (num1 < num2) {
return -1;
} else if (num1 > num2) {
return 1;
} else {
return 0;
}
}
// Convert the string pointed to by str to floating point
static void Bm_function_Strtod(benchmark::State &state)
{
const char *var[] = {"+2.86500000e+01", "3.1415", "29",
"-123.456", "1.23e5", "0x1.2p3",
"-inf", "123foo"};
const char *str = var[state.range(0)];
char *ptr;
for (auto _ : state) {
benchmark::DoNotOptimize(strtod(str, &ptr));
}
}
// Used to sort elements in an array
static void Bm_function_Qsort(benchmark::State &state)
{
int arr[] = {5, 3, 7, 1, 9};
int n = sizeof(arr) / sizeof(arr[0]);
for (auto _ : state) {
qsort(arr, n, sizeof(int), compare);
}
}
#endif
static void Bm_function_Getenv_TZ(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(getenv("TZ"));
}
}
static void Bm_function_Getenv_LD_LIBRARY_PATH(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(getenv("LD_LIBRARY_PATH"));
}
}
static void Bm_function_Getenv_LD_PRELOAD(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(getenv("LD_PRELOAD"));
}
}
static void Bm_function_Getenv_LC_ALL(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(getenv("LC_ALL"));
}
}
static void Bm_function_Getenv_LOGNAME(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(getenv("LOGNAME"));
}
}
#ifdef ONO_CURRENT_INTERFACE
// Converts any relative path to an absolute path
static void Bm_function_Realpath(benchmark::State &state)
{
const char *realpathvariable[] = {"./log", "../dev", "log/hilog", "../dev/zero", "/dev"};
const char *path = realpathvariable[state.range(0)];
char resolved_path[BUFFER];
for (auto _ : state) {
benchmark::DoNotOptimize(realpath(path, resolved_path));
}
}
MUSL_BENCHMARK_WITH_ARG(Bm_function_Strtod, "BENCHMARK_VARIABLE");
MUSL_BENCHMARK(Bm_function_Qsort);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Realpath, "REALPATH_VARIABLE");
#endif
MUSL_BENCHMARK(Bm_function_Getenv_TZ);
MUSL_BENCHMARK(Bm_function_Getenv_LD_LIBRARY_PATH);
MUSL_BENCHMARK(Bm_function_Getenv_LD_PRELOAD);
MUSL_BENCHMARK(Bm_function_Getenv_LC_ALL);
MUSL_BENCHMARK(Bm_function_Getenv_LOGNAME);
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "string.h"
#include "wchar.h"
#include "err.h"
#include "util.h"
using namespace std;
// Searches for the first occurrence of the character x in the first n bytes of the selected string
static void Bm_function_Memchr(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t limitsize = state.range(1);
const size_t bmmemchrAlignment = state.range(2);
vector<char> bmmemchr;
char *bmmemchrAligned = GetAlignedPtrFilled(&bmmemchr, bmmemchrAlignment, nbytes, 'n');
bmmemchrAligned[nbytes - 1] = '\0';
while (state.KeepRunning()) {
if (memchr(bmmemchrAligned, 'x', limitsize) != nullptr) {
errx(1, "ERROR: memchr found a chr where it should have failed.");
}
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
#ifdef ONO_CURRENT_INTERFACE
// Search for the last occurrence of the character 'm' in the string pointed to by the parameter test
static void Bm_function_Strrchr(benchmark::State &state)
{
const char test[] = "com.ohos.mms";
const char ch = 'm';
const void *ret;
for (auto _ : state) {
ret = strrchr(test, ch);
benchmark::DoNotOptimize(ret);
}
state.SetBytesProcessed(state.iterations());
}
#endif
// The selected range calculates the length
static void Bm_function_Strnlen(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t limitsize = state.range(1);
const size_t bmstrnlenAlignment = state.range(2);
vector<char> bmstrnlen;
char *bmstrnlenAligned = GetAlignedPtrFilled(&bmstrnlen, bmstrnlenAlignment, nbytes + 1, 'n');
bmstrnlenAligned[nbytes - 1] = '\0';
volatile int c __attribute__((unused)) = 0;
while (state.KeepRunning()) {
c += strnlen(bmstrnlenAligned, limitsize);
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
// Specifies the maximum number of copies to replicate
static void Bm_function_Stpncpy(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t limitsize = state.range(1);
const size_t srcAlignment = state.range(2);
const size_t dstAlignment = state.range(2);
vector<char> src;
vector<char> dst;
char *srcAligned = GetAlignedPtrFilled(&src, srcAlignment, nbytes, 'z');
char *dstAligned = GetAlignedPtr(&dst, dstAlignment, nbytes);
srcAligned[nbytes - 1] = '\0';
while (state.KeepRunning()) {
stpncpy(dstAligned, srcAligned, limitsize);
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
#ifdef ONO_CURRENT_INTERFACE
// Comparing whether two binary blocks of data are equal is functionally similar to MEMCMP
static void Bm_function_Bcmp(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t srcAlignment = state.range(1);
const size_t dstAlignment = state.range(2);
vector<char> src;
vector<char> dst;
char *srcAligned = GetAlignedPtrFilled(&src, srcAlignment, nbytes, 'x');
char *dstAligned = GetAlignedPtrFilled(&dst, dstAlignment, nbytes, 'x');
volatile int c __attribute__((unused)) = 0;
while (state.KeepRunning()) {
c += bcmp(dstAligned, srcAligned, nbytes);
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
// Find the first character in a given string that matches any character in another specified string
static void Bm_function_Strpbrk(benchmark::State &state)
{
const char test1[] = "open.harmony";
const char test2[] = "enh";
for (auto _ : state) {
benchmark::DoNotOptimize(strpbrk(test1, test2));
}
}
// Set the first n characters in the wide string to another wide character
static void Bm_function_Wmemset(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t alignment = state.range(1);
vector<wchar_t> buf;
wchar_t *bufAligned = GetAlignedPtr(&buf, alignment, nbytes + 1);
while (state.KeepRunning()) {
wmemset(bufAligned, L'n', nbytes);
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
static void Bm_function_Wmemcpy(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t srcAlignment = state.range(1);
const size_t dstAlignment = state.range(2);
vector<wchar_t> src;
vector<wchar_t> dst;
wchar_t *srcAligned = GetAlignedPtrFilled(&src, srcAlignment, nbytes, L'z');
wchar_t *dstAligned = GetAlignedPtr(&dst, dstAlignment, nbytes);
while (state.KeepRunning()) {
wmemcpy(dstAligned, srcAligned, nbytes);
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
#endif
// Returns the index value of the first successful match
static void Bm_function_Strcspn(benchmark::State &state)
{
const char *var1[] = {"system/lib64", "system/lib64/chipset-pub-sdk", "vendor/lib64/chipsetsdk",
"system/lib64/ndk", "system/lib64/platformsdk", "system/lib64/priv-platformsdk",
"system/lib64/module/data", "tem/lib64/module/security"};
const char *var2[] = {"vendor/lib64", "/system/lib64/chipset-sdk", "/system/lib64/ndk",
"lib64/chipset-pub-sdk", "priv-platformsdk", "/system/lib64/priv-module",
"/system/lib64/module/multimedia", "/system/lib"};
const char *test1 = var1[state.range(0)];
const char *test2 = var2[state.range(0)];
for (auto _ : state) {
benchmark::DoNotOptimize(strcspn(test1, test2));
}
}
static void Bm_function_Strchrnul_exist(benchmark::State &state)
{
const char *str = "musl.ld.debug.dlclose";
int c = 46;
for (auto _ : state) {
benchmark::DoNotOptimize(strchrnul(str, c));
}
}
static void Bm_function_Strchrnul_noexist(benchmark::State &state)
{
const char *str = "all";
int c = 46;
for (auto _ : state) {
benchmark::DoNotOptimize(strchrnul(str, c));
}
}
static void Bm_function_Strchrnul(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t haystackAlignment = state.range(1);
std::vector<char> haystack;
char *haystackAligned = GetAlignedPtrFilled(&haystack, haystackAlignment, nbytes, 'x');
haystackAligned[nbytes - 1] = '\0';
while (state.KeepRunning()) {
strchrnul(haystackAligned, '.');
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
static void Bm_function_Strcasecmp_capital_equal(benchmark::State &state)
{
const char *l = "ABCDEF";
const char *r = "ABCDEF";
for (auto _ : state) {
benchmark::DoNotOptimize(strcasecmp(l, r));
}
}
static void Bm_function_Strcasecmp_small_equal(benchmark::State &state)
{
const char *l = "abcdef";
const char *r = "abcdef";
for (auto _ : state) {
benchmark::DoNotOptimize(strcasecmp(l, r));
}
}
static void Bm_function_Strcasecmp_equal(benchmark::State &state)
{
const char *l = "aBcDeD";
const char *r = "ABCdEd";
for (auto _ : state) {
benchmark::DoNotOptimize(strcasecmp(l, r));
}
}
static void Bm_function_Strcasecmp_not_equal(benchmark::State &state)
{
const char *l = "bbcdef";
const char *r = "bBCdEd";
for (auto _ : state) {
benchmark::DoNotOptimize(strcasecmp(l, r));
}
}
static void Bm_function_Strcasecmp(benchmark::State &state)
{
const size_t nbytes = state.range(0);
const size_t haystackAlignment = state.range(1);
const size_t needleAlignment = state.range(2);
std::vector<char> haystack;
std::vector<char> needle;
char *haystackAligned = GetAlignedPtrFilled(&haystack, haystackAlignment, nbytes, 'x');
char *needleAligned = GetAlignedPtrFilled(&needle, needleAlignment, nbytes, 'x');
for (auto _ : state) {
benchmark::DoNotOptimize(strcasecmp(haystackAligned, needleAligned));
}
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
}
MUSL_BENCHMARK_WITH_ARG(Bm_function_Memchr, "STRING_LIMIT");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Strnlen, "STRING_LIMIT");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Stpncpy, "STRING_LIMIT");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Strcspn, "BENCHMARK_VARIABLE");
MUSL_BENCHMARK(Bm_function_Strchrnul_exist);
MUSL_BENCHMARK(Bm_function_Strchrnul_noexist);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Strchrnul, "ALIGNED_ONEBUF");
MUSL_BENCHMARK(Bm_function_Strcasecmp_capital_equal);
MUSL_BENCHMARK(Bm_function_Strcasecmp_small_equal);
MUSL_BENCHMARK(Bm_function_Strcasecmp_equal);
MUSL_BENCHMARK(Bm_function_Strcasecmp_not_equal);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Strcasecmp, "ALIGNED_TWOBUF");
#ifdef ONO_CURRENT_INTERFACE
MUSL_BENCHMARK(Bm_function_Strrchr);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Bcmp, "ALIGNED_TWOBUF");
MUSL_BENCHMARK(Bm_function_Strpbrk);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Wmemset, "ALIGNED_ONEBUF");
MUSL_BENCHMARK_WITH_ARG(Bm_function_Wmemcpy, "ALIGNED_TWOBUF");
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include <sys/syscall.h>
#include <string.h>
#include <sys/timex.h>
#include <signal.h>
#include <sys/resource.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <sys/utsname.h>
#include "util.h"
using namespace std;
#define BYTES_WRITTEN 8
#define BUFSIZE 10
// Gets the identification code of the current process
static void Bm_function_Syscall_getpid(benchmark::State &state)
{
pid_t pid;
for (auto _ : state) {
pid = syscall(SYS_getpid);
benchmark::DoNotOptimize(pid);
}
}
// The system call gets the current thread ID
static void Bm_function_Syscall_gettid(benchmark::State &state)
{
pid_t tid;
for (auto _ : state) {
tid = syscall(SYS_gettid);
benchmark::DoNotOptimize(tid);
}
}
// Used to read the kernel time
static void Bm_function_Syscall_adjtimex(benchmark::State &state)
{
struct timex time_info;
for (auto _ : state) {
benchmark::DoNotOptimize(syscall(SYS_adjtimex, &time_info));
}
}
// Writes the contents of the file of the user buffer to the corresponding location of the file on disk
static void Bm_function_Syscall_write(benchmark::State &state)
{
int fp = open("/dev/zero", O_RDWR);
if (fp == -1) {
perror("open sys_write");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
benchmark::DoNotOptimize(syscall(SYS_write, fp, "Bad Mind", BYTES_WRITTEN));
}
close(fp);
}
// Transfer 10 bytes into the BUFF in the open file
static void Bm_function_Syscall_read(benchmark::State &state)
{
char buf[BUFSIZE];
int fd = open("/dev/zero", O_RDONLY);
if (fd == -1) {
perror("syscall read");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
ssize_t ret = syscall(SYS_read, fd, buf, BUFSIZE);
if (ret < 0) {
perror("syscall read");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
}
// Manipulate the characteristics of a file based on the file descriptor
static void Bm_function_Syscall_fcntl(benchmark::State &state)
{
long int ret;
int fd = open("/dev/zero", O_RDONLY);
if (fd == -1) {
perror("syscall_fcntl open");
exit(EXIT_FAILURE);
}
for (auto _ : state) {
ret = syscall(SYS_fcntl, fd, F_GETFL);
if (ret == -1) {
perror("syscall_fcntl fail");
exit(EXIT_FAILURE);
}
benchmark::DoNotOptimize(ret);
}
close(fd);
}
// Obtain system resource usage status
static void Bm_function_Syscall_getrusage(benchmark::State &state)
{
struct rusage usage;
long result;
for (auto _ : state) {
result = syscall(SYS_getrusage, RUSAGE_SELF, &usage);
benchmark::DoNotOptimize(result);
}
if (result != 0) {
printf("SYS_getrusage error\n");
}
}
// Obtain system information
static void Bm_function_Syscall_uname(benchmark::State &state)
{
struct utsname buf;
long int ret;
for (auto _ : state) {
ret = syscall(SYS_uname, &buf);
benchmark::DoNotOptimize(ret);
}
if (ret != 0) {
printf("SYS_uname error\n");
}
}
MUSL_BENCHMARK(Bm_function_Syscall_getpid);
MUSL_BENCHMARK(Bm_function_Syscall_gettid);
MUSL_BENCHMARK(Bm_function_Syscall_adjtimex);
MUSL_BENCHMARK(Bm_function_Syscall_write);
MUSL_BENCHMARK(Bm_function_Syscall_read);
MUSL_BENCHMARK(Bm_function_Syscall_fcntl);
MUSL_BENCHMARK(Bm_function_Syscall_getrusage);
MUSL_BENCHMARK(Bm_function_Syscall_uname);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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.
*/
#ifdef ONO_CURRENT_INTERFACE
#include <benchmark/benchmark.h>
#include "time.h"
#include "util.h"
using namespace std;
// Used to put the current thread to sleep for the specified time
static void Bm_function_Nanosleep_0ns(benchmark::State &state)
{
struct timespec req, rem;
req.tv_nsec = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(nanosleep(&req, &rem));
}
}
static void Bm_function_Nanosleep_10ns(benchmark::State &state)
{
struct timespec req, rem;
req.tv_nsec = 10;
for (auto _ : state) {
benchmark::DoNotOptimize(nanosleep(&req, &rem));
}
}
static void Bm_function_Nanosleep_100ns(benchmark::State &state)
{
struct timespec req, rem;
req.tv_nsec = 100;
for (auto _ : state) {
benchmark::DoNotOptimize(nanosleep(&req, &rem));
}
}
// Used to set information about the time zone
static void Bm_function_Tzset(benchmark::State &state)
{
while (state.KeepRunning()) {
tzset();
}
}
MUSL_BENCHMARK(Bm_function_Nanosleep_0ns);
MUSL_BENCHMARK(Bm_function_Nanosleep_10ns);
MUSL_BENCHMARK(Bm_function_Nanosleep_100ns);
MUSL_BENCHMARK(Bm_function_Tzset);
#endif
\ No newline at end of file
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* 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 <benchmark/benchmark.h>
#include "unistd.h"
#include "stddef.h"
#include "fenv.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/uio.h"
#include "sys/utsname.h"
#include "fcntl.h"
#include "stdio.h"
#include "sys/time.h"
#include "time.h"
#include "err.h"
#include "util.h"
using namespace std;
#define BUFFER 1024
void ReadWriteTest(benchmark::State& state, bool isRead)
{
size_t chunkSize = state.range(0);
int fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
perror("open ReadWriteTest");
exit(EXIT_FAILURE);
}
char* bufRead = new char[chunkSize];
const char *bufWrite = "hello world!";
if (isRead) {
memset(bufRead, 0, chunkSize * sizeof(char));
}
while (state.KeepRunning()) {
if (isRead) {
if (read(fd, bufRead, chunkSize) == -1) {
perror("Read proc");
exit(EXIT_FAILURE);
}
} else {
if (write(fd, bufWrite, chunkSize) == -1) {
perror("write proc");
exit(EXIT_FAILURE);
}
}
}
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunkSize));
delete[] bufRead;
close(fd);
}
#ifdef ONO_CURRENT_INTERFACE
template <typename Fn>
void PreadWriteTest(benchmark::State &state, Fn f, bool buffered)
{
size_t chunkSize = state.range(0);
int fp = open("/dev/zero", O_RDWR);
char *buf = new char[chunkSize];
off64_t offset = 0;
while (state.KeepRunning()) {
if (f(fp, buf, strlen(buf), offset) == -1) {
errx(1, "ERROR: op of %zu bytes failed.", chunkSize);
}
}
state.SetBytesProcessed(int64_t(state.iterations()) * int64_t(chunkSize));
delete[] buf;
close(fp);
}
// Obtain the identification code of the current process
static void Bm_function_Getpid(benchmark::State &state)
{
pid_t pid;
for (auto _ : state) {
pid = getpid();
benchmark::DoNotOptimize(pid);
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Geteuid(benchmark::State &state)
{
uid_t uid;
for (auto _ : state) {
uid = geteuid();
benchmark::DoNotOptimize(uid);
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Close(benchmark::State &state)
{
for (auto _ : state) {
state.PauseTiming();
int fd = open("/dev/zero", O_RDONLY);
if (fd == -1) {
perror("open Close");
exit(EXIT_FAILURE);
}
state.ResumeTiming();
benchmark::DoNotOptimize(close(fd));
}
state.SetBytesProcessed(state.iterations());
}
#define SECOND 50
static void Bm_function_Usleep(benchmark::State &state)
{
for (auto _ : state) {
benchmark::DoNotOptimize(usleep(SECOND));
}
state.SetItemsProcessed(state.iterations());
}
void Bm_function_Pwrite64(benchmark::State &state)
{
PreadWriteTest(state, pwrite, true);
}
void Bm_function_Pread64(benchmark::State &state)
{
PreadWriteTest(state, pread, true);
}
// Stores the destination path of the symbolic link file into a buffer and returns the length of the destination path
static void Bm_function_Readlink(benchmark::State &state)
{
char path[BUFFER] = {0};
ssize_t len;
int i = symlink("/dev/zero", "/data/local/tmp/tmplink");
if (i == -1) {
perror("symlink");
exit(-1);
}
for (auto _ : state) {
len = readlink("/data/local/tmp/tmplink", path, sizeof(path));
benchmark::DoNotOptimize(len);
}
remove("/data/local/tmp/tmplink");
}
static void Bm_function_Getuid(benchmark::State &state)
{
uid_t uid;
for (auto _ : state) {
uid = getuid();
benchmark::DoNotOptimize(uid);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Getegid(benchmark::State &state)
{
gid_t gid;
for (auto _ : state) {
gid = getegid();
benchmark::DoNotOptimize(gid);
}
state.SetItemsProcessed(state.iterations());
}
static void Bm_function_Read(benchmark::State &state)
{
ReadWriteTest(state, true);
}
#endif
static void Bm_function_Write(benchmark::State &state)
{
ReadWriteTest(state, false);
}
#ifdef ONO_CURRENT_INTERFACE
static void Bm_function_Access_exist(benchmark::State &state)
{
const char *filename = "/data";
for (auto _ : state) {
benchmark::DoNotOptimize(access(filename, F_OK));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Access_read(benchmark::State &state)
{
const char *filename = "/data";
for (auto _ : state) {
benchmark::DoNotOptimize(access(filename, R_OK));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Access_write(benchmark::State &state)
{
const char *filename = "/data";
for (auto _ : state) {
benchmark::DoNotOptimize(access(filename, W_OK));
}
state.SetBytesProcessed(state.iterations());
}
static void Bm_function_Access_execute(benchmark::State &state)
{
const char *filename = "/data";
for (auto _ : state) {
benchmark::DoNotOptimize(access(filename, X_OK));
}
state.SetBytesProcessed(state.iterations());
}
static const char *writevvariable1[] = {"Pretend inferiority and", "hello,",
"non sa module libtoken_sync_manager_service.z.so",
"The variable device_company was",
"but never appeared in a",
"The build continued as if",
"product_name=rk3568", "build/toolchain/ohos:"};
static const char *writevvariable2[] = {"encourage others arrogance!", "world!", ":token_sync_manager_service",
"set as a build argument", "declare_args() block in any buildfile",
"that argument was unspecified", "ohos_build_type=", "ohos_clang_arm64"};
// Write the contents of multiple buffers to the file descriptor at once
static void Bm_function_Writev(benchmark::State &state)
{
int fd = open("/dev/zero", O_RDWR);
const char *str1 = writevvariable1[state.range(0)];
const char *str2 = writevvariable2[state.range(0)];
struct iovec iov[2];
ssize_t nwritten;
iov[0].iov_base = (void *)str1;
iov[0].iov_len = strlen(str1);
iov[1].iov_base = (void *)str2;
iov[1].iov_len = strlen(str2);
for (auto _ : state) {
nwritten = writev(fd, iov, 2);
benchmark::DoNotOptimize(nwritten);
}
close(fd);
}
static void Bm_function_Uname(benchmark::State &state)
{
for (auto _ : state) {
struct utsname buffer;
benchmark::DoNotOptimize(uname(&buffer));
}
state.SetItemsProcessed(state.iterations());
}
MUSL_BENCHMARK(Bm_function_Getpid);
MUSL_BENCHMARK(Bm_function_Geteuid);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Pwrite64, "COMMON_ARGS");
MUSL_BENCHMARK(Bm_function_Readlink);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Pread64, "COMMON_ARGS");
MUSL_BENCHMARK(Bm_function_Close);
MUSL_BENCHMARK(Bm_function_Usleep);
MUSL_BENCHMARK(Bm_function_Getuid);
MUSL_BENCHMARK(Bm_function_Getegid);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Read, "COMMON_ARGS");
MUSL_BENCHMARK(Bm_function_Access_exist);
MUSL_BENCHMARK(Bm_function_Access_read);
MUSL_BENCHMARK(Bm_function_Access_write);
MUSL_BENCHMARK(Bm_function_Access_execute);
MUSL_BENCHMARK_WITH_ARG(Bm_function_Writev, "BENCHMARK_VARIABLE");
MUSL_BENCHMARK(Bm_function_Uname);
#endif
MUSL_BENCHMARK_WITH_ARG(Bm_function_Write, "COMMON_ARGS");
\ No newline at end of file
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册