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 <gtest/gtest.h>
17 #include <dlfcn.h>
18 #include <fstream>
19 #include "gpu_data_plugin.h"
20 #include "plugin_module_api.h"
21 
22 using namespace testing::ext;
23 
24 namespace {
25 const std::string DEFAULT_TEST_PATH = "/data/local/tmp/resource";
26 const std::string SO_PATH = "libgpudataplugin.z.so";
27 const std::string DEFAULT_BIN_PATH("/data/local/tmp/gpudataplugintest");
28 
29 std::string g_path;
30 std::string g_testPath;
31 
32 #if defined(__LP64__)
33 const unsigned long long EXPECT_VAL = 12;
34 const unsigned long long SLEEP_TIME = 5;
35 constexpr uint32_t BUF_SIZE = 4 * 1024 * 1024;
36 #endif
37 
38 struct TestVmstat {
39     int64_t pgpgin;
40     int64_t pgpgout;
41 };
42 
43 class GpuDataPluginTest : public ::testing::Test {
44 public:
SetUpTestCase()45     static void SetUpTestCase() {}
46 
TearDownTestCase()47     static void TearDownTestCase()
48     {
49         if (access(g_testPath.c_str(), F_OK) == 0) {
50             std::string str = "rm -rf " + g_testPath;
51             system(str.c_str());
52         }
53     }
54 };
55 
Getexepath()56 string Getexepath()
57 {
58     char buf[PATH_MAX] = "";
59     std::string path = "/proc/self/exe";
60     size_t rslt = readlink(path.c_str(), buf, sizeof(buf));
61     if (rslt < 0 || (rslt >= sizeof(buf))) {
62         return "";
63     }
64     buf[rslt] = '\0';
65     for (int i = rslt; i >= 0; i--) {
66         if (buf[i] == '/') {
67             buf[i + 1] = '\0';
68             break;
69         }
70     }
71     return buf;
72 }
73 
GetFullPath(std::string path)74 std::string GetFullPath(std::string path)
75 {
76     if (path.size() > 0 && path[0] != '/') {
77         return Getexepath() + path;
78     }
79     return path;
80 }
81 
82 #if defined(__LP64__)
PluginGpuInfoStub(GpuDataPlugin& gpuPlugin, GpuData& gpuData, bool unusualBuff)83 bool PluginGpuInfoStub(GpuDataPlugin& gpuPlugin, GpuData& gpuData, bool unusualBuff)
84 {
85     GpuConfig protoConfig;
86     std::vector<uint8_t> configData(protoConfig.ByteSizeLong());
87     int ret = protoConfig.SerializeToArray(configData.data(), configData.size());
88     if (ret < 0) {
89         return false;
90     }
91 
92     // start
93     ret = gpuPlugin.Start(configData.data(), configData.size());
94     if (ret < 0) {
95         return false;
96     }
97 
98     gpuPlugin.file_.close();
99     gpuPlugin.file_.open(g_path);
100     // report
101     std::vector<uint8_t> bufferData(BUF_SIZE);
102     if (unusualBuff) { // buffer异常,调整缓冲区长度为1,测试异常情况
103         bufferData.resize(1, 0);
104     }
105 
106     ret = gpuPlugin.Report(bufferData.data(), bufferData.size());
107     if (ret > 0) {
108         gpuData.ParseFromArray(bufferData.data(), ret);
109         return true;
110     }
111     return false;
112 }
113 #endif
114 
115 /**
116  * @tc.name: gpu plugin
117  * @tc.desc: Test whether the path exists.
118  * @tc.type: FUNC
119  */
HWTEST_F(GpuDataPluginTest, TestPath, TestSize.Level1)120 HWTEST_F(GpuDataPluginTest, TestPath, TestSize.Level1)
121 {
122     g_path = GetFullPath(DEFAULT_TEST_PATH);
123     g_testPath = g_path;
124     EXPECT_NE("", g_path);
125     g_path += "/gpustat1.txt";
126 }
127 
128 /**
129  * @tc.name: gpu plugin
130  * @tc.desc: gpu information test for specific path.
131  * @tc.type: FUNC
132  */
HWTEST_F(GpuDataPluginTest, TestPlugin, TestSize.Level1)133 HWTEST_F(GpuDataPluginTest, TestPlugin, TestSize.Level1)
134 {
135 #if defined(__LP64__)
136     GpuDataPlugin gpuPlugin;
137     GpuData gpuData;
138 
139     EXPECT_TRUE(PluginGpuInfoStub(gpuPlugin, gpuData, false));
140     EXPECT_EQ(gpuData.gpu_utilisation(), EXPECT_VAL);
141 
142     EXPECT_EQ(gpuPlugin.ReadFile(), EXPECT_VAL);
143     sleep(SLEEP_TIME);
144     EXPECT_EQ(gpuPlugin.ReadFile(), EXPECT_VAL);
145     EXPECT_EQ(gpuPlugin.Stop(), 0);
146 
147     // 缓冲区异常
148     EXPECT_FALSE(PluginGpuInfoStub(gpuPlugin, gpuData, true));
149     EXPECT_EQ(gpuPlugin.Stop(), 0);
150 #endif
151 }
152 
153 
154 /**
155  * @tc.name: gpu plugin
156  * @tc.desc: gpu plugin registration test.
157  * @tc.type: FUNC
158  */
HWTEST_F(GpuDataPluginTest, TestPluginRegister, TestSize.Level1)159 HWTEST_F(GpuDataPluginTest, TestPluginRegister, TestSize.Level1)
160 {
161 #if defined(__LP64__)
162     void* handle = dlopen(SO_PATH.c_str(), RTLD_LAZY);
163     ASSERT_NE(handle, nullptr);
164     PluginModuleStruct* gpuPlugin = (PluginModuleStruct*)dlsym(handle, "g_pluginModule");
165     ASSERT_NE(gpuPlugin, nullptr);
166     EXPECT_STREQ(gpuPlugin->name, "gpu-plugin");
167     EXPECT_EQ(gpuPlugin->resultBufferSizeHint, BUF_SIZE);
168 
169     // Serialize config
170     GpuConfig protoConfig;
171     int configLength = protoConfig.ByteSizeLong();
172     ASSERT_EQ(configLength, 0);
173     std::vector<uint8_t> configBuffer(configLength);
174     EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer.data(), configLength));
175 
176     // run plugin
177     std::vector<uint8_t> dataBuffer(gpuPlugin->resultBufferSizeHint);
178     EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStart(configBuffer.data(), configLength), RET_SUCC);
179     ASSERT_GT(gpuPlugin->callbacks->onPluginReportResult(dataBuffer.data(), gpuPlugin->resultBufferSizeHint), 0);
180     EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStop(), RET_SUCC);
181 
182     // 反序列化失败导致的start失败
183     configLength++;
184     std::vector<uint8_t> configBuffer2(configLength);
185     EXPECT_TRUE(protoConfig.SerializeToArray(configBuffer2.data(), configLength));
186     EXPECT_EQ(gpuPlugin->callbacks->onPluginSessionStart(configBuffer2.data(), configLength+1), RET_FAIL);
187 #endif
188 }
189 
190 
191 } // namespace