ActsSchedApiTest.cpp 12.7 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>

#include "gtest/gtest.h"
#include "XtsActsUtil.h"
#include "XtsActsSchedApiExpect.h"

using namespace testing::ext;

class ActsSchedApiTest : public testing::Test {
protected:
    // SetUpTestCase: Testsuit setup, run before 1st testcase
    static void SetUpTestCase(void)
    {
    }
    // TearDownTestCase: Testsuit teardown, run after last testcase
    static void TearDownTestCase(void)
    {
    }
    // Testcase setup
    virtual void SetUp()
    {
        errno = 0;
    }
    // Testcase teardown
    virtual void TearDown()
    {
    }
};

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_GETPRIORITY_0100
* @tc.name       test getpriority api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testGetpriority, TestSize.Level1) {
    int which = PRIO_PROCESS;
    id_t pid;
    int returnPriority;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    returnPriority = getpriority(which, pid);
    LogPrint("    getpriority which:='PRIO_PROCESS' pid:='%d',   "
        "--> returnPriority:='%d'\n", pid, returnPriority);
    EXPECT_GE(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN)
        << "ErrInfo: " << "getpriority which:='PRIO_PROCESS' pid:='"
        << pid << "',   --> returnPriority:='" << returnPriority << "'";
    EXPECT_LE(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MAX)
        << "ErrInfo: " << "getpriority which:='PRIO_PROCESS' pid:='"
        << pid << "',   --> returnPriority:='" << returnPriority << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_GET_PRIORITY_MAX_0100
* @tc.name       test sched_get_priority_max api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedGetPriorityMax, TestSize.Level1) {
    int returnPriority;

    returnPriority = sched_get_priority_max(SCHED_FIFO);
    LogPrint("    sched_get_priority_max policy:='SCHED_FIFO',   --> returnPriority:='%d'\n", returnPriority);
    EXPECT_EQ(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MAX)
        << "ErrInfo:" << " sched_get_priority_max policy:='SCHED_FIFO',   --> returnPriority:='"
        << returnPriority << "'";

    returnPriority = sched_get_priority_max(SCHED_RR);
    LogPrint("    sched_get_priority_max policy:='SCHED_RR',   --> returnPriority:='%d'\n", returnPriority);
    EXPECT_EQ(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MAX)
        << "ErrInfo:" << " sched_get_priority_max policy:='SCHED_RR',   --> returnPriority:='"
        << returnPriority << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_GET_PRIORITY_MIN_0100
* @tc.name       test sched_get_priority_min api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedGetPriorityMin, TestSize.Level1) {
    int returnPriority;

    returnPriority = sched_get_priority_min(SCHED_FIFO);
    LogPrint("    sched_get_priority_min policy:='SCHED_FIFO',   --> returnPriority:='%d'\n", returnPriority);
    EXPECT_EQ(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN) << "ErrInfo: "
        << "sched_get_priority_min policy:='SCHED_FIFO',   --> returnPriority:='" << returnPriority << "'";

    returnPriority = sched_get_priority_min(SCHED_RR);
    LogPrint("    sched_get_priority_min policy:='SCHED_RR',   --> returnPriority:='%d'\n", returnPriority);
    EXPECT_EQ(returnPriority, XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN) << "ErrInfo: "
        << "sched_get_priority_min policy:='SCHED_FIFO',   --> returnPriority:='" << returnPriority << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_GETPARAM_0100
* @tc.name       test sched_getparam api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedGetParam, TestSize.Level1) {
    struct sched_param params;
    id_t pid;
    int returnVal;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    returnVal = sched_getparam(pid, &params);
    LogPrint("    sched_getparam pid:='%d' &params:='int sched_priority',"
        "   --> returnVal:='%d', params.sched_priority:='%d'\n", pid, returnVal, params.sched_priority);
    EXPECT_EQ(returnVal, 0)
        << "ErrInfo: sched_getparam pid:='" << pid
        << "' &params:='int sched_priority',   --> returnVal:='" << returnVal << "', params.sched_priority:='"
        << params.sched_priority <<"'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_GETSCHEDULER_0100
* @tc.name       test sched_getscheduler api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedGetScheduler, TestSize.Level1) {
    struct sched_param params;
    id_t pid;
    int returnVal;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    returnVal = sched_getscheduler(pid);
    LogPrint("    sched_getscheduler pid:='%d',   --> returnVal:='%d'\n", pid, returnVal);
    EXPECT_EQ(returnVal, SCHED_RR)
        << "ErrInfo: sched_getscheduler pid:='" << pid
        << "',   --> returnVal:='" << returnVal << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_RR_GET_INTERVAL_0100
* @tc.name       test sched_rr_get_interval api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedRRGetInterval, TestSize.Level1) {
    struct timespec intervalS1;
    struct timespec intervalS2;
    id_t pid;
    int returnVal1 = -1;
    int returnVal2 = -1;
    intervalS1.tv_sec = -1;
    intervalS1.tv_nsec = -1;
    intervalS2.tv_sec = -1;
    intervalS2.tv_nsec = -1;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    returnVal1 = sched_rr_get_interval(pid, &intervalS1);
    returnVal2 = sched_rr_get_interval(0, &intervalS2);
    LogPrint("    sched_rr_get_interval pid:='%d' *ts:='&intervalS1',   "
        "--> returnVal1:='%d' intervalS1.tv_sec:='%lld', intervalS1.tv_nsec:='%ld'\n",
        pid, returnVal1, intervalS1.tv_sec, intervalS1.tv_nsec);
    LogPrint("    sched_rr_get_interval pid:='0' *ts:='&intervalS2',   "
        "--> returnVal2:='%d' intervalS2.tv_sec:='%lld', intervalS2.tv_nsec:='%ld'\n",
        returnVal2, intervalS2.tv_sec, intervalS2.tv_nsec);
    EXPECT_EQ(returnVal1, returnVal2)
        << "ErrInfo: sched_rr_get_interval pid:='" << pid
        << ",0' *ts:='&intervalS1,&intervalS2',   --> returnVal1:='" << returnVal1 << "' returnVal2:='"
        << returnVal2 << "'";
    EXPECT_EQ(intervalS1.tv_sec, intervalS2.tv_sec)
        << "ErrInfo: sched_rr_get_interval pid:='" << pid
        << ",0' *ts:='&intervalS1,&intervalS2',   --> return intervalS1.tv_sec:='" << intervalS1.tv_sec
        << "' intervalS2.tv_sec:='" << intervalS2.tv_sec << "'";
    EXPECT_EQ(intervalS1.tv_nsec, intervalS2.tv_nsec)
        << "ErrInfo: sched_rr_get_interval pid:='" << pid
        << ",0' *ts:='&intervalS1,&intervalS2',   --> return intervalS1.tv_nsec:='" << intervalS1.tv_nsec
        << "' intervalS2.tv_nsec:='" << intervalS2.tv_nsec << "'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_SETPARAM_0100
* @tc.name       test sched_setparam api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedSetParam, TestSize.Level1) {
    struct sched_param params;
    id_t pid;
    int policy, oldPriority, newPriority, tmpErrno;
    int returnVal1, returnVal2, returnVal3;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    returnVal1 = sched_getparam(pid, &params);
    LogPrint("    sched_getparam pid:='%d' &params:='*param',   "
        "--> returnVal1:='%d', params.sched_priority:='%d'\n", pid, returnVal1, params.sched_priority);
    oldPriority = params.sched_priority;
	
    newPriority = params.sched_priority == XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN ?
        (params.sched_priority = XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MAX):
        (params.sched_priority = XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN);

    LogPrint("    newPriority:='%d'\n", newPriority);

    returnVal2 = sched_setparam(pid, &params);
    tmpErrno = errno;
    LogPrint("    sched_setparam pid:='%d' &params:='*param',   "
        "--> returnVal2:='%d', params.sched_priority:='%d'\n", pid, returnVal2, params.sched_priority);
    newPriority = params.sched_priority;
    EXPECT_EQ(returnVal2, 0) << "ErrInfo: sched_setparam pid:='" << pid
        << "' &params:='int sched_priority',   --> returnVal2:='" << returnVal2 << "', params.sched_priority:='"
        << params.sched_priority <<"'";
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SCHED_SETSCHEDULER_0100
* @tc.name       test sched_setscheduler api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSchedSetScheduler, TestSize.Level1) {
    struct sched_param params;
    int which = PRIO_PROCESS;
    id_t pid;
    int returnVal, oldPolicy, oldPriority, newPolicy, returnNewPolicy, tmpErrno;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    oldPolicy = sched_getscheduler(pid);
    LogPrint("    sched_getscheduler pid:='%d',   --> return oldPolicy:='%d'\n", pid, oldPolicy);
    oldPriority = getpriority(which, pid);
    LogPrint("    getpriority which:='PRIO_PROCESS' pid:='%d',   --> oldPriority:='%d'\n", pid, oldPriority);
	
    newPolicy = oldPolicy == SCHED_RR ? (newPolicy = SCHED_FIFO) : (newPolicy = SCHED_RR);
    LogPrint("    newPolicy:='%d'\n", newPolicy);
	
    params.sched_priority = XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MIN;
	
    returnVal = sched_setscheduler(pid, newPolicy, &params);
    tmpErrno = errno;
    LogPrint("    sched_setscheduler pid:='%d' policy:='%d' params.sched_priority:='%d',   "
        "--> returnVal:='%d'\n", pid, newPolicy, params.sched_priority, returnVal);
    EXPECT_NE(returnVal, -1)
        << "ErrInfo: sched_setscheduler pid:='" << pid << "' policy:='" << newPolicy
        << "' params.sched_priority:='" << params.sched_priority << "',   --> returnVal:='" << returnVal << "'";
    EXPECT_EQ(tmpErrno, 0)
        << "ErrInfo: sched_setscheduler pid:='" << pid << "' policy:='" << newPolicy
        << "' params.sched_priority:='" << params.sched_priority << "',   --> returnVal:='" << returnVal
        << "' errno:='" << errno << "'";
    
    returnNewPolicy = sched_getscheduler(pid);
    LogPrint("    sched_getscheduler pid:='%d',   --> returnNewPolicy:='%d'\n", pid, returnNewPolicy);
    EXPECT_EQ(returnNewPolicy, newPolicy)
        << "ErrInfo: sched_setscheduler pid:='" << pid << "' policy:='"
        << newPolicy << "' params.sched_priority:='" << params.sched_priority << "',   --> returnNewPolicy:='"
        << returnNewPolicy << "'";
	
    // teardown
    params.sched_priority = oldPriority;
    sched_setscheduler(pid, oldPolicy, &params);
}

