resource: Distinguish driver initalization from instance creation 82/279982/2 submit/sandbox/chanwoochoi/tizen/20220823.070906 submit/tizen/20220823.075403
authorDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 19 Aug 2022 09:21:57 +0000 (18:21 +0900)
committerDongwoo Lee <dwoo08.lee@samsung.com>
Mon, 22 Aug 2022 23:37:48 +0000 (08:37 +0900)
In the previous commits, each resource driver has the distinguished
operation for both initialization and creation. This makes the codes
are properly distributed into two operations.

Change-Id: Ia3d72914a3277e0d6d18634dff60a9e9763ec362
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/resource/resource-bus.c
src/resource/resource-cpu.c
src/resource/resource-disk.c
src/resource/resource-display.c
src/resource/resource-gpu.c
src/resource/resource-network.c
src/resource/resource-process-group.c
src/resource/resource-process.c
src/resource/resource-system.c

index 57c02c1..007d1f4 100644 (file)
@@ -219,7 +219,7 @@ static const struct resource_control bus_ctrls[] = {
        },
 };
 
-static int bus_init(struct resource *res)
+static int bus_create(struct resource *res)
 {
        struct bus_context *ctx;
 
@@ -232,7 +232,7 @@ static int bus_init(struct resource *res)
        return 0;
 }
 
-static void bus_exit(struct resource *res)
+static void bus_delete(struct resource *res)
 {
        struct bus_context *ctx;
 
@@ -259,8 +259,8 @@ static const struct resource_driver bus_resource_driver = {
        .ctrls          = bus_ctrls,
        .num_ctrls      = ARRAY_SIZE(bus_ctrls),
        .ops            = {
-               .init = bus_init,
-               .exit = bus_exit,
+               .create = bus_create,
+               .delete = bus_delete,
        },
 };
 RESOURCE_DRIVER_REGISTER(&bus_resource_driver)
index 23cc507..82ac454 100644 (file)
@@ -218,7 +218,7 @@ static const struct resource_control cpu_ctrls[] = {
        },
 };
 
-static int cpu_init(struct resource *res)
+static int cpu_create(struct resource *res)
 {
        struct cpu_context *ctx = get_resource_privdata(res);
 
@@ -231,7 +231,7 @@ static int cpu_init(struct resource *res)
        return 0;
 }
 
-static void cpu_exit(struct resource *res)
+static void cpu_delete(struct resource *res)
 {
        struct cpu_context *ctx;
 
@@ -259,8 +259,8 @@ static const struct resource_driver cpu_resource_driver = {
        .ctrls          = cpu_ctrls,
        .num_ctrls      = ARRAY_SIZE(cpu_ctrls),
        .ops            = {
-               .init = cpu_init,
-               .exit = cpu_exit,
+               .create = cpu_create,
+               .delete = cpu_delete,
        },
 };
 RESOURCE_DRIVER_REGISTER(&cpu_resource_driver)
index 3493241..cc6a983 100644 (file)
@@ -321,7 +321,7 @@ static int calculate_disk_stats(struct disk_context *ctx)
        return 0;
 }
 
-static int disk_init(struct resource *res)
+static int disk_create(struct resource *res)
 {
        struct disk_context *ctx;
 
@@ -336,7 +336,7 @@ static int disk_init(struct resource *res)
        return 0;
 }
 
-static void disk_exit(struct resource *res)
+static void disk_delete(struct resource *res)
 {
        struct disk_context *ctx;
 
@@ -402,8 +402,8 @@ static const struct resource_driver disk_resource_driver = {
        .ctrls          = disk_ctrls,
        .num_ctrls      = ARRAY_SIZE(disk_ctrls),
        .ops            = {
-               .init = disk_init,
-               .exit = disk_exit,
+               .create = disk_create,
+               .delete = disk_delete,
                .prepare_update = disk_prepare_update,
        },
 };
index 724b5df..bd3b203 100644 (file)
@@ -250,37 +250,7 @@ static int display_monitor_func(void *data, void **result)
        return THREAD_RETURN_CONTINUE;
 }
 
