15 static uint64_t getTimeUSec()
18 clock_gettime(CLOCK_MONOTONIC, &ts);
19 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
24 float totalCons; /* uAh */
25 float currentCons; /* mA */
28 static vector<Stat> newStat;
29 static map<string, Stat> oldStat;
31 static enum logd_db_query proc_stat_cb(const struct logd_proc_stat *proc_stat, void *user_data)
35 stat.totalCons = proc_stat->stime_power_cons + proc_stat->utime_power_cons;
36 stat.appid = proc_stat->application;
37 newStat.push_back(stat);
40 return LOGD_DB_QUERY_CONTINUE;
43 int main(int argc, char **argv)
45 float totalCurrent = 0;
47 uint64_t lastTime = 0;
50 uint64_t curTime = getTimeUSec();
52 logd_foreach_proc_stat(&proc_stat_cb, NULL);
53 totalCurrent = total = 0;
55 for (auto it = newStat.begin(); it != newStat.end(); ++it) {
57 if (oldStat.count(it->appid)) {
58 total += it->totalCons;
60 (it->totalCons - oldStat[it->appid].totalCons) * 3.6 /
61 (curTime - lastTime) * 1000;
62 totalCurrent += it->currentCons;
68 sort(newStat.begin(), newStat.end(),
69 [] (Stat lhs, Stat rhs)
71 return lhs.currentCons > rhs.currentCons;
75 printf("%-50.50s %15s %15s\n", "Application", "power cons, uah", "current, mA");
76 auto it = newStat.begin();
77 for (size_t i = 0; i < newStat.size(); ++i, ++it) {
78 if (i < 20 && totalCurrent)
79 printf("%-50.50s %15.2f %15.2f (%.2f%%)\n", it->appid.c_str(),
80 it->totalCons, it->currentCons, it->currentCons / totalCurrent * 100);
81 oldStat[it->appid] = *it;
83 printf("\n%-50.50s %15.2f %15.4f\n", "Total", total, totalCurrent);
85 printf("\033[2J\033[1;1H");