1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2016 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#include <benchmark/benchmark.h>
19
20#include "bench_common.h"
21#include "phosphor/trace_log.h"
22#include "utils/memory.h"
23
24using phosphor::utils::make_unique;
25
26class MockTraceLog : public phosphor::TraceLog {
27public:
28    using phosphor::TraceLog::TraceLog;
29
30    void replaceChunk() {
31        auto ctl = getChunkTenant();
32        if (!ctl) {
33            return;
34        }
35        phosphor::TraceLog::replaceChunk(*ctl.mutex());
36        if (!ctl.mutex()->chunk) {
37            ctl.unlock();
38            stop();
39        }
40    }
41};
42
43void RegisterTenants(benchmark::State& state) {
44    static MockTraceLog log{phosphor::TraceLogConfig()};
45    log.registerThread();
46    if (state.thread_index == 0) {
47        log.start(phosphor::TraceConfig(
48            phosphor::BufferMode::ring,
49            (sizeof(phosphor::TraceChunk) * (10 * state.threads))));
50    }
51
52    while (state.KeepRunning()) {
53        log.replaceChunk();
54    }
55    if (state.thread_index == 0) {
56        log.stop();
57    }
58    log.deregisterThread();
59}
60BENCHMARK(RegisterTenants)->ThreadRange(1, phosphor::benchNumThreads());
61
62BENCHMARK_MAIN()
63