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