Skip to content

Commit b28c867

Browse files
[#97] Make cache size configurable
1 parent 05c3273 commit b28c867

6 files changed

Lines changed: 120 additions & 5 deletions

File tree

doc/CONFIGURATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The `Bool` data type supports the following values: `on`, `1`, `true`, `off`, `0
2222
| device | | String | No | The default device name |
2323
| output | `[%n/%N] %d: %f [%i] (%t/%T) (%p)`| String | No | Defines the console output. Valid expansions are: `%n` (current track number), `%N` (total number of tracks), `%d` (device name), `%f` (file name), `%F` (full path of file), `%i` (file information), `%t` (current time), `%T` (total time), `%p` (percentage), `%b` (ringbuffer current size in Mb), `%B` (ringbuffer maximum size in Mb)|
2424
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
25+
| cache | 256Mb | Int | No | The cache size |
2526
| log_type | console | String | No | The logging type (console, file, syslog) |
2627
| log_level | info | String | No | The logging level, any of the (case insensitive) strings `FATAL`, `ERROR`, `WARN`, `INFO` and `DEBUG` (that can be more specific as `DEBUG1` thru `DEBUG5`). Debug level greater than 5 will be set to `DEBUG5`. Not recognized values will make the log_level be `INFO` |
2728
| log_path | hrmp.log | String | No | The log file location. Can be a strftime(3) compatible string. |

doc/man/hrmp.conf.5.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ output
3434
volume
3535
The volume in percent. -1 means use current volume
3636

37+
cache
38+
The cache size. Default is 256Mb
39+
3740
log_type
3841
The logging type (console, file, syslog). Default is console
3942

doc/manual/03-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The `Bool` data type supports the following values: `on`, `yes`, `1`, `true`, `o
2626
| device | | String | No | The default device name |
2727
| output | `[%n/%N] %d: %f [%i] (%t/%T) (%p)`| String | No | Defines the console output. Valid expansions are: `%n` (current track number), `%N` (total number of tracks), `%d` (device name), `%f` (file name), `%F` (full path of file), `%i` (file information), `%t` (current time), `%T` (total time), `%p` (percentage), `%b` (ringbuffer current size in Mb), `%B` (ringbuffer maximum size in Mb)|
2828
| volume | -1 | Int | No | The volume in percent. -1 means use current volume |
29+
| cache | 256Mb | Int | No | The cache size |
2930
| log_type | console | String | No | The logging type (console, file, syslog) |
3031
| log_level | info | String | No | The logging level, any of the (case insensitive) strings `FATAL`, `ERROR`, `WARN`, `INFO` and `DEBUG` (that can be more specific as `DEBUG1` thru `DEBUG5`). Debug level greater than 5 will be set to `DEBUG5`. Not recognized values will make the log_level be `INFO` |
3132
| log_path | hrmp.log | String | No | The log file location. Can be a strftime(3) compatible string. |

src/include/hrmp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ struct configuration
191191
int prev_volume; /**< The previous volume */
192192
bool is_muted; /**< Is muted */
193193

194+
size_t cache_size; /**< The cache size */
195+
194196
bool metadata; /**< Display metadata about files */
195197

196198
bool experimental; /**< Allow experimental features */

src/libhrmp/configuration.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <configuration.h>
2121
#include <devices.h>
2222
#include <logging.h>
23+
#include <ringbuffer.h>
2324
#include <shmem.h>
2425
#include <utils.h>
2526

@@ -47,6 +48,7 @@ static int as_logging_type(char* str);
4748
static int as_logging_level(char* str);
4849
static int as_logging_mode(char* str);
4950
static int as_volume(char* str);
51+
static int as_size(char* str, size_t def, size_t* size);
5052

5153
static unsigned int as_update_process_title(char* str, unsigned int* policy, unsigned int default_policy);
5254

@@ -93,6 +95,8 @@ hrmp_init_configuration(void* shm)
9395
config->prev_volume = -1;
9496
config->is_muted = false;
9597

98+
config->cache_size = HRMP_RINGBUFFER_MAX_BYTES;
99+
96100
config->metadata = false;
97101

98102
config->dop = false;
@@ -308,6 +312,13 @@ hrmp_read_configuration(void* shm, char* filename, bool emitWarnings)
308312
{
309313
drv.volume = as_volume(value);
310314
}
315+
else if (key_in_section("cache", section, key, true, &unknown))
316+
{
317+
if (as_size(value, HRMP_RINGBUFFER_MAX_BYTES, &config->cache_size))
318+
{
319+
unknown = true;
320+
}
321+
}
311322
else
312323
{
313324
unknown = true;
@@ -432,6 +443,11 @@ hrmp_validate_configuration(void* shm)
432443
}
433444
}
434445

446+
if (config->cache_size < HRMP_RINGBUFFER_MIN_BYTES)
447+
{
448+
config->cache_size = HRMP_RINGBUFFER_MIN_BYTES;
449+
}
450+
435451
return 0;
436452
}
437453

@@ -1383,3 +1399,90 @@ to_log_type(char* where, int value)
13831399

