add membench-NX - open source memory benchmark tool

also remove memtester, it doesnt really work
This commit is contained in:
souldbminersmwc
2026-06-06 17:33:14 -04:00
parent 380f621b39
commit 7634ff767b
24 changed files with 748 additions and 2888 deletions

View File

@@ -0,0 +1,22 @@
/*
* Copyright © 2011-2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

227
Source/Membench-NX/Makefile Normal file
View File

@@ -0,0 +1,227 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional)
#
# NO_ICON: if set to anything, do not use icon.
# NO_NACP: if set to anything, no .nacp file is generated.
# APP_TITLE is the name of the app stored in the .nacp file (Optional)
# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
# APP_VERSION is the version of the app stored in the .nacp file (Optional)
# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
# ICON is the filename of the icon (.jpg), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.jpg
# - icon.jpg
# - <libnx folder>/default_icon.jpg
#
# CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.json
# - config.json
# If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead
# of a homebrew executable (.nro). This is intended to be used for sysmodules.
# NACP building is skipped as well.
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
#ROMFS := romfs
APP_TITLE := Membench-NX
APP_VERSION := 1.0.0
APP_AUTHOR := KazushiMe and Horizon-OC
TARGET_VERSION := 1.0.0
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE -fPIC
CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -D__SWITCH__
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -pthread
LIBS := -lGLESv2 -lEGL -lglapi -ldrm_nouveau -lstdc++ -lnx -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(LIBNX)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
ifeq ($(strip $(CONFIG_JSON)),)
jsons := $(wildcard *.json)
ifneq (,$(findstring $(TARGET).json,$(jsons)))
export APP_JSON := $(TOPDIR)/$(TARGET).json
else
ifneq (,$(findstring config.json,$(jsons)))
export APP_JSON := $(TOPDIR)/config.json
endif
endif
else
export APP_JSON := $(TOPDIR)/$(CONFIG_JSON)
endif
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.jpg)
ifneq (,$(findstring $(TARGET).jpg,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).jpg
else
ifneq (,$(findstring icon.jpg,$(icons)))
export APP_ICON := $(TOPDIR)/icon.jpg
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
ifeq ($(strip $(NO_ICON)),)
export NROFLAGS += --icon=$(APP_ICON)
endif
ifeq ($(strip $(NO_NACP)),)
export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp
endif
ifneq ($(APP_TITLEID),)
export NACPFLAGS += --titleid=$(APP_TITLEID)
endif
ifneq ($(ROMFS),)
export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
ifeq ($(strip $(APP_JSON)),)
@rm -fr $(BUILD) $(TARGET).nro $(TARGET).nacp $(TARGET).elf
else
@rm -fr $(BUILD) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf
endif
#---------------------------------------------------------------------------------
else
.PHONY: all
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
ifeq ($(strip $(APP_JSON)),)
all : $(OUTPUT).nro
ifeq ($(strip $(NO_NACP)),)
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
else
$(OUTPUT).nro : $(OUTPUT).elf
endif
else
all : $(OUTPUT).nsp
$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm
$(OUTPUT).nso : $(OUTPUT).elf
endif
$(OUTPUT).elf : $(OFILES)
$(OFILES_SRC) : $(HFILES_BIN)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Source/Membench-NX/icon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

View File

@@ -0,0 +1,22 @@
This is a simple memory benchmark program, which tries to measure the peak
bandwidth of sequential memory accesses and the latency of random memory
accesses. Bandwidth is measured by running different assembly code for
the aligned memory blocks and attempting different prefetch strategies.
The benchmark results for some hardware can be found in the wiki page:
https://github.com/ssvb/tinymembench/wiki
This program can be compiled in either linux or windows (via mingw32 and msys)
by simply running make:
$ make
Adding extra optimization options is possible (in linux):
$ CFLAGS="-O2 -march=atom -mtune=atom" make
Example of crosscompiling for ARM (also in linux):
$ CC=arm-linux-gnueabihf-gcc CFLAGS="-O2 -mcpu=cortex-a9" make
Example of crosscompiling and running the benchmark on android device:
$ CC=arm-linux-gnueabihf-gcc CFLAGS="-O2 -mcpu=cortex-a8 -static" make
$ adb push tinymembench /data/local/tmp/tinymembench
$ adb shell /data/local/tmp/tinymembench

View File

@@ -0,0 +1,54 @@
/*
* Copyright © 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __AARCH64_ASM_H__
#define __AARCH64_ASM_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
void aligned_block_read_ldp_x_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_read_ldp_q_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_x_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_q_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ld1st1_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_q_pf32_l2strm_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_q_pf64_l2strm_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_q_pf32_l1keep_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_copy_ldpstp_q_pf64_l1keep_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_fill_stp_x_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_fill_stp_q_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_fill_stnp_x_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
void aligned_block_fill_stnp_q_aarch64(int64_t *__restrict dst, int64_t *__restrict src, int size);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,207 @@
/*
* Copyright © 2016 Siarhei Siamashka <siarhei.siamashka@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef __aarch64__
.cpu cortex-a57+fp+simd
.text
.align 2
#define PREFETCH_DISTANCE 320
.macro asm_function function_name
.global \function_name
.type \function_name,%function
.func \function_name
\function_name:
DST .req x0
SRC .req x1
SIZE .req x2
.endm
asm_function aligned_block_read_ldp_x_aarch64
0:
ldp x3, x4, [DST, #(0 * 16)]
ldp x5, x6, [DST, #(1 * 16)]
ldp x7, x8, [DST, #(2 * 16)]
ldp x9, x10, [DST, #(3 * 16)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_x_aarch64
0:
ldp x3, x4, [SRC, #(0 * 16)]
ldp x5, x6, [SRC, #(1 * 16)]
ldp x7, x8, [SRC, #(2 * 16)]
ldp x9, x10, [SRC, #(3 * 16)]
add SRC, SRC, #64
stp x3, x4, [DST, #(0 * 16)]
stp x5, x6, [DST, #(1 * 16)]
stp x7, x8, [DST, #(2 * 16)]
stp x9, x10, [DST, #(3 * 16)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_read_ldp_q_aarch64
0:
ldp q0, q1, [DST, #(0 * 32)]
ldp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_q_aarch64
0:
ldp q0, q1, [SRC, #(0 * 32)]
ldp q2, q3, [SRC, #(1 * 32)]
add SRC, SRC, #64
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_q_pf32_l2strm_aarch64
0:
prfm pldl2strm, [SRC, #(PREFETCH_DISTANCE + 0)]
ldp q0, q1, [SRC, #(0 * 32)]
prfm pldl2strm, [SRC, #(PREFETCH_DISTANCE + 32)]
ldp q2, q3, [SRC, #(1 * 32)]
add SRC, SRC, #64
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_q_pf64_l2strm_aarch64
0:
prfm pldl2strm, [SRC, #(PREFETCH_DISTANCE)]
ldp q0, q1, [SRC, #(0 * 32)]
ldp q2, q3, [SRC, #(1 * 32)]
add SRC, SRC, #64
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_q_pf32_l1keep_aarch64
0:
prfm pldl1keep, [SRC, #(PREFETCH_DISTANCE + 0)]
ldp q0, q1, [SRC, #(0 * 32)]
prfm pldl1keep, [SRC, #(PREFETCH_DISTANCE + 32)]
ldp q2, q3, [SRC, #(1 * 32)]
add SRC, SRC, #64
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ldpstp_q_pf64_l1keep_aarch64
0:
prfm pldl1keep, [SRC, #(PREFETCH_DISTANCE)]
ldp q0, q1, [SRC, #(0 * 32)]
ldp q2, q3, [SRC, #(1 * 32)]
add SRC, SRC, #64
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_fill_stp_x_aarch64
0:
stp x3, x4, [DST, #(0 * 16)]
stp x5, x6, [DST, #(1 * 16)]
stp x7, x8, [DST, #(2 * 16)]
stp x9, x10, [DST, #(3 * 16)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_fill_stp_q_aarch64
0:
stp q0, q1, [DST, #(0 * 32)]
stp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_fill_stnp_x_aarch64
0:
stnp x3, x4, [DST, #(0 * 16)]
stnp x5, x6, [DST, #(1 * 16)]
stnp x7, x8, [DST, #(2 * 16)]
stnp x9, x10, [DST, #(3 * 16)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_fill_stnp_q_aarch64
0:
stnp q0, q1, [DST, #(0 * 32)]
stnp q2, q3, [DST, #(1 * 32)]
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
asm_function aligned_block_copy_ld1st1_aarch64
0:
ld1 {v0.16b, v1.16b, v2.16b, v3.16b}, [SRC]
st1 {v0.16b, v1.16b, v2.16b, v3.16b}, [DST]
add SRC, SRC, #64
add DST, DST, #64
subs SIZE, SIZE, #64
bgt 0b
ret
.endfunc
#endif

View File

@@ -0,0 +1,263 @@
/*
* gpu_bw.c - GPU bandwidth benchmark
* Mirrors FUN_71000667e0 / FUN_7100056130 from decompiled binary exactly.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <switch.h>
#include "gpu_bw.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl31.h>
static EGLDisplay s_display = EGL_NO_DISPLAY;
static EGLContext s_context = EGL_NO_CONTEXT;
static bool egl_init(void) {
s_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (s_display == EGL_NO_DISPLAY) {
printf("EGL: no display\n");
consoleUpdate(NULL);
return false;
}
if (!eglInitialize(s_display, NULL, NULL)) {
printf("EGL: initialize failed (0x%x)\n", eglGetError());
consoleUpdate(NULL);
return false;
}
if (!eglBindAPI(EGL_OPENGL_API)) {
printf("EGL: bindAPI(OPENGL) failed (0x%x)\n", eglGetError());
consoleUpdate(NULL);
return false;
}
const char *exts = eglQueryString(s_display, EGL_EXTENSIONS);
if (!exts || !strstr(exts, "EGL_KHR_surfaceless_context")) {
printf("EGL: no surfaceless_context\n");
consoleUpdate(NULL);
return false;
}
static const EGLint cfg_attribs[] = {
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_DEPTH_SIZE,
24,
EGL_STENCIL_SIZE,
8,
EGL_NONE,
};
EGLConfig cfg;
EGLint n;
if (!eglChooseConfig(s_display, cfg_attribs, &cfg, 1, &n) || !n) {
printf("EGL: chooseConfig failed n=%d (0x%x)\n", (int)n, eglGetError());
consoleUpdate(NULL);
return false;
}
static const EGLint ctx_attribs[] = {
EGL_CONTEXT_MAJOR_VERSION_KHR, 4, EGL_CONTEXT_MINOR_VERSION_KHR, 3, EGL_NONE,
};
s_context = eglCreateContext(s_display, cfg, EGL_NO_CONTEXT, ctx_attribs);
if (s_context == EGL_NO_CONTEXT) {
printf("EGL: createContext failed (0x%x)\n", eglGetError());
consoleUpdate(NULL);
return false;
}
if (eglMakeCurrent(s_display, EGL_NO_SURFACE, EGL_NO_SURFACE, s_context) != EGL_TRUE) {
printf("EGL: makeCurrent failed (0x%x)\n", eglGetError());
consoleUpdate(NULL);
return false;
}
return true;
}
static void egl_exit(void) {
eglMakeCurrent(s_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (s_context != EGL_NO_CONTEXT) {
eglDestroyContext(s_display, s_context);
s_context = EGL_NO_CONTEXT;
}
if (s_display != EGL_NO_DISPLAY) {
eglTerminate(s_display);
s_display = EGL_NO_DISPLAY;
}
}
static GLuint compile_compute(const char *src, const char *name) {
GLuint sh = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(sh, 1, &src, NULL);
glCompileShader(sh);
GLint ok = 0;
glGetShaderiv(sh, GL_COMPILE_STATUS, &ok);
if (!ok) {
char log[256] = { 0 };
glGetShaderInfoLog(sh, sizeof(log), NULL, log);
printf("Compute shader compile failed: %s\n", log[0] ? log : name);
glDeleteShader(sh);
return 0;
}
GLuint prog = glCreateProgram();
glAttachShader(prog, sh);
glLinkProgram(prog);
glDeleteShader(sh);
glGetProgramiv(prog, GL_LINK_STATUS, &ok);
if (!ok) {
glDeleteProgram(prog);
return 0;
}
return prog;
}
/*
* Mirrors FUN_7100056130:
* - one warmup dispatch + glFinish (before timing)
* - `loops` timed dispatches + glFinish
* - cntpct_el0 timing (19.2 MHz, ticks * 625/12/1e9 = seconds)
* - returns (buf_bytes * loops) / elapsed / 1e6 [MB/s]
*/
static double run_pass(GLuint prog, GLuint ssbo_src, GLuint ssbo_dst, size_t buf_bytes, int loops) {
GLuint groups = (GLuint)(buf_bytes >> 10);
glUseProgram(prog);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo_src);
if (ssbo_dst)
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo_dst);
glDispatchCompute(groups, 1, 1);
glFinish();
if (glGetError() != GL_NO_ERROR)
return 0.0;
uint64_t t0, t1;
asm volatile("mrs %0, cntpct_el0" : "=r"(t0));
for (int i = 0; i < loops; i++)
glDispatchCompute(groups, 1, 1);
glFinish();
asm volatile("mrs %0, cntpct_el0" : "=r"(t1));
if (glGetError() != GL_NO_ERROR)
return 0.0;
double elapsed = (double)(t1 - t0) * 625.0 / 12.0 / 1000000000.0;
if (elapsed <= 0.0)
return 0.0;
return ((double)buf_bytes * (double)loops) / elapsed / 1000000.0;
}
bool gpu_bw_run(bool is_4gb, double *copy_out, double *read_out, double *write_out) {
/* Exact GLSL sources from binary — desktop GL 4.3 */
static const char *src_copy = "\n#version 430\n"
"layout(std430, binding = 0) buffer srcBuffer { volatile uint src[]; };\n"
"layout(std430, binding = 1) buffer dstBuffer { volatile uint dst[]; };\n"
"layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;\n"
"void main() {\n"
" dst[gl_GlobalInvocationID.x] = src[gl_GlobalInvocationID.x];\n"
"}\n";
static const char *src_read = "\n#version 430\n"
"layout(std430, binding = 0) buffer srcBuffer { volatile uint src[]; };\n"
"shared uint tmp;\n"
"layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;\n"
"void main() {\n"
" tmp |= src[gl_GlobalInvocationID.x];\n"
"}\n";
static const char *src_write = "\n#version 430\n"
"layout(std430, binding = 0) buffer srcBuffer { volatile uint src[]; };\n"
"layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;\n"
"void main() {\n"
" src[gl_GlobalInvocationID.x] = 0xAAAAAAAAu;\n"
"}\n";
if (!egl_init()) {
printf("Failed to initialize EGL/GL for GPU benchmark.\n");
consoleUpdate(NULL);
return false;
}
GLint max_ssbo = 0;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_ssbo);
if (max_ssbo < 1) {
printf("Failed to query GPU SSBO size.\n");
consoleUpdate(NULL);
egl_exit();
return false;
}
size_t buf_bytes = (size_t)max_ssbo;
if (buf_bytes > 0x8000000)
buf_bytes = 0x8000000;
if (!is_4gb)
buf_bytes >>= 1;
buf_bytes &= ~(size_t)0x3FF;
if (buf_bytes < 0x400) {
printf("GPU benchmark buffer is too small.\n");
consoleUpdate(NULL);
egl_exit();
return false;
}
int loops = is_4gb ? 400 : 800;
int wloops = loops / 10;
GLuint prog_copy = compile_compute(src_copy, "GPU Copy");
GLuint prog_read = compile_compute(src_read, "GPU Read");
GLuint prog_write = compile_compute(src_write, "GPU Write");
if (!prog_copy || !prog_read || !prog_write) {
if (prog_copy)
glDeleteProgram(prog_copy);
if (prog_read)
glDeleteProgram(prog_read);
if (prog_write)
glDeleteProgram(prog_write);
egl_exit();
return false;
}
GLuint ssbo[2] = { 0, 0 };
glGenBuffers(2, ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo[0]);
glBufferData(GL_SHADER_STORAGE_BUFFER, (GLsizeiptr)buf_bytes, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo[1]);
glBufferData(GL_SHADER_STORAGE_BUFFER, (GLsizeiptr)buf_bytes, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
if (glGetError() != GL_NO_ERROR) {
printf("Failed to allocate GPU buffers!\n");
consoleUpdate(NULL);
glDeleteBuffers(2, ssbo);
glDeleteProgram(prog_copy);
glDeleteProgram(prog_read);
glDeleteProgram(prog_write);
egl_exit();
return false;
}
/* Pass order mirrors binary: warmup write+read, timed copy/read/write */
run_pass(prog_write, ssbo[0], 0, buf_bytes, wloops);
run_pass(prog_read, ssbo[0], 0, buf_bytes, wloops);
*copy_out = run_pass(prog_copy, ssbo[0], ssbo[1], buf_bytes, loops);
*read_out = run_pass(prog_read, ssbo[0], 0, buf_bytes, loops);
*write_out = run_pass(prog_write, ssbo[0], 0, buf_bytes, loops);
glDeleteBuffers(2, ssbo);
glDeleteProgram(prog_copy);
glDeleteProgram(prog_read);
glDeleteProgram(prog_write);
egl_exit();
return true;
}

