tizen 2.3 release
[framework/system/deviced.git] / src / logd / src / liblogd-db / logd-db.h
1 /*
2  * logd
3  *
4  * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /**
19  * @file logd-db.h
20  *
21  * @desc Contains API to get information about event and processes statistics.
22  */
23
24 #ifndef _LOGD_LOGD_DATA_H_
25 #define _LOGD_LOGD_DATA_H_
26
27 #include <logd.h>
28 #include <time.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 enum logd_socket_req_type {
35         LOGD_UNKNOWN_REQ,
36         LOGD_PROC_STAT_REQ,
37         LOGD_DEV_STAT_REQ,
38         LOGD_BATTERY_LVL_REQ,
39         LOGD_EST_TIME_REQ,
40 };
41
42 struct logd_device_stat {
43         /** Device identifier */
44         enum logd_object type;
45         /** total power consumed by device in uAh */
46         float power_cons;
47         /** current power consumption in mA */
48         float cur_power_cons;
49 };
50
51 struct logd_event_info {
52         /** Object - 1st parameter in <CODE>logd_event</CODE> function*/
53         enum logd_object object;
54         /** Action - 2nd parameter in <CODE>logd_event</CODE> function*/
55         enum logd_action action;
56         /** Time when event was created */
57         uint64_t time;
58         /** Full path to application that created event */
59         char *application;
60         /** Message of event. 3rd parameter in <CODE>logd_event</CODE> function */
61         char *message;
62 };
63
64 struct logd_proc_stat {
65         /**
66          * Full path to application. If it starts from '[' then it's
67          * kernel thread
68          */
69         char *application;
70         uint64_t utime; /** User CPU time in msec */
71         uint64_t stime; /** System CPU time in msec */
72         float utime_power_cons, stime_power_cons;
73         int is_active; /* application is running now */
74         /*
75          * Persentage from CPU time of all applications.
76          * persentage = (utime + stime) / (all_app_utime + all_app_stime)
77          */
78         float percentage;
79 };
80
81
82 struct logd_events_filter {
83         /** Start process events from that time in unixtime format */
84         uint64_t from;
85         /** Process events till that time in unixtime format */
86         uint64_t to;
87         /**
88          * Mask that describes what events process. If you want to load only
89          * event whete action is <CODE>LOGD_WIFI</CODE> or
90          * <CODE>LOGD_DISPLAY</CODE> you must assign:
91          * @code
92          * filter.actions_mask[LOGD_WIFI] = 1;
93          * filter.actions_mask[LOGD_DISPLAY] = 1;
94          * @endcode
95          */
96         int actions_mask[LOGD_ACTION_MAX];
97         /** Is's like <CODE>actions_mask</CODE> but for object */
98         int objects_mask[LOGD_OBJECT_MAX];
99 };
100
101 enum logd_db_query {
102         LOGD_DB_QUERY_STOP,
103         LOGD_DB_QUERY_CONTINUE,
104 };
105
106 /**
107  * Through events that database contains.
108  *
109  * @param filter give ability to clarify parameters of events that you want
110  * to process. If NULL then process all event from database.
111  * @param cb pointer to function that will call for each event.
112  * @param user_data any data that must be given to <CODE>cb</CODE>. May be NULL.
113  *
114  * @return On succes, 0. On error, negative value.
115  *
116  * Example usage:
117  * @code
118  * enum logd_db_query cb(const struct logd_event_info *event, void *user_data)
119  * {
120  *      struct tm st;
121  *      time_t time_sec;
122  *      char timestr[200];
123  *
124  *      time_sec = event->time;
125  *      localtime_r(&time_sec, &st);
126  *      strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", &st);
127  *      printf("%s %s %s\n", event->application, timestr, event->message);
128  *
129  *      return LOGD_DB_QUERY_CONTINUE;
130  * }
131  *
132  * logd_foreach_events(&filter, &cb, NULL);
133  * @endcode
134  */
135 int logd_foreach_events(const struct logd_events_filter *filter,
136         enum logd_db_query (*cb)(const struct logd_event_info *, void *), void *user_data);
137
138 /**
139  * Through precess statistics. Statistics counted start from logd_grabber
140  * started (usually starts from target's switch on).
141  *
142  * @param cb pointer to function that will call for each process.
143  * @param user_data any data that must be given to <CODE>cb</CODE>. May be NULL.
144  *
145  * @return On succes, 0. On error, negative value.
146  *
147  * Example usage like <CODE>logd_foreach_events</CODE>.
148  */
149 int logd_foreach_proc_stat(enum logd_db_query (*cb) (const struct logd_proc_stat *, void *),
150         void *user_data);
151
152 /**
153  * Through devices statistics.
154  *
155  * @param cb pointer to function that will call for each device.
156  * (Now implemented only for backlight - display.
157  * @param user_data any data that must be given to <CODE>cb</CODE>. May be NULL.
158  *
159  * @return On succes, 0. On error, negative value.
160  *
161  * Example usage like <CODE>logd_foreach_events</CODE>.
162  */
163 int logd_foreach_devices_stat(enum logd_db_query (*cb)
164          (const struct logd_device_stat *, void *), void *user_data);
165
166
167 struct logd_battery_level {
168         time_t date;
169         int level;
170         float k; /** Speed of charging (may be negative) */
171 };
172
173 struct logd_battery_info {
174         struct logd_battery_level *levels;
175         int n;
176 };
177
178 /**
179  * Return information about battery level. Returned object can be provided in
180  * logd_battery_level_at to get battery level at exact time.
181  *
182  * @return On success <CODE>struct logd_battery_info</CODE> object. On error, NULL.
183  * Returned object must be released by <CODE>logd_free_battery_info</CODE>.
184  */
185 struct logd_battery_info* logd_get_battery_info(void);
186
187 /**
188  * Release memory used by <CODE>info</CODE>.
189  *
190  * @param info relased object.
191  */
192 void logd_free_battery_info(struct logd_battery_info *info);
193
194 /**
195  * Return index when info->levels[index].level == level.
196  *
197  * @param info object returned by <CODE>logd_get_battery_info</CODE>.
198  * @param level the battery level index of what you want to find.
199  *
200  * @return index of info->levels. Great then 0 if the level was found,
201  * 0 if not found, less then 0 in error case. Max level is 10000 - 100%.
202  */
203 int logd_seek_battery_level(struct logd_battery_info *info, int level);
204
205 /**
206  * Provide battery level at certain time. Max level is 10000 - 100%.
207  *
208  * @param info object returned by <CODE>logd_get_battery_info</CODE>.
209  * @param date battery level at that time will be returned.
210  *
211  * @return battery level in percentage.
212  */
213 float logd_battery_level_at(const struct logd_battery_info *info, time_t date);
214
215 /**
216  * Provide battery charging/discharging speed at certain time.
217  *
218  * @param info object returned by <CODE>logd_get_battery_info</CODE>.
219  * @param date battery charging speed at that time will be returned.
220  *
221  * @return battery charging/discharging (if value is negative) in persents/sec.
222  */
223 float logd_battery_charging_speed_at(const struct logd_battery_info *info, time_t date);
224
225 /**
226  * Provide info about estimate remaining battery lifetime for each
227  * available power mode from <CODE>logd_power_mode</CODE>.
228  *
229  * @param estimate_times pointer to int pointer that will cantains
230  * estimate time for all power modes. It has to be released by
231  * <CODE>free</CODE> if functions terminated successfully.
232  *
233  * @return On succes, 0. On error, negative value.
234  *
235  * Example usage:
236  * @code
237  * int *estimate_times = NULL;
238  * if (logd_get_estimate_battery_lifetime(&estimate_times) < 0) {
239  *      puts("error");
240  *      return -1;
241  * }
242  * for (int i = 0; i < LOGD_POWER_MODE_MAX; ++i)
243  *      printf("%d\n", estimate_times[i]);
244  * free(estimate_times);
245  * @endcode
246  */
247 int logd_get_estimate_battery_lifetime(int **estimate_times);
248
249 /**
250  * Provide information about energy efficiency for all launched applications.
251  * power_efficiency = power_cons / time * 3600 (how much energy will used
252  * in a hour by that application. More is worse.
253  *
254  * @param cb a callback that will called for all applications. It receive
255  * three parameters: path to the binary, energy efficiency for that binary
256  * and any user data (see next parameter). Power efficiency can be computed
257  * only when the application has running long enough (more then 2 hour in
258  * the current realization). If it's less then 2 hours then efficiency will
259  * be less then 0.
260  *
261  * @param user_data any data that will be given to <CODE>cb</CODE>. May be NULL.
262  *
263  * @return On succes, 0. On error, negative value.
264  */
265 int logd_foreach_apps_energy_efficiency(enum logd_db_query (*cb)
266         (const char *application, float efficiency, void *user_data), void *user_data);
267 #ifdef __cplusplus
268 }
269 #endif
270
271 #endif /* _LOGD_LOGD_DATA_H_ */