Added misses counters

This commit is contained in:
Casey 2024-02-25 00:44:39 +03:00
parent bf7c04fc8b
commit 3010d99592
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 50 additions and 4 deletions

View File

@ -1,8 +1,10 @@
#include "common.h" #include "common.h"
#include "miniaudio.h" #include "miniaudio.h"
#include <getopt.h> #include <getopt.h>
#include <limits.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -33,6 +35,8 @@ struct audio_data {
struct sfx_pool { struct sfx_pool {
struct sfx_pool_item { struct sfx_pool_item {
char key[KEY_LENGTH]; char key[KEY_LENGTH];
char path[PATH_MAX];
ma_sound *sounds; ma_sound *sounds;
int last_index; int last_index;
@ -62,8 +66,17 @@ struct ipc_data ipc = { 0 };
struct audio_data audio = { 0 }; struct audio_data audio = { 0 };
struct sfx_pool sounds_pool = { 0, 0, 0 }; struct sfx_pool sounds_pool = { 0, 0, 0 };
void usage(int argc, char **argv) { #ifndef NO_COUNTERS
struct global_counters {
uint64_t pool_read;
uint64_t pool_write;
uint64_t hash_misses_write;
uint64_t hash_misses_read;
} global_counters = { 0 };
#endif
void usage(int argc, char **argv) {
// TODO
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -177,7 +190,7 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
send_txt(tgt, "DBG: pool overflow: %d/%d (%.3f%%)\n", send_txt(tgt, "DBG: pool overflow: %d/%d (%.3f%%)\n",
sounds_pool.use, sounds_pool.cap, sounds_pool.use, sounds_pool.cap,
100.0 * sounds_pool.use / (float)sounds_pool.cap); 100.0 * sounds_pool.use / (float)sounds_pool.cap);
sfx_pool_grow(sounds_pool.cap + 32); sfx_pool_grow((int)(sounds_pool.cap * 1.5));
} }
send_txt(tgt, "Loading audio key=%s from %s\n", key, path); send_txt(tgt, "Loading audio key=%s from %s\n", key, path);
@ -256,15 +269,29 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
} else if (0 == strcmp(command, "dump")) { } else if (0 == strcmp(command, "dump")) {
for (int i = 0; i < sounds_pool.cap; i++) { for (int i = 0; i < sounds_pool.cap; i++) {
struct sfx_pool_item item = sounds_pool.sounds[i]; struct sfx_pool_item item = sounds_pool.sounds[i];
if (item.key[0] != '\0') { if (item.key[0] == '\0') {
send_txt(tgt, "%3d (unused)\n", i);
} else {
send_txt(tgt, "%3d vol=%7.5f pitch=%7.5f..%7.5f @%2d (0x%08x) \"%s\"\n", send_txt(tgt, "%3d vol=%7.5f pitch=%7.5f..%7.5f @%2d (0x%08x) \"%s\"\n",
i, item.volume, item.pitch_min, item.pitch_max, item.last_index, adler32(item.key, strlen(item.key)), item.key); i, item.volume, item.pitch_min, item.pitch_max, item.last_index, adler32(item.key, strlen(item.key)), item.key);
} }
} }
} else if (0 == strcmp(command, "source")) { } else if (0 == strcmp(command, "source")) {
execute_file(tgt, params, 0); execute_file(tgt, params, 0);
} else if (0 == strcmp(command, "dbg:counters")) {
#ifdef NO_COUNTERS
send_txt(tgt, "ERR: counters are disabled at compile-time with NO_COUNTERS feature flag.");
#else
send_txt(tgt, ".pool_read = %ld\n", global_counters.pool_read);
send_txt(tgt, ".pool_write = %ld\n", global_counters.pool_write);
send_txt(tgt, ".hash_misses_read = %ld\n", global_counters.hash_misses_read);
send_txt(tgt, ".hash_misses_write = %ld\n", global_counters.hash_misses_write);
send_txt(tgt, ".pool.use = %d\n", sounds_pool.use);
send_txt(tgt, ".pool.cap = %d\n", sounds_pool.cap);
#endif
} else if (0 == strcmp(command, "")) { } else if (0 == strcmp(command, "")) {
} else {
send_txt(tgt, "ERR: unknown command: %s\n", command);
} // commands } // commands
} }
@ -336,6 +363,12 @@ void sfx_pool_grow(int size) {
uint32_t new_hash = adler32(sounds_pool.sounds[i].key, strlen(sounds_pool.sounds[i].key)); uint32_t new_hash = adler32(sounds_pool.sounds[i].key, strlen(sounds_pool.sounds[i].key));
for (int offset = 0; offset < size; offset++) { for (int offset = 0; offset < size; offset++) {
int index = (new_hash + offset) % size; int index = (new_hash + offset) % size;
#ifndef NO_COUNTERS
global_counters.pool_write++;
if (offset != 0) {
global_counters.hash_misses_write++;
}
#endif
if (new_items[index].key[0] == '\0') { if (new_items[index].key[0] == '\0') {
memcpy(&new_items[index], &sounds_pool.sounds[i], sizeof(struct sfx_pool_item)); memcpy(&new_items[index], &sounds_pool.sounds[i], sizeof(struct sfx_pool_item));
used++; used++;
@ -356,6 +389,12 @@ struct sfx_pool_item *sfx_pool_lookup(const char *key) {
for (int offset = 0; offset < sounds_pool.cap; offset++) { for (int offset = 0; offset < sounds_pool.cap; offset++) {
int index = (hash + offset) % sounds_pool.cap; int index = (hash + offset) % sounds_pool.cap;
if (0 == strncmp(key, sounds_pool.sounds[index].key, KEY_LENGTH)) { if (0 == strncmp(key, sounds_pool.sounds[index].key, KEY_LENGTH)) {
#ifndef NO_COUNTERS
global_counters.pool_read++;
if (offset != 0) {
global_counters.hash_misses_read++;
}
#endif
return &sounds_pool.sounds[index]; return &sounds_pool.sounds[index];
} }
} }
@ -367,6 +406,12 @@ struct sfx_pool_item *sfx_pool_find_place_for(const char *key) {
for (int offset = 0; offset < sounds_pool.cap; offset++) { for (int offset = 0; offset < sounds_pool.cap; offset++) {
int index = (hash + offset) % sounds_pool.cap; int index = (hash + offset) % sounds_pool.cap;
if (sounds_pool.sounds[index].key[0] == '\0' || 0 == strncmp(sounds_pool.sounds[index].key, key, KEY_LENGTH)) { if (sounds_pool.sounds[index].key[0] == '\0' || 0 == strncmp(sounds_pool.sounds[index].key, key, KEY_LENGTH)) {
#ifndef NO_COUNTERS
global_counters.pool_write++;
if (offset != 0) {
global_counters.hash_misses_write++;
}
#endif
return &sounds_pool.sounds[index]; return &sounds_pool.sounds[index];
} }
} }
@ -403,6 +448,7 @@ struct sfx_pool_item *sfx_pool_load(const char *key, const char *path) {
} }
strncpy(sound->key, key, KEY_LENGTH); strncpy(sound->key, key, KEY_LENGTH);
strncpy(sound->path, path, PATH_MAX);
sounds_pool.use++; sounds_pool.use++;
return sound; return sound;