1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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 #include "gpu_plugin_config.pb.h"
19 #include "gpu_plugin_result.pb.h"
20 #include "plugin_module_api.h"
21
22 namespace {
23 int g_testCount = 10;
24 }
25
main(int agrc, char* agrv[])26 int main(int agrc, char* agrv[])
27 {
28 GpuConfig protoConfig;
29 PluginModuleStruct* gpuPlugin;
30 void* handle = dlopen("libgpudataplugin.z.so", RTLD_LAZY);
31 if (handle == nullptr) {
32 std::cout << "test:dlopen err: " << dlerror() << std::endl;
33 return 0;
34 }
35 std::cout << "test:handle = " << handle << std::endl;
36 gpuPlugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
37 std::cout << "test:name = " << gpuPlugin->name << std::endl;
38 std::cout << "test:buffer size = " << gpuPlugin->resultBufferSizeHint << std::endl;
39
40 // Serialize config
41 int configLength = protoConfig.ByteSizeLong();
42 std::vector<uint8_t> configBuffer(configLength);
43 int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size());
44 std::cout << "test:configLength = " << configLength << std::endl;
45 std::cout << "test:serialize success start plugin ret = " << ret << std::endl;
46
47 // Start
48 std::vector<uint8_t> dataBuffer(gpuPlugin->resultBufferSizeHint);
49 gpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
50 while (g_testCount--) {
51 int len = gpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
52 gpuPlugin->resultBufferSizeHint);
53 std::cout << "test:filler buffer length = " << len << std::endl;
54
55 if (len > 0) {
56 GpuData gpuData;
57 gpuData.ParseFromArray(dataBuffer.data(), len);
58 std::cout << "test:ParseFromArray length = " << len << std::endl;
59 std::cout << "gpu_utilisation:" << gpuData.gpu_utilisation() << std::endl;
60 }
61
62 std::cout << "test:sleep...................." << std::endl;
63 sleep(1);
64 }
65 gpuPlugin->callbacks->onPluginSessionStop();
66 dlclose(handle);
67
68 return 0;
69 }