• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifdef LOSCFG_KERNEL_SCHED_PLIMIT
32 #include "los_schedlimit.h"
33 #include "los_process_pri.h"
34 
35 STATIC ProcSchedLimiter *g_procSchedLimit = NULL;
36 
OsSchedLimitInit(UINTPTR limit)37 VOID OsSchedLimitInit(UINTPTR limit)
38 {
39     ProcSchedLimiter *schedLimit = (ProcSchedLimiter *)limit;
40     schedLimit->period = 0;
41     schedLimit->quota = 0;
42     g_procSchedLimit = schedLimit;
43 }
44 
OsSchedLimitAlloc(VOID)45 VOID *OsSchedLimitAlloc(VOID)
46 {
47     ProcSchedLimiter *plimit = (ProcSchedLimiter *)LOS_MemAlloc(m_aucSysMem1, sizeof(ProcSchedLimiter));
48     if (plimit == NULL) {
49         return NULL;
50     }
51     (VOID)memset_s(plimit, sizeof(ProcSchedLimiter), 0, sizeof(ProcSchedLimiter));
52     return (VOID *)plimit;
53 }
54 
OsSchedLimitFree(UINTPTR limit)55 VOID OsSchedLimitFree(UINTPTR limit)
56 {
57     ProcSchedLimiter *schedLimit = (ProcSchedLimiter *)limit;
58     if (schedLimit == NULL) {
59         return;
60     }
61 
62     (VOID)LOS_MemFree(m_aucSysMem1, (VOID *)limit);
63 }
64 
OsSchedLimitCopy(UINTPTR dest,UINTPTR src)65 VOID OsSchedLimitCopy(UINTPTR dest, UINTPTR src)
66 {
67     ProcSchedLimiter *plimitDest = (ProcSchedLimiter *)dest;
68     ProcSchedLimiter *plimitSrc = (ProcSchedLimiter *)src;
69     plimitDest->period = plimitSrc->period;
70     plimitDest->quota = plimitSrc->quota;
71     return;
72 }
73 
OsSchedLimitUpdateRuntime(LosTaskCB * runTask,UINT64 currTime,INT32 incTime)74 VOID OsSchedLimitUpdateRuntime(LosTaskCB *runTask, UINT64 currTime, INT32 incTime)
75 {
76     LosProcessCB *run = (LosProcessCB *)runTask->processCB;
77     if ((run == NULL) || (run->plimits == NULL)) {
78         return;
79     }
80 
81     ProcSchedLimiter *schedLimit = (ProcSchedLimiter *)run->plimits->limitsList[PROCESS_LIMITER_ID_SCHED];
82 
83     run->limitStat.allRuntime += incTime;
84     if ((schedLimit->period <= 0) || (schedLimit->quota <= 0)) {
85         return;
86     }
87 
88     if ((schedLimit->startTime <= currTime) && (schedLimit->allRuntime < schedLimit->quota)) {
89         schedLimit->allRuntime += incTime;
90         return;
91     }
92 
93     if (schedLimit->startTime <= currTime) {
94         schedLimit->startTime = currTime + schedLimit->period;
95         schedLimit->allRuntime = 0;
96     }
97 }
98 
OsSchedLimitCheckTime(LosTaskCB * task)99 BOOL OsSchedLimitCheckTime(LosTaskCB *task)
100 {
101     UINT64 currTime = OsGetCurrSchedTimeCycle();
102     LosProcessCB *processCB = (LosProcessCB *)task->processCB;
103     if ((processCB == NULL) || (processCB->plimits == NULL)) {
104         return TRUE;
105     }
106     ProcSchedLimiter *schedLimit = (ProcSchedLimiter *)processCB->plimits->limitsList[PROCESS_LIMITER_ID_SCHED];
107     if (schedLimit->startTime >= currTime) {
108         return FALSE;
109     }
110     return TRUE;
111 }
112 
OsSchedLimitSetPeriod(ProcSchedLimiter * schedLimit,UINT64 value)113 UINT32 OsSchedLimitSetPeriod(ProcSchedLimiter *schedLimit, UINT64 value)
114 {
115     UINT32 intSave;
116     if (schedLimit == NULL) {
117         return EINVAL;
118     }
119 
120     if (schedLimit == g_procSchedLimit) {
121         return EPERM;
122     }
123 
124     SCHEDULER_LOCK(intSave);
125     schedLimit->period = value;
126     SCHEDULER_UNLOCK(intSave);
127     return LOS_OK;
128 }
129 
OsSchedLimitSetQuota(ProcSchedLimiter * schedLimit,UINT64 value)130 UINT32 OsSchedLimitSetQuota(ProcSchedLimiter *schedLimit, UINT64 value)
131 {
132     UINT32 intSave;
133     if (schedLimit == NULL) {
134         return EINVAL;
135     }
136 
137     if (schedLimit == g_procSchedLimit) {
138         return EPERM;
139     }
140 
141     SCHEDULER_LOCK(intSave);
142     if (value > (UINT64)schedLimit->period) {
143         SCHEDULER_UNLOCK(intSave);
144         return EINVAL;
145     }
146 
147     schedLimit->quota = value;
148     SCHEDULER_UNLOCK(intSave);
149     return LOS_OK;
150 }
151 #endif
152