[REFACTOR] Refactor timerfd initialization
[platform/core/system/swap-probe.git] / helper / libdaprobe.c
index 899fd62..dc6435b 100755 (executable)
@@ -3,19 +3,19 @@
  *
  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
  *
- * Contact: 
+ * Contact:
  *
  * Jaewon Lim <jaewon81.lim@samsung.com>
  * Woojin Jung <woojin2.jung@samsung.com>
  * Juyoung Kim <j0.kim@samsung.com>
  * Nikita Kalyazin <n.kalyazin@samsung.com>
  * Anastasia Lyupa <a.lyupa@samsung.com>
- * 
+ *
  * This library is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
  * Free Software Foundation; either version 2.1 of the License, or (at your option)
  * any later version.
- * 
+ *
  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
@@ -28,7 +28,7 @@
  * Contributors:
  * - S-Core Co., Ltd
  * - Samsung RnD Institute Russia
- * 
+ *
  */
 
 #include <stdio.h>                     // for sprintf
@@ -41,6 +41,7 @@
 #include <unistd.h>                    // for write, alarm function, syscall
 #include <pthread.h>           // for pthread_mutex_lock
 #include <signal.h>
+#include <stdint.h>
 
 #include <sys/syscall.h>       // for syscall
 #include <sys/time.h>          // for gettimeofday
 #include "dautil.h"
 #include "dahelper.h"
 #include "dacollection.h"
+#include "da_sync.h"
+#include "daprobe.h"
 
 #include "binproto.h"
 
 #define APP_INSTALL_PATH               "/opt/apps"
-#define OSP_APP_POSTFIX                        ".exe"
+#define TISEN_APP_POSTFIX                      ".exe"
 #define UDS_NAME                               "/tmp/da.socket"
 #define TIMERFD_INTERVAL               100000000               // 0.1 sec
 
 __thread int           gProbeBlockCount = 0;
 __thread int           gProbeDepth = 0;
+pid_t gPid = -1;
 __thread pid_t         gTid = -1;
 
 int                    g_timerfd = 0;
@@ -83,7 +87,7 @@ bool printLog(log_t* log, int msgType);
 static void _configure(char* configstr)
 {
        char buf[64];
-       gTraceInfo.optionflag = atoi(configstr);
+       gTraceInfo.optionflag = atoll(configstr);
 
        sprintf(buf, "configure in probe : %s, %lx\n", configstr, gTraceInfo.optionflag);
        PRINTMSG(buf);
@@ -95,33 +99,32 @@ static int createSocket(void)
        ssize_t recvlen;
        int clientLen, ret = 0;
        struct sockaddr_un clientAddr;
-       char buf[16];
        log_t log;
 
-       if((gTraceInfo.socket.daemonSock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) != -1)
-       {
-               bzero(&clientAddr, sizeof(clientAddr));
+       gTraceInfo.socket.daemonSock =
+         socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+       if (gTraceInfo.socket.daemonSock != -1) {
+               memset(&clientAddr, '\0', sizeof(clientAddr));
                clientAddr.sun_family = AF_UNIX;
                sprintf(clientAddr.sun_path, "%s", UDS_NAME);
 
                clientLen = sizeof(clientAddr);
-               if(connect(gTraceInfo.socket.daemonSock, (struct sockaddr *)&clientAddr, clientLen) >= 0)
+               if (connect(gTraceInfo.socket.daemonSock,
+                           (struct sockaddr *)&clientAddr, clientLen) >= 0)
                {
-                       // recv initial configuration value
+                       char buf[64];
+                       /* recv initial configuration value */
                        recvlen = recv(gTraceInfo.socket.daemonSock, &log,
-                                       sizeof(log.type) + sizeof(log.length), MSG_WAITALL);
+                                      sizeof(log.type) + sizeof(log.length),
+                                      MSG_WAITALL);
 
-                       if(recvlen > 0) // recv succeed
-                       {
-                               if(log.length > 0)
-                               {
+                       if (recvlen > 0) {/* recv succeed */
+                               if (log.length > 0) {
                                        if(log.length >= DA_LOG_MAX)
                                                log.length = DA_LOG_MAX - 1;
                                        recvlen = recv(gTraceInfo.socket.daemonSock, log.data,
-                                               log.length, MSG_WAITALL);
-                               }
-                               else
-                               {
+                                                      log.length, MSG_WAITALL);
+                               } else {
                                        log.length = 0;
                                }
 
@@ -135,29 +138,22 @@ static int createSocket(void)
                                {
                                        // unexpected case
                                }
-                       }
-                       else if(recvlen < 0)
-                       {
-                               char buf[64];
+                       } else if (recvlen < 0) {
                                sprintf(buf, "recv failed in socket creation with error(%d)\n", recvlen);
+                       } else {
+                               /* closed by other peer */
                        }
-                       else    // closed by other peer
-                       {
 
-                       }
-                       sprintf(buf, "%d|%llu", getpid(), gTraceInfo.app.startTime);
+                       sprintf(buf, "%d|%llu", getpid(),
+                               gTraceInfo.app.startTime);
                        printLogStr(buf, MSG_PID);
                        PRINTMSG("createSocket connect() success\n");
-               }
-               else
-               {
+               } else {
                        close(gTraceInfo.socket.daemonSock);
                        gTraceInfo.socket.daemonSock = -1;
                        ret = -1;
                }
-       }
-       else
-       {
+       } else {
                ret = -1;
        }
 
@@ -180,8 +176,8 @@ static int determineCaller(char* tracestring)
        }
        else                            // user binary
        {
-#ifdef OSPAPP
-               substr = strstr(tracestring, OSP_APP_POSTFIX);
+#ifdef TISENAPP
+               substr = strstr(tracestring, TISEN_APP_POSTFIX);
                if(substr == NULL)
                        return 1;
 #endif
@@ -189,6 +185,14 @@ static int determineCaller(char* tracestring)
        }
 }
 
