Robust Distributed System Nucleus (rDSN)  ver 1.0.0
api_utilities.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Microsoft Corporation
5  *
6  * -=- Robust Distributed System Nucleus (rDSN) -=-
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 /*
28  * Description:
29  * useful utilities in rDSN exposed via C API
30  *
31  * Revision history:
32  * Feb., 2016, @imzhenyu (Zhenyu Guo), first version
33  * xxxx-xx-xx, author, fix bug about xxx
34  */
35 
36 # pragma once
37 
38 # include <dsn/c/api_common.h>
39 
40 # ifdef __cplusplus
41 extern "C" {
42 # endif
43 
70 extern DSN_API dsn_error_t dsn_error_register(const char* name);
71 
79 extern DSN_API const char* dsn_error_to_string(dsn_error_t err);
80 
89 extern DSN_API dsn_error_t dsn_error_from_string(const char* s, dsn_error_t default_err);
108 extern DSN_API const char* dsn_cli_run(const char* command_line);
109 
115 extern DSN_API void dsn_cli_free(const char* command_output);
116 
118 typedef struct dsn_cli_reply
119 {
120  const char* message;
121  uint64_t size;
122  void* context;
123 } dsn_cli_reply;
124 
126 typedef void(*dsn_cli_handler)(
127  void* context,
128  int argc,
129  const char** argv,
130  /*out*/dsn_cli_reply* reply
131  );
132 
134 typedef void(*dsn_cli_free_handler)(dsn_cli_reply reply);
135 
148 extern DSN_API dsn_handle_t dsn_cli_register(
149  const char* command,
150  const char* help_one_line,
151  const char* help_long,
152  void* context,
153  dsn_cli_handler cmd_handler,
154  dsn_cli_free_handler output_freer
155  );
156 
161 extern DSN_API dsn_handle_t dsn_cli_app_register(
162  const char* command,
163  const char* help_one_line,
164  const char* help_long,
165  void* context,
166  dsn_cli_handler cmd_handler,
167  dsn_cli_free_handler output_freer
168  );
169 
175 extern DSN_API void dsn_cli_deregister(dsn_handle_t cli_handle);
187 extern DSN_API const char* dsn_config_get_value_string(
188  const char* section,
189  const char* key,
190  const char* default_value,
191  const char* dsptr
192  );
193 extern DSN_API bool dsn_config_get_value_bool(
194  const char* section,
195  const char* key,
196  bool default_value,
197  const char* dsptr
198  );
199 extern DSN_API uint64_t dsn_config_get_value_uint64(
200  const char* section,
201  const char* key,
202  uint64_t default_value,
203  const char* dsptr
204  );
205 extern DSN_API double dsn_config_get_value_double(
206  const char* section,
207  const char* key,
208  double default_value,
209  const char* dsptr
210  );
211 // return all section count (may greater than buffer_count)
212 extern DSN_API int dsn_config_get_all_sections(
213  const char** buffers,
214  /*inout*/ int* buffer_count
215  );
216 // return all key count (may greater than buffer_count)
217 extern DSN_API int dsn_config_get_all_keys(
218  const char* section,
219  const char** buffers,
220  /*inout*/ int* buffer_count
221  );
222 extern DSN_API void dsn_config_dump(const char* file);
237 typedef enum dsn_log_level_t
238 {
239  LOG_LEVEL_INFORMATION,
240  LOG_LEVEL_DEBUG,
241  LOG_LEVEL_WARNING,
242  LOG_LEVEL_ERROR,
243  LOG_LEVEL_FATAL,
244  LOG_LEVEL_COUNT,
245  LOG_LEVEL_INVALID
246 } dsn_log_level_t;
247 
248 // logs with level smaller than this start_level will not be logged
249 extern DSN_API dsn_log_level_t dsn_log_start_level;
250 extern DSN_API dsn_log_level_t dsn_log_get_start_level();
251 extern DSN_API void dsn_logv(
252  const char *file,
253  const char *function,
254  const int line,
255  dsn_log_level_t log_level,
256  const char* title,
257  const char* fmt,
258  va_list args
259  );
260 extern DSN_API void dsn_logf(
261  const char *file,
262  const char *function,
263  const int line,
264  dsn_log_level_t log_level,
265  const char* title,
266  const char* fmt,
267  ...
268  );
269 extern DSN_API void dsn_log(
270  const char *file,
271  const char *function,
272  const int line,
273  dsn_log_level_t log_level,
274  const char* title
275  );
276 extern DSN_API void dsn_coredump();
277 
278 
279 #define dlog(level, title, ...) do {if (level >= dsn_log_start_level) \
280  dsn_logf(__FILE__, __FUNCTION__, __LINE__, level, title, __VA_ARGS__); } while(false)
281 #define dinfo(...) dlog(LOG_LEVEL_INFORMATION, __TITLE__, __VA_ARGS__)
282 #define ddebug(...) dlog(LOG_LEVEL_DEBUG, __TITLE__, __VA_ARGS__)
283 #define dwarn(...) dlog(LOG_LEVEL_WARNING, __TITLE__, __VA_ARGS__)
284 #define derror(...) dlog(LOG_LEVEL_ERROR, __TITLE__, __VA_ARGS__)
285 #define dfatal(...) dlog(LOG_LEVEL_FATAL, __TITLE__, __VA_ARGS__)
286 #define dassert(x, ...) do { if (!(x)) { \
287  dlog(LOG_LEVEL_FATAL, __FILE__, "assertion expression: "#x); \
288  dlog(LOG_LEVEL_FATAL, __FILE__, __VA_ARGS__); \
289  dsn_coredump(); \
290  } } while (false)
291 
292 #ifndef NDEBUG
293 #define dbg_dassert dassert
294 #else
295 #define dbg_dassert(x, ...)
296 #endif
297 
309 extern DSN_API uint32_t dsn_crc32_compute(const void* ptr, size_t size, uint32_t init_crc);
310 
311 //
312 // Given
313 // x_final = dsn_crc32_compute (x_ptr, x_size, x_init);
314 // and
315 // y_final = dsn_crc32_compute (y_ptr, y_size, y_init);
316 // compute CRC of concatenation of A and B
317 // x##y_crc = dsn_crc32_compute (x##y, x_size + y_size, xy_init);
318 // without touching A and B
319 //
320 extern DSN_API uint32_t dsn_crc32_concatenate(
321  uint32_t xy_init,
322  uint32_t x_init,
323  uint32_t x_final,
324  size_t x_size,
325  uint32_t y_init,
326  uint32_t y_final,
327  size_t y_size
328  );
329 
330 extern DSN_API uint64_t dsn_crc64_compute(const void* ptr, size_t size, uint64_t init_crc);
331 
332 //
333 // Given
334 // x_final = dsn_crc64_compute (x_ptr, x_size, x_init);
335 // and
336 // y_final = dsn_crc64_compute (y_ptr, y_size, y_init);
337 // compute CRC of concatenation of A and B
338 // x##y_crc = dsn_crc64_compute (x##y, x_size + y_size, xy_init);
339 // without touching A and B
340 //
341 
342 extern DSN_API uint64_t dsn_crc64_concatenate(
343  uint32_t xy_init,
344  uint64_t x_init,
345  uint64_t x_final,
346  size_t x_size,
347  uint64_t y_init,
348  uint64_t y_final,
349  size_t y_size);
364 typedef enum dsn_perf_counter_type_t
365 {
366  COUNTER_TYPE_NUMBER,
367  COUNTER_TYPE_RATE,
368  COUNTER_TYPE_NUMBER_PERCENTILES,
369  COUNTER_TYPE_INVALID,
370  COUNTER_TYPE_COUNT
371 } dsn_perf_counter_type_t;
372 
373 typedef enum dsn_perf_counter_percentile_type_t
374 {
375  COUNTER_PERCENTILE_50,
376  COUNTER_PERCENTILE_90,
377  COUNTER_PERCENTILE_95,
378  COUNTER_PERCENTILE_99,
379  COUNTER_PERCENTILE_999,
380 
381  COUNTER_PERCENTILE_COUNT,
382  COUNTER_PERCENTILE_INVALID
383 } dsn_perf_counter_percentile_type_t;
384 
385 extern DSN_API dsn_handle_t dsn_perf_counter_create(const char* section, const char* name, dsn_perf_counter_type_t type, const char* description);
386 extern DSN_API void dsn_perf_counter_remove(dsn_handle_t handle);
387 extern DSN_API void dsn_perf_counter_increment(dsn_handle_t handle);
388 extern DSN_API void dsn_perf_counter_decrement(dsn_handle_t handle);
389 extern DSN_API void dsn_perf_counter_add(dsn_handle_t handle, uint64_t val);
390 extern DSN_API void dsn_perf_counter_set(dsn_handle_t handle, uint64_t val);
391 extern DSN_API double dsn_perf_counter_get_value(dsn_handle_t handle);
392 extern DSN_API uint64_t dsn_perf_counter_get_integer_value(dsn_handle_t handle);
393 extern DSN_API double dsn_perf_counter_get_percentile(dsn_handle_t handle, dsn_perf_counter_percentile_type_t type);
406 extern DSN_API void* dsn_transient_malloc(uint32_t size);
407 
409 extern DSN_API void dsn_transient_free(void* ptr);
410 
412 extern DSN_API void* dsn_malloc(uint32_t size);
413 
415 extern DSN_API void dsn_free(void* ptr);
416 
419 # ifdef __cplusplus
420 }
421 # endif
uint64_t size
message_size
Definition: api_utilities.h:121
DSN_API dsn_error_t dsn_error_register(const char *name)
register error code
DSN_API void * dsn_malloc(uint32_t size)
common malloc, paird with dsn_free to ensure malloc/free are done by dsn.core
DSN_API void dsn_cli_deregister(dsn_handle_t cli_handle)
remove a cli handler
void(* dsn_cli_handler)(void *context, int argc, const char **argv, dsn_cli_reply *reply)
cli request handler definition
Definition: api_utilities.h:126
const char * message
zero-ended reply message
Definition: api_utilities.h:120
cli response definition
Definition: api_utilities.h:118
DSN_API void dsn_transient_free(void *ptr)
high-performance free for transient objects, paired with dsn_transient_malloc
DSN_API void * dsn_transient_malloc(uint32_t size)
high-performance malloc for transient objects, i.e., their life-time is short
void(* dsn_cli_free_handler)(dsn_cli_reply reply)
cli response resource gc handler definition
Definition: api_utilities.h:134
DSN_API dsn_handle_t dsn_cli_app_register(const char *command, const char *help_one_line, const char *help_long, void *context, dsn_cli_handler cmd_handler, dsn_cli_free_handler output_freer)
same as dsn_cli_register, except that the command name is auto-augmented by rDSN as $app_full_name...
void * context
context for free_handler
Definition: api_utilities.h:122
DSN_API const char * dsn_cli_run(const char *command_line)
run a given cli command
DSN_API dsn_error_t dsn_error_from_string(const char *s, dsn_error_t default_err)
parse string error code into integer code
DSN_API void dsn_free(void *ptr)
common free, paird with dsn_malloc to ensure malloc/free are done by dsn.core
DSN_API void dsn_cli_free(const char *command_output)
free memory occupied by cli response
struct dsn_cli_reply dsn_cli_reply
cli response definition
DSN_API dsn_handle_t dsn_cli_register(const char *command, const char *help_one_line, const char *help_long, void *context, dsn_cli_handler cmd_handler, dsn_cli_free_handler output_freer)
register a customized cli command handler
DSN_API const char * dsn_config_get_value_string(const char *section, const char *key, const char *default_value, const char *dsptr)
DSN_API const char * dsn_error_to_string(dsn_error_t err)
translate interger error code to a string