NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
telemetry_export_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2022 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <stdbool.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <inttypes.h>
35 
36 #include <doca_log.h>
38 
39 #define NB_EXAMPLE_STRINGS 5 /* Amount of example strings */
40 #define MAX_EXAMPLE_STRING_SIZE 256 /* Indicates the max length of string */
41 #define SINGLE_FIELD_VALUE 1 /* Indicates the field contains one value */
42 
43 DOCA_LOG_REGISTER(TELEMETRY_EXPORTER);
44 
45 static char *example_strings[NB_EXAMPLE_STRINGS] = {"example_str_1",
46  "example_str_2",
47  "example_str_3",
48  "example_str_4",
49  "example_str_5"};
50 
51 /* Event struct from which report will be serialized */
54  int32_t event_number;
55  int32_t iter_number;
56  uint64_t string_number;
58 } __attribute__((packed));
59 
60 /*
61  * This function fills up event buffer with the example string of specified number.
62  * It also saves number of iteration, number of string and overall number of events.
63  *
64  * @iter_number [in]: Iteration number to insert to event
65  * @string_number [in]: String number to insert to event
66  * @event [out]: The event getting filled
67  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
68  */
70 {
71  static int collected_example_events_count; /* Initialized to 0 by default, only in first call to function */
74 
76  if (result != DOCA_SUCCESS)
77  return result;
78 
79  event->timestamp = timestamp;
80  event->event_number = collected_example_events_count++;
81  event->iter_number = iter_number;
82  event->string_number = string_number;
86  return result;
87 }
88 
89 /*
90  * Registers the example fields to the doca_telemetry_exporter_type.
91  *
92  * @type [out]: The doca_telemetry_exporter_type whose getting registered
93  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
94  */
95 static doca_error_t telemetry_register_fields(struct doca_telemetry_exporter_type *type)
96 {
98  struct doca_telemetry_exporter_field *field;
99  const int nb_fields = 5;
100  int idx = 0;
101  struct {
102  const char *name;
103  const char *desc;
104  const char *type_name;
105  uint16_t len;
106  } fields_info[] = {
107  {"timestamp", "Event timestamp", DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_TIMESTAMP, SINGLE_FIELD_VALUE},
108  {"event_number", "Event number", DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_INT32, SINGLE_FIELD_VALUE},
109  {"iter_num", "Iteration number", DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_INT32, SINGLE_FIELD_VALUE},
110  {"string_number", "String number", DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_UINT64, SINGLE_FIELD_VALUE},
111  {"example_string", "String example", DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_CHAR, MAX_EXAMPLE_STRING_SIZE},
112  };
113 
114  for (idx = 0; idx < nb_fields; idx++) {
116  if (result != DOCA_SUCCESS)
117  return result;
118 
119  doca_telemetry_exporter_field_set_name(field, fields_info[idx].name);
120  doca_telemetry_exporter_field_set_description(field, fields_info[idx].desc);
121  doca_telemetry_exporter_field_set_type_name(field, fields_info[idx].type_name);
122  doca_telemetry_exporter_field_set_array_len(field, fields_info[idx].len);
123 
125  if (result != DOCA_SUCCESS)
126  return result;
127  }
128 
129  return result;
130 }
131 
132 /*
133  * Main sample function.
134  * Creates DOCA Telemetry schema and DOCA Telemetry source, prepares events,
135  * and sends events through DOCA Telemetry API.
136  *
137  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
138  */
140 {
141  bool file_write_enable = true; /* Enables writing to local machine as file */
142  bool ipc_enabled = true; /* Enables sending to DTS through ipc sockets */
143  int repetition = 10; /* Repetition amount of exporting telemetry */
145  int32_t iteration = 0;
146  uint64_t string_number = 0;
147  struct doca_telemetry_exporter_schema *doca_schema = NULL;
148  struct doca_telemetry_exporter_source *doca_source = NULL;
149  struct test_event_type test_event;
151 
152  /* Event type for DOCA Telemetry schema. Should be consistent with event struct */
153  struct doca_telemetry_exporter_type *example_type;
154 
155  /* DOCA Telemetry schema initialization and attributes configuration */
156 
157  /* Init DOCA schema */
158  result = doca_telemetry_exporter_schema_init("example_doca_schema_name", &doca_schema);
159  if (result != DOCA_SUCCESS) {
160  DOCA_LOG_ERR("Cannot init doca schema");
161  return result;
162  }
163 
164  /*
165  * Set buffer size in bytes to fit 5 example events. By default it is set to 60K.
166  * Data root should be set to keep data schemas and binary data if file_write
167  * is enabled.
168  */
169  doca_telemetry_exporter_schema_set_buf_size(doca_schema, sizeof(test_event) * 5);
170 
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Cannot create type");
174  goto err_schema;
175  }
176 
177  result = telemetry_register_fields(example_type);
178  if (result != DOCA_SUCCESS) {
179  DOCA_LOG_ERR("Cannot register fields");
181  goto err_schema;
182  }
183 
184  /*
185  * Enable file write during the app development.
186  * Check written files under data root to make sure that data format is correct.
187  * Default max_file_size is 1 Mb, default max_file_age is 1 hour.
188  */
189  if (file_write_enable)
191 
192  /*
193  * If IPC is enabled, DOCA Telemetry will try to find DOCA Telemetry Service (DTS) socket
194  * under ipc_sockets_dir. IPC is disabled by default.
195  * Optionally change parameters for IPC connection/reconnection tries
196  * and IPC socket timeout. Default values are 100 msec, 3 tries, and 500 ms accordingly.
197  * see doca_telemetry_exporter_schema_set_ipc_* functions for details.
198  */
199  if (ipc_enabled)
201 
202  /* Add DOCA Telemetry schema types */
203  result = doca_telemetry_exporter_schema_add_type(doca_schema, "example_event", example_type, &example_index);
204  if (result != DOCA_SUCCESS) {
205  DOCA_LOG_ERR("Cannot add type to doca_schema!");
206  goto err_schema;
207  }
208 
209  /* "apply" DOCA Telemetry schema */
211  if (result != DOCA_SUCCESS) {
212  DOCA_LOG_ERR("Cannot start doca_schema!");
213  goto err_schema;
214  }
215 
216  /* DOCA Telemetry source initialization */
217 
218  /* Create DOCA Telemetry Source context from DOCA Telemetry schema */
219  result = doca_telemetry_exporter_source_create(doca_schema, &doca_source);
220  if (result != DOCA_SUCCESS) {
221  DOCA_LOG_ERR("Cannot create doca_source!");
222  goto err_schema;
223  }
224 
225  doca_telemetry_exporter_source_set_id(doca_source, "source_1");
226  doca_telemetry_exporter_source_set_tag(doca_source, "source_1_tag");
227 
228  /* Start DOCA Telemetry source to apply attributes and start services */
230  if (result != DOCA_SUCCESS) {
231  DOCA_LOG_ERR("Cannot start doca_source!");
232  goto err_source;
233  }
234  /* Create more DOCA Telemetry sources if needed */
235 
236  /* Prepare events and report them via DOCA Telemetry */
237  for (iteration = 0; iteration < repetition; iteration++) {
239  DOCA_LOG_INFO("Progressing: k=%" PRId32 " \t i=%" PRIu64, iteration, string_number);
240  result = prepare_example_event(iteration, string_number, &test_event);
241  if (result != DOCA_SUCCESS) {
242  DOCA_LOG_ERR("Failed to create event");
243  goto err_source;
244  }
245  result = doca_telemetry_exporter_source_report(doca_source, example_index, &test_event, 1);
246  if (result != DOCA_SUCCESS) {
247  DOCA_LOG_ERR("Cannot report to doca_source!");
248  goto err_source;
249  }
250  }
251  if (iteration % 2 == 0) {
252  /*
253  * Optionally force DOCA Telemetry source buffer to flush.
254  * Handy for bursty events or specific event types.
255  */
257  if (result != DOCA_SUCCESS) {
258  DOCA_LOG_ERR("Cannot flush doca_source!");
259  goto err_source;
260  }
261  }
262  }
263 
264  /* Destroy all DOCA Telemetry sources and DOCA Telemetry schema to clean up */
267 
268  return DOCA_SUCCESS;
269 err_source:
271 err_schema:
273  return result;
274 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
enum doca_error doca_error_t
DOCA API return codes.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
#define DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_TIMESTAMP
DOCA telemetry timestamp type.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_schema_init(const char *schema_name, struct doca_telemetry_exporter_schema **doca_schema)
Initialize DOCA schema to prepare it for setting attributes and adding types. DOCA schema is used to ...
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_flush(struct doca_telemetry_exporter_source *doca_source)
Immediately flush the data of the DOCA source. This function is not thread-safe and should not be cal...
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_type_destroy(struct doca_telemetry_exporter_type *type)
Destroy doca telemetry type previously created by doca_telemetry_exporter_type_create()
DOCA_EXPERIMENTAL void doca_telemetry_exporter_field_set_type_name(struct doca_telemetry_exporter_field *field_info, const char *type)
Set doca telemetry field type.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_destroy(struct doca_telemetry_exporter_source *doca_source)
Destructor for DOCA source.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_field_set_description(struct doca_telemetry_exporter_field *field_info, const char *desc)
Set doca telemetry field description.
#define DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_INT32
DOCA telemetry int32 type.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_schema_set_ipc_enabled(struct doca_telemetry_exporter_schema *doca_schema)
Enable IPC IPC is disabled by default.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_source_set_id(struct doca_telemetry_exporter_source *doca_source, const char *source_id)
Set source id.
#define DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_UINT64
DOCA telemetry uint64 type.
uint8_t doca_telemetry_exporter_type_index_t
DOCA schema field type index.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_field_set_array_len(struct doca_telemetry_exporter_field *field_info, uint16_t len)
Set doca telemetry field length.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_schema_add_type(struct doca_telemetry_exporter_schema *doca_schema, const char *new_type_name, struct doca_telemetry_exporter_type *type, doca_telemetry_exporter_type_index_t *type_index)
Add user-defined fields to create new type in DOCA schema. The users loses the ownership of the type ...
DOCA_EXPERIMENTAL void doca_telemetry_exporter_schema_set_buf_size(struct doca_telemetry_exporter_schema *doca_schema, uint64_t size)
Set buffer size Default value is 60000 bytes.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_create(struct doca_telemetry_exporter_schema *doca_schema, struct doca_telemetry_exporter_source **doca_source)
Creates a single DOCA source from schema.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_field_set_name(struct doca_telemetry_exporter_field *field_info, const char *name)
Set doca telemetry field name.
uint64_t doca_telemetry_exporter_timestamp_t
DOCA schema type index type.
#define DOCA_TELEMETRY_EXPORTER_FIELD_TYPE_CHAR
DOCA telemetry char type.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_type_create(struct doca_telemetry_exporter_type **type)
Create new telemetry type.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_get_timestamp(doca_telemetry_exporter_timestamp_t *timestamp)
Get timestamp in the proper format.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_source_set_tag(struct doca_telemetry_exporter_source *doca_source, const char *source_tag)
Set source tag.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_field_create(struct doca_telemetry_exporter_field **field)
Create new telemetry field.
DOCA_EXPERIMENTAL void doca_telemetry_exporter_schema_set_file_write_enabled(struct doca_telemetry_exporter_schema *doca_schema)
Enable file write file write is disabled by default.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_schema_destroy(struct doca_telemetry_exporter_schema *doca_schema)
Destructor for DOCA schema.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_schema_start(struct doca_telemetry_exporter_schema *doca_schema)
Finalizes schema setup to start creating Doca Sources from the schema.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_type_add_field(struct doca_telemetry_exporter_type *type, struct doca_telemetry_exporter_field *field)
Add DOCA telemetry field to type. The users loses the ownership of the field after a successful invoc...
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_start(struct doca_telemetry_exporter_source *doca_source)
Applies source attribute and starts DOCA source.
DOCA_EXPERIMENTAL doca_error_t doca_telemetry_exporter_source_report(struct doca_telemetry_exporter_source *doca_source, doca_telemetry_exporter_type_index_t index, void *data, int count)
Report events data of the same type via DOCA source.
uint8_t type
Definition: packets.h:0
char example_string[MAX_EXAMPLE_STRING_SIZE]
doca_telemetry_exporter_timestamp_t timestamp
uint64_t string_number
static doca_error_t telemetry_register_fields(struct doca_telemetry_exporter_type *type)
static doca_error_t prepare_example_event(int32_t iter_number, uint64_t string_number, struct test_event_type *event)
struct test_event_type __attribute__((packed))
#define SINGLE_FIELD_VALUE
doca_telemetry_exporter_timestamp_t timestamp
static char * example_strings[NB_EXAMPLE_STRINGS]
doca_error_t telemetry_export(void)
#define NB_EXAMPLE_STRINGS
int32_t iter_number
#define MAX_EXAMPLE_STRING_SIZE
DOCA_LOG_REGISTER(TELEMETRY_EXPORTER)