+// return current process id
+static pid_t _getpid()
+{
+       if (gPid == -1)
+               gPid = getpid();
+       return gPid;
+}
+
 // return current thread id
 static pid_t _gettid()
 {
@@ -197,7 +201,7 @@ static pid_t _gettid()
        return gTid;
 }
 
-static void* recvThread(void* data)
+static void *recvThread(void __unused * data)
 {
        fd_set readfds, workfds;
        int maxfd = 0, rc;
@@ -269,13 +273,14 @@ static void* recvThread(void* data)
 
                                log.data[log.length] = '\0';
 
-                               if(log.type == MSG_CONFIG)
-                               {
+                               if (log.type == MSG_CAPTURE_SCREEN) {
+                                       captureScreen();
+                               } else if (log.type == MSG_CONFIG) {
                                        _configure(log.data);
                                }
                                else if(log.type == MSG_STOP)
                                {
-                                       app_efl_exit();
+                                       application_exit();
                                }
                                else
                                {
@@ -310,11 +315,37 @@ static void* recvThread(void* data)
 /*****************************************************************************
  * initialize / finalize function
  *****************************************************************************/
+static int init_timerfd(void)
+{
+       const struct timespec interval = {
+               .tv_sec = 0,
+               .tv_nsec = TIMERFD_INTERVAL
+       };
+       const struct itimerspec ctime = {
+               .it_interval = interval,
+               .it_value = interval
+       };
+
+       int timer = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
+
+       if (timer == -1) {
+               PRINTMSG("failed to create timerdf\n");
+               return -1;
+       }
+
+       if (timerfd_settime(timer, 0, &ctime, NULL) != 0) {
+               PRINTMSG("failed to set timerfd\n");
+               close(timer);
+               return -1;
+       }
+
+       return timer;
+}
 
 void __attribute__((constructor)) _init_probe()
 {
        struct timeval ttime;
-       struct itimerspec ctime;
+
 
        probeBlockStart();
 
@@ -332,27 +363,8 @@ void __attribute__((constructor)) _init_probe()
                + ttime.tv_usec;
 
        // create socket for communication with da_daemon
-       if(createSocket() == 0)
-       {
-               // create timerfd
-               g_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC);
-               if(g_timerfd > 0)
-               {
-                       ctime.it_value.tv_sec = 0;
-                       ctime.it_value.tv_nsec = TIMERFD_INTERVAL;
-                       ctime.it_interval.tv_sec = 0;
-                       ctime.it_interval.tv_nsec = TIMERFD_INTERVAL;
-                       if(0 > timerfd_settime(g_timerfd, 0, &ctime, NULL))
-                       {
-                               PRINTMSG("failed to set timerfd\n");
-                               close(g_timerfd);
-                               g_timerfd = 0;
-                       }
-               }
-               else
-               {
-                       PRINTMSG("failed to create timerdf\n");
-               }
+       if (createSocket() == 0) {
+               g_timerfd = init_timerfd();
 
                // create recv Thread
                if(pthread_create(&g_recvthread_id, NULL, recvThread, NULL) < 0)        // thread creation failed
@@ -419,8 +431,7 @@ void __attribute__((destructor)) _fini_probe()
 /************************************************************************
  * manipulate and print log functions
  ************************************************************************/
-
-bool printLog(log_t* log, int msgType)
+bool printLog(log_t *log, int msgType)
 {
        int res;
        if(unlikely(gTraceInfo.socket.daemonSock == -1))
@@ -431,9 +442,9 @@ bool printLog(log_t* log, int msgType)
 
        probeBlockStart();
        log->type = msgType;
-       pthread_mutex_lock(&(gTraceInfo.socket.sockMutex));
+       real_pthread_mutex_lock(&(gTraceInfo.socket.sockMutex));
        res = send(gTraceInfo.socket.daemonSock, log, sizeof(log->type) + sizeof(log->length) + log->length, 0);
-       pthread_mutex_unlock(&(gTraceInfo.socket.sockMutex));
+       real_pthread_mutex_unlock(&(gTraceInfo.socket.sockMutex));
        probeBlockEnd();
 
        return true;
@@ -460,9 +471,9 @@ bool printLogStr(const char* str, int msgType)
                log.length = 0;
        }
 
-       pthread_mutex_lock(&(gTraceInfo.socket.sockMutex));
+       real_pthread_mutex_lock(&(gTraceInfo.socket.sockMutex));
        res = send(gTraceInfo.socket.daemonSock, &log, sizeof(log.type) + sizeof(log.length) + log.length, MSG_DONTWAIT);
-       pthread_mutex_unlock(&(gTraceInfo.socket.sockMutex));
+       real_pthread_mutex_unlock(&(gTraceInfo.socket.sockMutex));
 
        probeBlockEnd();
 
@@ -475,10 +486,10 @@ bool printLogStr(const char* str, int msgType)
 // if token is not NULL then insert DEFAULT TOKEN before append input
 int __appendTypeLog(log_t* log, int nInput, char* token, ...)
 {
-       static char* default_token = DEFAULT_TOKEN;
+       static const char *default_token = DEFAULT_TOKEN;
        va_list p_arg;
        int i, type;
-       char* seperator = default_token;
+       const char *seperator = default_token;
 
        if(nInput <= 0 || log == NULL)
                return -1;
@@ -742,6 +753,13 @@ unsigned int getCurrentEventIndex()
        return gTraceInfo.index.eventIndex;
 }
 
+uint64_t get_current_nsec(void)
+{
+       struct timeval tv;
+       gettimeofday(&tv, NULL);
+       return (uint64_t)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
+}
+
 /************************************************************************
  * probe functions
  ************************************************************************/
@@ -755,12 +773,12 @@ bool setProbePoint(probeInfo_t* iProbe)
        probeBlockStart();
 
        // atomic operaion(gcc builtin) is more expensive then pthread_mutex
-       pthread_mutex_lock(&(gTraceInfo.index.eventMutex));     
+       real_pthread_mutex_lock(&(gTraceInfo.index.eventMutex));
        iProbe->eventIndex = gTraceInfo.index.eventIndex++;
-       pthread_mutex_unlock(&(gTraceInfo.index.eventMutex));
+       real_pthread_mutex_unlock(&(gTraceInfo.index.eventMutex));
 
        iProbe->currentTime = getCurrentTime();
-       iProbe->pID = getpid();
+       iProbe->pID = _getpid();
        iProbe->tID = _gettid();
        iProbe->callDepth = gProbeDepth;
 
@@ -785,5 +803,3 @@ int update_heap_memory_size(bool isAdd, size_t size)
 
        return 0;
 }
-
-