4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
8 * Jaewon Lim <jaewon81.lim@samsung.com>
9 * Woojin Jung <woojin2.jung@samsung.com>
10 * Juyoung Kim <j0.kim@samsung.com>
11 * Cherepanov Vitaliy <v.cherepanov@samsung.com>
12 * Nikita Kalyazin <n.kalyazin@samsung.com>
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
18 * http://www.apache.org/licenses/LICENSE-2.0
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
28 * - Samsung RnD Institute Russia
31 #define __STDC_FORMAT_MACROS
33 #include <stdlib.h> // for realpath
34 #include <string.h> // for strtok, strcpy, strncpy
35 #include <limits.h> // for realpath
38 #include <errno.h> // for errno
39 #include <sys/types.h> // for accept, mkdir, opendir, readdir
40 #include <sys/socket.h> // for accept
41 #include <sys/stat.h> // for mkdir
42 #include <sys/eventfd.h> // for eventfd
43 #include <sys/timerfd.h> // for timerfd
44 #include <unistd.h> // for access, sleep
46 #include <linux/netlink.h>
47 #include <linux/connector.h>
60 #include "da_protocol.h"
63 #include "input_events.h"
65 #include "us_interaction_msg.h"
69 #define DA_WORK_DIR "/home/developer/sdk_tools/da/"
70 #define DA_READELF_PATH "/home/developer/sdk_tools/da/readelf"
71 #define SCREENSHOT_DIR "/tmp/da"
73 #define MAX_APP_LAUNCH_TIME 60
74 #define MAX_CONNECT_TIMEOUT_TIME 5*60
76 uint64_t get_total_alloc_size(void)
79 uint64_t allocsize = 0;
81 for (i = 0; i < MAX_TARGET_COUNT; i++) {
82 if (manager.target[i].socket != -1 &&
83 manager.target[i].allocmem > 0)
84 allocsize += manager.target[i].allocmem;
89 static int getEmptyTargetSlot()
92 for (i = 0; i < MAX_TARGET_COUNT; i++) {
93 if (manager.target[i].socket == -1)
100 static void setEmptyTargetSlot(int index)
102 if (index >= 0 && index < MAX_TARGET_COUNT) {
103 manager.target[index].pid = -1;
104 manager.target[index].recv_thread = -1;
105 manager.target[index].allocmem = 0;
106 manager.target[index].initial_log = 0;
107 if (manager.target[index].event_fd != -1)
108 close(manager.target[index].event_fd);
109 manager.target[index].event_fd = -1;
110 if (manager.target[index].socket != -1)
111 close(manager.target[index].socket);
112 manager.target[index].socket = -1;
116 // =============================================================================
117 // start and terminate control functions
118 // =============================================================================
120 static Ecore_Fd_Handler *launch_timer_handler;
122 //stop application launch timer
123 static int stop_app_launch_timer()
125 close(manager.app_launch_timerfd);
126 manager.app_launch_timerfd = -1;
131 static Eina_Bool launch_timer_cb(void *data, Ecore_Fd_Handler *fd_handler)
133 LOGE("Failed to launch application\n");
134 if (stop_app_launch_timer() < 0)
135 LOGE("cannot stop app launch timer\n");
137 return ECORE_CALLBACK_CANCEL;
140 //start application launch timer function
141 static int start_app_launch_timer(int apps_count)
145 assert(apps_count >= 0 && "negative apps count");
150 manager.app_launch_timerfd =
151 timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
152 if (manager.app_launch_timerfd > 0) {
153 struct itimerspec ctime;
154 ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME * apps_count;
155 ctime.it_value.tv_nsec = 0;
156 ctime.it_interval.tv_sec = 0;
157 ctime.it_interval.tv_nsec = 0;
158 if (timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL) < 0) {
159 LOGE("fail to set app launch timer\n");
160 stop_app_launch_timer();
163 launch_timer_handler =
164 ecore_main_fd_handler_add(manager.app_launch_timerfd,
169 if (!launch_timer_handler) {
170 LOGE("fail to add app launch timer fd to \n");
171 stop_app_launch_timer();
174 LOGI("application launch time started\n");
178 LOGE("cannot create launch timer\n");
185 static inline void inc_apps_to_run()
187 manager.apps_to_run++;
190 static inline void dec_apps_to_run()
192 if (manager.apps_to_run > 0)
193 manager.apps_to_run--;
196 static inline int get_apps_to_run()
198 return manager.apps_to_run;
201 static int kill_app_by_info(const struct app_info_t *app_info)
205 if (app_info == NULL) {
206 LOGE("Cannot exec app. app_info is NULL");
210 switch (app_info->app_type) {
212 res = kill_app(app_info->exe_path);
214 case APP_TYPE_RUNNING:
215 // TODO: nothing, it's running
216 LOGI("already started\n");
218 case APP_TYPE_COMMON:
219 res = kill_app(app_info->exe_path);
222 LOGE("Unknown app type %d\n", app_info->app_type);
230 static int exec_app(const struct app_info_t *app_info)
234 if (app_info == NULL) {
235 LOGE("Cannot exec app. app_info is NULL");
239 switch (app_info->app_type) {
241 if (exec_app_tizen(app_info->app_id, app_info->exe_path)) {
242 LOGE("Cannot exec tizen app %s\n", app_info->app_id);
248 case APP_TYPE_RUNNING:
249 // TODO: nothing, it's running
250 LOGI("already started\n");
252 case APP_TYPE_COMMON:
253 if (exec_app_common(app_info->exe_path)) {
254 LOGE("Cannot exec common app %s\n", app_info->exe_path);
261 LOGE("Unknown app type %d\n", app_info->app_type);
266 LOGI("ret=%d\n", res);
270 // just send stop message to all target process
271 static void terminate_all_target()
275 msg_target_t sendlog;
277 sendlog.type = MSG_STOP;
280 for (i = 0; i < MAX_TARGET_COUNT; i++) {
281 if (manager.target[i].socket != -1) {
282 sendlen = send(manager.target[i].socket, &sendlog,
283 sizeof(sendlog.type) +
284 sizeof(sendlog.length), MSG_NOSIGNAL);
286 LOGI("TERMINATE send exit msg (socket %d) "
287 "by terminate_all_target()\n",
288 manager.target[i].socket);
294 // terminate all target and wait for threads
298 terminate_all_target();
300 // wait for all other thread exit
301 for (i = 0; i < MAX_TARGET_COUNT; i++) {
302 if (manager.target[i].recv_thread != -1) {
303 LOGI("join recv thread [%d] is started\n", i);
304 pthread_join(manager.target[i].recv_thread, NULL);
305 LOGI("join recv thread %d. done\n", i);
310 // terminate all profiling by critical error
311 // TODO: don't send data to host
312 static void terminate_error(char *errstr, int send_to_host)
314 LOGE("termination all with err '%s'\n", errstr);
315 struct msg_data_t *msg = NULL;
316 if (send_to_host != 0) {
317 msg = gen_message_error(errstr);
322 LOGI("cannot generate error message\n");
328 static Ecore_Fd_Handler *connect_timer_handler;
330 static Eina_Bool connect_timer_cb(void *data, Ecore_Fd_Handler *fd_handler)
332 terminate_error("no incoming connections", 1);
333 close(manager.connect_timeout_timerfd);
334 manager.connect_timeout_timerfd = -1;
335 LOGE("No connection in %d sec. shutdown.\n",
336 MAX_CONNECT_TIMEOUT_TIME);
337 ecore_main_loop_quit();
339 return ECORE_CALLBACK_CANCEL;
342 static int launch_timer_start(void)
346 manager.connect_timeout_timerfd =
347 timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
348 if (manager.connect_timeout_timerfd > 0) {
349 struct itimerspec ctime;
350 ctime.it_value.tv_sec = MAX_CONNECT_TIMEOUT_TIME;
351 ctime.it_value.tv_nsec = 0;
352 ctime.it_interval.tv_sec = 0;
353 ctime.it_interval.tv_nsec = 0;
354 if (timerfd_settime(manager.connect_timeout_timerfd, 0, &ctime, NULL) < 0) {
355 LOGE("fail to set connect timeout timer\n");
356 close(manager.connect_timeout_timerfd);
357 manager.connect_timeout_timerfd = -1;
359 connect_timer_handler =
360 ecore_main_fd_handler_add(manager.connect_timeout_timerfd,
365 if (!connect_timer_handler) {
366 LOGE("fail to add app connection timeout timer fd\n");
367 close(manager.connect_timeout_timerfd);
368 manager.connect_timeout_timerfd = -1;
370 LOGI("connection timeout timer started\n");
374 LOGE("cannot create connection timeout timer\n");
377 LOGI("ret=%d\n", res);
381 int prepare_profiling(void)
383 struct app_list_t *app = NULL;
384 const struct app_info_t *app_info = NULL;
386 app_info = app_info_get_first(&app);
387 if (app_info == NULL) {
388 LOGE("No app info found\n");
393 while (app_info != NULL) {
394 if (kill_app_by_info(app_info) != 0) {
395 LOGE("kill app failed\n");
398 app_info = app_info_get_next(&app);
400 //init rw for systeminfo
401 //init recv send network systeminfo
407 int start_profiling(void)
409 struct app_list_t *app = NULL;
410 const struct app_info_t *app_info = NULL;
413 app_info = app_info_get_first(&app);
414 if (app_info == NULL) {
415 LOGE("No app info found\n");
418 // remove previous screen capture files
419 remove_indir(SCREENSHOT_DIR);
420 if (mkdir(SCREENSHOT_DIR, 0777) == -1 && errno != EEXIST)
421 LOGW("Failed to create directory for screenshot : %s\n",
424 set_label_for_all(SCREENSHOT_DIR);
426 if (samplingStart() < 0) {
427 LOGE("Cannot start sampling\n");
432 if (IS_OPT_SET(FL_RECORDING))
435 while (app_info != NULL) {
436 if (exec_app(app_info)) {
437 LOGE("Cannot exec app\n");
441 app_info = app_info_get_next(&app);
444 if (start_app_launch_timer(get_apps_to_run()) < 0) {
452 if (IS_OPT_SET(FL_RECORDING))
457 LOGI("return %d\n", res);
461 void stop_profiling(void)
463 if (IS_OPT_SET(FL_RECORDING))
468 static void reconfigure_recording(struct conf_t conf)
470 uint64_t old_features = prof_session.conf.use_features0;
471 uint64_t new_features = conf.use_features0;
472 uint64_t to_enable = (new_features ^ old_features) & new_features;
473 uint64_t to_disable = (new_features ^ old_features) & old_features;
475 if (IS_OPT_SET_IN(FL_RECORDING, to_disable)) {
477 prof_session.conf.use_features0 &= ~FL_RECORDING;
480 if (IS_OPT_SET_IN(FL_RECORDING, to_enable)) {
482 prof_session.conf.use_features0 |= FL_RECORDING;
487 int reconfigure(struct conf_t conf)
489 reconfigure_recording(conf);
492 memcpy(&prof_session.conf, &conf, sizeof(conf));
493 if (samplingStart() < 0) {
494 LOGE("Cannot start sampling\n");
501 static Ecore_Fd_Handler *target_handlers[MAX_TARGET_COUNT];
503 static int target_event_pid_handler(int index, uint64_t msg)
505 struct app_list_t *app = NULL;
506 struct app_info_t *app_info = NULL;
507 if (index == 0) { // main application
508 app_info = app_info_get_first(&app);
509 if (app_info == NULL) {
510 LOGE("No app info found\n");
514 while (app_info != NULL) {
515 if (is_same_app_process(app_info->exe_path,
516 manager.target[index].pid))
518 app_info = app_info_get_next(&app);
521 if (app_info == NULL) {
522 LOGE("pid %d not found in app list\n",
523 manager.target[index].pid);
527 if (start_replay() != 0) {
528 LOGE("Cannot start replay thread\n");
532 manager.target[index].initial_log = 1;
536 static int target_event_stop_handler(int index, uint64_t msg)
538 LOGI("target close, socket(%d), pid(%d) : (remaining %d target)\n",
539 manager.target[index].socket, manager.target[index].pid,
540 manager.target_count - 1);
542 if (index == 0) // main application
545 ecore_main_fd_handler_del(target_handlers[index]);
547 setEmptyTargetSlot(index);
548 // all target client are closed
549 if (0 == __sync_sub_and_fetch(&manager.target_count, 1)) {
550 LOGI("all targets are stopped\n");
551 if (stop_all() != ERR_NO)
552 LOGE("Stop failed\n");
559 // return 0 if normal case
560 // return plus value if non critical error occur
561 // return minus value if critical error occur
562 // return -11 if all target process closed
563 static int target_event_handler(int index, uint64_t msg)
567 err = target_event_pid_handler(index, msg);
571 if (msg & EVENT_STOP || msg & EVENT_ERROR)
572 err = target_event_stop_handler(index, msg);
577 static Eina_Bool target_event_cb(void *data, Ecore_Fd_Handler *fd_handler)
581 int index = (int)data;
583 recvLen = read(manager.target[index].event_fd, &u, sizeof(uint64_t));
584 if (recvLen != sizeof(uint64_t)) {
585 // maybe closed, but ignoring is more safe then
586 // removing fd from event loop
588 if (-11 == target_event_handler(index, u)) {
589 LOGI("all target process is closed\n");
593 return ECORE_CALLBACK_RENEW;
597 * return 0 if normal case
598 * return plus value if non critical error occur
599 * return minus value if critical error occur
601 static int targetServerHandler(void)
603 msg_target_t log, pid_msg;
606 int index = getEmptyTargetSlot();
607 if (index == MAX_TARGET_COUNT) {
608 LOGW("Max target number(8) reached, no more target can connected\n");
612 manager.target[index].socket =
613 accept(manager.target_server_socket, NULL, NULL);
615 if (manager.target[index].socket >= 0) {
617 fd_setup_attributes(manager.target[index].socket);
619 /* send config message to target process */
620 log.type = MSG_OPTION;
621 log.length = sprintf(log.data, "%llu",
622 prof_session.conf.use_features0);
623 if (0 > send(manager.target[index].socket, &log,
624 sizeof(log.type) + sizeof(log.length) + log.length,
626 LOGE("fail to send data to target index(%d)\n", index);
629 manager.target[index].event_fd = eventfd(EFD_CLOEXEC, EFD_NONBLOCK);
630 if (manager.target[index].event_fd == -1) {
631 // fail to make event fd
632 LOGE("fail to make event fd for socket (%d)\n",
633 manager.target[index].socket);
634 goto TARGET_CONNECT_FAIL;
637 target_handlers[index] =
638 ecore_main_fd_handler_add(manager.target[index].event_fd,
643 if (!target_handlers[index]) {
644 LOGE("fail to add event fd for socket (%d)\n",
645 manager.target[index].socket);
646 goto TARGET_CONNECT_FAIL;
649 // make recv thread for target
650 if (makeRecvThread(index) != 0) {
651 // fail to make recv thread
652 LOGE("fail to make recv thread for socket (%d)\n",
653 manager.target[index].socket);
654 ecore_main_fd_handler_del(target_handlers[index]);
655 goto TARGET_CONNECT_FAIL;
660 if ((manager.app_launch_timerfd > 0) && (get_apps_to_run() == 0)) {
661 if (stop_app_launch_timer() < 0)
662 LOGE("cannot stop app launch timer\n");
665 LOGI("target connected = %d(running %d target)\n",
666 manager.target[index].socket, manager.target_count + 1);
668 manager.target_count++;
672 LOGE("Failed to accept at target server socket\n");
676 if (manager.target_count == 0) {
677 // if this connection is main connection
680 // if this connection is not main connection then ignore process by error
681 setEmptyTargetSlot(index);
686 static Ecore_Fd_Handler *host_ctrl_handler;
687 static Ecore_Fd_Handler *host_data_handler;
689 // return plus value if non critical error occur
690 // return minus value if critical error occur
691 // return -11 if socket closed
692 static int controlSocketHandler(int efd)
695 struct msg_t msg_head;
699 if (manager.connect_timeout_timerfd >= 0) {
700 LOGI("release connect timeout timer\n");
701 close(manager.connect_timeout_timerfd);
702 manager.connect_timeout_timerfd = -1;
705 recv_len = recv(manager.host.control_socket,
706 &msg_head, MSG_CMD_HDR_LEN, 0);
707 // error or close request from host
708 if (recv_len == -1 || recv_len == 0)
711 msg = malloc(MSG_CMD_HDR_LEN + msg_head.len);
713 LOGE("Cannot alloc msg\n");
714 sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0);
717 msg->id = msg_head.id;
718 msg->len = msg_head.len;
720 // Receive payload (if exists)
721 recv_len = recv(manager.host.control_socket,
722 msg->payload, msg->len, MSG_WAITALL);
726 printBuf((char *)msg, MSG_CMD_HDR_LEN + msg->len);
727 res = host_message_handler(msg);
734 static Eina_Bool host_ctrl_cb(void *data, Ecore_Fd_Handler *fd_handler)
736 int result = controlSocketHandler(manager.efd);
739 //if the host disconnected.
740 //In all other cases daemon must report an error and continue the loop
741 //close connect_timeoutt and host socket and quit
742 LOGI("Connection closed. Termination. (%d)\n",
743 manager.host.control_socket);
744 ecore_main_loop_quit();
745 } else if (result < 0) {
746 LOGE("Control socket handler.\n");
749 return ECORE_CALLBACK_RENEW;
752 static Eina_Bool host_data_cb(void *data, Ecore_Fd_Handler *fd_handler)
757 recvLen = recv(manager.host.data_socket, recvBuf, 32, MSG_DONTWAIT);
760 ecore_main_fd_handler_del(host_data_handler);
761 close(manager.host.data_socket);
762 manager.host.data_socket = -1;
763 // TODO: finish transfer thread
766 LOGI("host message from data socket %d\n", recvLen);
768 return ECORE_CALLBACK_RENEW;
771 // return 0 if normal case
772 // return plus value if non critical error occur
773 // return minus value if critical error occur
774 static int hostServerHandler(void)
776 static int hostserverorder = 0;
779 if (hostserverorder > 1) // control and data socket connected already
782 csocket = accept(manager.host_server_socket, NULL, NULL);
787 if (hostserverorder == 0) {
788 manager.host.control_socket = csocket;
790 LOGI("host control socket connected = %d\n", csocket);
792 ecore_main_fd_handler_add(manager.host.control_socket,
797 if (!host_ctrl_handler) {
798 LOGE("Failed to add host control socket fd\n");
803 manager.host.data_socket = csocket;
804 LOGI("host data socket connected = %d\n", csocket);
807 ecore_main_fd_handler_add(manager.host.data_socket,
812 if (!host_data_handler) {
813 LOGE("Failed to add host data socket fd\n");
823 LOGE("Failed to accept from host server socket\n");
828 static int kernel_handler(void)
832 struct nlmsghdr *nlh;
836 /* Get buffer size */
837 optlen = sizeof(size);
839 /* We're using SOCK_DGRAM, so, get it maximum size */
840 res = getsockopt(manager.kernel_socket, SOL_SOCKET, SO_SNDBUF, &size,
844 LOGE("Get maximum buffer size failed\n");
848 /* Alloc mem for nlh message struct and receive it */
852 len = recv(manager.kernel_socket, nlh, size, 0);
853 if ((len <= 0) || (nlh->nlmsg_len == 0)) {
858 /* nlh data field contains connectors message */
859 msg = NLMSG_DATA(nlh);
865 switch(*(enum us_interaction_k2u_msg_t *) msg->data) {
866 case US_INT_PAUSE_APPS:
869 case US_INT_CONT_APPS:
873 LOGE("Unknown command\n");
876 /* Insert your message handler here */
887 static Ecore_Fd_Handler *host_connect_handler;
888 static Ecore_Fd_Handler *target_connect_handler;
889 static Ecore_Fd_Handler *kernel_connect_handler;
891 static Eina_Bool host_connect_cb(void *data, Ecore_Fd_Handler *fd_handler)
893 // connect request from host
894 int result = hostServerHandler();
896 LOGE("Internal DA framework error (hostServerHandler)\n");
899 return ECORE_CALLBACK_RENEW;
902 static Eina_Bool target_connect_cb(void *data, Ecore_Fd_Handler *fd_handler)
904 if (targetServerHandler() < 0) {
906 terminate_error("Internal DA framework error, "
907 "Please re-run the profiling "
908 "(targetServerHandler)\n", 1);
911 return ECORE_CALLBACK_RENEW;
914 static Eina_Bool kernel_connect_cb(void *data, Ecore_Fd_Handler *fd_handler)
916 if (kernel_handler() < 0) {
917 LOGE("Internal DA framework error (kernel_handler)\n");
920 return ECORE_CALLBACK_RENEW;
923 static bool initialize_events(void)
925 host_connect_handler =
926 ecore_main_fd_handler_add(manager.host_server_socket,
931 if (!host_connect_handler) {
932 LOGE("Host server socket add error\n");
936 target_connect_handler =
937 ecore_main_fd_handler_add(manager.target_server_socket,
942 if (!target_connect_handler) {
943 LOGE("Target server socket add error\n");
947 kernel_connect_handler =
948 ecore_main_fd_handler_add(manager.kernel_socket,
953 if (!kernel_connect_handler) {
954 LOGE("Kernel socket add error\n");
961 // return 0 for normal case
964 int return_value = 0;
968 if (init_input_events() == -1) {
973 if (!initialize_events()) {
978 if (launch_timer_start() < 0) {
979 LOGE("Launch timer start failed\n");
984 init_prof_session(&prof_session);
986 ecore_main_loop_begin();