report-generator: move clock func to seaprate header 76/183576/2
authorLukasz Stanislawski <lukasz.stanislawski@gmail.com>
Sun, 8 Jul 2018 11:22:45 +0000 (13:22 +0200)
committerLukasz Stanislawski <lukasz.stanislawski@gmail.com>
Mon, 9 Jul 2018 19:21:22 +0000 (21:21 +0200)
Use realtime clock for report time.

Change-Id: I4623ef15d9c5b842077035525248015051b9e34b

src/CMakeLists.txt
src/clock.c [new file with mode: 0644]
src/clock.h [new file with mode: 0644]
src/report-generator.c

index 37806ba..3bd16d4 100644 (file)
@@ -26,6 +26,7 @@ SET(SRCS
        appinfo-provider.c
        report-json-serializer.c
        stats.c
+       clock.c
 )
 ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS})
 
diff --git a/src/clock.c b/src/clock.c
new file mode 100644 (file)
index 0000000..034573b
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+
+#include "clock.h"
+#include "log.h"
+
+struct timespec clock_monotonic_get()
+{
+       struct timespec ret = {0,};
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ret) != 0) {
+               ERR("Platform do not support monotonic clock type");
+               abort();
+       }
+
+       return ret;
+}
+
+struct timespec clock_realtime_get()
+{
+       struct timespec ret = {0,};
+
+       if (clock_gettime(CLOCK_REALTIME, &ret) != 0) {
+               ERR("Platform do not support realtime clock type");
+               abort();
+       }
+
+       return ret;
+}
+
+bool clock_is_supported()
+{
+       struct timespec ret = {0,};
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ret) != 0) {
+               ERR("Platform do not support monotonic clock type");
+               return false;
+       }
+       if (clock_gettime(CLOCK_REALTIME, &ret) != 0) {
+               ERR("Platform do not support realtime clock type");
+               return false;
+       }
+       return true;
+}
+
diff --git a/src/clock.h b/src/clock.h
new file mode 100644 (file)
index 0000000..cdca73b
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _CLOCK_MONOTONIC_H
+#define _CLOCK_MONOTONIC_H
+
+#include <time.h>
+#include <stdbool.h>
+
+/**
+ * @brief Check if platform supports required clock types
+ *
+ * @return true if supported, false otherwise.
+ */
+bool clock_is_supported();
+
+/**
+ * @brief Gets current time using monotonic clock
+ *
+ * @note the func will abort if monotonic clock is not supported.
+ * call @clock_is_supported beforehead to validate if platform
+ * supports all clock types.
+ */
+struct timespec clock_monotonic_get();
+
+/**
+ * @brief Gets current time using realtime clock
+ *
+ * @note the func will abort if realtime clock is not supported.
+ * call @clock_is_supported beforehead to validate if platform
+ * supports all clock types.
+ *
+ */
+struct timespec clock_realtime_get();
+
+#endif
index 6535021..05f16d6 100644 (file)
@@ -15,7 +15,6 @@
  */
 
 #include <stdlib.h>
-#include <assert.h>
 #include <string.h>
 #include <stdbool.h>
 #include <unistd.h>
@@ -25,6 +24,7 @@
 #include "err-check.h"
 #include "appinfo-provider.h"
 #include "stats.h"
+#include "clock.h"
 
 struct report_generator_system {
        /** system cpu usage statistics */
@@ -47,21 +47,6 @@ struct report_generator_app
 
 int _app_report_generator_setup_process_generator(report_generator_app_t *generator);
 
-static struct timespec clock_get_monotonic()
-{
-       struct timespec ret = {0,};
-
-       if (clock_gettime(CLOCK_MONOTONIC, &ret) != 0) {
-               ERR("Platform do not support monotonic clock type");
-               //TODO consider adding first init function to evaluate
-               //if clock_gettime can be used, so calling module could
-               //handle cases without monotonic clock gracefully.
-               abort();
-       }
-
-       return ret;
-}
-
 report_generator_system_t *report_generator_new_system_report_generator()
 {
        report_generator_system_t *ret = calloc(1, sizeof(struct report_generator_system));
@@ -168,7 +153,7 @@ int report_generator_generate_system_cpu_usage_report(
        }
 
        report->usage = usage;
-       report->time = clock_get_monotonic().tv_sec;
+       report->time = clock_realtime_get().tv_sec;
 
        generator->previous = current;
 
@@ -188,7 +173,7 @@ int report_generator_generate_system_memory_usage_report(
                return -1;
        }
 
-       report->time = clock_get_monotonic().tv_sec;
+       report->time = clock_realtime_get().tv_sec;
        report->usage = usage;
 
        return 0;
@@ -225,7 +210,7 @@ int report_generator_generate_process_cpu_usage_report(
                return -1;
        }
 
-       report->time = clock_get_monotonic().tv_sec;
+       report->time = clock_realtime_get().tv_sec;
        report->pid = generator->pid;
        report->usage = usage;
 
@@ -248,7 +233,7 @@ int report_generator_generate_process_memory_usage_report(
                return -1;
        }
 
-       report->time = clock_get_monotonic().tv_sec;
+       report->time = clock_realtime_get().tv_sec;
        report->usage = usage;
 
        return 0;
@@ -332,7 +317,7 @@ int report_generator_generate_load_average_report(struct system_load_average_rep
                return -1;
        }
 
-       report->time = clock_get_monotonic().tv_sec;
+       report->time = clock_realtime_get().tv_sec;
        report->one_min_avg = a1;
        report->five_min_avg = a5;
        report->fifteen_min_avg = a15;