1 /*
2  * Copyright (c) 2021 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 #ifndef DISTRIBUTED_TEST_SYSINFO_H
16 #define DISTRIBUTED_TEST_SYSINFO_H
17 
18 #include <string.h>
19 #include "distributeddb_log_print.h"
20 #if defined(RUNNING_ON_LINUX)
21 #include <linux/limits.h>
22 #elif defined RUNNING_ON_WIN
23 #endif
24 
25 const uint64_t SYSTEM_INFO_BUFFER_SIZE = 20;
26 const uint64_t PROC_BUFFER_LENGTH = 4096;
27 const uint64_t DEFAULT_INTEVAL = 250000;
28 const uint64_t DEFAULT_COUNT = 5;
29 
30 #if defined(RUNNING_ON_LINUX)
31 const std::string SYS_MEM_FILE = "/proc/meminfo";
32 const std::string SYS_CPU_FILE = "/proc/stat";
33 const std::string POWER_FOLLOW_FILE = "/sys/class/power_supply/Battery/current_now";
34 const std::string VOLTAGE_FILE = "/sys/class/power_supply/Battery/voltage_now";
35 const std::string FILE_READ_PERMISSION = "r";
36 
37 // struct of mem occupy
38 struct MemOccupy {
39     char memTotalName_[SYSTEM_INFO_BUFFER_SIZE];
40     uint64_t memTotal_;
41     char memFreeName_[SYSTEM_INFO_BUFFER_SIZE];
42     uint64_t memFree_;
43     char buffersName_[SYSTEM_INFO_BUFFER_SIZE];
44     uint64_t buffers_;
45     char cachedName_[SYSTEM_INFO_BUFFER_SIZE];
46     uint64_t cached_;
47     char swapCachedName_[SYSTEM_INFO_BUFFER_SIZE];
48     uint64_t swapCached_;
49 };
50 
51 // struct of cpu occupy
52 struct CpuOccupy {
53     char name_[SYSTEM_INFO_BUFFER_SIZE];
54     uint64_t user_;
55     uint64_t nice_;
56     uint64_t system_;
57     uint64_t idle_;
58     uint64_t iowait_;
59     uint64_t irq_;
60     uint64_t softirq_;
61 };
62 #elif defined RUNNING_ON_WIN
63 #endif
64 
65 enum SeqNo {
66     FIRST = 1,
67     SECOND
68 };
69 
70 // this class get system info, such as system or cpu storage and power at the moment
71 class DistributedTestSysInfo final {
72 public:
73 
74     DistributedTestSysInfo();
~DistributedTestSysInfo()75     ~DistributedTestSysInfo() {}
76 
77     // Delete the copy and assign constructors
78     DistributedTestSysInfo(const DistributedTestSysInfo &distributeTestSysInfo) = delete;
79     DistributedTestSysInfo& operator=(const DistributedTestSysInfo &distributeTestSysInfo) = delete;
80     DistributedTestSysInfo(DistributedTestSysInfo &&distributeTestSysInfo) = delete;
81     DistributedTestSysInfo& operator=(DistributedTestSysInfo &&distributeTestSysInfo) = delete;
82 
83     // read memory information from system file
84     void GetSysMemOccpy(SeqNo seqNo);
85     // read cpu information from system file sleep microSeconds between first and second read.
86     void GetSysCpuUsage(SeqNo seqNo, uint64_t microSeconds);
87     // read value from specific file
88     float ReadSysValFromFile(const std::string &filePath);
89     // read value from specific file and take the average within totalCount
90     float GetSysMeanCurrentVal(const std::string &filePath, int totalCount, uint64_t microSeconds);
91     // read power from specific file
92     void GetSysCurrentPower(SeqNo seqNo, int totalCount, uint64_t microSeconds);
93 
94     uint64_t GetFirstMemFree() const;
95     uint64_t GetSecondMemFree() const;
96     float GetFirstCpuUsage() const;
97     float GetSecondCpuUsage() const;
98     float GetFirstPower() const;
99     float GetSecondPower() const;
100 
101     void SaveSecondToFirst();
102 
103 private:
104 #if defined(RUNNING_ON_LINUX)
105     MemOccupy memStatFirst_, memStatSecond_;
106     CpuOccupy cpuStatFirst_, cpuStatSecond_;
107 #elif defined RUNNING_ON_WIN
108 #endif
109     float cpuStatFirstUsage_, cpuStatSecondUsage_;
110     float powerStatFirst_, powerStatSecond_;
111     float val_;
112 };
113 #endif // DISTRIBUTED_TEST_SYSINFO_H