1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2017 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#pragma once
19
20#include "couch_common.h"
21
22#include <libcouchstore/visibility.h>
23#include <platform/histogram.h>
24#include <platform/processclock.h>
25
26using CouchLatencyHisto = MicrosecondHistogram;
27using CouchLatencyMicroSec = UnsignedMicroseconds;
28using CouchLatencyMicroSecRep = UnsignedMicroseconds::rep;
29
30/**
31 * Begin to collect Couchstore API latency.
32 */
33LIBCOUCHSTORE_API
34void couchstore_latency_collector_start();
35
36/**
37 * The callback function used by couchstore_get_latency_info(),
38 * to get latency histogram data.
39 *
40 * @param stat_name Name of statistic item.
41 * @param latencies Latency histogram of the statistic item.
42 * @param elapsed_time Total elapsed time in microseconds.
43 * @param ctx User context
44 * @return 1 to stop getting latency info, 0 otherwise.
45 */
46using couchstore_latency_callback_fn =
47        std::function<int(const char*,
48                          CouchLatencyHisto* latencies,
49                          const CouchLatencyMicroSecRep elapsed_time,
50                          void* ctx)>;
51
52/**
53 * Latency info dump options.
54 */
55typedef struct _couchstore_latency_dump_options {
56    /**
57     * Currently empty, but left it for future extension.
58     */
59} couchstore_latency_dump_options;
60
61/**
62 * Dump collected latency data through the given
63 * callback function.
64 *
65 * @param callback Callback function.
66 * @param options Latency dump options.
67 * @param ctx User context
68 */
69LIBCOUCHSTORE_API
70void couchstore_get_latency_info(couchstore_latency_callback_fn callback,
71                                 couchstore_latency_dump_options options,
72                                 void *ctx);
73
74/**
75 * Stop collecting Couchstore API latency.
76 */
77LIBCOUCHSTORE_API
78void couchstore_latency_collector_stop();
79
80