},
};
-static int bus_init(struct resource *res)
+static int bus_create(struct resource *res)
{
struct bus_context *ctx;
return 0;
}
-static void bus_exit(struct resource *res)
+static void bus_delete(struct resource *res)
{
struct bus_context *ctx;
.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)
},
};
-static int cpu_init(struct resource *res)
+static int cpu_create(struct resource *res)
{
struct cpu_context *ctx = get_resource_privdata(res);
return 0;
}
-static void cpu_exit(struct resource *res)
+static void cpu_delete(struct resource *res)
{
struct cpu_context *ctx;
.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)
return 0;
}
-static int disk_init(struct resource *res)
+static int disk_create(struct resource *res)
{
struct disk_context *ctx;
return 0;
}
-static void disk_exit(struct resource *res)
+static void disk_delete(struct resource *res)
{
struct disk_context *ctx;
.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,
},
};
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;
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;
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,
.ctrls = display_ctrls,
.num_ctrls = ARRAY_SIZE(display_ctrls),
.ops = {
+ .create = display_create,
+ .delete = display_delete,
.init = display_init,
.exit = display_exit,
},
},
};
-static int gpu_init(struct resource *res)
+static int gpu_create(struct resource *res)
{
struct gpu_context *ctx;
return 0;
}
-static void gpu_exit(struct resource *res)
+static void gpu_delete(struct resource *res)
{
struct gpu_context *ctx;
.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)
},
};
-static int network_init(struct resource *res)
+static int network_create(struct resource *res)
{
struct network_context *ctx;
return 0;
}
-static void network_exit(struct resource *res)
+static void network_delete(struct resource *res)
{
struct network_context *ctx;
.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)
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,
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[] = {
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)
return 0;
}
-static void process_group_exit(struct resource *res)
+static void process_group_delete(struct resource *res)
{
struct process_group_context *ctx;
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,
.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)
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,
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:
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,
const void *data)
{
struct process_context *ctx;
- u_int64_t total_memory;
int ret;
bool include_gpu_mem;
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();
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;
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,
.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,
},
};
},
};
-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);
return ret;
}
-static void system_driver_exit(struct resource *res)
+static void system_delete(struct resource *res)
{
struct system_resource_data *sysdata;
.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,
},
};