• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _CPU_USAGE_H_
18 #define _CPU_USAGE_H_
19 
20 #include <statstype.h>
21 
22 #define CPU_USAGE_BUFFER_SIZE (6 * 30)
23 #define TOP_PROCESS_COUNT (5)
24 #define CPU_USAGE_PROFILE_THRESHOLD (50)
25 
26 #define PROCPROF_THRESHOLD "cpu.procprof.threshold"
27 #define CPU_DISABLED "cpu.disabled"
28 #define CPU_DEBUG "cpu.debug"
29 #define CPU_TOPCOUNT "cpu.topcount"
30 
31 namespace android {
32 namespace pixel {
33 namespace perfstatsd {
34 
35 struct CpuData {
36     uint64_t cpuUsage;
37     uint64_t cpuTime;
38     uint64_t userUsage;
39     uint64_t sysUsage;
40     uint64_t ioUsage;
41 };
42 
43 struct ProcData {
44     uint32_t pid;
45     std::string name;
46     float usageRatio;
47     uint64_t usage;
48     uint64_t user;
49     uint64_t system;
50 };
51 
52 class CpuUsage : public StatsType {
53   public:
54     CpuUsage(void);
55     void refresh(void);
56     void setOptions(const std::string &key, const std::string &value);
57 
58   private:
59     std::chrono::system_clock::time_point mLast;
60     uint32_t mCores;  // cpu core num
61     uint32_t mProfileThreshold;
62     uint32_t mTopcount;
63     bool mDisabled;
64     bool mProfileProcess;
65     CpuData mPrevUsage;                                    // cpu usage of last record
66     std::vector<CpuData> mPrevCoresUsage;                  // cpu usage per core of last record
67     std::unordered_map<uint32_t, ProcData> mPrevProcdata;  // <pid, last_usage>
68     uint64_t mDiffCpu;
69     float mTotalRatio;
70     void getOverallUsage(std::chrono::system_clock::time_point &, std::string *);
71     void profileProcess(std::string *);
72 };
73 
74 struct ProcdataCompare {
75     // sort process by usage percentage in descending order
operatorProcdataCompare76     bool operator()(const ProcData &a, const ProcData &b) const {
77         return a.usageRatio < b.usageRatio;
78     }
79 };
80 
81 }  // namespace perfstatsd
82 }  // namespace pixel
83 }  // namespace android
84 
85 #endif /*  _CPU_USAGE_H_ */
86