106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. 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#include "gpu_plugin_config.pb.h"
1906f6ba60Sopenharmony_ci#include "gpu_plugin_result.pb.h"
2006f6ba60Sopenharmony_ci#include "plugin_module_api.h"
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_cinamespace {
2306f6ba60Sopenharmony_ci    int g_testCount = 10;
2406f6ba60Sopenharmony_ci}
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_ciint main(int agrc, char* agrv[])
2706f6ba60Sopenharmony_ci{
2806f6ba60Sopenharmony_ci    GpuConfig protoConfig;
2906f6ba60Sopenharmony_ci    PluginModuleStruct* gpuPlugin;
3006f6ba60Sopenharmony_ci    void* handle = dlopen("libgpudataplugin.z.so", RTLD_LAZY);
3106f6ba60Sopenharmony_ci    if (handle == nullptr) {
3206f6ba60Sopenharmony_ci        std::cout << "test:dlopen err: " << dlerror() << std::endl;
3306f6ba60Sopenharmony_ci        return 0;
3406f6ba60Sopenharmony_ci    }
3506f6ba60Sopenharmony_ci    std::cout << "test:handle = " << handle << std::endl;
3606f6ba60Sopenharmony_ci    gpuPlugin = reinterpret_cast<PluginModuleStruct*>(dlsym(handle, "g_pluginModule"));
3706f6ba60Sopenharmony_ci    std::cout << "test:name = " << gpuPlugin->name << std::endl;
3806f6ba60Sopenharmony_ci    std::cout << "test:buffer size = " << gpuPlugin->resultBufferSizeHint << std::endl;
3906f6ba60Sopenharmony_ci
4006f6ba60Sopenharmony_ci    // Serialize config
4106f6ba60Sopenharmony_ci    int configLength = protoConfig.ByteSizeLong();
4206f6ba60Sopenharmony_ci    std::vector<uint8_t> configBuffer(configLength);
4306f6ba60Sopenharmony_ci    int ret = protoConfig.SerializeToArray(configBuffer.data(), configBuffer.size());
4406f6ba60Sopenharmony_ci    std::cout << "test:configLength = " << configLength << std::endl;
4506f6ba60Sopenharmony_ci    std::cout << "test:serialize success start plugin ret = " << ret << std::endl;
4606f6ba60Sopenharmony_ci
4706f6ba60Sopenharmony_ci    // Start
4806f6ba60Sopenharmony_ci    std::vector<uint8_t> dataBuffer(gpuPlugin->resultBufferSizeHint);
4906f6ba60Sopenharmony_ci    gpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength);
5006f6ba60Sopenharmony_ci    while (g_testCount--) {
5106f6ba60Sopenharmony_ci        int len = gpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(),
5206f6ba60Sopenharmony_ci                                                             gpuPlugin->resultBufferSizeHint);
5306f6ba60Sopenharmony_ci        std::cout << "test:filler buffer length = " << len << std::endl;
5406f6ba60Sopenharmony_ci
5506f6ba60Sopenharmony_ci        if (len > 0) {
5606f6ba60Sopenharmony_ci            GpuData gpuData;
5706f6ba60Sopenharmony_ci            gpuData.ParseFromArray(dataBuffer.data(), len);
5806f6ba60Sopenharmony_ci            std::cout << "test:ParseFromArray length = " << len << std::endl;
5906f6ba60Sopenharmony_ci            std::cout << "gpu_utilisation:" << gpuData.gpu_utilisation() << std::endl;
6006f6ba60Sopenharmony_ci        }
6106f6ba60Sopenharmony_ci
6206f6ba60Sopenharmony_ci        std::cout << "test:sleep...................." << std::endl;
6306f6ba60Sopenharmony_ci        sleep(1);
6406f6ba60Sopenharmony_ci    }
6506f6ba60Sopenharmony_ci    gpuPlugin->callbacks->onPluginSessionStop();
6606f6ba60Sopenharmony_ci    dlclose(handle);
6706f6ba60Sopenharmony_ci
6806f6ba60Sopenharmony_ci    return 0;
6906f6ba60Sopenharmony_ci}