• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <dlfcn.h>
17 #include <unistd.h>
18 
19 #include "diskio_plugin_config.pb.h"
20 #include "diskio_plugin_result.pb.h"
21 #include "plugin_module_api.h"
22 
23 namespace {
24 int g_testCount = 10;
25 const std::string writeFile = "/data/local/tmp/diskio_write_test.txt";
26 constexpr int BLOCK_LEN = 100 * 1024;
27 constexpr int SLEEP_TIME = 10;
28 
IoTest()29 void IoTest()
30 {
31     // 一次累加16B,直至100KB
32     std::string str = "";
33     while (str.length() < BLOCK_LEN) {
34         str += "this is IO test.";
35     }
36 
37     // 一次写100K数据,写10次
38     int count = 0;
39     FILE* writeFp = fopen(writeFile.c_str(), "w");
40     if (writeFp == nullptr) {
41         printf("fopen() error");
42         return;
43     }
44     while (count < g_testCount) {
45         (void)fwrite(const_cast<char*>(str.c_str()), 1, BLOCK_LEN, writeFp);
46         fflush(writeFp);
47         fsync(fileno(writeFp));
48         count++;
49     }
50     (void)fclose(writeFp);
51 
52     // delete file
53     std::string command = "rm " + writeFile;
54     system(command.c_str());
55 }
56 } // namespace
57 
main(int agrc,char * agrv[])58 int main(int agrc, char* agrv[])
59 {
60     bool isTestDiskIO = false;
61     for (int i = 1; i < agrc; i++) {
62         isTestDiskIO = atoi(agrv[i]);
63     }
64 
65     if (isTestDiskIO) {
66         IoTest();
67         sleep(SLEEP_TIME);
68     } else {
69         DiskioConfig protoConfig;
70         PluginModuleStruct* diskioPlugin;
71         void* handle = dlopen("./libdiskiodataplugin.z.so", RTLD_LAZY);
72         if (handle == nullptr) {
73             std::cout << "test:dlopen err: " << dlerror() << std::endl;
74             return 0;
75         }
76         std::cout << "test:handle = " << handle << std::endl;
77         diskioPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
78         std::cout << "test:name = " << diskioPlugin->name << std::endl;
79         std::cout << "test:buffer size = " << diskioPlugin->resultBufferSizeHint << std::endl;
80 
81         // Serialize config
82         int configLength = protoConfig.ByteSizeLong();
83         std::vector<uint8_t> configBuffer(configLength);
84         int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size());
85         std::cout << "test:configLength = " << configLength << std::endl;
86         std::cout << "test:serialize success start plugin ret = " << ret << std::endl;
87 
88         // Start
89         std::vector<uint8_t> dataBuffer(diskioPlugin->resultBufferSizeHint);
90         diskioPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
91         while (g_testCount--) {
92             int len = diskioPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
93                                                                     diskioPlugin->resultBufferSizeHint);
94             std::cout << "test:filler buffer length = " << len << std::endl;
95 
96             if (len > 0) {
97                 DiskioData diskioData;
98                 diskioData.ParseFromArray(dataBuffer.data(), len);
99                 std::cout << "test:ParseFromArray length = " << len << std::endl;
100 
101                 std::cout << "prev_rd_sectors_kb:" << diskioData.prev_rd_sectors_kb() << std::endl;
102                 std::cout << "prev_wr_sectors_kb:" << diskioData.prev_wr_sectors_kb() << std::endl;
103                 std::cout << "prev_timestamp.tv_sec:" << diskioData.prev_timestamp().tv_sec() << std::endl;
104                 std::cout << "prev_timestamp.tv_nsec:" << diskioData.prev_timestamp().tv_nsec() << std::endl;
105                 std::cout << "rd_sectors_kb:" << diskioData.rd_sectors_kb() << std::endl;
106                 std::cout << "wr_sectors_kb:" << diskioData.wr_sectors_kb() << std::endl;
107                 std::cout << "timestamp.tv_sec:" << diskioData.timestamp().tv_sec() << std::endl;
108                 std::cout << "timestamp.tv_nsec:" << diskioData.timestamp().tv_nsec() << std::endl;
109             }
110 
111             std::cout << "test:sleep...................." << std::endl;
112             sleep(1);
113         }
114         diskioPlugin->callbacks->onPluginSessionStop();
115         dlclose(handle);
116     }
117 
118     return 0;
119 }
120