From 2bc82f4fddfdbfec67e04332587f87e9989d114f Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Fri, 27 Apr 2018 08:27:31 +0200 Subject: [PATCH] initial interface design Change-Id: Ief54674697228aa481e07e12eabfc1636e97e8bf --- CMakeLists.txt | 2 +- src/appinfo-provider.c | 0 src/appinfo-provider.h | 24 +++++ src/procfs.c | 57 +++++++++++ src/procfs.h | 150 ++++++++++++++++++++++++++++ src/report-generator.c | 0 src/report-generator.h | 199 +++++++++++++++++++++++++++++++++++++ src/report-json-serializer.c | 0 src/report-json-serializer.h | 79 +++++++++++++++ src/report.h | 101 +++++++++++++++++++ src/scheduler.c | 0 src/{task_worker.c => scheduler.h} | 8 +- src/task-worker.c | 24 +++++ 13 files changed, 639 insertions(+), 5 deletions(-) create mode 100644 src/appinfo-provider.c create mode 100644 src/appinfo-provider.h create mode 100644 src/procfs.c create mode 100644 src/procfs.h create mode 100644 src/report-generator.c create mode 100644 src/report-generator.h create mode 100644 src/report-json-serializer.c create mode 100644 src/report-json-serializer.h create mode 100644 src/report.h create mode 100644 src/scheduler.c rename src/{task_worker.c => scheduler.h} (91%) create mode 100644 src/task-worker.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 122bee6..fc203db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc) SET(SRCS - src/task_worker.c + src/task-worker.c ) ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) diff --git a/src/appinfo-provider.c b/src/appinfo-provider.c new file mode 100644 index 0000000..e69de29 diff --git a/src/appinfo-provider.h b/src/appinfo-provider.h new file mode 100644 index 0000000..7da097e --- /dev/null +++ b/src/appinfo-provider.h @@ -0,0 +1,24 @@ +/* + * 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 __APPINFO_PROVIDER_H_ +#define __APPINFO_PROVIDER_H_ + +#include "report.h" + +char **app_info_provider_get_running_applications(); + +#endif diff --git a/src/procfs.c b/src/procfs.c new file mode 100644 index 0000000..3a285e4 --- /dev/null +++ b/src/procfs.c @@ -0,0 +1,57 @@ +/* + * 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 "procfs.h" + +int procfs_read_system_load_average(struct procfs_load_average_info *avg) +{ + return -1; +} + +int profcs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage) +{ + return -1; +} + +int profcs_read_system_percpu_usage(struct procfs_system_percpu_usage_info **usage) +{ + return -1; +} + +int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage) +{ + return -1; +} + +int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage) +{ + return -1; +} + +int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage) +{ + return -1; +} + +int procfs_read_uptime(int *uptime) +{ + return -1; +} + +int procfs_read_cpu_count(int *cpu_count) +{ + return -1; +} diff --git a/src/procfs.h b/src/procfs.h new file mode 100644 index 0000000..734bd23 --- /dev/null +++ b/src/procfs.h @@ -0,0 +1,150 @@ +/* + * 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 __PROCFS_H_ +#define __PROCFS_H_ + +#include + +/** + * @brief System memory information. + */ +struct procfs_system_memory_usage_info { + unsigned long total; /**< Total memory (KiB) */ + unsigned long used; /**< Used memory (KiB) */ + unsigned long free; /**< Free memory (KiB) */ + unsigned long cache; /**< Cache memory (KiB) */ + unsigned long swap; /**< Swap memory (KiB) */ +}; + +/** + * @brief System cpu usage information. + */ +struct procfs_system_cpu_usage_info { + unsigned long long user; /** Time running un-niced user processes (clock ticks) */ + unsigned long long system; /** Time running kernel process (clock ticks) */ + unsigned long long nice; /** Time running niced user processes (clock ticks) */ + unsigned long long idle; /** Time running in idle task user processes (clock ticks) */ + unsigned long long iowait; /** Time waiting for I/O completion (clock ticks) */ + unsigned long long irq; /** Time servicing interrupts (clock ticks) */ + unsigned long long softirq; /** Time servicing soft interrupts (clock ticks) */ +}; + +/** + * @brief System load average information. + * @notice For more info refer to: + * http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html + * https://github.com/torvalds/linux/blob/master/kernel/sched/loadavg.c + */ +struct procfs_load_average_info { + float one_min_avg; /** One minute load average */ + float five_min_avg; /** Five minutes load average */ + float fifteen_min_avg; /** Fifteen minutes load average */ +}; + +/** + * @brief Process memory information. + */ +struct procfs_process_memory_usage_info { + unsigned long vsz; /** Virtual memory size (KiB) */ + unsigned long rss; /** Resident set size (KiB) */ + unsigned long pss; /** Proportional set size (KiB) */ + unsigned long shared_clean; /** Not modified and mapped by other processes (KiB) */ + unsigned long shared_dirty; /** Modified and mapped by other processes (KiB) */ + unsigned long private_clean; /** Not modified and available only to that process (KiB) */ + unsigned long private_dirty; /** Modified and available only to that process (KiB) */ +}; + +/** + * @brief Process cpu usage information. + */ +struct procfs_process_cpu_usage_info { + unsigned long long utime; /** Amount of time that this process has been scheduled in user mode (clock ticks) */ + unsigned long long stime; /** Amount of time that this process has been scheduled in kernel mode (clock ticks) */ +}; + +/** + * @brief Parses information from /proc/loadavg + * + * @param[out] avg structure to be filled. + * @return: 0 on success, other value on error + */ +int procfs_read_system_load_average(struct procfs_load_average_info *avg); + +/** + * @brief Parses information from /proc/stat + * + * @param[out] avg structure to be filled. + * @return: 0 on success, other value on error + */ +int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage); + +/** + * @brief Parses information from /proc/stat, returns system cpu usage per + * logical cpu (core). + * + * @param[in] max_cpus maximum number of cpus to read stats from. + * @param[out] usage array to be filled. + * @return: on success - number of entries correctly written to usage array, + * value < 0 if an error occured. + * + * @remark the usage array must be have at least max_cpus lenght. + */ +int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage_info usage[]); + +/** + * @brief Parses information from /proc/meminfo + * + * @param[out] avg structure to be filled. + * @return: 0 on success, other value on error + */ +int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage); + +/** + * @brief Parses information from /proc/{pid}/smaps + * + * @param[in] process pid + * @param[out] avg structure to be filled. + * @return: 0 on success, other value on error + */ +int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage); + +/** + * @brief Parses information from /proc/{pid}/stat + * + * @param[in] process pid + * @param[out] avg structure to be filled. + * @return: 0 on success, other value on error + */ +int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage); + +/** + * @brief Parses information from /proc/uptime + * + * @param[out] uptime Number of seconds after boot + * @return: 0 on success, other value on error + */ +int procfs_read_uptime(int *uptime); + +/** + * @brief Parses information from /sys/devices/system/cpu/possible + * + * @param[out] cpu_count Number of logical cpus availabe in the system. + * @return: 0 on success, other value on error + */ +int procfs_read_cpu_count(int *cpu_count); + +#endif diff --git a/src/report-generator.c b/src/report-generator.c new file mode 100644 index 0000000..e69de29 diff --git a/src/report-generator.h b/src/report-generator.h new file mode 100644 index 0000000..b1dc4e9 --- /dev/null +++ b/src/report-generator.h @@ -0,0 +1,199 @@ +/* + * 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 __REPORT_GENERATOR_H_ +#define __REPORT_GENERATOR_H_ + +#include "report.h" + +/** Generator for system-wide reports */ +typedef struct report_generator_system report_generator_system_t; + +/** Generator for per process report */ +typedef struct report_generator_process report_generator_process_t; + +/** Generator for per application reports */ +typedef struct report_generator_app report_generator_app_t; + +/** + * @brief Creates new instance of report_generator_system_t + * @return new report_generator_system_t object, or NULL on error + * + * @remark return value should be released with + * @report_generator_free_system_generator + */ +report_generator_system_t *report_generator_new_system_report_generator(); + +/** + * @brief Releasd report_generator_system_t created with + * @report_generator_new_system_report_generator + */ +void report_generator_free_system_generator(report_generator_system_t *generator); + +/** + * @brief Creates new instance of report_generator_process_t + * + * @param[in] procsss PID + * @return new report_generator_system_t object, or NULL on error + * + * @remark return value should be released with + * @report_generator_free_process_generator + */ +report_generator_process_t *report_generator_new_process_report_generator(int pid); + +/** + * @brief Releasd report_generator_process_t created with + * @report_generator_new_process_report_generator_ + */ +void report_generator_free_process_generator(report_generator_process_t *generator); + +/** + * @brief Creates new instance of report_generator_app_t_ + * + * @param[in] appid Tizen application id + * @return new report_generator_app_t_ object, or NULL on error + * + * @remark return value should be released with + * @report_generator_free_process_generator + */ +report_generator_app_t *report_generator_new_app_report_generator(const char *appid); + +/** + * @brief Releasd report_generator_app_t created with + * @report_generator_new_app_report_generator_ + */ +void report_generator_free_app_generator(report_generator_app_t *generator); + +/** + * @brief Fills system_cpu_usage_report. + * + * When interval > 0 the function will block for interval seconds and return report with + * system cpu usage calculated as average during interval period. + * When interval == 0 the function will not block and return report with cpu + * system usage calculated as average between current time and last call to + * report_generator_generate_system_cpu_usage_report for generator + * + * @param[in] generator system generator + * @param[in] interval in seconds, Should be >= 0 + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_system_cpu_usage_report( + report_generator_system_t *generator, + int interval, + struct system_cpu_usage_report *report); + +/** + * @brief Fills system_percpu_usage_report. + * + * When interval > 0 the function will block for interval seconds and return report with + * system cpus usage calculated as average during interval period. + * When interval == 0 the function will not block and return report with system cpus + * usage calculated as average between current time and last call to + * report_generator_generate_system_percpu_usage_report for generator + * + * @param[in] generator system generator + * @param[in] interval in seconds, Should be >= 0 + * @param[out] report + * @return 0 on success, other value on failure + * + * @remark report should be released with @free + */ +int report_generator_generate_percpu_usage_report( + report_generator_system_t *generator, + int interval, + struct system_percpu_usage_report **report); + +/** + * @brief Fills system_memory_usage_report. + * + * @param[in] generator system generator + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_system_memory_usage_report( + report_generator_system_t *generator, + struct system_memory_usage_report *report); + +/** + * @brief Fills process_cpu_usage_report. + * + * When interval > 0 the function will block for interval seconds and return report with + * process cpu usage calculated as average during interval period. + * When interval == 0 the function will not block and return report with process + * cpu usage calculated as average between current time and last call to + * report_generator_generate_process_cpu_usage_report for generator + * + * @param[in] generator system generator + * @param[in] interval in seconds, Should be >= 0 + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_process_cpu_usage_report( + report_generator_process_t *generator, + int interval, + struct process_cpu_usage_report *report); + +/** + * @brief Fills process_memory_usage_report. + * + * @param[in] generator system generator + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_proccess_memory_usage_report( + report_generator_process_t *generator, + struct process_memory_usage_report *report); + +/** + * @brief Fills app_cpu_usage_report. + * + * When interval > 0 the function will block for interval seconds and return report with + * process cpu usage calculated as average during interval period. + * When interval == 0 the function will not block and return report with process + * cpu usage calculated as average between current time and last call to + * report_generator_generate_app_cpu_usage_report for generator + * + * @param[in] generator system generator + * @param[in] interval in seconds, Should be >= 0 + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_app_cpu_usage_report( + report_generator_app_t *generator, + int interval, + struct app_cpu_usage_report *report); + +/** + * @brief Fills process_memory_usage_report. + * + * @param[in] generator system generator + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_app_memory_usage_report( + report_generator_app_t *generator, + struct app_memory_usage_report *report); + +/** + * @brief Fills system_load_average_report + * + * @param[out] report + * @return 0 on success, other value on failure + */ +int report_generator_generate_load_average_report(struct system_load_average_report *report); + +#endif diff --git a/src/report-json-serializer.c b/src/report-json-serializer.c new file mode 100644 index 0000000..e69de29 diff --git a/src/report-json-serializer.h b/src/report-json-serializer.h new file mode 100644 index 0000000..c953c00 --- /dev/null +++ b/src/report-json-serializer.h @@ -0,0 +1,79 @@ +/* + * 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 __REPORT_JSON_SERIALIZER_H_ +#define __REPORT_JSON_SERIALIZER_H_ + +#include "report.h" + +/** + * @brief Serializes system_load_average_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_system_load_average_report(struct system_load_average_report *report); + +/** + * @brief Serializes system_memory_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_system_memory_usage_report(struct system_memory_usage_report *report); + +/** + * @brief Serializes system_cpus_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_system_cpu_usage_report(struct system_cpu_usage_report *report); + +/** + * @brief Serializes system_cpus_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_system_cpus_usage_report(struct system_percpu_usage_report *report); + +/** + * @brief Serializes process_memory_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_process_cpu_usage_report(struct process_cpu_usage_report *report); + +/** + * @brief Serializes process_memory_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_process_memory_usage_report(struct process_memory_usage_report *report); + +/** + * @brief Serializes app_system_cpu_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_app_cpu_usage_report(struct app_cpu_usage_report *report); + +/** + * @brief Serializes app_memory_usage_report to json string + * @return dynamically allocated string on NULL on error. + * @remark returned value should be released with @free + */ +char *report_json_serializer_serialize_app_memory_usage_report(struct app_memory_usage_report *report); + +#endif diff --git a/src/report.h b/src/report.h new file mode 100644 index 0000000..9081f00 --- /dev/null +++ b/src/report.h @@ -0,0 +1,101 @@ +/* + * 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 __REPORT_H_ +#define __REPORT_H_ + +#include + +/** + * @brief Load average report + */ +struct system_load_average_report { + time_t time; /** Number of seconds after the Epoch */ + float one_min_avg; /** One minute load averate */ + float five_min_avg; /** Five minutes load average */ + float fifteen_min_avg; /** Fifteen minutes load average */ +}; + +/** + * @brief System Memory usage report + */ +struct system_memory_usage_report { + time_t time; /** Number of seconds after the Epoch */ + int total; /** Total memory (KiB) */ + int used; /** Used memory (KiB) */ + int free; /** Free memory (KiB) */ + int cache; /** Cache memory (KiB) */ + int swap; /** Swap memory (KiB) */ +}; + +/** + * @brief System CPU usage report + */ +struct system_cpu_usage_report { + time_t time; /** Number of seconds after the Epoch */ + float usage; /** Cpu utilization (Percent) */ +}; + +/** + * @brief System CPU usage report per core. + */ +struct system_percpu_usage_report { + unsigned int cpu_count; + struct system_cpu_usage_report cpus[]; +}; + +/** + * @brief Process CPU usage report. + */ +struct process_cpu_usage_report { + time_t time; /** Number of seconds after the Epoch */ + int pid; /** Process Pid */ + float usage; /** CPU utilization of process (Percent) */ +}; + +/** + * @brief Process memory usage report. + */ +struct process_memory_usage_report { + time_t time; /** Number of seconds after the Epoch */ + int pid; /** Process Pid */ + int vsz; /** Virtual memory size (KiB) */ + int rss; /** Resident set size (KiB) */ + int pss; /** Proportional set size (KiB) */ + int shared_clean; /** Not modified and mapped by other processes (KiB) */ + int shared_dirty; /** Modified and mapped by other processes (KiB) */ + int private_clean; /** Not modified and available only to that process (KiB) */ + int private_dirty; /** Modified and available only to that process (KiB) */ +}; + +/** + * @brief Application CPU usage report. + */ +struct app_cpu_usage_report { + const char appid[256]; + struct process_cpu_usage_report process_report; +}; + +/** + * @brief Application memory usage report. + */ +struct app_memory_usage_report { + const char appid[256]; + struct process_memory_usage_report process_report; +}; + +#endif + diff --git a/src/scheduler.c b/src/scheduler.c new file mode 100644 index 0000000..e69de29 diff --git a/src/task_worker.c b/src/scheduler.h similarity index 91% rename from src/task_worker.c rename to src/scheduler.h index 14bbd50..72912cb 100644 --- a/src/task_worker.c +++ b/src/scheduler.h @@ -14,8 +14,8 @@ * limitations under the License. */ +#ifndef __SCHEDULER_H_ +#define __SCHEDULER_H_ + +#endif -int main(int argc, char *argv[]) -{ - return 0; -} diff --git a/src/task-worker.c b/src/task-worker.c new file mode 100644 index 0000000..6e7291c --- /dev/null +++ b/src/task-worker.c @@ -0,0 +1,24 @@ +/* + * 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 "procfs.h" +#include "report-generator.h" +#include "report-json-serializer.h" + +int main(int argc, char *argv[]) +{ + return 0; +} -- 2.7.4