[FIX] stop profiling when children terminate (Tizen app) 04/26904/1
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Mon, 1 Sep 2014 07:46:13 +0000 (11:46 +0400)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Mon, 1 Sep 2014 07:46:13 +0000 (11:46 +0400)
Change-Id: I4a0cdc299d8b088a63ab63d99a2614302d3d0652
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
daemon/da_protocol.h
daemon/daemon.c
daemon/daemon.h
daemon/main.c
daemon/threads.c
daemon/utils.c
daemon/utils.h

index 77c5bad..b326e94 100644 (file)
@@ -221,6 +221,7 @@ typedef uint32_t log_interval_t;
 
 //app, libs, probes
 enum app_type_t {
+       APP_TYPE_UNKNOWN = 0,
        APP_TYPE_TIZEN = 1,
        APP_TYPE_RUNNING = 2,
        APP_TYPE_COMMON = 3,
index 03d6707..e264f74 100644 (file)
 #define MAX_CONNECT_TIMEOUT_TIME       5*60
 
 
-int get_comm_pid(void)
-{
-       return manager.comm_pid;
-}
-
-void set_comm_pid(int pid)
-{
-       manager.comm_pid = pid;
-}
-
-void reset_comm_pid(void)
-{
-       set_comm_pid(no_comm_pid);
-}
-
 uint64_t get_total_alloc_size_by_pid(pid_t pid)
 {
        int i;
@@ -326,8 +311,6 @@ void terminate_all()
                        LOGI("join recv thread %d. done\n", i);
                }
        }
-
-       reset_comm_pid();
 }
 
 // terminate all profiling by critical error
@@ -524,10 +507,77 @@ int reconfigure(struct conf_t conf)
 
 static Ecore_Fd_Handler *target_handlers[MAX_TARGET_COUNT];
 
