106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include <dlfcn.h>
1706f6ba60Sopenharmony_ci#include <unistd.h>
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_ci#include "diskio_plugin_config.pb.h"
2006f6ba60Sopenharmony_ci#include "diskio_plugin_result.pb.h"
2106f6ba60Sopenharmony_ci#include "plugin_module_api.h"
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_cinamespace {
2406f6ba60Sopenharmony_ciint g_testCount = 10;
2506f6ba60Sopenharmony_ciconst std::string writeFile = "/data/local/tmp/diskio_write_test.txt";
2606f6ba60Sopenharmony_ciconstexpr int BLOCK_LEN = 100 * 1024;
2706f6ba60Sopenharmony_ciconstexpr int SLEEP_TIME = 10;
2806f6ba60Sopenharmony_ci
2906f6ba60Sopenharmony_civoid IoTest()
3006f6ba60Sopenharmony_ci{
3106f6ba60Sopenharmony_ci    // 一次累加16B,直至100KB
3206f6ba60Sopenharmony_ci    std::string str = "";
3306f6ba60Sopenharmony_ci    while (str.length() < BLOCK_LEN) {
3406f6ba60Sopenharmony_ci        str += "this is IO test.";
3506f6ba60Sopenharmony_ci    }
3606f6ba60Sopenharmony_ci
3706f6ba60Sopenharmony_ci    // 一次写100K数据,写10次
3806f6ba60Sopenharmony_ci    int count = 0;
3906f6ba60Sopenharmony_ci    FILE* writeFp = fopen(writeFile.c_str(), "w");
4006f6ba60Sopenharmony_ci    if (writeFp == nullptr) {
4106f6ba60Sopenharmony_ci        printf("fopen() error");
4206f6ba60Sopenharmony_ci        return;
4306f6ba60Sopenharmony_ci    }
4406f6ba60Sopenharmony_ci    while (count < g_testCount) {
4506f6ba60Sopenharmony_ci        (void)fwrite(const_cast<char*>(str.c_str()), 1, BLOCK_LEN, writeFp);
4606f6ba60Sopenharmony_ci        fflush(writeFp);
4706f6ba60Sopenharmony_ci        fsync(fileno(writeFp));
4806f6ba60Sopenharmony_ci        count++;
4906f6ba60Sopenharmony_ci    }
5006f6ba60Sopenharmony_ci    (void)fclose(writeFp);
5106f6ba60Sopenharmony_ci
5206f6ba60Sopenharmony_ci    // delete file
5306f6ba60Sopenharmony_ci    std::string command = "rm " + writeFile;
5406f6ba60Sopenharmony_ci    system(command.c_str());
5506f6ba60Sopenharmony_ci}
5606f6ba60Sopenharmony_ci} // namespace
5706f6ba60Sopenharmony_ci
5806f6ba60Sopenharmony_ciint main(int agrc, char* agrv[])
5906f6ba60Sopenharmony_ci{
6006f6ba60Sopenharmony_ci    bool isTestDiskIO = false;
6106f6ba60Sopenharmony_ci    for (int i = 1; i < agrc; i++) {
6206f6ba60Sopenharmony_ci        isTestDiskIO = atoi(agrv[i]);
6306f6ba60Sopenharmony_ci    }
6406f6ba60Sopenharmony_ci
6506f6ba60Sopenharmony_ci    if (isTestDiskIO) {
6606f6ba60Sopenharmony_ci        IoTest();
6706f6ba60Sopenharmony_ci        sleep(SLEEP_TIME);
6806f6ba60Sopenharmony_ci    } else {
6906f6ba60Sopenharmony_ci        DiskioConfig protoConfig;
7006f6ba60Sopenharmony_ci        PluginModuleStruct* diskioPlugin;
7106f6ba60Sopenharmony_ci        void* handle = dlopen("libdiskiodataplugin.z.so", RTLD_LAZY);
7206f6ba60Sopenharmony_ci        if (handle == nullptr) {
7306f6ba60Sopenharmony_ci            std::cout << "test:dlopen err: " << dlerror() << std::endl;
7406f6ba60Sopenharmony_ci            return 0;
7506f6ba60Sopenharmony_ci        }
7606f6ba60Sopenharmony_ci        std::cout << "test:handle = " << handle << std::endl;
7706f6ba60Sopenharmony_ci        diskioPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
7806f6ba60Sopenharmony_ci        std::cout << "test:name = " << diskioPlugin->name << std::endl;
7906f6ba60Sopenharmony_ci        std::cout << "test:buffer size = " << diskioPlugin->resultBufferSizeHint << std::endl;
8006f6ba60Sopenharmony_ci
8106f6ba60Sopenharmony_ci        // Serialize config
8206f6ba60Sopenharmony_ci        int configLength = protoConfig.ByteSizeLong();
8306f6ba60Sopenharmony_ci        std::vector<uint8_t> configBuffer(configLength);
8406f6ba60Sopenharmony_ci        int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size());
8506f6ba60Sopenharmony_ci        std::cout << "test:configLength = " << configLength << std::endl;
8606f6ba60Sopenharmony_ci        std::cout << "test:serialize success start plugin ret = " << ret << std::endl;
8706f6ba60Sopenharmony_ci
8806f6ba60Sopenharmony_ci        // Start
8906f6ba60Sopenharmony_ci        std::vector<uint8_t> dataBuffer(diskioPlugin->resultBufferSizeHint);
9006f6ba60Sopenharmony_ci        diskioPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
9106f6ba60Sopenharmony_ci        while (g_testCount--) {
9206f6ba60Sopenharmony_ci            int len = diskioPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
9306f6ba60Sopenharmony_ci                                                                    diskioPlugin->resultBufferSizeHint);
9406f6ba60Sopenharmony_ci            std::cout << "test:filler buffer length = " << len << std::endl;
9506f6ba60Sopenharmony_ci
9606f6ba60Sopenharmony_ci            if (len > 0) {
9706f6ba60Sopenharmony_ci                DiskioData diskioData;
9806f6ba60Sopenharmony_ci                diskioData.ParseFromArray(dataBuffer.data(), len);
9906f6ba60Sopenharmony_ci                std::cout << "test:ParseFromArray length = " << len << std::endl;
10006f6ba60Sopenharmony_ci
10106f6ba60Sopenharmony_ci                std::cout << "prev_rd_sectors_kb:" << diskioData.prev_rd_sectors_kb() << std::endl;
10206f6ba60Sopenharmony_ci                std::cout << "prev_wr_sectors_kb:" << diskioData.prev_wr_sectors_kb() << std::endl;
10306f6ba60Sopenharmony_ci                std::cout << "prev_timestamp.tv_sec:" << diskioData.prev_timestamp().tv_sec() << std::endl;
10406f6ba60Sopenharmony_ci                std::cout << "prev_timestamp.tv_nsec:" << diskioData.prev_timestamp().tv_nsec() << std::endl;
10506f6ba60Sopenharmony_ci                std::cout << "rd_sectors_kb:" << diskioData.rd_sectors_kb() << std::endl;
10606f6ba60Sopenharmony_ci                std::cout << "wr_sectors_kb:" << diskioData.wr_sectors_kb() << std::endl;
10706f6ba60Sopenharmony_ci                std::cout << "timestamp.tv_sec:" << diskioData.timestamp().tv_sec() << std::endl;
10806f6ba60Sopenharmony_ci                std::cout << "timestamp.tv_nsec:" << diskioData.timestamp().tv_nsec() << std::endl;
10906f6ba60Sopenharmony_ci            }
11006f6ba60Sopenharmony_ci
11106f6ba60Sopenharmony_ci            std::cout << "test:sleep...................." << std::endl;
11206f6ba60Sopenharmony_ci            sleep(1);
11306f6ba60Sopenharmony_ci        }
11406f6ba60Sopenharmony_ci        diskioPlugin->callbacks->onPluginSessionStop();
11506f6ba60Sopenharmony_ci        dlclose(handle);
11606f6ba60Sopenharmony_ci    }
11706f6ba60Sopenharmony_ci
11806f6ba60Sopenharmony_ci    return 0;
11906f6ba60Sopenharmony_ci}
120