• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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 <random>
17 #include <csignal>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <unistd.h>
21 #include <cstdint>
22 #include <cstring>
23 #include <cerrno>
24 #include <fcntl.h>
25 #include <cstdbool>
26 #include <sys/ioctl.h>
27 #include <gtest/gtest.h>
28 #ifndef WITH_NO_MOCKER
29 #include <mockcpp/mockcpp.hpp>
30 #include "util/cpu_boost_wrapper.h"
31 #endif
32 #include "ffrt_inner.h"
33 #include "dfx/log/ffrt_log_api.h"
34 #include "c/ffrt_cpu_boost.h"
35 #include "../common.h"
36 
37 using namespace std;
38 using namespace testing;
39 #ifdef HWTEST_TESTING_EXT_ENABLE
40 using namespace testing::ext;
41 #endif
42 using namespace ffrt;
43 
44 #define IOCTL_SET_CPU_BOOST_CONFIG	_IOWR('x', 51, struct CpuBoostCfg)
45 struct CpuBoostCfg {
46     int pid;             // process id
47     int id;              // code part context id (0-32), shouldn't be duplicate
48     unsigned int size;   // this context using how much ddr size set 0x100000(1MB) as default
49     unsigned int port;   // 0 as default I/D cache cpu boost work on the same time
50     unsigned int offset; // how many ahead used by cpu boost prefecher
51 #ifndef __OHOS__
52     bool ffrt;
53 #endif
54 };
55 
InitCfg(int ctxId)56 int InitCfg(int ctxId)
57 {
58     int fd;
59 
60     struct CpuBoostCfg cfg = {
61         .pid = getpid(),
62         .id = ctxId,
63         .size = 1048576,
64         .port = 0,
65         .offset = 256,
66 #ifndef __OHOS__
67         .ffrt = true,
68 #endif
69     };
70 
71     printf("get para: pid-%d id-%d size-0x%x port-%u offset-%u.\n",
72         cfg.pid, cfg.id, cfg.size, cfg.port, cfg.offset);
73 
74     fd = open("/dev/hisi_perf_ctrl", O_RDWR);
75     if (fd < 0) {
76         printf("open /dev/hisi_perf_ctrl failed.\n");
77         return -1;
78     }
79 
80     if (ioctl(fd, IOCTL_SET_CPU_BOOST_CONFIG, &cfg) == -1) {
81         printf("Error %d (%s) in IOCTL_SET_CPU_BOOST_CONFIG\n", errno, strerror(errno));
82         close(fd);
83         return -1;
84     }
85 
86     close(fd);
87     printf("cpu boost cfg finished.\n");
88     return 0;
89 }
90 
91 class CpuBoostTest : public testing::Test {
92 protected:
SetUpTestCase()93     static void SetUpTestCase()
94     {
95     }
96 
TearDownTestCase()97     static void TearDownTestCase()
98     {
99     }
100 
SetUp()101     void SetUp() override
102     {
103     }
104 
TearDown()105     void TearDown() override
106     {
107     }
108 };
109 
110 HWTEST_F(CpuBoostTest, FFRTCpuBoostApiSuccess, TestSize.Level1)
111 {
112     int i = 0;
113     InitCfg(1);
114     ffrt_cpu_boost_start(1);
115     i++;
116     ffrt_cpu_boost_end(1);
117     EXPECT_EQ(i, 1);
118 }