From da3c9ec137aaec74635ca1f5584040c20b1d3d7b Mon Sep 17 00:00:00 2001 From: David Goulet Date: Fri, 12 Apr 2013 15:47:13 -0400 Subject: [PATCH] Initial import of the snapshot ABI/API in lttng-ctl Nothing on the session daemon side as been added except the command values added to the process client command subsystem. Signed-off-by: David Goulet --- include/Makefile.am | 4 +- include/lttng/snapshot-internal.h | 67 +++++ include/lttng/snapshot.h | 145 ++++++++++ src/bin/lttng-sessiond/main.c | 24 ++ src/common/defaults.h | 2 + src/common/sessiond-comm/sessiond-comm.h | 16 ++ src/lib/lttng-ctl/Makefile.am | 2 +- src/lib/lttng-ctl/lttng-ctl.c | 5 + src/lib/lttng-ctl/snapshot.c | 325 +++++++++++++++++++++++ 9 files changed, 588 insertions(+), 2 deletions(-) create mode 100644 include/lttng/snapshot-internal.h create mode 100644 include/lttng/snapshot.h create mode 100644 src/lib/lttng-ctl/snapshot.c diff --git a/include/Makefile.am b/include/Makefile.am index 0bcb6f92f..f3413e6df 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1 +1,3 @@ -lttnginclude_HEADERS = lttng/lttng.h lttng/lttng-error.h +lttnginclude_HEADERS = lttng/lttng.h lttng/lttng-error.h lttng/snapshot.h + +noinst_HEADERS = lttng/snapshot-internal.h diff --git a/include/lttng/snapshot-internal.h b/include/lttng/snapshot-internal.h new file mode 100644 index 000000000..a14564b3b --- /dev/null +++ b/include/lttng/snapshot-internal.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 - David Goulet + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef LTTNG_SNAPSHOT_INTERNAL_ABI_H +#define LTTNG_SNAPSHOT_INTERNAL_ABI_H + +#include +#include + +/* + * Object used for the snapshot API. This is opaque to the public library. + */ +struct lttng_snapshot_output { + /* + * ID of the snapshot output. This is only used when they are listed. It is + * assigned by the session daemon so when adding an output, this value will + * not be used. + */ + uint32_t id; + /* + * Maximum size in bytes of the snapshot meaning the total size of all + * stream combined. A value of 0 is unlimited. + */ + uint64_t max_size; + /* Name of the output so it can be recognized easily when listing them. */ + char name[NAME_MAX]; + /* Destination of the output. See lttng(1) for URL format. */ + char ctrl_url[PATH_MAX]; + /* Destination of the output. See lttng(1) for URL format. */ + char data_url[PATH_MAX]; +}; + +/* + * Snapshot output list object opaque to the user. + */ +struct lttng_snapshot_output_list { + /* + * The position in the output array. This is changed by a get_next call. + */ + int index; + + /* + * Number of element in the array. + */ + size_t count; + + /* + * Containes snapshot output object. + */ + struct lttng_snapshot_output *array; +}; + +#endif /* LTTNG_SNAPSHOT_INTERNAL_ABI_H */ diff --git a/include/lttng/snapshot.h b/include/lttng/snapshot.h new file mode 100644 index 000000000..8f5ee898e --- /dev/null +++ b/include/lttng/snapshot.h @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2013 - David Goulet + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef LTTNG_SNAPSHOT_H +#define LTTNG_SNAPSHOT_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Snapshot output object is opaque to the user. Use the helper functions below + * to use them. + */ +struct lttng_snapshot_output; +struct lttng_snapshot_output_list; + +/* + * Return an newly allocated snapshot output object or NULL on error. + */ +struct lttng_snapshot_output *lttng_snapshot_output_create(void); + +/* + * Free a given snapshot output object. + */ +void lttng_snapshot_output_destroy(struct lttng_snapshot_output *output); + +/* + * Snapshot output getter family functions. They all return the value present + * in the object. + */ + +/* Return snapshot ID. */ +uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output *output); +/* Return maximum size of a snapshot. */ +uint64_t lttng_snapshot_output_get_maxsize(struct lttng_snapshot_output *output); +/* Return snapshot name. */ +const char *lttng_snapshot_output_get_name(struct lttng_snapshot_output *output); +/* Return snapshot control URL in a text format. */ +const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output *output); +/* Return snapshot data URL in a text format. */ +const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output *output); + +/* + * Snapshot output setter family functions. + * + * For every set* call, 0 is returned on success or else LTTNG_ERR_INVALID is + * returned indicating that at least one given parameter is invalid. + */ + +/* Set a custom ID. */ +int lttng_snapshot_output_set_id(uint32_t id, + struct lttng_snapshot_output *output); +/* Set the maximum size. */ +int lttng_snapshot_output_set_size(uint64_t size, + struct lttng_snapshot_output *output); +/* Set the snapshot name. */ +int lttng_snapshot_output_set_name(const char *name, + struct lttng_snapshot_output *output); +/* Set the control URL. Local and remote URL are supported. */ +int lttng_snapshot_output_set_ctrl_url(const char *url, + struct lttng_snapshot_output *output); +/* Set the data URL. Local and remote URL are supported. */ +int lttng_snapshot_output_set_data_url(const char *url, + struct lttng_snapshot_output *output); + +/* + * Add an output object to a session identified by name. + * + * Return 0 on success or else a negative LTTNG_ERR code. + */ +int lttng_snapshot_add_output(const char *session_name, + struct lttng_snapshot_output *output); + +/* + * Delete an output object to a session identified by name. + * + * Return 0 on success or else a negative LTTNG_ERR code. + */ +int lttng_snapshot_del_output(const char *session_name, + struct lttng_snapshot_output *output); + +/* + * List all snapshot output(s) of a session identified by name. The output list + * object is populated and can be iterated over with the get_next call below. + * + * Return 0 on success or else a negative LTTNG_ERR code and the list pointer + * is untouched. + */ +int lttng_snapshot_list_output(const char *session_name, + struct lttng_snapshot_output_list **list); + +/* + * Return the next available snapshot output object in the given list. A list + * output command MUST have been done before. + * + * Return the next object on success or else NULL indicating the end of the + * list. + */ +struct lttng_snapshot_output *lttng_snapshot_output_list_get_next( + struct lttng_snapshot_output_list *list); + +/* + * Free an output list object. + */ +void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list *list); + +/* + * Snapshot a trace for the given session. + * + * The output object can be NULL but an add output MUST be done prior to this + * call. If it's not NULL, it will be used to snapshot a trace. + * + * The wait parameter is ignored for now. The snapshot record command will + * ALWAYS wait for the snapshot to complete before returning meaning the + * snapshot has been written on disk or streamed over the network to a relayd. + * + * Return 0 on success or else a negative LTTNG_ERR value. + */ +int lttng_snapshot_record(const char *session_name, + struct lttng_snapshot_output *output, int wait); + +#ifdef __cplusplus +} +#endif + +#endif /* LTTNG_SNAPSHOT_H */ diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index f5a4db4dc..31f48a273 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -2494,6 +2494,10 @@ static int process_client_msg(struct command_ctx *cmd_ctx, int sock, case LTTNG_START_TRACE: case LTTNG_STOP_TRACE: case LTTNG_DATA_PENDING: + case LTTNG_SNAPSHOT_ADD_OUTPUT: + case LTTNG_SNAPSHOT_DEL_OUTPUT: + case LTTNG_SNAPSHOT_LIST_OUTPUT: + case LTTNG_SNAPSHOT_RECORD: need_domain = 0; break; default: @@ -3176,6 +3180,26 @@ skip_domain: ret = cmd_data_pending(cmd_ctx->session); break; } + case LTTNG_SNAPSHOT_ADD_OUTPUT: + { + ret = LTTNG_ERR_UND; + break; + } + case LTTNG_SNAPSHOT_DEL_OUTPUT: + { + ret = LTTNG_ERR_UND; + break; + } + case LTTNG_SNAPSHOT_LIST_OUTPUT: + { + ret = LTTNG_ERR_UND; + break; + } + case LTTNG_SNAPSHOT_RECORD: + { + ret = LTTNG_ERR_UND; + break; + } default: ret = LTTNG_ERR_UND; break; diff --git a/src/common/defaults.h b/src/common/defaults.h index c04063488..388b9f0d7 100644 --- a/src/common/defaults.h +++ b/src/common/defaults.h @@ -204,6 +204,8 @@ #define DEFAULT_UST_STREAM_FD_NUM 2 /* Number of fd per UST stream. */ +#define DEFAULT_SNAPSHOT_NAME "snapshot" + extern size_t default_channel_subbuf_size; extern size_t default_metadata_subbuf_size; extern size_t default_ust_pid_channel_subbuf_size; diff --git a/src/common/sessiond-comm/sessiond-comm.h b/src/common/sessiond-comm/sessiond-comm.h index c0b89a123..aeca78e50 100644 --- a/src/common/sessiond-comm/sessiond-comm.h +++ b/src/common/sessiond-comm/sessiond-comm.h @@ -28,6 +28,7 @@ #define _GNU_SOURCE #include #include +#include #include #include #include @@ -82,6 +83,10 @@ enum lttcomm_sessiond_command { LTTNG_ENABLE_EVENT_WITH_FILTER = 22, LTTNG_HEALTH_CHECK = 23, LTTNG_DATA_PENDING = 24, + LTTNG_SNAPSHOT_ADD_OUTPUT = 25, + LTTNG_SNAPSHOT_DEL_OUTPUT = 26, + LTTNG_SNAPSHOT_LIST_OUTPUT = 27, + LTTNG_SNAPSHOT_RECORD = 28, }; enum lttcomm_relayd_command { @@ -240,6 +245,13 @@ struct lttcomm_session_msg { /* Number of lttng_uri following */ uint32_t size; } LTTNG_PACKED uri; + struct { + struct lttng_snapshot_output output; + } LTTNG_PACKED snapshot_output; + struct { + uint32_t wait; + struct lttng_snapshot_output output; + } LTTNG_PACKED snapshot_record; } u; } LTTNG_PACKED; @@ -271,6 +283,10 @@ struct lttcomm_lttng_msg { char payload[]; } LTTNG_PACKED; +struct lttcomm_lttng_output_id { + uint32_t id; +} LTTNG_PACKED; + struct lttcomm_health_msg { uint32_t component; uint32_t cmd; diff --git a/src/lib/lttng-ctl/Makefile.am b/src/lib/lttng-ctl/Makefile.am index 542f4bbcf..2178ed32f 100644 --- a/src/lib/lttng-ctl/Makefile.am +++ b/src/lib/lttng-ctl/Makefile.am @@ -4,7 +4,7 @@ SUBDIRS = filter lib_LTLIBRARIES = liblttng-ctl.la -liblttng_ctl_la_SOURCES = lttng-ctl.c +liblttng_ctl_la_SOURCES = lttng-ctl.c snapshot.c liblttng_ctl_la_LIBADD = \ $(top_builddir)/src/common/sessiond-comm/libsessiond-comm.la \ diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index afd55a1ab..c9693f711 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -340,6 +340,11 @@ static int connect_sessiond(void) { int ret; + /* Don't try to connect if already connected. */ + if (connected) { + return 0; + } + ret = set_session_daemon_path(); if (ret < 0) { goto error; diff --git a/src/lib/lttng-ctl/snapshot.c b/src/lib/lttng-ctl/snapshot.c new file mode 100644 index 000000000..9dc2c679b --- /dev/null +++ b/src/lib/lttng-ctl/snapshot.c @@ -0,0 +1,325 @@ +/* + * Copyright (C) 2013 - David Goulet + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License, version 2.1 only, + * as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _GNU_SOURCE +#include +#include + +#include +#include +#include +#include + +#include "lttng-ctl-helper.h" + +/* + * Add an output object to a session identified by name. + * + * Return 0 on success or else a negative LTTNG_ERR code. + */ +int lttng_snapshot_add_output(const char *session_name, + struct lttng_snapshot_output *output) +{ + int ret; + struct lttcomm_session_msg lsm; + struct lttcomm_lttng_output_id *reply; + + if (!session_name || !output) { + return -LTTNG_ERR_INVALID; + } + + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_SNAPSHOT_ADD_OUTPUT; + + lttng_ctl_copy_string(lsm.session.name, session_name, + sizeof(lsm.session.name)); + memcpy(&lsm.u.snapshot_output.output, output, + sizeof(lsm.u.snapshot_output.output)); + + ret = lttng_ctl_ask_sessiond(&lsm, (void **) &reply); + if (ret < 0) { + return ret; + } + + output->id = reply->id; + free(reply); + + return 0; +} + +/* + * Delete an output object to a session identified by name. + * + * Return 0 on success or else a negative LTTNG_ERR code. + */ +int lttng_snapshot_del_output(const char *session_name, + struct lttng_snapshot_output *output) +{ + struct lttcomm_session_msg lsm; + + if (!session_name || !output) { + return -LTTNG_ERR_INVALID; + } + + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_SNAPSHOT_DEL_OUTPUT; + + lttng_ctl_copy_string(lsm.session.name, session_name, + sizeof(lsm.session.name)); + memcpy(&lsm.u.snapshot_output.output, output, + sizeof(lsm.u.snapshot_output.output)); + + return lttng_ctl_ask_sessiond(&lsm, NULL); +} + +/* + * List all snapshot output(s) of a session identified by name. The output list + * object is populated and can be iterated over with the get_next call below. + * + * Return 0 on success or else a negative LTTNG_ERR code and the list pointer + * is untouched. + */ +int lttng_snapshot_list_output(const char *session_name, + struct lttng_snapshot_output_list **list) +{ + int ret; + struct lttcomm_session_msg lsm; + struct lttng_snapshot_output_list *new_list = NULL; + + if (!session_name || !list) { + ret = -LTTNG_ERR_INVALID; + goto error; + } + + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_SNAPSHOT_LIST_OUTPUT; + + lttng_ctl_copy_string(lsm.session.name, session_name, + sizeof(lsm.session.name)); + + new_list = zmalloc(sizeof(*new_list)); + if (!new_list) { + ret = -LTTNG_ERR_NOMEM; + goto error; + } + + ret = lttng_ctl_ask_sessiond(&lsm, (void **) &new_list->array); + if (ret < 0) { + goto free_error; + } + + new_list->count = ret / sizeof(struct lttng_snapshot_output); + *list = new_list; + return 0; + +free_error: + free(new_list); +error: + return ret; +} + +/* + * Return the next available snapshot output object in the given list. A list + * output command MUST have been done before. + * + * Return the next object on success or else NULL indicating the end of the + * list. + */ +struct lttng_snapshot_output *lttng_snapshot_output_list_get_next( + struct lttng_snapshot_output_list *list) +{ + struct lttng_snapshot_output *output = NULL; + + if (!list) { + goto error; + } + + /* We've reached the end. */ + if (list->index == list->count) { + goto end; + } + + output = &list->array[list->index]; + list->index++; + +end: +error: + return output; +} + +/* + * Free an output list object. + */ +void lttng_snapshot_output_list_destroy(struct lttng_snapshot_output_list *list) +{ + if (!list) { + return; + } + + free(list->array); + free(list); +} + +/* + * Snapshot a trace for the given session. + * + * The output object can be NULL but an add output MUST be done prior to this + * call. If it's not NULL, it will be used to snapshot a trace. + * + * The wait parameter is ignored for now. The snapshot record command will + * ALWAYS wait for the snapshot to complete before returning meaning the + * snapshot has been written on disk or streamed over the network to a relayd. + * + * Return 0 on success or else a negative LTTNG_ERR value. + */ +int lttng_snapshot_record(const char *session_name, + struct lttng_snapshot_output *output, int wait) +{ + struct lttcomm_session_msg lsm; + + if (!session_name) { + return -LTTNG_ERR_INVALID; + } + + memset(&lsm, 0, sizeof(lsm)); + lsm.cmd_type = LTTNG_SNAPSHOT_RECORD; + + lttng_ctl_copy_string(lsm.session.name, session_name, + sizeof(lsm.session.name)); + + /* + * Not having an output object will use the default one of the session that + * would need to be set by a call to add output prior to calling snapshot + * record. + */ + if (output) { + memcpy(&lsm.u.snapshot_record.output, output, + sizeof(lsm.u.snapshot_record.output)); + } + + /* The wait param is ignored. */ + + return lttng_ctl_ask_sessiond(&lsm, NULL); +} + +/* + * Return an newly allocated snapshot output object or NULL on error. + */ +struct lttng_snapshot_output *lttng_snapshot_output_create(void) +{ + return zmalloc(sizeof(struct lttng_snapshot_output)); +} + +/* + * Free a given snapshot output object. + */ +void lttng_snapshot_output_destroy(struct lttng_snapshot_output *obj) +{ + if (obj) { + free(obj); + } +} + +/* + * Getter family functions of snapshot output. + */ + +uint32_t lttng_snapshot_output_get_id(struct lttng_snapshot_output *output) +{ + return output->id; +} + +const char *lttng_snapshot_output_get_name( + struct lttng_snapshot_output *output) +{ + return output->name; +} + +const char *lttng_snapshot_output_get_data_url(struct lttng_snapshot_output *output) +{ + return output->data_url; +} + +const char *lttng_snapshot_output_get_ctrl_url(struct lttng_snapshot_output *output) +{ + return output->ctrl_url; +} + +uint64_t lttng_snapshot_output_get_maxsize( + struct lttng_snapshot_output *output) +{ + return output->max_size; +} + +/* + * Setter family functions for snapshot output. + */ + +int lttng_snapshot_output_set_id(uint32_t id, + struct lttng_snapshot_output *output) +{ + if (!output || id == 0) { + return -LTTNG_ERR_INVALID; + } + + output->id = id; + return 0; +} + +int lttng_snapshot_output_set_size(uint64_t size, + struct lttng_snapshot_output *output) +{ + if (!output) { + return -LTTNG_ERR_INVALID; + } + + output->max_size = size; + return 0; +} + +int lttng_snapshot_output_set_name(const char *name, + struct lttng_snapshot_output *output) +{ + if (!output || !name) { + return -LTTNG_ERR_INVALID; + } + + lttng_ctl_copy_string(output->name, name, sizeof(output->name)); + return 0; +} + +int lttng_snapshot_output_set_ctrl_url(const char *url, + struct lttng_snapshot_output *output) +{ + if (!output || !url) { + return -LTTNG_ERR_INVALID; + } + + lttng_ctl_copy_string(output->ctrl_url, url, sizeof(output->ctrl_url)); + return 0; +} + +int lttng_snapshot_output_set_data_url(const char *url, + struct lttng_snapshot_output *output) +{ + if (!output || !url) { + return -LTTNG_ERR_INVALID; + } + + lttng_ctl_copy_string(output->data_url, url, sizeof(output->data_url)); + return 0; +} -- 2.34.1