dart-sdk/benchmarks/NativeCall/dart2/dlopen_helper.dart
Clement Skau 36652d3b1a [benchmark] Adds native call benchmark.
Adds new benchmarks for native calls, parallel to the
existing benchmark for FFI calls.

This makes it possible (with some caveats) to compare
overheads of calling through native and FFI.

Local results on Linux, x64, AOT (for reference):

NativeCall.Uint8x01(RunTime): 585.9797891036907 us.
NativeCall.Int64x20(RunTime): 1340.2451440053583 us.
NativeCall.Doublex01(RunTime): 694.4875 us.
NativeCall.Doublex20(RunTime): 1610.102172164119 us.
NativeCall.Handlex01(RunTime): 735.7863184994483 us.
NativeCall.Handlex20(RunTime): 836.6783772480134 us.

FfiCall.Uint8x01(RunTime): 202.5837131570951 us.
FfiCall.Int64x20(RunTime): 328.16931911402787 us.
FfiCall.Doublex01(RunTime): 220.58028231142478 us.
FfiCall.Doublex20(RunTime): 373.4350261389096 us.
FfiCall.Handlex01(RunTime): 357.4213724088635 us.
FfiCall.Handlex20(RunTime): 1152.427995391705 us.

TEST=Manually ran benchmark locally.
Change-Id: Ib28455fbd9f739c1e3ba487b932b464fc12b7e04
Cq-Include-Trybots: luci.dart.try:benchmark-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/218920
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
2021-11-04 15:16:24 +00:00

63 lines
1.7 KiB
Dart

// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart=2.9
import 'dart:ffi';
import 'dart:io';
const arm = 'arm';
const arm64 = 'arm64';
const ia32 = 'ia32';
const x64 = 'x64';
// https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
final _unames = {
'arm': arm,
'aarch64_be': arm64,
'aarch64': arm64,
'armv8b': arm64,
'armv8l': arm64,
'i386': ia32,
'i686': ia32,
'x86_64': x64,
};
String _checkRunningMode(String architecture) {
// Check if we're running in 32bit mode.
final int pointerSize = sizeOf<IntPtr>();
if (pointerSize == 4 && architecture == x64) return ia32;
if (pointerSize == 4 && architecture == arm64) return arm;
return architecture;
}
String _architecture() {
final String uname = Process.runSync('uname', ['-m']).stdout.trim();
final String architecture = _unames[uname];
if (architecture == null) {
throw Exception('Unrecognized architecture: "$uname"');
}
// Check if we're running in 32bit mode.
return _checkRunningMode(architecture);
}
String _platformPath(String name, {String path = ''}) {
if (Platform.isMacOS || Platform.isIOS) {
return '${path}mac/${_architecture()}/lib$name.dylib';
}
if (Platform.isWindows) {
return '${path}win/${_checkRunningMode(x64)}/$name.dll';
}
// Unknown platforms default to Unix implementation.
return '${path}linux/${_architecture()}/lib$name.so';
}
DynamicLibrary dlopenPlatformSpecific(String name, {String path}) {
final String fullPath = _platformPath(name, path: path);
return DynamicLibrary.open(fullPath);
}