cleanup; api testing framework

This commit is contained in:
nakst 2021-12-31 18:16:04 +00:00
parent f5cf248cd2
commit ccdec37e3e
13 changed files with 230 additions and 76 deletions

View File

@ -2,12 +2,13 @@
// It is released under the terms of the MIT license -- see LICENSE.md.
// Written by: nakst.
#define ES_PRIVATE_APIS
#define INSTALLER
#define ES_CRT_WITHOUT_PREFIX
#include <essence.h>
#include <shared/hash.cpp>
#include <shared/crc.h>
#include <shared/strings.cpp>
#include <shared/partitions.cpp>
#include <shared/array.cpp>

View File

@ -23,9 +23,9 @@
#define SHARED_COMMON_WANT_ALL
#define SHARED_MATH_WANT_ALL
#include <shared/ini.h>
#include <shared/crc.h>
#include <shared/heap.cpp>
#include <shared/linked_list.cpp>
#include <shared/hash.cpp>
#include <shared/png_decoder.cpp>
#include <shared/hash_table.cpp>
#include <shared/array.cpp>
@ -119,67 +119,6 @@ const EsBundle bundleDesktop = {
.bytes = -1,
};
struct {
Array<EsSystemConfigurationGroup> systemConfigurationGroups;
EsMutex systemConfigurationMutex;
Array<MountPoint> mountPoints;
Array<EsMessageDevice> connectedDevices;
bool foundBootFileSystem;
EsProcessStartupInformation *startupInformation;
GlobalData *global;
EsMutex messageMutex;
volatile uintptr_t messageMutexThreadID;
Array<_EsMessageWithObject> postBox;
EsMutex postBoxMutex;
Array<Timer> timers;
EsMutex timersMutex;
EsHandle timersThread;
EsHandle timersEvent;
EsSpinlock performanceTimerStackLock;
#define PERFORMANCE_TIMER_STACK_SIZE (100)
double performanceTimerStack[PERFORMANCE_TIMER_STACK_SIZE];
uintptr_t performanceTimerStackCount;
EsHandle workAvailable;
EsMutex workMutex;
Array<Work> workQueue;
Array<EsHandle> workThreads;
volatile bool workFinish;
const uint16_t *keyboardLayout;
uint16_t keyboardLayoutIdentifier;
} api;
ptrdiff_t tlsStorageOffset;
// Miscellanous forward declarations.
extern "C" void EsUnimplemented();
extern "C" uintptr_t ProcessorTLSRead(uintptr_t offset);
extern "C" uint64_t ProcessorReadTimeStamp();
void UndoManagerDestroy(EsUndoManager *manager);
struct APIInstance *InstanceSetup(EsInstance *instance);
EsTextStyle TextPlanGetPrimaryStyle(EsTextPlan *plan);
EsFileStore *FileStoreCreateFromEmbeddedFile(const EsBundle *bundle, const char *path, size_t pathBytes);
EsFileStore *FileStoreCreateFromPath(const char *path, size_t pathBytes);
EsFileStore *FileStoreCreateFromHandle(EsHandle handle);
void FileStoreCloseHandle(EsFileStore *fileStore);
EsError NodeOpen(const char *path, size_t pathBytes, uint32_t flags, _EsNodeInformation *node);
const char *EnumLookupNameFromValue(const EnumString *array, int value);
EsSystemConfigurationItem *SystemConfigurationGetItem(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, bool createIfNeeded = false);
EsSystemConfigurationGroup *SystemConfigurationGetGroup(const char *section, ptrdiff_t sectionBytes, bool createIfNeeded = false);
uint8_t *ApplicationStartupInformationToBuffer(const _EsApplicationStartupInformation *information, size_t *dataBytes = nullptr);
char *SystemConfigurationGroupReadString(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, size_t *valueBytes = nullptr);
int64_t SystemConfigurationGroupReadInteger(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, int64_t defaultValue = 0);
MountPoint *NodeFindMountPoint(const char *prefix, size_t prefixBytes);
EsWindow *WindowFromWindowID(EsObjectID id);
void POSIXCleanup();
extern "C" void _init();
struct ProcessMessageTiming {
double startLogic, endLogic;
double startLayout, endLayout;
@ -247,6 +186,67 @@ struct APIInstance {
EsTextbox *fileMenuNameTextbox; // Also used by the file save dialog.
};
struct {
Array<EsSystemConfigurationGroup> systemConfigurationGroups;
EsMutex systemConfigurationMutex;
Array<MountPoint> mountPoints;
Array<EsMessageDevice> connectedDevices;
bool foundBootFileSystem;
EsProcessStartupInformation *startupInformation;
GlobalData *global;
EsMutex messageMutex;
volatile uintptr_t messageMutexThreadID;
Array<_EsMessageWithObject> postBox;
EsMutex postBoxMutex;
Array<Timer> timers;
EsMutex timersMutex;
EsHandle timersThread;
EsHandle timersEvent;
EsSpinlock performanceTimerStackLock;
#define PERFORMANCE_TIMER_STACK_SIZE (100)
double performanceTimerStack[PERFORMANCE_TIMER_STACK_SIZE];
uintptr_t performanceTimerStackCount;
EsHandle workAvailable;
EsMutex workMutex;
Array<Work> workQueue;
Array<EsHandle> workThreads;
volatile bool workFinish;
const uint16_t *keyboardLayout;
uint16_t keyboardLayoutIdentifier;
} api;
ptrdiff_t tlsStorageOffset;
// Miscellanous forward declarations.
extern "C" void EsUnimplemented();
extern "C" uintptr_t ProcessorTLSRead(uintptr_t offset);
extern "C" uint64_t ProcessorReadTimeStamp();
void UndoManagerDestroy(EsUndoManager *manager);
struct APIInstance *InstanceSetup(EsInstance *instance);
EsTextStyle TextPlanGetPrimaryStyle(EsTextPlan *plan);
EsFileStore *FileStoreCreateFromEmbeddedFile(const EsBundle *bundle, const char *path, size_t pathBytes);
EsFileStore *FileStoreCreateFromPath(const char *path, size_t pathBytes);
EsFileStore *FileStoreCreateFromHandle(EsHandle handle);
void FileStoreCloseHandle(EsFileStore *fileStore);
EsError NodeOpen(const char *path, size_t pathBytes, uint32_t flags, _EsNodeInformation *node);
const char *EnumLookupNameFromValue(const EnumString *array, int value);
EsSystemConfigurationItem *SystemConfigurationGetItem(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, bool createIfNeeded = false);
EsSystemConfigurationGroup *SystemConfigurationGetGroup(const char *section, ptrdiff_t sectionBytes, bool createIfNeeded = false);
uint8_t *ApplicationStartupInformationToBuffer(const _EsApplicationStartupInformation *information, size_t *dataBytes = nullptr);
char *SystemConfigurationGroupReadString(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, size_t *valueBytes = nullptr);
int64_t SystemConfigurationGroupReadInteger(EsSystemConfigurationGroup *group, const char *key, ptrdiff_t keyBytes, int64_t defaultValue = 0);
MountPoint *NodeFindMountPoint(const char *prefix, size_t prefixBytes);
EsWindow *WindowFromWindowID(EsObjectID id);
void POSIXCleanup();
extern "C" void _init();
#include "syscall.cpp"
#include "profiling.cpp"
#include "renderer.cpp"

82
desktop/api_tests.cpp Normal file
View File

@ -0,0 +1,82 @@
#ifdef API_TESTS_FOR_RUNNER
#define TEST(_callback) { .cName = #_callback }
typedef struct Test { const char *cName; } Test;
#else
#define ES_PRIVATE_APIS
#include <essence.h>
#include <shared/crc.h>
#define TEST(_callback) { .callback = _callback }
struct Test { bool (*callback)(); };
bool SuccessTest() {
return true;
}
bool FailureTest() {
return false;
}
bool TimeoutTest() {
EsProcessTerminateCurrent();
return true;
}
#endif
const Test tests[] = {
TEST(TimeoutTest), TEST(SuccessTest), TEST(FailureTest)
};
#ifndef API_TESTS_FOR_RUNNER
void RunTests() {
size_t fileSize;
EsError error;
void *fileData = EsFileReadAll(EsLiteral("|Settings:/test.dat"), &fileSize, &error);
if (error == ES_ERROR_FILE_DOES_NOT_EXIST) {
return; // Not in test mode.
} else if (error != ES_SUCCESS) {
EsPrint("Could not read test.dat (error %d).\n", error);
} else if (fileSize != sizeof(uint32_t)) {
EsPrint("test.dat is the wrong size (got %d, expected %d).\n", fileSize, sizeof(uint32_t));
} else {
uint32_t index;
EsMemoryCopy(&index, fileData, sizeof(uint32_t));
EsHeapFree(fileData);
if (index >= sizeof(tests) / sizeof(tests[0])) {
EsPrint("Test index out of bounds.\n");
} else if (tests[index].callback()) {
EsPrint("[APITests-Success]\n");
} else {
EsPrint("[APITests-Failure]\n");
}
}
EsSyscall(ES_SYSCALL_SHUTDOWN, SHUTDOWN_ACTION_POWER_OFF, 0, 0, 0);
EsProcessTerminateCurrent();
}
void _start() {
_init();
while (true) {
EsMessage *message = EsMessageReceive();
if (message->type == ES_MSG_INSTANCE_CREATE) {
EsInstance *instance = EsInstanceCreate(message, EsLiteral("API Tests"));
EsApplicationStartupRequest request = EsInstanceGetStartupRequest(instance);
if (request.flags & ES_APPLICATION_STARTUP_BACKGROUND_SERVICE) {
RunTests();
}
}
}
}
#endif

7
desktop/api_tests.ini Normal file
View File

@ -0,0 +1,7 @@
[general]
name=API Tests
background_service=1
permission_shutdown=1
[build]
source=desktop/api_tests.cpp

View File

@ -289,7 +289,11 @@ ES_EXTERN_C void _start();
/* --------- Internals: */
#if defined(ES_API) || defined(KERNEL) || defined(INSTALLER)
#if defined(ES_API) || defined(KERNEL)
#define ES_PRIVATE_APIS
#endif
#ifdef ES_PRIVATE_APIS
struct _EsPOSIXSyscall {
intptr_t index;

View File

@ -51,6 +51,7 @@ The following commands are available in the interactive prompt:
- `get-source <folder name> <url>` The file at the URL is downloaded and cached in `bin/cache`. The download is skipped if the file was already cached. It is then extracted and untar'd. The folder of the given name is then moved to `bin/source`.
- `make-crash-report` Copies various system files and logs into a `.tar.gz` which can be used to report a crash.
- `setup-pre-built-toolchain` Setup the pre-built toolchain for use by the build system. You can download and prepare it by running `./start.sh get-source prefix https://github.com/nakst/build-gcc/releases/download/gcc-11.1.0/gcc-x86_64-essence.tar.xz` followed by `./start.sh setup-pre-built-toolchain`.
- `run-tests` Run the API tests. `desktop/api_tests.ini` must be added to `bin/extra_applications.ini`, and `Emulator.SerialToFile` must be enabled.
## Levels of optimisation

View File

@ -127,8 +127,8 @@ This file contains a list and description of the files and folders in the source
- `avl_tree.cpp` AVL lookup tree, including support for searches for the "smallest above or equal" and "largest below or equal".
- `bitset.cpp` Efficient large bitsets.
- `common.cpp` Various common functions, including management of `EsRectangle`s, rendering, string utilities, memory utilities (like `EsMemoryCopy`), printing and string formatting, pseudo random number generation, standard algorithms like sorting, synchronisation primitives, byte swapping, implementation of the `EsCRT-` functions, `EsBuffer` functions, and time and date calculations.
- `crc.h` Implementation of CRC32 and CRC64 hash functions.
- `fat.cpp` Common definitions for the FAT file system.
- `hash.cpp` Implementation of CRC32 and CRC64 hash functions.
- `hash_table.cpp` A hash table, including an implementation of the FNV1a hash function.
- `heap.cpp` A memory allocator for arbitrary allocation and freeing of non-fixed size memory blocks.
- `linked_list.cpp` Two implementations of a doubly-linked list: `LinkedList<T>` and `SimpleList<T>` where the latter requires less memory but provides less features.

View File

@ -182,14 +182,14 @@ extern "C" {
// ---------------------------------------------------------------------------------------------------------------
#ifndef IMPLEMENTATION
#include <shared/ini.h>
#include <shared/crc.h>
#include <shared/bitset.cpp>
#include <shared/arena.cpp>
#include <shared/avl_tree.cpp>
#include <shared/range_set.cpp>
#include <shared/ini.h>
#include <shared/partitions.cpp>
#include <shared/heap.cpp>
#include <shared/hash.cpp>
#define ARRAY_IMPLEMENTATION_ONLY
#include <shared/array.cpp>

View File

@ -35,8 +35,12 @@
#include <pthread.h>
#include <sys/wait.h>
#include <spawn.h>
#include "../shared/hash.cpp"
#include "../shared/crc.h"
#define API_TESTS_FOR_RUNNER
#include "../desktop/api_tests.cpp"
#define ColorError "\033[0;33m"
#define ColorHighlight "\033[0;36m"
#define ColorNormal "\033[0m"
@ -47,6 +51,8 @@ bool coloredOutput;
bool encounteredErrors;
bool interactiveMode;
bool canBuildLuigi;
int emulatorTimeout;
bool emulatorDidTimeout;
FILE *systemLog;
char compilerPath[4096];
int argc;
@ -421,6 +427,7 @@ void Build(int optimise, bool compile) {
#define EMULATOR_QEMU (0)
#define EMULATOR_BOCHS (1)
#define EMULATOR_VIRTUALBOX (2)
#define EMULATOR_QEMU_NO_GUI (3)
#define DEBUG_LATER (0)
#define DEBUG_START (1)
#define DEBUG_NONE (2)
@ -429,7 +436,16 @@ void Run(int emulator, int log, int debug) {
LoadOptions();
switch (emulator) {
case EMULATOR_QEMU_NO_GUI: /* fallthrough */
case EMULATOR_QEMU: {
char timeoutFlags[256];
if (emulatorTimeout) {
snprintf(timeoutFlags, sizeof(timeoutFlags), "timeout %ds ", emulatorTimeout);
} else {
timeoutFlags[0] = 0;
}
const char *biosFlags = "";
const char *drivePrefix = "-drive file=bin/drive";
@ -519,15 +535,23 @@ void Run(int emulator, int log, int debug) {
serialFlags[0] = 0;
}
if (CallSystemF("%s %s " QEMU_EXECUTABLE " %s%s %s -m %d %s -smp cores=%d -cpu Haswell "
const char *displayFlags = emulator == EMULATOR_QEMU_NO_GUI ? " -display none " : "";
int exitCode = CallSystemF("%s %s %s " QEMU_EXECUTABLE " %s%s %s -m %d %s -smp cores=%d -cpu Haswell "
" -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,id=mykeyboard -device usb-mouse,bus=xhci.0,id=mymouse "
" -netdev user,id=u1 -device e1000,netdev=u1 -object filter-dump,id=f1,netdev=u1,file=bin/Logs/net.dat "
" %s %s %s %s %s %s %s ",
audioFlags, IsOptionEnabled("Emulator.RunWithSudo") ? "sudo " : "", drivePrefix, driveFlags, cdromFlags,
" %s %s %s %s %s %s %s %s ",
timeoutFlags, audioFlags, IsOptionEnabled("Emulator.RunWithSudo") ? "sudo " : "", drivePrefix, driveFlags, cdromFlags,
atoi(GetOptionString("Emulator.MemoryMB")),
debug ? (debug == DEBUG_NONE ? "-enable-kvm" : "-s -S") : "-s",
cpuCores, audioFlags2, logFlags, usbFlags, usbFlags2, secondaryDriveFlags, biosFlags, serialFlags)) {
printf("Unable to start Qemu. To manually run the system, use the drive image located at \"bin/drive\".\n");
cpuCores, audioFlags2, logFlags, usbFlags, usbFlags2, secondaryDriveFlags, biosFlags, serialFlags, displayFlags);
if (emulatorTimeout) {
emulatorDidTimeout = (exitCode >> 8) == 124;
} else {
if (exitCode) {
printf("Unable to start Qemu. To manually run the system, use the drive image located at \"bin/drive\".\n");
}
}
// Watch serial file as it is being written to:
@ -1592,6 +1616,41 @@ void DoCommand(const char *l) {
getcwd(cwd, sizeof(cwd));
strcat(cwd, "/crash-report.tar.gz");
fprintf(stderr, "Crash report made at " ColorHighlight "%s" ColorNormal ".\n", cwd);
} else if (0 == strcmp(l, "run-tests")) {
// TODO Capture (and compress) emulator memory dump if a test causes a KernelPanic or EsPanic.
int successCount = 0, failureCount = 0;
CallSystem("mkdir -p root/Essence/Settings/API\\ Tests");
for (int optimisations = 0; optimisations <= 1; optimisations++) {
for (uint32_t index = 0; index < sizeof(tests) / sizeof(tests[0]); index++) {
CallSystem("rm -f bin/Logs/qemu_serial1.txt");
FILE *f = fopen("root/Essence/Settings/API Tests/test.dat", "wb");
fwrite(&index, 1, sizeof(uint32_t), f);
fclose(f);
emulatorTimeout = 10;
if (optimisations) BuildAndRun(OPTIMISE_FULL, true, DEBUG_NONE, EMULATOR_QEMU_NO_GUI, LOG_NORMAL);
else BuildAndRun(OPTIMISE_OFF, true, DEBUG_LATER, EMULATOR_QEMU_NO_GUI, LOG_NORMAL);
emulatorTimeout = 0;
if (emulatorDidTimeout) encounteredErrors = false;
if (encounteredErrors) { fprintf(stderr, "Compile errors, stopping tests.\n"); goto stopTests; }
char *log = (char *) LoadFile("bin/Logs/qemu_serial1.txt", NULL);
if (!log) { fprintf(stderr, "No log file, stopping tests.\n"); goto stopTests; }
bool success = strstr(log, "[APITests-Success]\n") && !emulatorDidTimeout;
bool failure = strstr(log, "[APITests-Failure]\n");
if (emulatorDidTimeout) fprintf(stderr, "'%s' (%d/%d): " ColorError "timeout" ColorNormal ".\n", tests[index].cName, optimisations, index);
else if (success) fprintf(stderr, "'%s' (%d/%d): success.\n", tests[index].cName, optimisations, index);
else if (failure) fprintf(stderr, "'%s' (%d/%d): " ColorError "failure" ColorNormal ".\n", tests[index].cName, optimisations, index);
else fprintf(stderr, "'%s' (%d/%d): " ColorError "no response" ColorNormal ".\n", tests[index].cName, optimisations, index);
if (success) successCount++;
else failureCount++;
free(log);
if (!success) CallSystemF("mv bin/Logs/qemu_serial1.txt bin/Logs/test_%d_%d.txt", optimisations, index);
}
}
stopTests:;
fprintf(stderr, ColorHighlight "%d/%d tests succeeded." ColorNormal "\n", successCount, successCount + failureCount);
} else if (0 == strcmp(l, "setup-pre-built-toolchain")) {
CallSystem("mv bin/source cross");
CallSystem("mkdir -p cross/bin2");

View File

@ -155,7 +155,7 @@ File FileOpen(const char *path, char mode) {
#define EsFSError() exit(1)
#include "../shared/hash.cpp"
#include "../shared/crc.h"
#include "../shared/partitions.cpp"
#include "build_common.h"
#include "../shared/esfs2.h"

View File

@ -169,7 +169,7 @@ UIRectangle ScaleRectangle(UIRectangle r, float by) {
#include "../shared/math.cpp"
#include "../shared/array.cpp"
#include "../shared/hash.cpp"
#include "../shared/crc.h"
#include "../shared/hash_table.cpp"
#include "../desktop/renderer.cpp"
#include "../desktop/theme.cpp"

View File

@ -598,7 +598,7 @@ void OutputC(Entry *root) {
Entry *entry = root->children + i;
if (entry->isPrivate) {
FilePrintFormat(output, "#if defined(ES_API) || defined(KERNEL) || defined(INSTALLER)\n");
FilePrintFormat(output, "#if defined(ES_PRIVATE_APIS)\n");
}
if (entry->type == ENTRY_DEFINE) {