diff options
author | Charles(Yen-Cheng) Chan <charleschan@google.com> | 2019-01-10 16:39:54 +0800 |
---|---|---|
committer | Jimmy Shiu <jimmyshiu@google.com> | 2019-01-11 14:09:06 +0800 |
commit | a78dcb96b83190136f82952f91abf1f8e7ecb6b1 (patch) | |
tree | 5f71732a98ba0604deb4c73d2f21a1eb439f7573 /perfstatsd | |
parent | c4410131a557341570fb78209b37f88e267f1d38 (diff) | |
download | pixel-a78dcb96b83190136f82952f91abf1f8e7ecb6b1.tar.gz |
Refine perfstatsd daemon and cpu usage part
Use consistent naming style for perfstatsd daemon and cpu usage and
convert Android.mk to Android.bp
Bug: 117587967
Test: verify cpu and io data dumped correctly in dumpstate_board
Change-Id: Ia9b563c25d74a293c276f83bf6baad4dca8b6cad
Diffstat (limited to 'perfstatsd')
-rw-r--r-- | perfstatsd/Android.bp | 80 | ||||
-rw-r--r-- | perfstatsd/Android.mk | 35 | ||||
-rw-r--r-- | perfstatsd/binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl | 2 | ||||
-rw-r--r-- | perfstatsd/cpu_usage.cpp | 178 | ||||
-rw-r--r-- | perfstatsd/include/cpu_usage.h | 38 | ||||
-rw-r--r-- | perfstatsd/include/io_usage.h | 2 | ||||
-rw-r--r-- | perfstatsd/include/perfstats_buffer.h | 13 | ||||
-rw-r--r-- | perfstatsd/include/perfstatsd.h | 10 | ||||
-rw-r--r-- | perfstatsd/include/perfstatsd_service.h | 6 | ||||
-rw-r--r-- | perfstatsd/include/statstype.h | 14 | ||||
-rw-r--r-- | perfstatsd/io_usage.cpp | 2 | ||||
-rw-r--r-- | perfstatsd/main.cpp | 51 | ||||
-rw-r--r-- | perfstatsd/perfstats_buffer.cpp | 8 | ||||
-rw-r--r-- | perfstatsd/perfstatsd.cpp | 18 | ||||
-rw-r--r-- | perfstatsd/perfstatsd.rc (renamed from perfstatsd/init-perfstatsd.rc) | 0 | ||||
-rw-r--r-- | perfstatsd/perfstatsd_service.cpp | 10 |
16 files changed, 258 insertions, 209 deletions
diff --git a/perfstatsd/Android.bp b/perfstatsd/Android.bp new file mode 100644 index 00000000..a65d1a9c --- /dev/null +++ b/perfstatsd/Android.bp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +cc_defaults { + name: "perfstatsd_defaults", + + shared_libs: [ + "libbase", + "libbinder", + "libcutils", + "libhidlbase", + "libhwbinder", + "liblog", + "libutils", + ], + + cflags: [ + "-Wall", + "-Werror", + "-Wextra", + "-Wno-unused-parameter" + ], +} + +cc_binary { + name: "perfstatsd", + + defaults: ["perfstatsd_defaults"], + + srcs: ["main.cpp"], + local_include_dirs: ["include"], + static_libs: ["libperfstatsd"], + + init_rc: ["perfstatsd.rc"], + + owner: "google", + vendor: true, +} + +cc_library_static { + name: "libperfstatsd", + + defaults: ["perfstatsd_defaults"], + + srcs: [ + "perfstatsd.cpp", + "perfstatsd_service.cpp", + "perfstats_buffer.cpp", + "cpu_usage.cpp", + "io_usage.cpp", + ":perfstatsd_aidl_private", + ], + local_include_dirs: ["include"], + aidl: { + export_aidl_headers: true, + local_include_dirs: ["binder"], + }, + vendor: true, +} + +filegroup { + name: "perfstatsd_aidl_private", + srcs: [ + "binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl", + ], + path: "binder", +} diff --git a/perfstatsd/Android.mk b/perfstatsd/Android.mk deleted file mode 100644 index 4a6eb719..00000000 --- a/perfstatsd/Android.mk +++ /dev/null @@ -1,35 +0,0 @@ -LOCAL_PATH:= $(call my-dir) - -# /vendor/bin/perfstatsd -include $(CLEAR_VARS) -LOCAL_SRC_FILES := main.cpp -LOCAL_C_INCLUDES := $(LOCAL_PATH)/include -LOCAL_MODULE := perfstatsd -LOCAL_VENDOR_MODULE := true -LOCAL_MODULE_CLASS := EXECUTABLES -LOCAL_MODULE_OWNER := google -LOCAL_MODULE_TAGS := eng debug -LOCAL_STATIC_LIBRARIES := libperfstatsd -LOCAL_SHARED_LIBRARIES := libbase libbinder libcutils libhwbinder liblog libutils -include $(BUILD_EXECUTABLE) - -include $(CLEAR_VARS) -LOCAL_SRC_FILES := perfstatsd.cpp perfstatsd_service.cpp perfstats_buffer.cpp cpu_usage.cpp io_usage.cpp -LOCAL_SRC_FILES += $(call all-Iaidl-files-under, binder) -LOCAL_SRC_FILES += $(call all-Iaidl-files-under, ../../../../frameworks/native/libs/binder/aidl) -LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(TOP)/system/core/include -LOCAL_MODULE := libperfstatsd -LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/binder -LOCAL_VENDOR_MODULE := true -LOCAL_MODULE_TAGS := eng debug -LOCAL_SHARED_LIBRARIES := libbinder -include $(BUILD_STATIC_LIBRARY) - -# /vendor/etc/init/init-perfstatsd.rc -include $(CLEAR_VARS) -LOCAL_MODULE := init-perfstatsd.rc -LOCAL_SRC_FILES := $(LOCAL_MODULE) -LOCAL_MODULE_CLASS := ETC -LOCAL_MODULE_TAGS := eng debug -LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_ETC)/init -include $(BUILD_PREBUILT) diff --git a/perfstatsd/binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl b/perfstatsd/binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl index 6e49b4ff..944a9272 100644 --- a/perfstatsd/binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl +++ b/perfstatsd/binder/android/pixel/perfstatsd/IPerfstatsdPrivate.aidl @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/perfstatsd/cpu_usage.cpp b/perfstatsd/cpu_usage.cpp index 8d302f89..6bbeb2db 100644 --- a/perfstatsd/cpu_usage.cpp +++ b/perfstatsd/cpu_usage.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,10 +25,10 @@ using namespace android::pixel::perfstatsd; static bool cDebug = false; static constexpr char FMT_CPU_TOTAL[] = "[CPU: %lld.%03llds][T:%.2f%%,U:%.2f%%,S:%.2f%%,IO:%.2f%%]"; -static constexpr char TOP_HEADER[] = "[CPU_TOP] PID, PROCESS NAME, USR_TIME, SYS_TIME\n"; +static constexpr char TOP_HEADER[] = "[CPU_TOP] PID, PROCESS_NAME, USR_TIME, SYS_TIME\n"; static constexpr char FMT_TOP_PROFILE[] = "%6.2f%% %5d %s %" PRIu64 " %" PRIu64 "\n"; -cpu_usage::cpu_usage(void) { +CpuUsage::CpuUsage(void) { std::string procstat; if (android::base::ReadFileToString("/proc/stat", &procstat)) { std::istringstream stream(procstat); @@ -36,7 +36,7 @@ cpu_usage::cpu_usage(void) { while (getline(stream, line)) { std::vector<std::string> fields = android::base::Split(line, " "); if (fields[0].find("cpu") != std::string::npos && fields[0] != "cpu") { - cpudata data; + CpuData data; mPrevCoresUsage.push_back(data); } } @@ -46,7 +46,7 @@ cpu_usage::cpu_usage(void) { mTopcount = TOP_PROCESS_COUNT; } -void cpu_usage::setOptions(const std::string &key, const std::string &value) { +void CpuUsage::setOptions(const std::string &key, const std::string &value) { if (key == PROCPROF_THRESHOLD || key == CPU_DISABLED || key == CPU_DEBUG || key == CPU_TOPCOUNT) { uint32_t val = 0; @@ -71,22 +71,22 @@ void cpu_usage::setOptions(const std::string &key, const std::string &value) { } } -void cpu_usage::profileProcess(uint64_t diffcpu, std::string *out) { +void CpuUsage::profileProcess(std::string *out) { // Read cpu usage per process and find the top ones DIR *dir; struct dirent *ent; - std::unordered_map<uint32_t, procdata> proc_usage; - std::priority_queue<procdata, std::vector<procdata>, ProcdataCompare> proclist; + std::unordered_map<uint32_t, ProcData> procUsage; + std::priority_queue<ProcData, std::vector<ProcData>, ProcdataCompare> procList; if ((dir = opendir("/proc/")) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type == DT_DIR) { - std::string pid_str = ent->d_name; - std::string::const_iterator it = pid_str.begin(); - while (it != pid_str.end() && isdigit(*it)) ++it; - if (!pid_str.empty() && it == pid_str.end()) { - std::string pid_stat; - if (android::base::ReadFileToString("/proc/" + pid_str + "/stat", &pid_stat)) { - std::vector<std::string> fields = android::base::Split(pid_stat, " "); + std::string pidStr = ent->d_name; + std::string::const_iterator it = pidStr.begin(); + while (it != pidStr.end() && isdigit(*it)) ++it; + if (!pidStr.empty() && it == pidStr.end()) { + std::string pidStat; + if (android::base::ReadFileToString("/proc/" + pidStr + "/stat", &pidStat)) { + std::vector<std::string> fields = android::base::Split(pidStat, " "); uint32_t pid = 0; uint64_t utime = 0; uint64_t stime = 0; @@ -98,7 +98,7 @@ void cpu_usage::profileProcess(uint64_t diffcpu, std::string *out) { !base::ParseUint(fields[14], &stime) || !base::ParseUint(fields[15], &cutime) || !base::ParseUint(fields[16], &cstime)) { - LOG_TO(SYSTEM, ERROR) << "Invalid proc data\n" << pid_stat; + LOG_TO(SYSTEM, ERROR) << "Invalid proc data\n" << pidStat; continue; } std::string proc = fields[1]; @@ -106,45 +106,44 @@ void cpu_usage::profileProcess(uint64_t diffcpu, std::string *out) { proc.length() > 2 ? proc.substr(1, proc.length() - 2) : ""; uint64_t user = utime + cutime; uint64_t system = stime + cstime; - uint64_t totalusage = user + system; + uint64_t totalUsage = user + system; - uint64_t diffuser = user - mPrevProcdata[pid].user; - uint64_t diffsystem = system - mPrevProcdata[pid].system; - uint64_t diffusage = totalusage - mPrevProcdata[pid].usage; + uint64_t diffUser = user - mPrevProcdata[pid].user; + uint64_t diffSystem = system - mPrevProcdata[pid].system; + uint64_t diffUsage = totalUsage - mPrevProcdata[pid].usage; - procdata ldata; + ProcData ldata; ldata.user = user; ldata.system = system; - ldata.usage = totalusage; - proc_usage[pid] = ldata; + ldata.usage = totalUsage; + procUsage[pid] = ldata; - float usage_ratio = (float)(diffusage * 100.0 / diffcpu); - if (cDebug && usage_ratio > 100) { - LOG_TO(SYSTEM, INFO) << "pid: " << pid << " , ratio: " << usage_ratio + float usageRatio = (float)(diffUsage * 100.0 / mDiffCpu); + if (cDebug && usageRatio > 100) { + LOG_TO(SYSTEM, INFO) << "pid: " << pid << " , ratio: " << usageRatio << " , prev usage: " << mPrevProcdata[pid].usage - << " , cur usage: " << totalusage - << " , total cpu diff: " << diffcpu; + << " , cur usage: " << totalUsage + << " , total cpu diff: " << mDiffCpu; } - procdata data; + ProcData data; data.pid = pid; data.name = name; - data.usage_ratio = usage_ratio; - data.user = diffuser; - data.system = diffsystem; - proclist.push(data); + data.usageRatio = usageRatio; + data.user = diffUser; + data.system = diffSystem; + procList.push(data); } } } } - mPrevProcdata = std::move(proc_usage); - uint32_t count = 0; + mPrevProcdata = std::move(procUsage); out->append(TOP_HEADER); - while (!proclist.empty() && count++ < mTopcount) { - procdata data = proclist.top(); - out->append(android::base::StringPrintf(FMT_TOP_PROFILE, data.usage_ratio, data.pid, + for (uint32_t count = 0; !procList.empty() && count < mTopcount; count++) { + ProcData data = procList.top(); + out->append(android::base::StringPrintf(FMT_TOP_PROFILE, data.usageRatio, data.pid, data.name.c_str(), data.user, data.system)); - proclist.pop(); + procList.pop(); } closedir(dir); } else { @@ -152,25 +151,20 @@ void cpu_usage::profileProcess(uint64_t diffcpu, std::string *out) { } } -void cpu_usage::refresh(void) { - if (mDisabled) - return; - - std::string out, proc_stat; - uint64_t diffcpu; - float totalRatio = 0.0f; - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); +void CpuUsage::getOverallUsage(std::chrono::system_clock::time_point &now, std::string *out) { + mDiffCpu = 0; + mTotalRatio = 0.0f; + std::string procStat; // Get overall cpu usage - if (android::base::ReadFileToString("/proc/stat", &proc_stat)) { - std::istringstream stream(proc_stat); + if (android::base::ReadFileToString("/proc/stat", &procStat)) { + std::istringstream stream(procStat); std::string line; while (getline(stream, line)) { std::vector<std::string> fields = android::base::Split(line, " "); if (fields[0].find("cpu") != std::string::npos) { - std::string cpu_str = fields[0]; - std::string core = - cpu_str.length() > 3 ? cpu_str.substr(3, cpu_str.length() - 3) : ""; + std::string cpuStr = fields[0]; + std::string core = cpuStr.length() > 3 ? cpuStr.substr(3, cpuStr.length() - 3) : ""; uint64_t user = 0; uint64_t nice = 0; uint64_t system = 0; @@ -196,39 +190,39 @@ void cpu_usage::refresh(void) { continue; } - uint64_t cputime = user + nice + system + idle + iowait + irq + softirq + steal; - uint64_t cpuusage = cputime - idle - iowait; - uint64_t userusage = user + nice; + uint64_t cpuTime = user + nice + system + idle + iowait + irq + softirq + steal; + uint64_t cpuUsage = cpuTime - idle - iowait; + uint64_t userUsage = user + nice; if (!core.compare("")) { - uint64_t diffusage = cpuusage - mPrevUsage.cpuusage; - diffcpu = cputime - mPrevUsage.cputime; - uint64_t diffuser = userusage - mPrevUsage.userusage; - uint64_t diffsys = system - mPrevUsage.sysusage; - uint64_t diffio = iowait - mPrevUsage.iousage; + uint64_t diffUsage = cpuUsage - mPrevUsage.cpuUsage; + mDiffCpu = cpuTime - mPrevUsage.cpuTime; + uint64_t diffUser = userUsage - mPrevUsage.userUsage; + uint64_t diffSys = system - mPrevUsage.sysUsage; + uint64_t diffIo = iowait - mPrevUsage.ioUsage; - totalRatio = (float)(diffusage * 100.0 / diffcpu); - float userRatio = (float)(diffuser * 100.0 / diffcpu); - float sysRatio = (float)(diffsys * 100.0 / diffcpu); - float ioRatio = (float)(diffio * 100.0 / diffcpu); + mTotalRatio = (float)(diffUsage * 100.0 / mDiffCpu); + float userRatio = (float)(diffUser * 100.0 / mDiffCpu); + float sysRatio = (float)(diffSys * 100.0 / mDiffCpu); + float ioRatio = (float)(diffIo * 100.0 / mDiffCpu); if (cDebug) { LOG_TO(SYSTEM, INFO) - << "prev total: " << mPrevUsage.cpuusage - << " , cur total: " << cpuusage << " , diffusage: " << diffusage - << " , diffcpu: " << diffcpu << " , ratio: " << totalRatio; + << "prev total: " << mPrevUsage.cpuUsage + << " , cur total: " << cpuUsage << " , diffusage: " << diffUsage + << " , diffcpu: " << mDiffCpu << " , ratio: " << mTotalRatio; } - mPrevUsage.cpuusage = cpuusage; - mPrevUsage.cputime = cputime; - mPrevUsage.userusage = userusage; - mPrevUsage.sysusage = system; - mPrevUsage.iousage = iowait; + mPrevUsage.cpuUsage = cpuUsage; + mPrevUsage.cpuTime = cpuTime; + mPrevUsage.userUsage = userUsage; + mPrevUsage.sysUsage = system; + mPrevUsage.ioUsage = iowait; auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - mLast); - out.append(android::base::StringPrintf(FMT_CPU_TOTAL, ms.count() / 1000, - ms.count() % 1000, totalRatio, - userRatio, sysRatio, ioRatio)); + out->append(android::base::StringPrintf(FMT_CPU_TOTAL, ms.count() / 1000, + ms.count() % 1000, mTotalRatio, + userRatio, sysRatio, ioRatio)); } else { // calculate total cpu usage of each core uint32_t c = 0; @@ -236,35 +230,45 @@ void cpu_usage::refresh(void) { LOG_TO(SYSTEM, ERROR) << "Invalid core: " << core; continue; } - uint64_t diffusage = cpuusage - mPrevCoresUsage[c].cpuusage; - float coreTotalRatio = (float)(diffusage * 100.0 / diffcpu); + uint64_t diffUsage = cpuUsage - mPrevCoresUsage[c].cpuUsage; + float coreTotalRatio = (float)(diffUsage * 100.0 / mDiffCpu); if (cDebug) { LOG_TO(SYSTEM, INFO) - << "core " << c << " , prev cpu usage: " << mPrevCoresUsage[c].cpuusage - << " , cur cpu usage: " << cpuusage << " , diffusage: " << diffusage - << " , difftotalcpu: " << diffcpu << " , ratio: " << coreTotalRatio; + << "core " << c << " , prev cpu usage: " << mPrevCoresUsage[c].cpuUsage + << " , cur cpu usage: " << cpuUsage << " , diffusage: " << diffUsage + << " , difftotalcpu: " << mDiffCpu << " , ratio: " << coreTotalRatio; } - mPrevCoresUsage[c].cpuusage = cpuusage; + mPrevCoresUsage[c].cpuUsage = cpuUsage; char buf[64]; sprintf(buf, "%.2f%%]", coreTotalRatio); - out.append("[" + core + ":" + std::string(buf)); + out->append("[" + core + ":" + std::string(buf)); } } } - out.append("\n"); + out->append("\n"); } else { LOG_TO(SYSTEM, ERROR) << "Fail to read /proc/stat"; } +} + +void CpuUsage::refresh(void) { + if (mDisabled) + return; + + std::string out; + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + + getOverallUsage(now, &out); - if (totalRatio >= mProfileThreshold) { + if (mTotalRatio >= mProfileThreshold) { if (cDebug) LOG_TO(SYSTEM, INFO) << "Total CPU usage over " << mProfileThreshold << "%"; - std::string profile_result; - profileProcess(diffcpu, &profile_result); + std::string profileResult; + profileProcess(&profileResult); if (mProfileProcess) { // Dump top processes once met threshold continuously at least twice. - out.append(profile_result); + out.append(profileResult); } else mProfileProcess = true; } else diff --git a/perfstatsd/include/cpu_usage.h b/perfstatsd/include/cpu_usage.h index e6456357..16cff2c4 100644 --- a/perfstatsd/include/cpu_usage.h +++ b/perfstatsd/include/cpu_usage.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef _CPU_USAGE_H_ #define _CPU_USAGE_H_ @@ -31,26 +32,26 @@ namespace android { namespace pixel { namespace perfstatsd { -struct cpudata { - uint64_t cpuusage; - uint64_t cputime; - uint64_t userusage; - uint64_t sysusage; - uint64_t iousage; +struct CpuData { + uint64_t cpuUsage; + uint64_t cpuTime; + uint64_t userUsage; + uint64_t sysUsage; + uint64_t ioUsage; }; -struct procdata { +struct ProcData { uint32_t pid; std::string name; - float usage_ratio; + float usageRatio; uint64_t usage; uint64_t user; uint64_t system; }; -class cpu_usage : public statstype { +class CpuUsage : public StatsType { public: - cpu_usage(void); + CpuUsage(void); void refresh(void); void setOptions(const std::string &key, const std::string &value); @@ -61,16 +62,19 @@ class cpu_usage : public statstype { uint32_t mTopcount; bool mDisabled; bool mProfileProcess; - cpudata mPrevUsage; // cpu usage of last record - std::vector<cpudata> mPrevCoresUsage; // cpu usage per core of last record - std::unordered_map<uint32_t, procdata> mPrevProcdata; // <pid, last_usage> - void profileProcess(uint64_t, std::string *); + CpuData mPrevUsage; // cpu usage of last record + std::vector<CpuData> mPrevCoresUsage; // cpu usage per core of last record + std::unordered_map<uint32_t, ProcData> mPrevProcdata; // <pid, last_usage> + uint64_t mDiffCpu; + float mTotalRatio; + void getOverallUsage(std::chrono::system_clock::time_point &, std::string *); + void profileProcess(std::string *); }; struct ProcdataCompare { // sort process by usage percentage in descending order - bool operator()(const procdata &a, const procdata &b) const { - return a.usage_ratio < b.usage_ratio; + bool operator()(const ProcData &a, const ProcData &b) const { + return a.usageRatio < b.usageRatio; } }; diff --git a/perfstatsd/include/io_usage.h b/perfstatsd/include/io_usage.h index 4df4cafb..82c32a14 100644 --- a/perfstatsd/include/io_usage.h +++ b/perfstatsd/include/io_usage.h @@ -148,7 +148,7 @@ class IoStats { bool dump(std::stringstream *output); }; -class io_usage : public statstype { +class io_usage : public StatsType { private: IoStats mStats; diff --git a/perfstatsd/include/perfstats_buffer.h b/perfstatsd/include/perfstats_buffer.h index 5d10d61b..3115c8b4 100644 --- a/perfstatsd/include/perfstats_buffer.h +++ b/perfstatsd/include/perfstats_buffer.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef _PERFSTATS_BUFFER_H_ #define _PERFSTATS_BUFFER_H_ @@ -47,7 +48,7 @@ constexpr auto operator""_KiB(unsigned long long const num) { return num * 1024; } -class statsdata { +class StatsData { public: std::chrono::system_clock::time_point getTime() const { return mTime; } std::string getData() const { return mData; } @@ -65,17 +66,17 @@ class PerfstatsBuffer { size_t count() { return mStorage.size(); } void setSize(size_t size) { mBufferSize = size; } - void emplace(statsdata &&data); - const std::queue<statsdata> &dump(void); + void emplace(StatsData &&data); + const std::queue<StatsData> &dump(void); private: size_t mBufferSize = 0U; - std::queue<statsdata> mStorage; + std::queue<StatsData> mStorage; }; struct StatsdataCompare { // sort time in ascending order - bool operator()(const statsdata &a, const statsdata &b) const { + bool operator()(const StatsData &a, const StatsData &b) const { return a.getTime() > b.getTime(); } }; diff --git a/perfstatsd/include/perfstatsd.h b/perfstatsd/include/perfstatsd.h index d39b9412..3a831c4d 100644 --- a/perfstatsd/include/perfstatsd.h +++ b/perfstatsd/include/perfstatsd.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,16 +28,16 @@ namespace android { namespace pixel { namespace perfstatsd { -class perfstatsd_t : public RefBase { +class Perfstatsd : public RefBase { private: - std::list<std::unique_ptr<statstype>> mStats; + std::list<std::unique_ptr<StatsType>> mStats; uint32_t mRefreshPeriod; public: - perfstatsd_t(void); + Perfstatsd(void); void refresh(void); void pause(void) { sleep(mRefreshPeriod); } - void get_history(std::string *ret); + void getHistory(std::string *ret); void setOptions(const std::string &key, const std::string &value); }; diff --git a/perfstatsd/include/perfstatsd_service.h b/perfstatsd/include/perfstatsd_service.h index 1f16ad98..ef9d9f6d 100644 --- a/perfstatsd/include/perfstatsd_service.h +++ b/perfstatsd/include/perfstatsd_service.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,7 @@ using namespace android::pixel::perfstatsd; -extern android::sp<perfstatsd_t> perfstatsd_sp; +extern android::sp<Perfstatsd> perfstatsdSp; class PerfstatsdPrivateService : public android::BinderService<PerfstatsdPrivateService>, public BnPerfstatsdPrivate { @@ -36,6 +36,6 @@ class PerfstatsdPrivateService : public android::BinderService<PerfstatsdPrivate android::binder::Status setOptions(const std::string &key, const std::string &value); }; -android::sp<IPerfstatsdPrivate> get_perfstatsd_pri_service(); +android::sp<IPerfstatsdPrivate> getPerfstatsdPrivateService(); #endif /* _PERFSTATSD_SERVICE_H_ */ diff --git a/perfstatsd/include/statstype.h b/perfstatsd/include/statstype.h index 151ec604..c00655c6 100644 --- a/perfstatsd/include/statstype.h +++ b/perfstatsd/include/statstype.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,13 +23,13 @@ namespace android { namespace pixel { namespace perfstatsd { -class statstype : public RefBase { +class StatsType : public RefBase { public: virtual void refresh() = 0; virtual void setOptions(const std::string &, const std::string &) = 0; - void dump(std::priority_queue<statsdata, std::vector<statsdata>, StatsdataCompare> *queue) { + void dump(std::priority_queue<StatsData, std::vector<StatsData>, StatsdataCompare> *queue) { std::unique_lock<std::mutex> mlock(mMutex); - std::queue<statsdata> buffer = mBuffer.dump(); + std::queue<StatsData> buffer = mBuffer.dump(); while (!buffer.empty()) { queue->push(buffer.front()); buffer.pop(); @@ -40,12 +40,12 @@ class statstype : public RefBase { size_t bufferCount() { return mBuffer.count(); } protected: - void append(statsdata &&data) { + void append(StatsData &&data) { std::unique_lock<std::mutex> mlock(mMutex); - mBuffer.emplace(std::forward<statsdata>(data)); + mBuffer.emplace(std::forward<StatsData>(data)); } void append(std::chrono::system_clock::time_point &time, std::string &content) { - statsdata data; + StatsData data; data.setTime(time); data.setData(content); append(std::move(data)); diff --git a/perfstatsd/io_usage.cpp b/perfstatsd/io_usage.cpp index c1dff1cc..9585f524 100644 --- a/perfstatsd/io_usage.cpp +++ b/perfstatsd/io_usage.cpp @@ -20,8 +20,8 @@ #include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <android-base/strings.h> +#include <cutils/android_filesystem_config.h> #include <inttypes.h> -#include <private/android_filesystem_config.h> #include <pwd.h> using namespace android::pixel::perfstatsd; diff --git a/perfstatsd/main.cpp b/perfstatsd/main.cpp index e70f5fb8..fc054841 100644 --- a/perfstatsd/main.cpp +++ b/perfstatsd/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,28 +19,23 @@ #include <perfstatsd.h> #include <perfstatsd_service.h> -using namespace android; -using namespace android::base; -using namespace std; -using namespace std::this_thread; - enum MODE { DUMP_HISTORY, SET_OPTION }; -sp<perfstatsd_t> perfstatsd_sp; +android::sp<Perfstatsd> perfstatsdSp; -void *perfstatsd_main(void *) { +void *perfstatsdMain(void *) { LOG_TO(SYSTEM, INFO) << "main thread started"; - perfstatsd_sp = new perfstatsd_t(); + perfstatsdSp = new Perfstatsd(); while (true) { - perfstatsd_sp->refresh(); - perfstatsd_sp->pause(); + perfstatsdSp->refresh(); + perfstatsdSp->pause(); } return NULL; } void help(char **argv) { - string usage = argv[0]; + std::string usage = argv[0]; usage = "Usage: " + usage + " [-s][-d][-o]\n" + "Options:\n" " -s, start as service\n" @@ -51,13 +46,13 @@ void help(char **argv) { } int startService(void) { - pthread_t perfstatsd_main_thread; - errno = pthread_create(&perfstatsd_main_thread, NULL, perfstatsd_main, NULL); + pthread_t perfstatsdMainThread; + errno = pthread_create(&perfstatsdMainThread, NULL, perfstatsdMain, NULL); if (errno != 0) { PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread"; return -1; } else { - pthread_setname_np(perfstatsd_main_thread, "perfstatsd_main"); + pthread_setname_np(perfstatsdMainThread, "perfstatsd_main"); } android::ProcessState::initWithDriver("/dev/vndbinder"); @@ -69,16 +64,16 @@ int startService(void) { LOG_TO(SYSTEM, INFO) << "perfstatsd_pri_service started"; android::ProcessState::self()->startThreadPool(); - IPCThreadState::self()->joinThreadPool(); - pthread_join(perfstatsd_main_thread, NULL); + android::IPCThreadState::self()->joinThreadPool(); + pthread_join(perfstatsdMainThread, NULL); return 0; } -int serviceCall(int mode, const string &key, const string &value) { +int serviceCall(int mode, const std::string &key, const std::string &value) { android::ProcessState::initWithDriver("/dev/vndbinder"); - sp<IPerfstatsdPrivate> perfstatsd_pri_service = get_perfstatsd_pri_service(); - if (perfstatsd_pri_service == NULL) { + android::sp<IPerfstatsdPrivate> perfstatsdPrivateService = getPerfstatsdPrivateService(); + if (perfstatsdPrivateService == NULL) { PLOG_TO(SYSTEM, ERROR) << "Cannot find perfstatsd service."; fprintf(stdout, "Cannot find perfstatsd service.\n"); return -1; @@ -86,9 +81,9 @@ int serviceCall(int mode, const string &key, const string &value) { switch (mode) { case DUMP_HISTORY: { - string history; + std::string history; LOG_TO(SYSTEM, INFO) << "dump perfstats history."; - if (!perfstatsd_pri_service->dumpHistory(&history).isOk() || history.empty()) { + if (!perfstatsdPrivateService->dumpHistory(&history).isOk() || history.empty()) { PLOG_TO(SYSTEM, ERROR) << "perf stats history is not available"; fprintf(stdout, "perf stats history is not available\n"); return -1; @@ -98,9 +93,9 @@ int serviceCall(int mode, const string &key, const string &value) { } case SET_OPTION: LOG_TO(SYSTEM, INFO) << "set option: " << key << " , " << value; - if (!perfstatsd_pri_service - ->setOptions(std::forward<const string>(key), - std::forward<const string>(value)) + if (!perfstatsdPrivateService + ->setOptions(std::forward<const std::string>(key), + std::forward<const std::string>(value)) .isOk()) { PLOG_TO(SYSTEM, ERROR) << "fail to set options"; fprintf(stdout, "fail to set options\n"); @@ -112,7 +107,7 @@ int serviceCall(int mode, const string &key, const string &value) { } int serviceCall(int mode) { - string empty(""); + std::string empty(""); return serviceCall(mode, empty, empty); } @@ -127,8 +122,8 @@ int main(int argc, char **argv) { case 'o': // set options if (argc == 4) { - string key(argv[2]); - string value(argv[3]); + std::string key(argv[2]); + std::string value(argv[3]); return serviceCall(SET_OPTION, std::move(key), std::move(value)); } FALLTHROUGH_INTENDED; diff --git a/perfstatsd/perfstats_buffer.cpp b/perfstatsd/perfstats_buffer.cpp index 73bad91d..07d0bc32 100644 --- a/perfstatsd/perfstats_buffer.cpp +++ b/perfstatsd/perfstats_buffer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,16 +20,16 @@ using namespace android::pixel::perfstatsd; -void PerfstatsBuffer::emplace(statsdata &&data) { +void PerfstatsBuffer::emplace(StatsData &&data) { while (mStorage.size() + 1 > mBufferSize) { if (mStorage.empty()) { break; } mStorage.pop(); } - mStorage.emplace(std::forward<statsdata>(data)); + mStorage.emplace(std::forward<StatsData>(data)); } -const std::queue<statsdata> &PerfstatsBuffer::dump(void) { +const std::queue<StatsData> &PerfstatsBuffer::dump(void) { return mStorage; } diff --git a/perfstatsd/perfstatsd.cpp b/perfstatsd/perfstatsd.cpp index ec98d1c5..c69612cd 100644 --- a/perfstatsd/perfstatsd.cpp +++ b/perfstatsd/perfstatsd.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,33 +20,33 @@ using namespace android::pixel::perfstatsd; -perfstatsd_t::perfstatsd_t(void) { +Perfstatsd::Perfstatsd(void) { mRefreshPeriod = DEFAULT_DATA_COLLECT_PERIOD; - std::unique_ptr<statstype> cpuUsage(new cpu_usage); + std::unique_ptr<StatsType> cpuUsage(new CpuUsage); cpuUsage->setBufferSize(CPU_USAGE_BUFFER_SIZE); mStats.emplace_back(std::move(cpuUsage)); - std::unique_ptr<statstype> ioUsage(new io_usage); + std::unique_ptr<StatsType> ioUsage(new io_usage); ioUsage->setBufferSize(IO_USAGE_BUFFER_SIZE); mStats.emplace_back(std::move(ioUsage)); } -void perfstatsd_t::refresh(void) { +void Perfstatsd::refresh(void) { for (auto const &stats : mStats) { stats->refresh(); } return; } -void perfstatsd_t::get_history(std::string *ret) { - std::priority_queue<statsdata, std::vector<statsdata>, StatsdataCompare> mergedQueue; +void Perfstatsd::getHistory(std::string *ret) { + std::priority_queue<StatsData, std::vector<StatsData>, StatsdataCompare> mergedQueue; for (auto const &stats : mStats) { stats->dump(&mergedQueue); } while (!mergedQueue.empty()) { - statsdata data = mergedQueue.top(); + StatsData data = mergedQueue.top(); auto raw_time = data.getTime(); auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(raw_time); auto d = raw_time - seconds; @@ -68,7 +68,7 @@ void perfstatsd_t::get_history(std::string *ret) { << *ret; } -void perfstatsd_t::setOptions(const std::string &key, const std::string &value) { +void Perfstatsd::setOptions(const std::string &key, const std::string &value) { if (key == PERFSTATSD_PERIOD) { uint32_t val = 0; if (!base::ParseUint(value, &val) || val < 1) { diff --git a/perfstatsd/init-perfstatsd.rc b/perfstatsd/perfstatsd.rc index f112830f..f112830f 100644 --- a/perfstatsd/init-perfstatsd.rc +++ b/perfstatsd/perfstatsd.rc diff --git a/perfstatsd/perfstatsd_service.cpp b/perfstatsd/perfstatsd_service.cpp index 26e96eaa..160b5ab2 100644 --- a/perfstatsd/perfstatsd_service.cpp +++ b/perfstatsd/perfstatsd_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 The Android Open Source Project + * Copyright (C) 2019 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,18 +24,18 @@ android::status_t PerfstatsdPrivateService::start() { } android::binder::Status PerfstatsdPrivateService::dumpHistory(std::string *_aidl_return) { - perfstatsd_sp->get_history(_aidl_return); + perfstatsdSp->getHistory(_aidl_return); return android::binder::Status::ok(); } android::binder::Status PerfstatsdPrivateService::setOptions(const std::string &key, const std::string &value) { - perfstatsd_sp->setOptions(std::forward<const std::string>(key), - std::forward<const std::string>(value)); + perfstatsdSp->setOptions(std::forward<const std::string>(key), + std::forward<const std::string>(value)); return android::binder::Status::ok(); } -android::sp<IPerfstatsdPrivate> get_perfstatsd_pri_service() { +android::sp<IPerfstatsdPrivate> getPerfstatsdPrivateService() { android::sp<android::IServiceManager> sm = android::defaultServiceManager(); if (sm == NULL) return NULL; |