Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement configuration for BufferedInputStream::MAX_SIZE #1955

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,9 @@ These settings are various limitations to prevent :program:`MPD` from using too
- The maximum size a command list. Default is 2048 (2 MiB).
* - **max_output_buffer_size KBYTES**
- The maximum size of the output buffer to a client (maximum response size). Default is 8192 (8 MiB).
* - **max_buffered_input_stream_size KBYTES**
- Network streams of known size read from a server with seek ability and smaller than this size will be entirely read in memory. Larger streams are read by chunks, as needed. The default is 128 MiB, which may be too big for small machines.


Buffer Settings
^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions src/config/Option.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum class ConfigOption {
DESPOTIFY_HIGH_BITRATE,

MIXRAMP_ANALYZER,
MAX_BUFFERED_INPUT_STREAM_SIZE,

MAX
};
Expand Down
1 change: 1 addition & 0 deletions src/config/Templates.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const ConfigTemplate config_param_templates[] = {
{ "despotify_password", false, true },
{ "despotify_high_bitrate", false, true },
{ "mixramp_analyzer" },
{ "max_buffered_input_stream_size" },
};

static constexpr unsigned n_config_param_templates =
Expand Down
2 changes: 2 additions & 0 deletions src/input/BufferedInputStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <string.h>

offset_type BufferedInputStream::MAX_SIZE = 128 * 1024 * 1024;

BufferedInputStream::BufferedInputStream(InputStreamPtr _input)
:InputStream(_input->GetUriView(), _input->mutex),
BufferingInputStream(std::move(_input))
Expand Down
8 changes: 6 additions & 2 deletions src/input/BufferedInputStream.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
#include "BufferingInputStream.hxx"

#include <cassert>
#include <iostream>

/**
* A "huge" buffer which remembers the (partial) contents of an
* #InputStream. This works only if the #InputStream is a "file", not
* a "stream"; see IsEligible() for details.
*/
class BufferedInputStream final : public InputStream, BufferingInputStream {
// TODO: make configurable
static constexpr offset_type MAX_SIZE = 128 * 1024 * 1024;
// Default 128 MB, set in BufferedInputStream.cxx. Configurable, set from Init.cxx
static offset_type MAX_SIZE;

public:
BufferedInputStream(InputStreamPtr _input);
Expand Down Expand Up @@ -46,6 +47,9 @@ public:
size_t Read(std::unique_lock<Mutex> &lock,
void *ptr, size_t size) override;

static void SetMaxSize(offset_type maxsize) noexcept {
MAX_SIZE = maxsize;
}
private:
/* virtual methods from class BufferingInputStream */
void OnBufferAvailable() noexcept override {
Expand Down
13 changes: 13 additions & 0 deletions src/input/Init.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "Init.hxx"
#include "Registry.hxx"
#include "InputPlugin.hxx"
#include "BufferedInputStream.hxx"
#include "config/Data.hxx"
#include "config/Option.hxx"
#include "config/Block.hxx"
#include "config/Parser.hxx"
#include "Log.hxx"
#include "PluginUnavailable.hxx"
#include "lib/fmt/RuntimeError.hxx"
Expand All @@ -21,6 +23,7 @@
#endif

static constexpr Domain input_domain("input");
static constexpr size_t KILOBYTE = 1024;

void
input_stream_global_init(const ConfigData &config, EventLoop &event_loop)
Expand All @@ -29,6 +32,16 @@ input_stream_global_init(const ConfigData &config, EventLoop &event_loop)
InitUringInputPlugin(event_loop);
#endif

if (auto *param = config.GetParam(ConfigOption::MAX_BUFFERED_INPUT_STREAM_SIZE)) {
offset_type buffer_size = param->With([](const char *s){
size_t result = ParseSize(s, KILOBYTE);
if (result <= 0)
throw FmtRuntimeError("max buffered input size \"{}\" is not a positive integer", s);
return result;
});
BufferedInputStream::SetMaxSize(buffer_size);
}

const ConfigBlock empty;

for (unsigned i = 0; input_plugins[i] != nullptr; ++i) {
Expand Down