From 649168d3dafe8b73401560adb288c2c13ad7806a Mon Sep 17 00:00:00 2001 From: Przemyslaw Kedzierski Date: Mon, 3 Mar 2014 16:14:55 +0100 Subject: [PATCH] Add utils, tools and tests. These tools are for testing core functionalities used by packet and system like small in-file databases, hash tables, cgroups, network udp traffic, and getting various system information about processes and network. Change-Id: I775706b19d3b1c829d7b457b065562da7f33ac92 Signed-off-by: Przemyslaw Kedzierski --- CMakeLists/init-needed-environment.txt | 1 + CMakeLists/resourced.txt | 10 +++ src/utils/cgroup-test.c | 41 ++++++++++++ src/utils/database.c | 30 +++++++++ src/utils/hashtest.c | 16 +++++ src/utils/monitored.c | 11 ++++ src/utils/proc-stat-test.c | 114 +++++++++++++++++++++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 src/utils/cgroup-test.c create mode 100644 src/utils/database.c create mode 100644 src/utils/hashtest.c create mode 100644 src/utils/monitored.c create mode 100644 src/utils/proc-stat-test.c diff --git a/CMakeLists/init-needed-environment.txt b/CMakeLists/init-needed-environment.txt index c13e78b..838550b 100644 --- a/CMakeLists/init-needed-environment.txt +++ b/CMakeLists/init-needed-environment.txt @@ -82,6 +82,7 @@ SET(INCLUDE_PUBLIC_DIR ${CMAKE_SOURCE_DIR}/include) SET(RESOURCED_INCLUDEDIR ${INCLUDE_COMMON_DIR} ${INCLUDE_PUBLIC_DIR}) SET(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) +SET(UTILS_SOURCE_DIR ${SOURCE_DIR}/utils) SET(RESOURCED_SOURCE_DIR ${SOURCE_DIR}/resourced) SET(LIBRESOURCED_SOURCE_DIR ${SOURCE_DIR}/libresourced) SET(PROC-STAT_SOURCE_DIR ${SOURCE_DIR}/proc-stat) diff --git a/CMakeLists/resourced.txt b/CMakeLists/resourced.txt index 49b2f41..b29876a 100644 --- a/CMakeLists/resourced.txt +++ b/CMakeLists/resourced.txt @@ -93,6 +93,16 @@ ENDFOREACH(flag) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +ADD_EXECUTABLE(hashtest ${UTILS_SOURCE_DIR}/hashtest.c) +TARGET_LINK_LIBRARIES(hashtest ${daemon_pkgs_LDFLAGS}) + +ADD_EXECUTABLE(cgroup-test ${UTILS_SOURCE_DIR}/cgroup-test.c + ${CGROUP_SOURCE_DIR}/cgroup.c + ${COMMON_SOURCE_DIR}/file-helper.c + ${COMMON_SOURCE_DIR}/appid-helper.c + ) +TARGET_LINK_LIBRARIES(cgroup-test ${daemon_pkgs_LDFLAGS} "-L/lib/ -lrt") + ADD_EXECUTABLE (${PROJECT_NAME} ${HEADERS} ${SOURCES}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${daemon_pkgs_LDFLAGS} diff --git a/src/utils/cgroup-test.c b/src/utils/cgroup-test.c new file mode 100644 index 0000000..e1046da --- /dev/null +++ b/src/utils/cgroup-test.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include + +#include "cgroup.h" + +int64_t timespecDiff(struct timespec *time_a, struct timespec *time_b) +{ + return ((time_a->tv_sec * 1000000000) + time_a->tv_nsec) - + ((time_b->tv_sec * 1000000000) + time_b->tv_nsec); +} + +#define START_MEASURE {\ + struct timespec start, end; \ + clock_gettime(CLOCK_MONOTONIC, &start); + +#define END_MEASURE \ + clock_gettime(CLOCK_MONOTONIC, &end); \ + int64_t timeElapsed = timespecDiff(&end, &start);\ + printf("time diff %"PRId64"\n", timeElapsed); \ + } + +#define TEST_NUM 100 + +int main(void) +{ + int i = 0; + char cgroup_name[128]; + + printf("start measure"); + for (; i < TEST_NUM; ++i) { + sprintf(cgroup_name, "com.samsung.browser%d", i); + START_MEASURE + make_net_cls_cgroup_with_pid(i, cgroup_name); + END_MEASURE + } + + return 0; +} diff --git a/src/utils/database.c b/src/utils/database.c new file mode 100644 index 0000000..b1cbbfc --- /dev/null +++ b/src/utils/database.c @@ -0,0 +1,30 @@ +#include +#include +#include + +#include "app-stat.h" +#include "macro.h" +#include "storage.h" + +int main(int argc, char **argv) +{ + struct application_stat requests[] = { + {"emacs", 24, 42}, + {"vim", 43, 49}, + {"emacs", 52, 56} + };/*It's not standards compliant, but more robust */ + + int index; + struct application_stat_tree *app_tree = create_app_stat_tree(); + + init_database("./base.db"); + + for (index = 0; index != ARRAY_SIZE(requests); ++index) + g_tree_insert((GTree *) app_tree->tree, + (gpointer)index, (gpointer)(requests + index)); + + store_result(app_tree, 0); /*0 time period means alway perform storing*/ + close_database(); + free_app_stat_tree(app_tree); + return 0; +} diff --git a/src/utils/hashtest.c b/src/utils/hashtest.c new file mode 100644 index 0000000..e4fc481 --- /dev/null +++ b/src/utils/hashtest.c @@ -0,0 +1,16 @@ +#include +#include +static gboolean int_cmp(const void *ptr1, const void *ptr2) +{ + return (GPOINTER_TO_INT(ptr1) == GPOINTER_TO_INT(ptr2)) ? TRUE : FALSE; +} + +int main(void) +{ + GHashTable *table = g_hash_table_new(g_direct_hash, int_cmp); + void *ptr = 0; + g_hash_table_insert(table, GINT_TO_POINTER(42), main); + ptr = g_hash_table_lookup(table, GINT_TO_POINTER(42)); + printf("%p\n%p\n", ptr, main); + return 0; +} diff --git a/src/utils/monitored.c b/src/utils/monitored.c new file mode 100644 index 0000000..5b6abf3 --- /dev/null +++ b/src/utils/monitored.c @@ -0,0 +1,11 @@ +#include "classid-helper.h" +#include "macro.h" + +int main(int argc, char **argv) +{ + int_array *pids = get_monitored_pids(); + array_foreach(key, int, pids) { + printf("%d\n", *key); + } + return 0; +} diff --git a/src/utils/proc-stat-test.c b/src/utils/proc-stat-test.c new file mode 100644 index 0000000..d24647b --- /dev/null +++ b/src/utils/proc-stat-test.c @@ -0,0 +1,114 @@ +/* + * resourced + * + * Lib for getting process statistics + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 +#include +#include + + + +int main(void) +{ + GArray *valid_proc_infos = NULL; + GArray *terminated_proc_infos = NULL; + proc_stat_system_time st_diff; + + terminated_proc_infos = g_array_new(false, false, sizeof(proc_stat_process_info)); + valid_proc_infos = g_array_new(false, false, sizeof(proc_stat_process_info)); + + + proc_stat_init(); + + while (true) { + + proc_stat_get_process_info(valid_proc_infos, terminated_proc_infos, NULL); + proc_stat_get_system_time_diff(&st_diff); + + if (st_diff.total_time != 0) { + + double total_time = st_diff.total_time; + + printf("Total CPU Info : %3.2lf%%us %3.2lf%%sy %3.2lf%%ni %3.2lf%%id %3.2lf%%iowait %3.2lf%%irq %3.2lf%%softirq\n", + (double)st_diff.user_time / total_time * 100, + (double)st_diff.system_time / total_time * 100, + (double)st_diff.nice_time / total_time * 100, + (double)st_diff.idle_time / total_time * 100, + (double)st_diff.iowait_time / total_time * 100, + (double)st_diff.irq_time / total_time * 100, + (double)st_diff.softirq_time / total_time * 100); + + unsigned int total, free; + if (proc_stat_get_total_mem_size(&total) && proc_stat_get_free_mem_size(&free)) + printf("Total Memory Info : Total:%dMB Free:%dMB Used:%dMB\n", total, free, total - free); + + unsigned int i = 0; + for (i = 0; i < valid_proc_infos->len; ++i) { + proc_stat_process_info *ps = &g_array_index(valid_proc_infos, proc_stat_process_info, i); + + if ((ps->active) || (ps->fresh)) { + if (ps->fresh) + printf("N "); + else + printf(" "); + + printf("[pid:%d\t name:%40s utime:%3.2lf%% stime:%3.2lf%% rss:%dKb\n", + ps->pid, ps->name, + (double)(ps->utime_diff)/(double)st_diff.total_time*100, + (double)(ps->stime_diff)/(double)st_diff.total_time*100, + ps->rss); + } + } + + for (i = 0; i < terminated_proc_infos->len; ++i) { + + proc_stat_process_info *ps = &g_array_index(terminated_proc_infos, proc_stat_process_info, i); + + printf("T "); + printf("[pid:%d\t name:%40s\n", + ps->pid, ps->name); + } + + } + + usleep(1000000); + g_array_set_size(valid_proc_infos, 0); + g_array_set_size(terminated_proc_infos, 0); + + printf("-------------------------------------------------------------------------------\n"); + + } + + if (valid_proc_infos) { + g_array_free(valid_proc_infos, true); + valid_proc_infos = NULL; + } + + + if (terminated_proc_infos) { + g_array_free(terminated_proc_infos, true); + terminated_proc_infos = NULL; + } + + proc_stat_finalize(); + + return 0; +} -- 2.7.4