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