Skip to content

Commit 8a002ce

Browse files
committed
Core: add option to change ring size
1 parent da5bf73 commit 8a002ce

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/core/main.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@
3838
*
3939
*/
4040

41+
4142
#include <iostream>
4243
#include <string>
4344
#include <vector>
4445
#include <sys/types.h> // getpid()
4546
#include <unistd.h>
47+
#include <cstdlib>
48+
#include <cinttypes>
4649

4750
#include <ipfixcol2.h>
4851
#include <iostream>
@@ -90,10 +93,11 @@ print_help()
9093
<< " (default: " << fds_api_cfg_dir() << ")\n"
9194
<< " -P FILE Path to a PID file (without this option, no PID file is created)\n"
9295
<< " -d Run as a standalone daemon process\n"
96+
<< " -r SIZE Ring buffer size (default: " << ipx_configurator::RING_DEF_SIZE << ")\n"
9397
<< " -h Show this help message and exit\n"
9498
<< " -V Show version information and exit\n"
9599
<< " -v Increase verbosity level (by default, show only error messages)\n"
96-
<< " (can be used up to 3 times: adds warnings/infos/debug messages)\n";
100+
<< " (can be used up to 3 times to add warning/info/debug messages)\n";
97101
}
98102

99103
/**
@@ -154,6 +158,36 @@ pid_remove(const char *file)
154158
return IPX_OK;
155159
}
156160

161+
/**
162+
* \brief Change size of ring buffers
163+
* \param[in] conf IPFIXcol configurator
164+
* \param[in] new_size New size (from command line)
165+
* \return #IPX_OK on success
166+
* \return #IPX_ERR_FORMAT if the \p new_size is not valid size
167+
*/
168+
static int
169+
ring_size_change(ipx_configurator &conf, const char *new_size)
170+
{
171+
char *end_ptr = nullptr;
172+
errno = 0;
173+
unsigned int size = std::strtoul(new_size, &end_ptr, 10);
174+
if (errno != 0 || (end_ptr != nullptr && (*end_ptr) != '\0')) {
175+
IPX_ERROR(module, "Size '%s' of the ring buffers is not a valid number!", new_size);
176+
return IPX_ERR_FORMAT;
177+
}
178+
179+
const uint32_t min_size = ipx_configurator::RING_MIN_SIZE;
180+
if (size < min_size) {
181+
IPX_ERROR(module, "Size of the ring buffers must be at least %" PRIu32 " messages.",
182+
min_size);
183+
return IPX_ERR_FORMAT;
184+
}
185+
186+
conf.set_buffer_size(size);
187+
IPX_INFO(module, "Ring buffer size set to %u messages", size);
188+
return IPX_OK;
189+
}
190+
157191
/**
158192
* \brief Main function
159193
* \param[in] argc Number of arguments
@@ -165,13 +199,14 @@ int main(int argc, char *argv[])
165199
const char *cfg_startup = nullptr;
166200
const char *cfg_iedir = nullptr;
167201
const char *pid_file = nullptr;
202+
const char *ring_size = nullptr;
168203
bool daemon_en = false;
169204
ipx_configurator conf;
170205

171206
// Parse configuration
172207
int opt;
173208
opterr = 0; // Disable default error messages
174-
while ((opt = getopt(argc, argv, "c:vVhdp:e:P:")) != -1) {
209+
while ((opt = getopt(argc, argv, "c:vVhdp:e:P:r:")) != -1) {
175210
switch (opt) {
176211
case 'c': // Configuration file
177212
cfg_startup = optarg;
@@ -197,6 +232,9 @@ int main(int argc, char *argv[])
197232
case 'P':
198233
pid_file = optarg;
199234
break;
235+
case 'r':
236+
ring_size = optarg;
237+
break;
200238
default: // ?
201239
std::cerr << "Unknown parameter '" << static_cast<char>(optopt) << "'!" << std::endl;
202240
return EXIT_FAILURE;
@@ -224,6 +262,11 @@ int main(int argc, char *argv[])
224262
}
225263
}
226264

265+
if (ring_size != nullptr && ring_size_change(conf, ring_size) != IPX_OK) {
266+
// Failed to set the size
267+
return EXIT_FAILURE;
268+
}
269+
227270
// Always use the default directory for looking for plugins, but with the lowest priority
228271
conf.finder.path_add(IPX_DEFAULT_PLUGINS_DIR);
229272
conf.iemgr_set_dir(cfg_iedir);

0 commit comments

Comments
 (0)