/**
* @tc.number     SUB_KERNEL_NDKAPI_SCHED_SETPRIORITY_0100
* @tc.name       test setpriority api
* @tc.desc       [C- SOFTWARE -0200]
*/
HWTEST_F(ActsSchedApiTest, testSetpriority, TestSize.Level1) {
    int which = PRIO_PROCESS;
    id_t pid;
    int oldPriority, newPriority, returnVal, tmpErrno;

    pid = getpid();
    LogPrint("    getpid,   --> pid:='%d'\n", pid);

    oldPriority = getpriority(which, pid);
    LogPrint("    getpriority which:='PRIO_PROCESS' pid:='%d',   --> oldPriority:='%d'\n", pid, oldPriority);
	
    newPriority = oldPriority == XTS_ACTS_SCHED_API_EXPECT_USER_PRIORITY_MAX ?
        (newPriority = oldPriority - 1):
        (newPriority = oldPriority + 1);

    returnVal = setpriority(which, pid, newPriority);
    tmpErrno = errno;
    LogPrint("    setpriority which:='PRIO_PROCESS' pid:='%d' priority:='%d',   "
        "--> returnVal:='%d', errno:='%d'\n", pid, newPriority, returnVal, tmpErrno);
	
    EXPECT_EQ(returnVal, 0)
        << "ErrInfo: setpriority which:='PRIO_PROCESS' pid:='" << pid << "' priority:='"
        << newPriority << "',   --> returnVal:='" << returnVal << "'";
    EXPECT_EQ(tmpErrno, 0)
        << "ErrInfo: setpriority which:='PRIO_PROCESS' pid:='" << pid << "' priority:='"
        << newPriority << "',   --> returnVal:='" << returnVal << "' errno:='" << tmpErrno << "'";
}