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 <cstdio>
17 #include <cinttypes>
18 #include <cstdint>
19 #include <unordered_map>
20 
21 #include "core/entity.h"
22 #include "sched/interval.h"
23 #include "sched/sched_deadline.h"
24 
25 namespace ffrt {
26 namespace TaskLoadTracking {
27 #ifdef PER_TASK_LOAD_TRACKING
28 
29 static std::unordered_map<const char*, uint64_t> taskLoad;
30 static __thread uint64_t start;
31 
32 // Called at eu/co_routine.cpp CoStart(task).
Begin(CPUEUTask * task)33 void Begin(CPUEUTask* task)
34 {
35     (void)task;
36     start = perf_mmap_read_current();
37 }
38 
End(CPUEUTask * task)39 void End(CPUEUTask* task)
40 {
41     uint64_t load = perf_mmap_read_current() - start;
42 
43     taskLoad[task->identity] = load;
44 }
45 
46 // Get historical load based on its identity. 0 on the first time.
GetLoad(CPUEUTask * task)47 uint64_t GetLoad(CPUEUTask* task)
48 {
49     return taskLoad[task->identity];
50 }
51 
52 #else // !PER_TASK_LOAD_TRACKING
53 void Begin(CPUEUTask* task)
54 {
55     (void)task;
56 }
57 void End(CPUEUTask* task)
58 {
59     (void)task;
60 }
61 uint64_t GetLoad(CPUEUTask* task)
62 {
63     (void)task;
64     return 0;
65 }
66 #endif // PER_TASK_LOAD_TRACKING
67 } // namespace TaskLoadTracking
68 } // namespace ffrt
69