Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 METRICSD_COLLECTORS_CPU_USAGE_COLLECTOR_H_ |
| 18 | #define METRICSD_COLLECTORS_CPU_USAGE_COLLECTOR_H_ |
| 19 | |
| 20 | #include <base/time/time.h> |
| 21 | |
| 22 | #include "metrics/metrics_library.h" |
| 23 | |
| 24 | class CpuUsageCollector { |
| 25 | public: |
Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 26 | explicit CpuUsageCollector(MetricsLibraryInterface* metrics_library); |
Bertrand SIMONNET | 0ada2ca | 2015-11-02 14:08:44 -0800 | [diff] [blame] | 27 | |
| 28 | // Initialize this collector's state. |
| 29 | void Init(); |
| 30 | |
| 31 | // Schedule a collection interval. |
| 32 | void Schedule(); |
| 33 | |
| 34 | // Callback called at the end of the collection interval. |
| 35 | void CollectCallback(); |
| 36 | |
| 37 | // Measure the cpu use and report it. |
| 38 | void Collect(); |
| 39 | |
| 40 | // Gets the current cumulated Cpu usage. |
| 41 | base::TimeDelta GetCumulativeCpuUse(); |
| 42 | |
| 43 | private: |
| 44 | FRIEND_TEST(CpuUsageTest, ParseProcStat); |
| 45 | bool ParseProcStat(const std::string& stat_content, |
| 46 | uint64_t *user_ticks, |
| 47 | uint64_t *user_nice_ticks, |
| 48 | uint64_t *system_ticks); |
| 49 | |
| 50 | int num_cpu_; |
| 51 | uint32_t ticks_per_second_; |
| 52 | |
| 53 | base::TimeDelta collect_interval_; |
| 54 | base::TimeDelta latest_cpu_use_; |
| 55 | |
| 56 | MetricsLibraryInterface* metrics_lib_; |
| 57 | }; |
| 58 | |
| 59 | #endif // METRICSD_COLLECTORS_CPU_USAGE_COLLECTOR_H_ |