X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fenable_channels.c;h=4816d62c68263a4a26a245fabc9764c17365424e;hp=fbf02a0580c520fc37f9baf62cdd3ec2a962387c;hb=de65565af61d60ddad1b309c969d6a2129ba4f19;hpb=e132a0d4a23036da549cb5d09ea9170183c45543 diff --git a/src/bin/lttng/commands/enable_channels.c b/src/bin/lttng/commands/enable_channels.c index fbf02a058..4816d62c6 100644 --- a/src/bin/lttng/commands/enable_channels.c +++ b/src/bin/lttng/commands/enable_channels.c @@ -24,10 +24,14 @@ #include #include #include +#include +#include #include "../command.h" +#include "../utils.h" #include +#include static char *opt_channels; static int opt_kernel; @@ -66,7 +70,7 @@ static struct poptOption long_options[] = { {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0}, {"discard", 0, POPT_ARG_NONE, 0, OPT_DISCARD, 0, 0}, {"overwrite", 0, POPT_ARG_NONE, 0, OPT_OVERWRITE, 0, 0}, - {"subbuf-size", 0, POPT_ARG_DOUBLE, 0, OPT_SUBBUF_SIZE, 0, 0}, + {"subbuf-size", 0, POPT_ARG_STRING, 0, OPT_SUBBUF_SIZE, 0, 0}, {"num-subbuf", 0, POPT_ARG_INT, 0, OPT_NUM_SUBBUF, 0, 0}, {"switch-timer", 0, POPT_ARG_INT, 0, OPT_SWITCH_TIMER, 0, 0}, {"read-timer", 0, POPT_ARG_INT, 0, OPT_READ_TIMER, 0, 0}, @@ -99,17 +103,15 @@ static void usage(FILE *ofp) DEFAULT_CHANNEL_OVERWRITE ? "" : " (default)"); fprintf(ofp, " --overwrite Flight recorder mode%s\n", DEFAULT_CHANNEL_OVERWRITE ? " (default)" : ""); - fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes\n"); + fprintf(ofp, " --subbuf-size SIZE Subbuffer size in bytes {+k,+M,+G}\n"); fprintf(ofp, " (default: %zu, kernel default: %zu)\n", default_get_channel_subbuf_size(), default_get_kernel_channel_subbuf_size()); - fprintf(ofp, " Needs to be a power of 2 for\n"); - fprintf(ofp, " kernel and ust tracers\n"); + fprintf(ofp, " Rounded up to the next power of 2.\n"); fprintf(ofp, " --num-subbuf NUM Number of subbufers\n"); fprintf(ofp, " (default: %u)\n", DEFAULT_CHANNEL_SUBBUF_NUM); - fprintf(ofp, " Needs to be a power of 2 for\n"); - fprintf(ofp, " kernel and ust tracers\n"); + fprintf(ofp, " Rounded up to the next power of 2.\n"); fprintf(ofp, " --switch-timer USEC Switch timer interval in usec (default: %u)\n", DEFAULT_CHANNEL_SWITCH_TIMER); fprintf(ofp, " --read-timer USEC Read timer interval in usec (UST default: %u, kernel default: %u)\n", @@ -206,11 +208,9 @@ static int enable_channel(char *session_name) if ((chan.attr.tracefile_size > 0) && (chan.attr.tracefile_size < chan.attr.subbuf_size)) { - ERR("Tracefile_size must be greater than or equal to subbuf_size " - "(%" PRIu64 " < %" PRIu64 ")", + WARN("Tracefile size rounded up from (%" PRIu64 ") to subbuffer size (%" PRIu64 ")", chan.attr.tracefile_size, chan.attr.subbuf_size); - ret = CMD_ERROR; - goto error; + chan.attr.tracefile_size = chan.attr.subbuf_size; } /* Setting channel output */ @@ -299,6 +299,7 @@ int cmd_enable_channels(int argc, const char **argv) int opt, ret = CMD_SUCCESS; static poptContext pc; char *session_name = NULL; + char *opt_arg = NULL; init_channel_config(); @@ -319,38 +320,139 @@ int cmd_enable_channels(int argc, const char **argv) DBG("Channel set to overwrite"); break; case OPT_SUBBUF_SIZE: - /* TODO Replace atol with strtol and check for errors */ - chan.attr.subbuf_size = atol(poptGetOptArg(pc)); + { + uint64_t rounded_size; + int order; + + /* Parse the size */ + opt_arg = poptGetOptArg(pc); + if (utils_parse_size_suffix(opt_arg, &chan.attr.subbuf_size) < 0 || !chan.attr.subbuf_size) { + ERR("Wrong value in --subbuf-size parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + + order = get_count_order_u64(chan.attr.subbuf_size); + assert(order >= 0); + rounded_size = 1ULL << order; + if (rounded_size != chan.attr.subbuf_size) { + WARN("The subbuf size (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", + chan.attr.subbuf_size, rounded_size); + chan.attr.subbuf_size = rounded_size; + } + + /* Should now be power of 2 */ + assert(!((chan.attr.subbuf_size - 1) & chan.attr.subbuf_size)); + DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size); break; + } case OPT_NUM_SUBBUF: - /* TODO Replace atoi with strtol and check for errors */ - chan.attr.num_subbuf = atoi(poptGetOptArg(pc)); + { + uint64_t rounded_size; + int order; + + errno = 0; + opt_arg = poptGetOptArg(pc); + chan.attr.num_subbuf = strtoull(opt_arg, NULL, 0); + if (errno != 0 || !chan.attr.num_subbuf || !isdigit(opt_arg[0])) { + ERR("Wrong value in --num-subbuf parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + + order = get_count_order_u64(chan.attr.num_subbuf); + assert(order >= 0); + rounded_size = 1ULL << order; + if (rounded_size != chan.attr.num_subbuf) { + WARN("The number of subbuffers (%" PRIu64 ") is rounded to the next power of 2 (%" PRIu64 ")", + chan.attr.num_subbuf, rounded_size); + chan.attr.num_subbuf = rounded_size; + } + + /* Should now be power of 2 */ + assert(!((chan.attr.num_subbuf - 1) & chan.attr.num_subbuf)); + DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf); break; + } case OPT_SWITCH_TIMER: - /* TODO Replace atoi with strtol and check for errors */ - chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc)); + { + unsigned long v; + + errno = 0; + opt_arg = poptGetOptArg(pc); + v = strtoul(opt_arg, NULL, 0); + if (errno != 0 || !isdigit(opt_arg[0])) { + ERR("Wrong value in --switch-timer parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + if (v != (uint32_t) v) { + ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + chan.attr.switch_timer_interval = (uint32_t) v; DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval); break; + } case OPT_READ_TIMER: - /* TODO Replace atoi with strtol and check for errors */ - chan.attr.read_timer_interval = atoi(poptGetOptArg(pc)); + { + unsigned long v; + + errno = 0; + opt_arg = poptGetOptArg(pc); + v = strtoul(opt_arg, NULL, 0); + if (errno != 0 || !isdigit(opt_arg[0])) { + ERR("Wrong value in --read-timer parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + if (v != (uint32_t) v) { + ERR("32-bit overflow in --read-timer parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + chan.attr.read_timer_interval = (uint32_t) v; DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval); break; + } case OPT_USERSPACE: opt_userspace = 1; break; case OPT_TRACEFILE_SIZE: - chan.attr.tracefile_size = atoll(poptGetOptArg(pc)); + opt_arg = poptGetOptArg(pc); + if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) { + ERR("Wrong value in --tracefile-size parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } DBG("Maximum tracefile size set to %" PRIu64, chan.attr.tracefile_size); break; case OPT_TRACEFILE_COUNT: - chan.attr.tracefile_count = atoll(poptGetOptArg(pc)); + { + unsigned long v; + + errno = 0; + opt_arg = poptGetOptArg(pc); + v = strtoul(opt_arg, NULL, 0); + if (errno != 0 || !isdigit(opt_arg[0])) { + ERR("Wrong value in --tracefile-count parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + if (v != (uint32_t) v) { + ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg); + ret = CMD_ERROR; + goto end; + } + chan.attr.tracefile_count = (uint32_t) v; DBG("Maximum tracefile count set to %" PRIu64, chan.attr.tracefile_count); break; + } case OPT_LIST_OPTIONS: list_cmd_options(stdout, long_options); goto end;