View File

@@ -0,0 +1,4 @@
#pragma once
#include <stdbool.h>
bool gpu_bw_run(bool is_4gb, double *copy_out, double *read_out, double *write_out);

View File

@@ -0,0 +1,422 @@
/*
* Copyright © 2011 Siarhei Siamashka <siarhei.siamashka@gmail.com>
* Copyright (c) 20xx KazushiMe
* Copyright (c) 2025 Souldbminer
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include <math.h>
#include <pthread.h>
#include <sched.h>
#include <semaphore.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#define SIZE (32 * 1024 * 1024)
#ifndef MAXREPEATS
#define MAXREPEATS 10
#endif
#ifndef LATBENCH_COUNT
#define LATBENCH_COUNT 10000000
#endif
#define ALIGN_PADDING 0x100000
#define CACHE_LINE_SIZE 128
#include <switch.h>
#include "gpu_bw.h"
#define YEL "\033[1;92m"
#define GRN "\033[1;92m"
#define RST "\033[0m"
PadState pad;
// Threads
struct f_data {
void (*func)(int64_t *, int64_t *, int);
int64_t *arg1;
int64_t *arg2;
int arg3;
};
pthread_cond_t p_ready, p_start;
pthread_mutex_t p_lock;
pthread_t *p_worker = NULL;
struct f_data *worker_data = NULL;
int p_worker_not_ready, p_workers_ready;
void *thread_func(void *data) {
struct f_data *d = data;
pthread_mutex_lock(&p_lock);
p_worker_not_ready--;
if (!p_worker_not_ready)
pthread_cond_signal(&p_ready);
while (p_workers_ready != 1)
pthread_cond_wait(&p_start, &p_lock);
pthread_mutex_unlock(&p_lock);
(d->func)(d->arg1, d->arg2, d->arg3);
pthread_exit(NULL);
}
static void parallel_run(void) {
pthread_mutex_lock(&p_lock);
p_workers_ready = 1;
pthread_mutex_unlock(&p_lock);
pthread_cond_broadcast(&p_start);
}
static void parallel_init(int threads) {
pthread_attr_t attr;
pthread_cond_init(&p_ready, NULL);
pthread_cond_init(&p_start, NULL);
pthread_mutex_init(&p_lock, NULL);
p_worker_not_ready = threads;
p_workers_ready = 0;
pthread_attr_init(&attr);
if (!p_worker || !worker_data) {
p_worker = malloc(threads * sizeof(pthread_t));
worker_data = malloc(threads * sizeof(struct f_data));
}
for (int i = 0; i < threads; i++)
pthread_create(p_worker + i, &attr, thread_func, worker_data + i);
pthread_mutex_lock(&p_lock);
while (p_worker_not_ready != 0)
pthread_cond_wait(&p_ready, &p_lock);
pthread_mutex_unlock(&p_lock);
}
// memops
void aligned_block_copy(int64_t *__restrict dst_, int64_t *__restrict src, int size) {
volatile int64_t *dst = dst_;
int64_t t1, t2, t3, t4;
while ((size -= 64) >= 0) {
t1 = *src++;
t2 = *src++;
t3 = *src++;
t4 = *src++;
*dst++ = t1;
*dst++ = t2;
*dst++ = t3;
*dst++ = t4;
t1 = *src++;
t2 = *src++;
t3 = *src++;
t4 = *src++;
*dst++ = t1;
*dst++ = t2;
*dst++ = t3;
*dst++ = t4;
}
}
void aligned_block_fetch(int64_t *__restrict dst, int64_t *__restrict src_, int size) {
volatile int64_t *src = src_;
(void)dst;
while ((size -= 64) >= 0) {
*src++;
*src++;
*src++;
*src++;
*src++;
*src++;
*src++;
*src++;
}
}
void aligned_block_fill(int64_t *__restrict dst_, int64_t *__restrict src, int size) {
volatile int64_t *dst = dst_;
int64_t data = *src;
while ((size -= 64) >= 0) {
*dst++ = data;
*dst++ = data;
*dst++ = data;
*dst++ = data;
*dst++ = data;
*dst++ = data;
*dst++ = data;
*dst++ = data;
}
}
double gettime(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)((int64_t)tv.tv_sec * 1000000 + tv.tv_usec) / 1000000.;
}
static double bandwidth_bench_helper(int threads, int64_t *dstbuf, int64_t *srcbuf, int size, void (*f)(int64_t *, int64_t *, int)) {
int i, loopcount, innerloopcount, n;
double t, t1, t2, speed, maxspeed, s, s0, s1, s2;
s = s0 = s1 = s2 = 0.;
maxspeed = 0.;
for (n = 0; n < MAXREPEATS; n++) {
loopcount = 0;
innerloopcount = 1;
t = 0.;
do {
loopcount += innerloopcount;
for (i = 0; i < innerloopcount; i++) {
parallel_init(threads);
for (int pt = 0; pt < threads; pt++) {
(worker_data + pt)->func = f;
(worker_data + pt)->arg1 = dstbuf + size * pt / sizeof(int64_t);
(worker_data + pt)->arg2 = srcbuf + size * pt / sizeof(int64_t);
(worker_data + pt)->arg3 = size;
}
t1 = gettime();
parallel_run();
for (int pt = 0; pt < threads; pt++)
pthread_join(p_worker[pt], NULL);
t2 = gettime();
t += t2 - t1;
}
innerloopcount *= 2;
} while (t < 0.5);
speed = (double)size * threads * loopcount / t / 1000000.;
s0 += 1.;
s1 += speed;
s2 += speed * speed;
if (speed > maxspeed)
maxspeed = speed;
if (s0 > 2.) {
s = sqrt((s0 * s2 - s1 * s1) / (s0 * (s0 - 1)));
if (s < maxspeed / 1000.)
break;
}
}
return maxspeed;
}
static char *align_up(char *ptr, int align) {
return (char *)(((uintptr_t)ptr + align - 1) & ~(uintptr_t)(align - 1));
}
void *alloc_nonaliased_buffers(void **buf1_, int size1, void **buf2_, int size2, void **buf3_, int size3) {
char **buf1 = (char **)buf1_, **buf2 = (char **)buf2_, **buf3 = (char **)buf3_;
int mask = (ALIGN_PADDING - 1) & ~(CACHE_LINE_SIZE - 1);
char *buf = malloc(size1 + size2 + size3 + 9 * ALIGN_PADDING);
char *ptr = buf;
memset(buf, 0xCC, size1 + size2 + size3 + 9 * ALIGN_PADDING);
ptr = align_up(ptr, ALIGN_PADDING);
if (buf1) {
*buf1 = ptr + (0xAAAAAAAA & mask);
ptr = align_up(*buf1 + size1, ALIGN_PADDING);
}
if (buf2) {
*buf2 = ptr + (0x55555555 & mask);
ptr = align_up(*buf2 + size2, ALIGN_PADDING);
}
if (buf3) {
*buf3 = ptr + (0xCCCCCCCC & mask);
}
return buf;
}
#pragma GCC diagnostic push
static void __attribute__((noinline)) random_read_test(char *buf, int count, int nbits) {
uint32_t seed = 0;
uintptr_t mask = (1 << nbits) - 1;
uint32_t v;
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
static volatile uint32_t dummy;
#define RMA() \
seed = seed * 1103515245 + 12345; \
v = (seed >> 16) & 0xFF; \
seed = seed * 1103515245 + 12345; \
v |= (seed >> 8) & 0xFF00; \
seed = seed * 1103515245 + 12345; \
v |= seed & 0x7FFF0000; \
seed |= buf[v & mask];
while (count >= 16) {
RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() RMA() count -= 16;
}
dummy = seed;
#undef RMA
}
#pragma GCC diagnostic pop
static double latency_measure(char *buf, int nbits, int count) {
double t_noaccess = 0, t_before, t_after, t, xs1 = 0, xs2 = 0, min_t = 0;
double xs;
int n;
for (n = 1; n <= MAXREPEATS; n++) {
t_before = gettime();
random_read_test(buf, count, 1);
t_after = gettime();
if (n == 1 || t_after - t_before < t_noaccess)
t_noaccess = t_after - t_before;
}
for (n = 1; n <= MAXREPEATS; n++) {
t_before = gettime();
random_read_test(buf, count, nbits);
t_after = gettime();
t = t_after - t_before - t_noaccess;
if (t < 0)
t = 0;
xs1 += t;
xs2 += t * t;
if (n == 1 || t < min_t)
min_t = t;
if (n > 2) {
xs = sqrt((xs2 * n - xs1 * xs1) / (n * (n - 1)));
if (xs < min_t / 1000.)
break;
}
}
return min_t * 1000000000.0 / count;
}
static void latency_bench(double *l2_out, double *ram_out) {
char *buf_alloc = malloc(0x2001000);
char *buf = (char *)(((uintptr_t)buf_alloc + 4095) & ~(uintptr_t)4095);
memset(buf, 0, 0x2000000);
/* nbits=20 (1MB) for L2, nbits=25 (32MB) for RAM — from binary assembly */
*l2_out = latency_measure(buf, 20, LATBENCH_COUNT);
*ram_out = latency_measure(buf, 25, LATBENCH_COUNT);
free(buf_alloc);
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
consoleInit(NULL);
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
padInitializeDefault(&pad);
const int threads = 3;
const int size = SIZE;
bool is_4gb = (appletGetAppletType() == AppletType_Application);
loop:
printf("Membench-NX\n\n");
printf("Press A to run all benchmarks.\n");
printf("Press any other key to exit.\n\n");
consoleUpdate(NULL);
while (appletMainLoop()) {
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
if (kDown & HidNpadButton_A)
break;
if (kDown) {
consoleExit(NULL);
return 0;
}
consoleUpdate(NULL);
}
appletSetAutoSleepDisabled(true);
consoleClear();
uint32_t cpu_hz = 0, gpu_hz = 0, mem_hz = 0;
if (R_SUCCEEDED(clkrstInitialize())) {
ClkrstSession s;
clkrstOpenSession(&s, PcvModuleId_CpuBus, 3);
clkrstGetClockRate(&s, &cpu_hz);
clkrstCloseSession(&s);
clkrstOpenSession(&s, PcvModuleId_GPU, 3);
clkrstGetClockRate(&s, &gpu_hz);
clkrstCloseSession(&s);
clkrstOpenSession(&s, PcvModuleId_EMC, 3);
clkrstGetClockRate(&s, &mem_hz);
clkrstCloseSession(&s);
clkrstExit();
}
printf("Membench-NX\n\n");
printf("----------------------------\n");
printf("CPU: %.1f MHz\n", cpu_hz / 1000000.0);
printf("GPU: %.1f MHz\n", gpu_hz / 1000000.0);
printf("MEM: %.1f MHz\n", mem_hz / 1000000.0);
printf("Mode: " GRN "%s" RST "\n", is_4gb ? "Application" : "Applet");
printf("Threads: %d (max: %d)\n", threads, threads);
printf("----------------------------\n");
consoleUpdate(NULL);
printf("\nStarted Bandwidth benchmark via GPU...\n");
consoleUpdate(NULL);
double gpu_copy = 0, gpu_read = 0, gpu_write = 0;
gpu_bw_run(is_4gb, &gpu_copy, &gpu_read, &gpu_write);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "GPU Copy", gpu_copy);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "GPU Read", gpu_read);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "GPU Write", gpu_write);
consoleUpdate(NULL);
int64_t *srcbuf, *dstbuf;
void *poolbuf = alloc_nonaliased_buffers((void **)&srcbuf, size * threads, (void **)&dstbuf, size * threads, NULL, 0);
printf("\nStarted Bandwidth benchmark with %d threads...\n", threads);
consoleUpdate(NULL);
double cpu_copy = bandwidth_bench_helper(threads, dstbuf, srcbuf, size, aligned_block_copy);
double cpu_read = bandwidth_bench_helper(threads, dstbuf, srcbuf, size, aligned_block_fetch);
double cpu_write = bandwidth_bench_helper(threads, dstbuf, srcbuf, size, aligned_block_fill);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "CPU Copy", cpu_copy);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "CPU Read", cpu_read);
printf(" " YEL "%-40s" RST " : %8.1f MB/s\n", "CPU Write", cpu_write);
consoleUpdate(NULL);
free(poolbuf);
printf("\nStarted Latency benchmark with 1 thread...\n");
consoleUpdate(NULL);
double l2_ns = 0, ram_ns = 0;
latency_bench(&l2_ns, &ram_ns);
printf(" " YEL "%-40s" RST " : %8.1f ns\n", "L2", l2_ns);
printf(" " YEL "%-40s" RST " : %8.1f ns\n", "Full RAM", ram_ns);
consoleUpdate(NULL);
appletSetAutoSleepDisabled(false);
printf("\nPress A to continue, any other key to exit.\n");
consoleUpdate(NULL);
while (appletMainLoop()) {
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
if (kDown & HidNpadButton_A) {
consoleClear();
goto loop;
}
if (kDown)
break;
consoleUpdate(NULL);
}
consoleExit(NULL);
return 0;
}