Skip to content

Commit

Permalink
add rhealstone benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
lukileczo committed Nov 27, 2024
1 parent 8c96571 commit f61c6ee
Show file tree
Hide file tree
Showing 15 changed files with 967 additions and 0 deletions.
11 changes: 11 additions & 0 deletions _user/rhealstone/common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Makefile for Phoenix-RTOS librhcommon
#
# Copyright 2024 Phoenix Systems
#

NAME := librhcommon
LOCAL_HEADERS := rhcommon.h
LOCAL_SRCS := common.c

include $(static-lib.mk)
74 changes: 74 additions & 0 deletions _user/rhealstone/common/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Phoenix-RTOS
*
* Rhealstone benchmark
*
* Common functions
*
* Copyright 2024 Phoenix Systems
* Author: Lukasz Leczkowski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include "rhcommon.h"

#include <stdatomic.h>
#include <stdio.h>
#include <sys/threads.h>
#include <unistd.h>


uint64_t printResult(uint64_t start, uint64_t end, int loops, uint64_t loopOverhead, uint64_t singleOverhead)
{
uint64_t elapsed = end - start - loopOverhead;
float time = ((float)elapsed / (float)loops) - (float)singleOverhead;
printf("Result: %.1f cycles\n", time);

return elapsed;
}


static void emptyThread(void *arg)
{
endthread();
}


uint64_t threadJoinOverhead(void)
{
uint64_t start, end;
int tid;
char stack[512];
if (beginthreadex(emptyThread, 0, stack, sizeof(stack), NULL, &tid) < 0) {
perror("beginthreadex");
return 0;
}

usleep(100000);

start = getCntr();
threadJoin(tid, 0);
end = getCntr();

return end - start;
}


uint64_t mutexLockOverhead(handle_t mutex)
{
const int loops = 100;
uint64_t start, end, total = 0;

for (int i = 0; i < loops; i++) {
start = getCntr();
mutexLock(mutex);
end = getCntr();
mutexUnlock(mutex);
total += end - start;
}

return total / loops;
}
53 changes: 53 additions & 0 deletions _user/rhealstone/common/rhcommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Phoenix-RTOS
*
* Rhealstone benchmark
*
* Common functions
*
* Copyright 2024 Phoenix Systems
* Author: Lukasz Leczkowski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _RHCOMMON_H_
#define _RHCOMMON_H_


#include <stdio.h>
#include <sys/threads.h>


#define BENCH_NAME(name) \
do { \
printf("Rhealstone benchmark suite\n%s\n", name); \
} while (0)

uint64_t printResult(uint64_t start, uint64_t end, int loops, uint64_t loopOverhead, uint64_t singleOverhead);


static inline uint64_t getCntr(void)
{
uint32_t asr22, asr23;
uint64_t cntr;
__asm__ volatile(
"rd %%asr22, %0\n\t"
"rd %%asr23, %1\n\t"
: "=r"(asr22), "=r"(asr23) :);

cntr = ((uint64_t)(asr22 & 0xffffffu) << 32) | asr23;

return cntr;
}


uint64_t threadJoinOverhead(void);


uint64_t mutexLockOverhead(handle_t mutex);


#endif
11 changes: 11 additions & 0 deletions _user/rhealstone/deadlock-break/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Makefile for user application
#
# Copyright 2024 Phoenix Systems
#

NAME := rh-deadlock-break
LOCAL_SRCS := main.c
DEP_LIBS := librhcommon

include $(binary.mk)
170 changes: 170 additions & 0 deletions _user/rhealstone/deadlock-break/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Phoenix-RTOS
*
* Rhealstone benchmark
*
* Task switch
*
* Copyright 2024 Phoenix Systems
* Author: Lukasz Leczkowski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <stdatomic.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/threads.h>

#include <board_config.h>
#include <rhcommon.h>


#define BENCHMARKS 10000

bool deadBrk;
volatile bool done = false;
handle_t mutex;

unsigned char stack[3][4096];
int maxLoops = BENCHMARKS;
int tid1, tid2, tid3;

atomic_bool t3_started = false;

uint64_t totalNoDeadBrk = 0;
uint64_t totalDeadBrk = 0;

/* High priority task */
void task3(void *arg)
{
t3_started = true;
uint64_t time = getCntr();
if (deadBrk) {
mutexLock(mutex);
}

if (deadBrk) {
mutexUnlock(mutex);
time = getCntr() - time;
totalDeadBrk += time;
}
else {
time = getCntr() - time;
totalNoDeadBrk += time;
}
done = true;
endthread();
}


/* Medium priority task */
void task2(void *arg)
{
if (beginthreadex(task3, 1, stack[2], sizeof(stack[2]), NULL, &tid3)) {
perror("beginthreadex");
endthread();
}

while (!done) {
__asm__ volatile("nop");
}

threadJoin(tid3, 0);

endthread();
}

/* Low priority task */
void task1(void *arg)
{
if (deadBrk) {
mutexLock(mutex);
}

if (beginthreadex(task2, 2, stack[1], sizeof(stack[1]), NULL, &tid2)) {
perror("beginthreadex");
endthread();
}

while (!t3_started) {
usleep(0);
}

if (deadBrk) {
mutexUnlock(mutex);
}

threadJoin(tid2, 0);
t3_started = false;
endthread();
}


int doTest(void)
{
if (beginthreadex(task1, 3, stack[0], sizeof(stack[0]), NULL, &tid1)) {
perror("beginthreadex");
return -1;
}

priority(4);

usleep(0);

threadJoin(tid1, 0);

priority(0);

return 0;
}


int main(int argc, char *argv[])
{
BENCH_NAME("Deadlock Break");

if (argc > 1) {
maxLoops = atoi(argv[1]);
}

priority(0);

mutexCreate(&mutex);

uint64_t mutexOverhead = mutexLockOverhead(mutex);
uint64_t joinOverhead = threadJoinOverhead();

deadBrk = false;


for (int i = 0; i < maxLoops; i++) {
if (doTest() < 0) {
return -1;
}
}

printf("No deadlock: \n");
printResult(0, totalNoDeadBrk, 1, joinOverhead, 0);

deadBrk = true;

for (int i = 0; i < maxLoops; i++) {
if (doTest() < 0) {
return -1;
}
}

printf("Deadlocks: full time\n");
printResult(0, totalDeadBrk, 1, joinOverhead, 0);

printf("Deadlocks: per resolution\n");
printResult(0, totalDeadBrk, maxLoops, totalNoDeadBrk, mutexOverhead);

return 0;
}
11 changes: 11 additions & 0 deletions _user/rhealstone/irq-latency/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#
# Makefile for user application
#
# Copyright 2024 Phoenix Systems
#

NAME := rh-irq-latency
LOCAL_SRCS := main.c
DEP_LIBS := librhcommon

include $(binary.mk)
Loading

0 comments on commit f61c6ee

Please sign in to comment.