-static int init_display_monitor_thread(struct resource *res)
-{
-       int ret;
-
-       g_fps_monitor.resource_count = get_resource_device_count(get_resource_type(res));
-       g_fps_monitor.last_fps = calloc(g_fps_monitor.resource_count, sizeof(double));
-       if (!g_fps_monitor.last_fps)
-               return -ENOMEM;
-
-       ret = create_timer_thread(&g_fps_monitor.thread, DISPLAY_SAMPLE_RATE_MSEC,
-                       display_monitor_func, NULL);
-
-       return ret;
-}
-
-static void __DESTRUCTOR__ exit_display_monitor_thread(void)
-{
-       if (g_fps_monitor.thread != NULL) {
-               destroy_thread(g_fps_monitor.thread);
-               g_fps_monitor.thread = NULL;
-       }
-
-       if (g_fps_monitor.last_fps) {
-               free(g_fps_monitor.last_fps);
-               g_fps_monitor.last_fps = NULL;
-       }
-
-       g_fps_monitor.resource_count = 0;
-}
-
-static int display_init(struct resource *res)
+static int display_create(struct resource *res)
 {
        int ret = 0;
        struct display_context *ctx;
@@ -294,18 +264,14 @@ static int display_init(struct resource *res)
        set_resource_privdata(res, ctx);
 
        g_mutex_lock(&g_fps_monitor.display_lock);
-       if (g_fps_monitor.holder++ == 0) {
-               if (!g_fps_monitor.thread)
-                       ret = init_display_monitor_thread(res);
-               else
-                       resume_thread(g_fps_monitor.thread);
-       }
+       if (g_fps_monitor.holder++ == 0)
+               resume_thread(g_fps_monitor.thread);
        g_mutex_unlock(&g_fps_monitor.display_lock);
 
        return ret;
 }
 
-static void display_exit(struct resource *res)
+static void display_delete(struct resource *res)
 {
        struct display_context *ctx;
 
@@ -323,11 +289,41 @@ static void display_exit(struct resource *res)
        set_resource_privdata(res, NULL);
 
        g_mutex_lock(&g_fps_monitor.display_lock);
-       if (--g_fps_monitor.holder == 0 && g_fps_monitor.thread != NULL)
+       if (--g_fps_monitor.holder == 0)
                suspend_thread(g_fps_monitor.thread);
        g_mutex_unlock(&g_fps_monitor.display_lock);
 }
 