+
+static int file2str(const char *filename, char *buf, int len)
+{
+       int fd, num_read;
+
+       fd = open(filename, O_RDONLY, 0);
+       if (fd == -1)
+               return -1;
+
+       num_read = read(fd, buf, len - 1);
+
+       close(fd);
+
+       if (num_read <= 0)
+               return -1;
+
+       buf[num_read] = '\0';
+
+       return num_read;
+}
+
+static int get_lpad_pid(int pid)
+{
+       static pid_t lpad_pid = -1;
+       static const char lpad_path[] = DEBUG_LAUNCH_PRELOAD_PATH;
+       enum { lpad_path_len = sizeof(lpad_path) };
+
+       if (lpad_pid == -1) {
+               char fname[64];
+               char buf[lpad_path_len];
+
+               sprintf(fname, "/proc/%d/cmdline", pid);
+               if (-1 == file2str(fname, buf, lpad_path_len))
+                       return lpad_pid;
+
+               buf[lpad_path_len - 1] = '\0';
+
+               if (strncmp(buf, lpad_path, lpad_path_len - 1) == 0)
+                       lpad_pid = pid;
+       }
+
+       return lpad_pid;
+}
+
+static pid_t get_current_pid(void)
+{
+       static pid_t pid = -1;
+
+        if (pid == -1)
+                pid = getpid();
+
+        return pid;
+}
+
+static void taget_set_type(__da_target_info *taget)
+{
+       if (get_current_pid() == taget->ppid) {
+               taget->app_type = APP_TYPE_COMMON;
+       } else if (get_lpad_pid(taget->ppid) == taget->ppid) {
+               taget->app_type = APP_TYPE_TIZEN;
+       }
+}
+
+
 static int target_event_pid_handler(int index, uint64_t msg)
 {
        struct app_list_t *app = NULL;
        struct app_info_t *app_info = NULL;
+
+       taget_set_type(&manager.target[index]);
+
        if (index == 0) {       // main application
                app_info = app_info_get_first(&app);
                if (app_info == NULL) {
@@ -559,15 +609,12 @@ static int target_event_pid_handler(int index, uint64_t msg)
 
 static int target_event_stop_handler(int index, uint64_t msg)
 {
-       int cnt, comm_pid;
+       int cnt;
 
        LOGI("target close, socket(%d), pid(%d) : (remaining %d target)\n",
             manager.target[index].socket, manager.target[index].pid,
             manager.target_count - 1);
 
-       comm_pid = get_comm_pid();
-       if (manager.target[index].pid == comm_pid)
-               reset_comm_pid();
 
        if (index == 0)         // main application
                stop_replay();
@@ -577,11 +624,15 @@ static int target_event_stop_handler(int index, uint64_t msg)
        setEmptyTargetSlot(index);
        // all target client are closed
        cnt = __sync_sub_and_fetch(&manager.target_count, 1);
-       if (0 == cnt && no_comm_pid == comm_pid) {
-               LOGI("all targets are stopped\n");
-               if (stop_all() != ERR_NO)
-                       LOGE("Stop failed\n");
-               return -11;
+       if (0 == cnt) {
+               switch (manager.target[index].app_type) {
+               case APP_TYPE_TIZEN:
+               case APP_TYPE_COMMON:
+                       LOGI("all targets are stopped\n");
+                       if (stop_all() != ERR_NO)
+                               LOGE("Stop failed\n");
+                       return -11;
+               }
        }
 
        return 0;
index 251722b..f3749a4 100644 (file)
@@ -144,10 +144,12 @@ typedef struct
 {
        int64_t                 allocmem;               // written only by recv thread
        pid_t                           pid;                    // written only by recv thread
+       pid_t                   ppid;                   // written only by recv thread
        int                             socket;                 // written only by main thread
        pthread_t               recv_thread;    // written only by main thread
        int                             event_fd;               // for thread communication (from recv thread to main thread)
        int                             initial_log;    // written only by main thread
+       enum app_type_t         app_type;
 } __da_target_info;
 
 typedef struct
@@ -164,7 +166,6 @@ typedef struct
 
 typedef struct
 {
-       int comm_pid;
        int host_server_socket;
        int target_server_socket;
        int target_count;
@@ -187,13 +188,6 @@ typedef struct
 extern __da_manager manager;
 
 
-enum { no_comm_pid = -1 };
-
-int get_comm_pid(void);
-void set_comm_pid(int pid);
-void reset_comm_pid(void);
-
-
 uint64_t get_total_alloc_size(void);
 void initialize_log(void);
 int daemonLoop(void);
index 8474895..42d6aec 100644 (file)
@@ -62,7 +62,6 @@
 // initialize global variable
 __da_manager manager =
 {
-       .comm_pid = no_comm_pid,
        .host_server_socket = -1,
        .target_server_socket = -1,
        .target_count = 0,
index 10ad9d9..7797ba3 100644 (file)
@@ -117,14 +117,16 @@ static void* recvThread(void* data)
                }
                else if(log.type == MSG_PID)
                {
-                       LOGI("MSG_PID arrived : %s\n", log.data);
+                       LOGI("MSG_PID arrived (pid ppid): %s\n", log.data);
 
                        // only when first MSG_PID is arrived
                        if(target->pid == -1)
                        {
-                               char* barloc;
-                               barloc = strchr(log.data, '|');
-                               if(barloc == NULL)
+                               int n;
+                               pid_t pid, ppid;
+
+                               n = sscanf(log.data, "%d %d", &pid, &ppid);
+                               if (n != 2)
                                {
                                        /**
                                         * TODO: complain to host about wrong
@@ -136,10 +138,12 @@ static void* recvThread(void* data)
                                              sizeof(uint64_t));
                                        break;
                                }
-                               barloc[0] = '\0';
-                               barloc++;
 
-                               target->pid = atoi(log.data);
+                               /* set pid and ppid */
+                               target->pid = pid;
+                               target->ppid = ppid;
+
+                               /* send event */
                                event = EVENT_PID;
                                write(target->event_fd, &event, sizeof(uint64_t));
                        }
index 12212e3..4151ffa 100644 (file)
@@ -185,7 +185,6 @@ int exec_app_common(const char* exec_path)
                return -1;
 
        if (pid > 0) { /* parent */
-               set_comm_pid(pid);
                return 0;
        } else { /* child */
                execl(SHELL_CMD, SHELL_CMD, "-c", command, NULL);
index 7f1f770..b8d3295 100644 (file)
@@ -40,6 +40,7 @@ extern "C" {
 #endif
 
 #define LAUNCH_APP_PATH                        "/usr/bin/launch_app"
+#define DEBUG_LAUNCH_PRELOAD_PATH      "/usr/bin/debug_launchpad_preloading_preinitializing_daemon"
 #define KILL_APP_PATH                  "/usr/bin/pkill"
 #define LAUNCH_APP_NAME                        "launch_app"
 #define WRT_LAUNCHER_PATH              "/usr/bin/wrt-launcher"