1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <mutex>
17
18#include "diskio_data_plugin.h"
19
20namespace {
21constexpr uint32_t MAX_BUFFER_SIZE = 4 * 1024 * 1024;
22std::unique_ptr<DiskioDataPlugin> g_plugin = nullptr;
23std::mutex g_taskMutex;
24} // namespace
25
26static int DiskioDataPluginSessionStart(const uint8_t* configData, uint32_t configSize)
27{
28    std::lock_guard<std::mutex> guard(g_taskMutex);
29    g_plugin = std::make_unique<DiskioDataPlugin>();
30    return g_plugin->Start(configData, configSize);
31}
32
33static int DiskioPluginReportResult(uint8_t* bufferData, uint32_t bufferSize)
34{
35    std::lock_guard<std::mutex> guard(g_taskMutex);
36    CHECK_NOTNULL(g_plugin, -1, "g_plugin -s nullptr");
37    return g_plugin->Report(bufferData, bufferSize);
38}
39
40static int DiskioPluginReportResultOptimize(RandomWriteCtx* randomWrite)
41{
42    std::lock_guard<std::mutex> guard(g_taskMutex);
43    return g_plugin->ReportOptimize(randomWrite);
44}
45
46static int DiskioPluginSessionStop()
47{
48    std::lock_guard<std::mutex> guard(g_taskMutex);
49    CHECK_NOTNULL(g_plugin, -1, "g_plugin is nullptr");
50    g_plugin->Stop();
51    g_plugin = nullptr;
52    return 0;
53}
54
55static PluginModuleCallbacks g_callbacks = {
56    .onPluginSessionStart = DiskioDataPluginSessionStart,
57    .onPluginReportResult = DiskioPluginReportResult,
58    .onPluginSessionStop = DiskioPluginSessionStop,
59    .onPluginReportResultOptimize = DiskioPluginReportResultOptimize,
60};
61
62EXPORT_API PluginModuleStruct g_pluginModule = {
63    .callbacks = &g_callbacks,
64    .name = "diskio-plugin",
65    .version = "1.02",
66    .resultBufferSizeHint = MAX_BUFFER_SIZE,
67};