13841400
return 0;
13851401
}
1402+
1403+
static int
1404+
as_size(char* str, size_t def, size_t* size)
1405+
{
1406+
int multiplier = 1;
1407+
int index;
1408+
char value[MISC_LENGTH];
1409+
bool multiplier_set = false;
1410+
int i_value = def;
1411+
1412+
if (is_empty_string(str))
1413+
{
1414+
*size = def;
1415+
return 0;
1416+
}
1417+
1418+
index = 0;
1419+
for (size_t i = 0; i < strlen(str); i++)
1420+
{
1421+
if (isdigit(str[i]))
1422+
{
1423+
value[index++] = str[i];
1424+
}
1425+
else if (isalpha(str[i]) && multiplier_set)
1426+
{
1427+
// allow a 'B' suffix on a multiplier
1428+
// like for instance 'MB', but don't allow it
1429+
// for bytes themselves ('BB')
1430+
if (multiplier == 1 || (str[i] != 'b' && str[i] != 'B'))
1431+
{
1432+
// another non-digit char not allowed
1433+
goto error;
1434+
}
1435+
}
1436+
else if (isalpha(str[i]) && !multiplier_set)
1437+
{
1438+
if (str[i] == 'M' || str[i] == 'm')
1439+
{
1440+
multiplier = 1024 * 1024;
1441+
multiplier_set = true;
1442+
}
1443+
else if (str[i] == 'G' || str[i] == 'g')
1444+
{
1445+
multiplier = 1024 * 1024 * 1024;
1446+
multiplier_set = true;
1447+
}
1448+
else if (str[i] == 'K' || str[i] == 'k')
1449+
{
1450+
multiplier = 1024;
1451+
multiplier_set = true;
1452+
}
1453+
else if (str[i] == 'B' || str[i] == 'b')
1454+
{
1455+
multiplier = 1;
1456+
multiplier_set = true;
1457+
}
1458+
}
1459+
else
1460+
{
1461+
// do not allow alien chars
1462+
goto error;
1463+
}
1464+
}
1465+
1466+
value[index] = '\0';
1467+
if (!as_int(value, &i_value))
1468+
{
1469+
// sanity check: the value
1470+
// must be a positive number!
1471+
if (i_value >= 0)
1472+
{
1473+
*size = i_value * multiplier;
1474+
}
1475+
else
1476+
{
1477+
goto error;
1478+
}
1479+
1480+
return 0;
1481+
}
1482+
else
1483+
{
1484+
error:
1485+
*size = def;
1486+
return 1;
1487+
}
1488+
}

src/libhrmp/playback.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,14 @@ struct sndfile_vio_state
313313
static size_t
314314
ringbuffer_target_capacity(size_t file_size)
315315
{
316+
struct configuration* config = (struct configuration*)shmem;
317+
316318
if (file_size == 0)
317319
{
318320
return HRMP_RINGBUFFER_MIN_BYTES;
319321
}
320322

321-
size_t target = MIN(HRMP_RINGBUFFER_MAX_BYTES, file_size);
323+
size_t target = MIN(config->cache_size, file_size);
322324
if (target < HRMP_RINGBUFFER_MIN_BYTES)
323325
{
324326
target = HRMP_RINGBUFFER_MIN_BYTES;
@@ -330,12 +332,14 @@ ringbuffer_target_capacity(size_t file_size)
330332
static size_t
331333
ringbuffer_target_max(size_t file_size)
332334
{
333-
if (file_size > 0 && file_size < HRMP_RINGBUFFER_MAX_BYTES)
335+
struct configuration* config = (struct configuration*)shmem;
336+
337+
if (file_size > 0 && file_size < config->cache_size)
334338
{
335339
return file_size < HRMP_RINGBUFFER_MIN_BYTES ? HRMP_RINGBUFFER_MIN_BYTES : file_size;
336340
}
337341

338-
return HRMP_RINGBUFFER_MAX_BYTES;
342+
return config->cache_size;
339343
}
340344

341345
static void
@@ -1618,6 +1622,7 @@ playback_init(int number, int total,
16181622
{
16191623
char* desc = NULL;
16201624
struct playback* pb = NULL;
1625+
struct configuration* config = (struct configuration*)shmem;
16211626

16221627
*playback = NULL;
16231628

@@ -1646,8 +1651,8 @@ playback_init(int number, int total,
16461651

16471652
size_t cap = ringbuffer_target_capacity(pb->file_size);
16481653

1649-
/* If file < 256MiB, allow max up to file size (but never below the 4MiB minimum). */
1650-
size_t max_size = (pb->file_size > 0 && pb->file_size < HRMP_RINGBUFFER_MAX_BYTES) ? pb->file_size : HRMP_RINGBUFFER_MAX_BYTES;
1654+
/* If file < config->cache_size, allow max up to file size (but never below the 4MiB minimum). */
1655+
size_t max_size = (pb->file_size > 0 && pb->file_size < config->cache_size) ? pb->file_size : config->cache_size;
16511656
if (max_size < HRMP_RINGBUFFER_MIN_BYTES)
16521657
{
16531658
max_size = HRMP_RINGBUFFER_MIN_BYTES;

0 commit comments

Comments
 (0)