[FEATURE] Implement kernel -> user connection
[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 #define MSG_MAX_NUM NMSG_SWAP_INST_REMOVE
72
73 enum ErrorCode{
74         ERR_NO                                                                  =0,             //success
75         ERR_LOCKFILE_CREATE_FAILED                              =-101,  //lock file create failed
76         ERR_ALREADY_RUNNING                                             =-102,  //already running
77         ERR_INITIALIZE_SYSTEM_INFO_FAILED               =-103,  //initialize system info failed
78         ERR_HOST_SERVER_SOCKET_CREATE_FAILED    =-104,  //host server socket create failed
79         ERR_TARGET_SERVER_SOCKET_CREATE_FAILED  =-105,  //target server socket create failed
80
81         ERR_SIGNAL_MASK_SETTING_FAILED                  =-106, //TODO del (old parametr)
82
83         ERR_WRONG_MESSAGE_FORMAT                                =-201,  //wrong message format
84         ERR_WRONG_MESSAGE_TYPE                                  =-202,  //wrong message type
85         ERR_WRONG_MESSAGE_DATA                                  =-203,  //wrong message data
86         ERR_CANNOT_START_PROFILING                              =-204,  //cannot start profiling
87         ERR_SERV_SOCK_CREATE                                    =-900,  //server socket creation failed (written in /tmp/da.port file)
88         ERR_SERV_SOCK_BIND                                              =-901,  //server socket bind failed (written in /tmp/da.port file)
89         ERR_SERV_SOCK_LISTEN                                    =-902,  //server socket listen failed (written in /tmp/da.port file)
90         ERR_UNKNOWN                                                     =-999   //unknown error
91 };
92
93 enum feature_code{
94         FL_RESERVED1                    = 0x000000001,
95         FL_RESERVED2                    = 0x000000002,
96         FL_FUNCTION_PROFILING           = 0x000000004, //On/Off the UserSpaceInst
97         FL_MEMORY_ALLOC_PROBING         = 0x000000008, //memory allocation API (glibc)
98         FL_FILE_API_PROBING             = 0x000000010, //file API (glibc, OSP)
99         FL_THREAD_API_PROBING           = 0x000000020, //thread API (glibc, OSP)
100         FL_OSP_UI_API_PROBING           = 0x000000040, //UI API (OSP)
101         FL_SCREENSHOT                   = 0x000000080, //Screenshot
102         FL_USER_EVENT                   = 0x000000100, //events of Touch, Gesture, Orientation, Key
103         FL_RECORDING                    = 0x000000200, //recording the user event
104         FL_SYSTCALL_FILE                = 0x000000400, //File operation syscalls tracing
105         FL_SYSTCALL_IPC                 = 0x000000800, //IPC syscall tracing
106         FL_SYSTCALL_PROCESS             = 0x000001000, //Process syscalls tracing
107         FL_SYSTCALL_SIGNAL              = 0x000002000, //Signal syscalls tracing
108         FL_SYSTCALL_NETWORK             = 0x000004000, //Network syscalls tracing
109         FL_SYSTCALL_DESC                = 0x000008000, //Descriptor syscalls tracing
110         FL_CONTEXT_SWITCH               = 0x000010000, //Context switch tracing
111         FL_NETWORK_API_PROBING          = 0x000020000, //network API (glibc, OSP, libsoap, openssl)
112         FL_OPENGL_API_PROBING           = 0x000040000, //openGL API
113         FL_FUNCTION_SAMPLING            = 0x000080000, //Function sampling
114         FL_CPU                          = 0x000100000, //CPU core load, frequency
115         FL_PROCESSES                    = 0x000200000, //Process load
116         FL_MEMORY                       = 0x000400000, //Process size(VSS, PSS. RSS), heap usage(application, library), physical memory in use
117         FL_DISK                         = 0x000800000,
118         FL_NETWORK                      = 0x001000000,
119         FL_DEVICE                       = 0x002000000,
120         FL_ENERGY                       = 0x004000000,
121         FL_RESERVED3                    = 0x008000000,
122         FL_MEMORY_ALLOC_ALWAYS_PROBING  = 0x010000000, //memory allocation API (glibc) always
123         FL_FILE_API_ALWAYS_PROBING      = 0x020000000, //file API (glibc, OSP) always
124         FL_THREAD_API_ALWAYS_PROBING    = 0x040000000, //thread API (glibc, OSP) always
125         FL_OSP_UI_API_ALWAYS_PROBING    = 0x080000000, //UI API (OSP) always
126         FL_NETWORK_API_ALWAYS_PROBING   = 0x100000000ULL, //network API (glibc, OSP, libsoap, openssl) always
127         FL_OPENGL_API_ALWAYS_PROBING    = 0x200000000ULL, //openGL API always
128
129         FL_ALL_FEATURES                 = 0x3FFFFFFFFULL &
130                                         (~FL_RESERVED1) &
131                                         (~FL_RESERVED2) &
132                                         (~FL_RESERVED3)
133 };
134
135 #define IS_OPT_SET_IN(OPT, reg) (reg & (OPT))
136 #define IS_OPT_SET(OPT) IS_OPT_SET_IN((OPT), prof_session.conf.use_features0)
137
138 enum app_type {
139         AT_TIZEN        =0x01,
140         AT_LAUNCHED     =0x02,
141         AT_COMMON       =0x03
142 };
143 enum supported_device {
144         DEVICE_FLASH,
145         DEVICE_CPU,
146         DEVICE_LCD
147 };
148 static const char *supported_devices_strings[] = {
149         "FLASH",
150         "CPU",
151         "LCD"
152 };
153 #define array_size(x) (sizeof(x)/sizeof((x)[0]))
154 enum { supported_devices_count = array_size(supported_devices_strings) };
155
156 #define MAX_FILENAME 128
157
158 #define MSG_DATA_HDR_LEN 20
159 struct msg_data_t {
160         uint32_t id;
161         uint32_t seq_num;
162         uint32_t sec;
163         uint32_t nsec;
164         uint32_t len;
165         char payload[0];
166 };
167
168 #define MSG_CMD_HDR_LEN 8
169 //conf
170 struct msg_buf_t {
171         char *payload;
172         char *cur_pos;
173         char *end;
174         uint32_t len;
175 };
176
177 struct msg_t {
178         uint32_t id;
179         uint32_t len;
180         char payload[0];
181 };
182
183
184 struct conf_t {
185         uint64_t use_features0;
186         uint64_t use_features1;
187         uint32_t system_trace_period;
188         uint32_t data_message_period;
189 };
190
191 typedef uint32_t log_interval_t;
192
193 //app, libs, probes
194 enum app_type_t {
195         APP_TYPE_TIZEN = 1,
196         APP_TYPE_RUNNING = 2,
197         APP_TYPE_COMMON = 3,
198 };
199
200 struct app_info_t {
201         uint32_t app_type;
202         char *app_id;
203         char *exe_path;
204 };
205
206
207
208 struct us_func_inst_plane_t {
209         //format
210         //name       | type   | len       | info
211         //------------------------------------------
212         //func_addr  | uint64 | 8         |
213         //args       | string | len(args) |end with '\0'
214         //ret_type   | char   | 1         |
215         uint64_t func_addr;
216         char args[0];
217 };
218
219 struct us_lib_inst_t {
220         char *bin_path;
221 };
222
223 struct user_space_inst_t {
224         uint32_t app_num;
225         struct app_list_t *app_inst_list;
226         uint32_t lib_num;
227         struct lib_list_t *lib_inst_list;
228 };
229
230 //replays
231 struct replay_event_t {
232         uint32_t id;
233         struct input_event ev;
234 };
235
236 struct replay_event_seq_t {
237         uint32_t enabled;
238         struct timeval tv;
239         uint32_t event_num;
240         struct replay_event_t *events;
241 };
242
243 struct prof_session_t {
244         struct conf_t conf;
245         struct user_space_inst_t user_space_inst;
246         struct replay_event_seq_t replay_event_seq;
247         unsigned running_status:1; // to stop properly (1 - it is running, 0 - no)
248 };
249
250 int parseHostMessage(struct msg_t *log, char *msg);
251 int host_message_handler(struct msg_t *msg);
252
253 char *msg_ID_str(enum HostMessageT ID);
254
255 // testing
256 #include <sys/stat.h>
257 #include <fcntl.h>
258 #include <unistd.h>
259
260 //data protocol
261 struct thread_info_t {
262         uint32_t pid;
263         float load;
264 };
265
266 struct process_info_t {
267         uint32_t id;
268         float load;
269 };
270
271 /**
272  * NOTE:
273  *
274  * Adding pointer memebers to struct %system_info_t REQUIRES updating code,
275  * calculating length of bytevector, suitable for serializing it. See
276  * msg_data_payload_length()
277  *
278  */
279 struct system_info_t {
280         // system_cpu
281         float app_cpu_usage;
282         float *cpu_frequency;
283         float *cpu_load;
284         uint32_t count_of_threads;
285         struct thread_info_t *thread_load;
286
287         // system_processes
288         uint32_t count_of_processes;
289         struct process_info_t *process_load;
290
291         // system_memory
292         uint32_t virtual_memory;
293         uint32_t resident_memory;
294         uint32_t shared_memory;
295         uint32_t pss_memory;
296         uint64_t total_alloc_size;
297         uint64_t system_memory_total;
298         uint64_t system_memory_used;
299
300         // system_disk
301         uint32_t total_used_drive;
302         uint32_t disk_reads;
303         uint32_t disk_sectors_read;
304         uint32_t disk_writes;
305         uint32_t disk_sectors_write;
306
307         // system_network
308         uint32_t network_send_size;
309         uint32_t network_receive_size;
310
311         // system_device
312         uint32_t wifi_status;
313         uint32_t bt_status;
314         uint32_t gps_status;
315         uint32_t brightness_status;
316         uint32_t camera_status;
317         uint32_t sound_status;
318         uint32_t audio_status;
319         uint32_t vibration_status;
320         uint32_t voltage_status;
321         uint32_t rssi_status;
322         uint32_t video_status;
323         uint32_t call_status;
324         uint32_t dnet_status;
325
326         // system_energy
327         uint32_t energy;
328         uint32_t energy_per_device[supported_devices_count];
329         uint32_t app_energy_per_device[supported_devices_count];
330 };
331
332 struct recorded_event_t {
333         uint32_t id;
334         uint32_t type;
335         uint32_t code;
336         uint32_t value;
337 };
338 #define static_assert(cond) \
339         char __attribute__((unused)) __static_assert[(cond) ? 1 : -1];
340
341 #define pack_int64(to, n) do {                                          \
342                 static_assert(sizeof(n) == 8);                          \
343                 *(uint64_t *)to = n;                                    \
344                 to += sizeof(uint64_t);                                 \
345         } while (0)
346
347 #define pack_int32(to, n) do {                                          \
348                 static_assert(sizeof(n) == 4);                          \
349                 *(uint32_t *)to = n;                                    \
350                 to += sizeof(uint32_t);                                 \
351         } while (0)
352
353 #define pack_time(to, n)                                                \
354         do {                                                            \
355                 pack_int32(to, n.tv_sec);                               \
356                 pack_int32(to, n.tv_usec);                              \
357         } while (0)
358
359 #define pack_float(to, n)                                       \
360         do {                                                    \
361                 *(float *)to = n;                               \
362                 to += sizeof(float);                            \
363         } while (0)
364
365 #define pack_str(to, n)                         \
366         do {                                    \
367                 memcpy(to, n, strlen(n) + 1);   \
368                 to += strlen(n) + 1;            \
369         } while (0)
370
371 static inline void* pack_str_array(void *buffer, const char **strings,
372                                    size_t count)
373 {
374         int index;
375         for (index = 0; index != count; ++index)
376                 pack_str(buffer, strings[index]);
377         return buffer;
378 }
379
380 struct msg_data_t *pack_system_info(struct system_info_t *sys_info);
381 int write_to_buf(struct msg_data_t *msg);
382 void free_msg_data(struct msg_data_t *msg);
383 void free_msg_payload(struct msg_t *msg);
384 void free_sys_info(struct system_info_t *sys_info);
385 int start_replay(void);
386 void stop_replay(void);
387
388 enum ErrorCode stop_all(void);
389 enum ErrorCode stop_all_no_lock(void);
390 int stop_all_in_process(void);
391 void stop_all_done(void);
392
393 void reset_msg(struct msg_t *msg);
394 void reset_replay_event_seq(struct replay_event_seq_t *res);
395 void reset_system_info(struct system_info_t *sys);
396 int check_running_status(const struct prof_session_t *prof_session);
397
398 extern struct prof_session_t prof_session;
399
400 //debugs
401 void print_replay_event(struct replay_event_t *ev, uint32_t num, char *tab);
402
403 int sendACKToHost(enum HostMessageT resp, enum ErrorCode err_code,
404                         char *payload, int payload_size);
405
406 int parse_int8(struct msg_buf_t *msg, uint8_t *val);
407 int parse_int32(struct msg_buf_t *msg, uint32_t *val);
408 int parse_int64(struct msg_buf_t *msg, uint64_t *val);
409 int parse_string(struct msg_buf_t *msg, char **str);
410 int parse_string_no_alloc(struct msg_buf_t *msg, char *str);
411 int parse_replay_event_seq(struct msg_buf_t *msg, struct replay_event_seq_t *res);
412
413 void init_prof_session(struct prof_session_t *prof_session);
414 #endif /* _DA_PROTOCOL_ */