[FIX] wrong pack size of system memory total/used
[platform/core/system/swap-manager.git] / daemon / da_protocol.h
1 /*
2  *  DA manager
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  *
8  * Cherepanov Vitaliy <v.cherepanov@samsung.com>
9  * Nikita Kalyazin    <n.kalyazin@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Contributors:
24  * - Samsung RnD Institute Russia
25  *
26  */
27
28
29 #ifndef _DA_PROTOCOL_
30 #define _DA_PROTOCOL_
31
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <linux/input.h>
37
38 enum HostMessageT {
39 NMSG_KEEP_ALIVE                 =0x0001,
40 NMSG_START                              =0x0002,
41 NMSG_STOP                               =0x0003,
42 NMSG_CONFIG                             =0x0004,
43 NMSG_BINARY_INFO                =0x0005,
44 NMSG_GET_TARGET_INFO    =0x0007,
45 NMSG_SWAP_INST_ADD              =0x0008,
46 NMSG_SWAP_INST_REMOVE   =0x0009,
47
48 NMSG_KEEP_ALIVE_ACK                     =0x1001,
49 NMSG_START_ACK                          =0x1002,
50 NMSG_STOP_ACK                           =0x1003,
51 NMSG_CONFIG_ACK                         =0x1004,
52 NMSG_BINARY_INFO_ACK            =0x1005,
53 NMSG_SWAP_INST_ACK                      =0x1006,
54 NMSG_GET_TARGET_INFO_ACK        =0x1007,
55 NMSG_SWAP_INST_ADD_ACK          =0x1008,
56 NMSG_SWAP_INST_REMOVE_ACK       =0x1009,
57
58 NMSG_PROCESS_INFO                       =0x0001,        //      target process info
59 NMSG_TERMINATE                          =0x0002,        //terminate
60 NMSG_ERROR                                      =0x0003,        //error message
61 NMSG_SAMPLE                                     =0x0004,        //N     10ms
62 NMSG_SYSTEM                                     =0x0005,        //N     10~1000ms       DaData, start sending immediately after start message from host, first system message time is tracing start time
63 NMSG_IMAGE                                      =0x0006,        //N     irregular       image
64 NMSG_RECORD                                     =0x0007,        //N     irregular       replay event
65 NMSG_FUNCTION_ENTRY                     =0x0008,        //N     irregular       swap instrumentation, Instrumented functions by AppInst and LibInst
66 NMSG_FUNCTION_EXIT                      =0x0009,        //N     irregular       swap instrumentation, Instrumented functions by AppInst and LibInst
67 NMSG_CONTEXT_SWITCH_ENTRY       =0x0010,        //N     irregular       swap instrumentation for kernel
68 NMSG_CONTEXT_SWITCH_EXIT        =0x0011,        //N     irregular       swap instrumentation for kernel
69
70 NMSG_PROBE                                      =0x0100,        //N     irregular       resource log
71 NMSG_PROBE_MEMORY                       =0x0101,        //N     irregular       resource log
72 NMSG_PROBE_UICONTROL            =0x0102,        //N     irregular       resource log
73 NMSG_PROBE_UIEVENT                      =0x0103,        //N     irregular       resource log
74 NMSG_PROBE_RESOURCE                     =0x0104,        //N     irregular       resource log
75 NMSG_PROBE_LIFECYCLE            =0x0105,        //N     irregular       resource log
76 NMSG_PROBE_SCREENSHOT           =0x0106,        //N     irregular       resource log
77 NMSG_PROBE_SCENE                        =0x0107,        //N     irregular       resource log
78 NMSG_PROBE_THREAD                       =0x0108,        //N     irregular       resource log
79 NMSG_PROBE_CUSTOM                       =0x0109,        //N     irregular       resource log
80 NMSG_PROBE_SYNC                         =0x0110 //N     irregular       resource log
81
82
83 };
84 #define MSG_MAX_NUM NMSG_SWAP_INST_REMOVE
85
86 enum ErrorCode{
87         ERR_NO                                                                  =0,             //success
88         ERR_LOCKFILE_CREATE_FAILED                              =-101,  //lock file create failed
89         ERR_ALREADY_RUNNING                                             =-102,  //already running
90         ERR_INITIALIZE_SYSTEM_INFO_FAILED               =-103,  //initialize system info failed
91         ERR_HOST_SERVER_SOCKET_CREATE_FAILED    =-104,  //host server socket create failed
92         ERR_TARGET_SERVER_SOCKET_CREATE_FAILED  =-105,  //target server socket create failed
93
94         ERR_SIGNAL_MASK_SETTING_FAILED                  =-106, //TODO del (old parametr)
95
96         ERR_WRONG_MESSAGE_FORMAT                                =-201,  //wrong message format
97         ERR_WRONG_MESSAGE_TYPE                                  =-202,  //wrong message type
98         ERR_WRONG_MESSAGE_DATA                                  =-203,  //wrong message data
99         ERR_CANNOT_START_PROFILING                              =-204,  //cannot start profiling
100         ERR_SERV_SOCK_CREATE                                    =-900,  //server socket creation failed (written in /tmp/da.port file)
101         ERR_SERV_SOCK_BIND                                              =-901,  //server socket bind failed (written in /tmp/da.port file)
102         ERR_SERV_SOCK_LISTEN                                    =-902,  //server socket listen failed (written in /tmp/da.port file)
103         ERR_UNKNOWN                                                     =-999   //unknown error
104 };
105
106 enum feature_code{
107         FL_RESERVED1                = 0x00000001,
108         FL_RESERVED2                = 0x00000002,
109         FL_FUNCTION_PROFILING       = 0x00000004, //On/Off the UserSpaceInst
110         FL_MEMORY_ALLCATION_PROBING = 0x00000008, //memory allocation API (glibc)
111         FL_FILE_API_PROBING         = 0x00000010, //file API (glibc, OSP)
112         FL_THREAD_API_PROBING       = 0x00000020, //thread API (glibc, OSP)
113         FL_OSP_UI_API_PROBING       = 0x00000040, //UI API (OSP)
114         FL_SCREENSHOT               = 0x00000080, //Screenshot
115         FL_USER_EVENT               = 0x00000100, //events of Touch, Gesture, Orientation, Key
116         FL_RECORDING                = 0x00000200, //recording the user event
117         FL_SYSTCALL_FILE            = 0x00000400, //File operation syscalls tracing
118         FL_SYSTCALL_IPC             = 0x00000800, //IPC syscall tracing
119         FL_SYSTCALL_PROCESS         = 0x00001000, //Process syscalls tracing
120         FL_SYSTCALL_SIGNAL          = 0x00002000, //Signal syscalls tracing
121         FL_SYSTCALL_NETWORK         = 0x00004000, //Network syscalls tracing
122         FL_SYSTCALL_DESC            = 0x00008000, //Descriptor syscalls tracing
123         FL_CONTEXT_SWITCH           = 0x00010000, //Context switch tracing
124         FL_NETWORK_API_PROBING      = 0x00020000, //network API (glibc, OSP, libsoap, openssl)
125         FL_OPENGL_API_PROBING       = 0x00040000, //openGL API
126         FL_FUNCTION_SAMPLING        = 0x00080000, //Function sampling
127         FL_CPU                      = 0x00100000, //CPU core load, frequency
128         FL_PROCESSES                = 0x00200000, //Process load
129         FL_MEMORY                   = 0x00400000, //Process size(VSS, PSS. RSS), heap usage(application, library), physical memory in use
130         FL_DISK                     = 0x00800000,
131         FL_NETWORK                  = 0x01000000,
132         FL_DEVICE                   = 0x02000000,
133         FL_ENERGY                   = 0x04000000,
134         FL_RESERVED3                = 0x08000000,
135
136         FL_ALL_FEATURES             = 0xFFFFFFFF &
137                                       (~FL_RESERVED1) &
138                                       (~FL_RESERVED2) &
139                                       (~FL_RESERVED3)
140 };
141
142 #define IS_OPT_SET_IN(OPT, reg) (reg & (OPT))
143 #define IS_OPT_SET(OPT) IS_OPT_SET_IN((OPT), prof_session.conf.use_features0)
144 #define IS_SYSTEM_INFO_NEEDED() (                                       \
145                 IS_OPT_SET(FL_CPU) ||                                   \
146                 IS_OPT_SET(FL_PROCESSES) ||                             \
147                 IS_OPT_SET(FL_MEMORY) ||                                \
148                 IS_OPT_SET(FL_DISK) ||                                  \
149                 IS_OPT_SET(FL_NETWORK) ||                               \
150                 IS_OPT_SET(FL_DEVICE) ||                                \
151                 IS_OPT_SET(FL_ENERGY))
152
153 enum app_type {
154         AT_TIZEN        =0x01,
155         AT_LAUNCHED     =0x02,
156         AT_COMMON       =0x03
157 };
158 enum supported_device {
159   DEVICE_FLASH,
160   DEVICE_CPU
161 };
162 static const char *supported_devices_strings[] = {
163         "FLASH",
164         "CPU"
165 };
166 #define array_size(x) (sizeof(x)/sizeof((x)[0]))
167 enum { supported_devices_count = array_size(supported_devices_strings) };
168
169 #define MAX_FILENAME 128
170
171 #define MSG_DATA_HDR_LEN 20
172 struct msg_data_t {
173         uint32_t id;
174         uint32_t seq_num;
175         uint32_t sec;
176         uint32_t nsec;
177         uint32_t len;
178         char payload[0];
179 };
180
181 #define MSG_CMD_HDR_LEN 8
182 //conf
183 struct msg_buf_t {
184         char *payload;
185         char *cur_pos;
186         char *end;
187         uint32_t len;
188 };
189
190 struct msg_t {
191         uint32_t id;
192         uint32_t len;
193         char payload[0];
194 };
195
196
197 struct conf_t {
198         uint64_t use_features0;
199         uint64_t use_features1;
200         uint32_t system_trace_period;
201         uint32_t data_message_period;
202 };
203
204 typedef uint32_t log_interval_t;
205
206 //app, libs, probes
207 enum app_type_t {
208         APP_TYPE_TIZEN = 1,
209         APP_TYPE_RUNNING = 2,
210         APP_TYPE_COMMON = 3,
211 };
212
213 struct app_info_t {
214         uint32_t app_type;
215         char *app_id;
216         char *exe_path;
217 };
218
219
220
221 struct us_func_inst_plane_t {
222         uint64_t func_addr;
223         char args[0];
224 };
225
226 struct us_lib_inst_t {
227         char *bin_path;
228 };
229
230 struct user_space_inst_t {
231         uint32_t app_num;
232         struct app_list_t *app_inst_list;
233         uint32_t lib_num;
234         struct lib_list_t *lib_inst_list;
235 };
236
237 //replays
238 struct replay_event_t {
239         uint32_t id;
240         struct input_event ev;
241 };
242
243 struct replay_event_seq_t {
244         uint32_t enabled;
245         struct timeval tv;
246         uint32_t event_num;
247         struct replay_event_t *events;
248 };
249
250 struct prof_session_t {
251         struct conf_t conf;
252         struct user_space_inst_t user_space_inst;
253         struct replay_event_seq_t replay_event_seq;
254         unsigned running_status:1; // to stop properly (1 - it is running, 0 - no)
255 };
256
257 int parseHostMessage(struct msg_t *log, char *msg);
258 int host_message_handler(struct msg_t *msg);
259
260 char *msg_ID_str(enum HostMessageT ID);
261
262 // testing
263 #include <sys/stat.h>
264 #include <fcntl.h>
265 #include <unistd.h>
266
267 //data protocol
268 struct thread_info_t {
269         uint32_t pid;
270         float load;
271 };
272
273 struct process_info_t {
274         uint32_t id;
275         float load;
276 };
277
278 struct system_info_t {
279         uint32_t energy;
280         uint32_t wifi_status;
281         uint32_t bt_status;
282         uint32_t gps_status;
283         uint32_t brightness_status;
284         uint32_t camera_status;
285         uint32_t sound_status;
286         uint32_t audio_status;
287         uint32_t vibration_status;
288         uint32_t voltage_status;
289         uint32_t rssi_status;
290         uint32_t video_status;
291         uint32_t call_status;
292         uint32_t dnet_status;
293         float *cpu_frequency;
294         float app_cpu_usage;
295         float *cpu_load;
296         uint32_t virtual_memory;
297         uint32_t resident_memory;
298         uint32_t shared_memory;
299         uint32_t pss_memory;
300         uint32_t total_alloc_size;
301         uint64_t system_memory_total;
302         uint64_t system_memory_used;
303         uint32_t total_used_drive;
304         uint32_t count_of_threads;
305         struct thread_info_t *thread_load;
306         uint32_t count_of_processes;
307         struct process_info_t *process_load;
308         uint32_t disk_reads;
309         uint32_t disk_sectors_read;
310         uint32_t disk_writes;
311         uint32_t disk_sectors_write;
312         uint32_t network_send_size;
313         uint32_t network_receive_size;
314         uint32_t energy_per_device[supported_devices_count];
315         uint32_t app_energy_per_device[supported_devices_count];
316 };
317
318 struct recorded_event_t {
319         uint32_t id;
320         uint32_t type;
321         uint32_t code;
322         uint32_t value;
323 };
324
325 #define pack_int(to, n)                                 \
326         do {                                            \
327                 *(typeof(n) *) to = n;                  \
328                 to += sizeof(typeof(n));                \
329         } while (0)
330
331 #define pack_int64(to, n)                                       \
332         do {                                                    \
333                 *(uint64_t *)to = n;                            \
334                 to += sizeof(uint64_t);                         \
335         } while (0)
336
337 #define pack_int32(to, n)                                       \
338         do {                                                    \
339                 *(uint32_t *)to = n;                            \
340                 to += sizeof(uint32_t);                         \
341         } while (0)
342
343 #define pack_time(to, n)                                                \
344         do {                                                            \
345                 pack_int32(to, n.tv_sec);                               \
346                 pack_int32(to, n.tv_usec);                              \
347         } while (0)
348
349 #define pack_float(to, n)                                       \
350         do {                                                    \
351                 *(float *)to = n;                               \
352                 to += sizeof(float);                            \
353         } while (0)
354
355 #define pack_str(to, n)                         \
356         do {                                    \
357                 memcpy(to, n, strlen(n) + 1);   \
358                 to += strlen(n) + 1;            \
359         } while (0)
360
361 static inline void* pack_str_array(void *buffer, const char **strings,
362                                    size_t count)
363 {
364         int index;
365         for (index = 0; index != count; ++index)
366                 pack_str(buffer, strings[index]);
367         return buffer;
368 }
369
370 struct msg_data_t *pack_system_info(struct system_info_t *sys_info);
371 int write_to_buf(struct msg_data_t *msg);
372 void free_msg_data(struct msg_data_t *msg);
373 void free_msg_payload(struct msg_t *msg);
374 void free_sys_info(struct system_info_t *sys_info);
375 int start_replay(void);
376 void stop_replay(void);
377
378 enum ErrorCode stop_all(void);
379 enum ErrorCode stop_all_no_lock(void);
380 int stop_all_in_process(void);
381 void stop_all_done(void);
382
383 void reset_msg(struct msg_t *msg);
384 void reset_replay_event_seq(struct replay_event_seq_t *res);
385 void reset_system_info(struct system_info_t *sys);
386 int check_running_status(struct prof_session_t *prof_session);
387
388 extern struct prof_session_t prof_session;
389
390 //debugs
391 void print_replay_event(struct replay_event_t *ev, uint32_t num, char *tab);
392
393 int sendACKToHost(enum HostMessageT resp, enum ErrorCode err_code,
394                         char *payload, int payload_size);
395
396 int parse_int32(struct msg_buf_t *msg, uint32_t *val);
397 int parse_int64(struct msg_buf_t *msg, uint64_t *val);
398 int parse_string(struct msg_buf_t *msg, char **str);
399 int parse_string_no_alloc(struct msg_buf_t *msg, char *str);
400 int parse_replay_event_seq(struct msg_buf_t *msg, struct replay_event_seq_t *res);
401
402 void init_prof_session(struct prof_session_t *prof_session);
403 #endif /* _DA_PROTOCOL_ */