tizen 2.3 release
[framework/system/deviced.git] / src / logd / src / battery / power-monitor.cpp
1 #include <algorithm>
2 #include <logd.h>
3 #include <logd-db.h>
4 #include <iostream>
5 #include <map>
6 #include <string>
7 #include <stdlib.h>
8 #include <vector>
9 #include <string.h>
10 #include <unistd.h>
11 #include <time.h>
12
13 using namespace std;
14
15 static uint64_t getTimeUSec()
16 {
17         struct timespec ts;
18         clock_gettime(CLOCK_MONOTONIC, &ts);
19         return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
20 }
21
22 struct Stat {
23         string appid;
24         float totalCons; /* uAh */
25         float currentCons; /* mA */
26 };
27
28 static vector<Stat> newStat;
29 static map<string, Stat> oldStat;
30
31 static enum logd_db_query proc_stat_cb(const struct logd_proc_stat *proc_stat, void *user_data)
32 {
33         Stat stat;
34
35         stat.totalCons = proc_stat->stime_power_cons + proc_stat->utime_power_cons;
36         stat.appid = proc_stat->application;
37         newStat.push_back(stat);
38
39
40         return LOGD_DB_QUERY_CONTINUE;
41 }
42
43 int main(int argc, char **argv)
44 {
45         float totalCurrent = 0;
46         float total = 0;
47         uint64_t lastTime = 0;
48
49         while (1) {
50                 uint64_t curTime = getTimeUSec();
51                 newStat.clear();
52                 logd_foreach_proc_stat(&proc_stat_cb, NULL);
53                 totalCurrent = total = 0;
54
55                 for (auto it = newStat.begin(); it != newStat.end(); ++it) {
56                         it->currentCons = 0;
57                         if (oldStat.count(it->appid)) {
58                                 total += it->totalCons;
59                                 it->currentCons =
60                                         (it->totalCons - oldStat[it->appid].totalCons) * 3.6 /
61                                         (curTime - lastTime) * 1000;
62                                 totalCurrent += it->currentCons;
63                         }
64                 }
65                 oldStat.clear();
66                 lastTime = curTime;
67
68                 sort(newStat.begin(), newStat.end(),
69                         [] (Stat lhs, Stat rhs)
70                         {
71                                 return lhs.currentCons > rhs.currentCons;
72                         });
73
74
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;
82                 }
83                 printf("\n%-50.50s %15.2f  %15.4f\n", "Total", total, totalCurrent);
84                 sleep(1);
85                 printf("\033[2J\033[1;1H");
86         }
87 }