+static int display_init(void)
+{
+       int ret;
+
+       g_fps_monitor.resource_count = get_resource_device_count(RESOURCE_TYPE_DISPLAY);
+       g_fps_monitor.last_fps = calloc(g_fps_monitor.resource_count, sizeof(double));
+       if (!g_fps_monitor.last_fps)
+               return -ENOMEM;
+
+       ret = create_timer_thread(&g_fps_monitor.thread, DISPLAY_SAMPLE_RATE_MSEC,
+                       display_monitor_func, NULL);
+
+       return ret;
+}
+
+static void display_exit(void)
+{
+       if (g_fps_monitor.thread) {
+               destroy_thread(g_fps_monitor.thread);
+               g_fps_monitor.thread = NULL;
+       }
+
+       if (g_fps_monitor.last_fps) {
+               free(g_fps_monitor.last_fps);
+               g_fps_monitor.last_fps = NULL;
+       }
+
+       g_fps_monitor.resource_count = 0;
+}
+
 static const struct resource_driver display_resource_driver = {
        .name           = "DISPLAY",
        .type           = RESOURCE_TYPE_DISPLAY,
@@ -336,6 +332,8 @@ static const struct resource_driver display_resource_driver = {
        .ctrls          = display_ctrls,
        .num_ctrls      = ARRAY_SIZE(display_ctrls),
        .ops            = {
+               .create = display_create,
+               .delete = display_delete,
                .init = display_init,
                .exit = display_exit,
        },
index 9112f3c..5deae14 100644 (file)
@@ -219,7 +219,7 @@ static const struct resource_control gpu_ctrls[] = {
        },
 };
 
-static int gpu_init(struct resource *res)
+static int gpu_create(struct resource *res)
 {
        struct gpu_context *ctx;
 
@@ -232,7 +232,7 @@ static int gpu_init(struct resource *res)
        return 0;
 }
 
-static void gpu_exit(struct resource *res)
+static void gpu_delete(struct resource *res)
 {
        struct gpu_context *ctx;
 
@@ -259,8 +259,8 @@ static const struct resource_driver gpu_resource_driver = {
        .ctrls          = gpu_ctrls,
        .num_ctrls      = ARRAY_SIZE(gpu_ctrls),
        .ops            = {
-               .init = gpu_init,
-               .exit = gpu_exit,
+               .create = gpu_create,
+               .delete = gpu_delete,
        },
 };
 RESOURCE_DRIVER_REGISTER(&gpu_resource_driver)
index cfc4887..b05f9ce 100644 (file)
@@ -114,7 +114,7 @@ static const struct resource_control network_ctrls[] = {
        },
 };
 
-static int network_init(struct resource *res)
+static int network_create(struct resource *res)
 {
        struct network_context *ctx;
 
@@ -129,7 +129,7 @@ static int network_init(struct resource *res)
        return 0;
 }
 
-static void network_exit(struct resource *res)
+static void network_delete(struct resource *res)
 {
        struct network_context *ctx;
 
@@ -155,8 +155,8 @@ static const struct resource_driver network_resource_driver = {
        .ctrls          = network_ctrls,
        .num_ctrls      = ARRAY_SIZE(network_ctrls),
        .ops            = {
-               .init = network_init,
-               .exit = network_exit,
+               .create = network_create,
+               .delete = network_delete,
        },
 };
 RESOURCE_DRIVER_REGISTER(&network_resource_driver)
index f0a9a4d..5bdbf52 100644 (file)
@@ -51,7 +51,7 @@ struct process_group_context {
 
 static u_int64_t total_memory;
 static long jiffy;
-static int taskstat_support = -1;
+static bool taskstat_support;
 
 static int process_group_get_pid_list(struct resource *res,
                                const struct resource_attribute *attr,
@@ -243,13 +243,13 @@ static int process_group_get_disk_attrs(struct resource *res,
 static bool process_group_check_taskstat_support(struct resource *resource,
                                            const struct resource_attribute *attr)
 {
-       return !!taskstat_support;
+       return taskstat_support;
 }
 
 static bool process_group_check_gpu_support(struct resource *resource,
                                            const struct resource_attribute *attr)
 {
-       return !!taskstat_support && kernel_check_gpu_support();
+       return taskstat_support && kernel_check_gpu_support();
 }
 
 static const struct resource_attribute process_group_attrs[] = {
@@ -649,27 +649,9 @@ out_close:
        return ret;
 }
 
-static int process_group_init(struct resource *res)
+static int process_group_create(struct resource *res)
 {
        struct process_group_context *ctx;
-       int ret;
-
-       if (taskstat_support < 0)
-               taskstat_support = (int)kernel_check_taskstat_support();
-
-       if (jiffy == 0) {
-               /* get system USER_HZ at once */
-               jiffy = sysconf(_SC_CLK_TCK);
-               if (jiffy < 0)
-                       return -EINVAL;
-       }
-
-       if (total_memory == 0) {
-               /* get system total memory once at init*/
-               ret = kernel_get_memory_total(&total_memory);
-               if (ret < 0)
-                       return -EINVAL;
-       }
 
        ctx = malloc(sizeof(struct process_group_context));
        if (!ctx)
@@ -683,7 +665,7 @@ static int process_group_init(struct resource *res)
        return 0;
 }
 
-static void process_group_exit(struct resource *res)
+static void process_group_delete(struct resource *res)
 {
        struct process_group_context *ctx;
 
@@ -703,6 +685,25 @@ static void process_group_exit(struct resource *res)
        set_resource_privdata(res, NULL);
 }
 
+static int process_group_init(void)
+{
+       int ret;
+
+       /* get system USER_HZ at once */
+       jiffy = sysconf(_SC_CLK_TCK);
+       if (jiffy < 0)
+               return -EINVAL;
+
+       /* get system total memory once at init*/
+       ret = kernel_get_memory_total(&total_memory);
+       if (ret < 0)
+               return -EINVAL;
+
+       taskstat_support = kernel_check_taskstat_support();
+
+       return 0;
+}
+
 static const struct resource_driver process_group_driver = {
        .name           = "PROCESS_GROUP",
        .type           = RESOURCE_TYPE_PROCESS_GROUP,
@@ -712,9 +713,10 @@ static const struct resource_driver process_group_driver = {
        .ctrls          = process_group_ctrls,
        .num_ctrls      = ARRAY_SIZE(process_group_ctrls),
        .ops            = {
-               .init = process_group_init,
-               .exit = process_group_exit,
+               .create = process_group_create,
+               .delete = process_group_delete,
                .prepare_update = process_group_prepare_update,
+               .init = process_group_init,
        },
 };
 RESOURCE_DRIVER_REGISTER(&process_group_driver)
index e77d765..dfbe086 100644 (file)
@@ -37,11 +37,11 @@ struct process_context {
        u_int64_t prev_total_time;
        double cpu_period;
        int online_cpu;
-       u_int64_t total_memory;
 };
 
+static u_int64_t total_memory;
 static long jiffy;
-static int taskstat_support = -1;
+static bool taskstat_support;
 
 static int process_get_cpu_util(struct resource *res,
                                const struct resource_attribute *attr,
@@ -113,7 +113,7 @@ static int process_get_mem_attrs(struct resource *res,
 
                curr = &ctx->curr;
                *percent = (curr->coremem * 1024) / curr->ac_stime;
-               *percent /= ctx->total_memory * 100.0;
+               *percent /= total_memory * 100.0;
                break;
        }
        case PROCESS_ATTR_MEM_PSS:
@@ -252,7 +252,7 @@ static int process_get_context_data(struct resource *res,
 static bool process_check_taskstat_support(struct resource *resource,
                                            const struct resource_attribute *attr)
 {
-       return !!taskstat_support;
+       return taskstat_support;
 }
 
 static bool process_check_gpu_support(struct resource *resource,
@@ -394,7 +394,6 @@ static int process_setup_tgid(struct resource *res,
                                const void *data)
 {
        struct process_context *ctx;
-       u_int64_t total_memory;
        int ret;
        bool include_gpu_mem;
 
@@ -406,10 +405,8 @@ static int process_setup_tgid(struct resource *res,
                return -EINVAL;
 
        include_gpu_mem = is_resource_attr_interested(res, PROCESS_ATTR_MEM_GPU);
-       total_memory = ctx->total_memory;
        memset(ctx, 0, sizeof(*ctx));
 
-       ctx->total_memory = total_memory;
        ctx->tgid = (pid_t)(intptr_t)data;
        ctx->prev_total_time = get_total_cpu_time();
 
@@ -487,37 +484,20 @@ static int process_prepare_update(struct resource *res)
        return 0;
 }
 
-static int process_init(struct resource *res)
+static int process_create(struct resource *res)
 {
        struct process_context *ctx;
-       int ret;
-
-       if (jiffy == 0) {
-               /* get system USER_HZ at once */
-               jiffy = sysconf(_SC_CLK_TCK);
-               if (jiffy < 0)
-                       return -EINVAL;
-       }
-
-       if (taskstat_support < 0)
-               taskstat_support = (int)kernel_check_taskstat_support();
 
        ctx = calloc(1, sizeof(struct process_context));
        if (!ctx)
                return -ENOMEM;
 
-       ret = kernel_get_memory_total(&ctx->total_memory);
-       if (ret < 0) {
-               free(ctx);
-               return -EINVAL;
-       }
-
        set_resource_privdata(res, ctx);
 
        return 0;
 }
 
-static void process_exit(struct resource *res)
+static void process_delete(struct resource *res)
 {
        struct process_context *ctx;
 
@@ -532,6 +512,24 @@ static void process_exit(struct resource *res)
        set_resource_privdata(res, NULL);
 }
 
+static int process_init(void)
+{
+       int ret;
+
+       /* get system USER_HZ at once */
+       jiffy = sysconf(_SC_CLK_TCK);
+       if (jiffy < 0)
+               return -EINVAL;
+
+       ret = kernel_get_memory_total(&total_memory);
+       if (ret < 0)
+               return -EINVAL;
+
+       taskstat_support = kernel_check_taskstat_support();
+
+       return 0;
+}
+
 static const struct resource_driver process_resource_driver = {
        .name           = "PROCESS",
        .type           = RESOURCE_TYPE_PROCESS,
@@ -541,8 +539,9 @@ static const struct resource_driver process_resource_driver = {
        .ctrls          = process_ctrls,
        .num_ctrls      = ARRAY_SIZE(process_ctrls),
        .ops = {
+               .create = process_create,
+               .delete = process_delete,
                .init = process_init,
-               .exit = process_exit,
                .prepare_update = process_prepare_update,
        },
 };
index dcbb343..6343b11 100644 (file)
@@ -226,7 +226,7 @@ static const struct resource_attribute system_attrs[] = {
        },
 };
 
-static int system_driver_init(struct resource *res)
+static int system_create(struct resource *res)
 {
        struct system_resource_data *sysdata;
        const char *res_name = get_resource_name(res);
@@ -272,7 +272,7 @@ err:
        return ret;
 }
 
-static void system_driver_exit(struct resource *res)
+static void system_delete(struct resource *res)
 {
        struct system_resource_data *sysdata;
 
@@ -324,8 +324,8 @@ static const struct resource_driver system_resource_driver = {
        .attrs          = system_attrs,
        .num_attrs      = ARRAY_SIZE(system_attrs),
        .ops = {
-               .init           = system_driver_init,
-               .exit           = system_driver_exit,
+               .create         = system_create,
+               .delete         = system_delete,
                .prepare_update = system_driver_prepare_update,
        },
 };