From: Vincent Guittot vincent.guittot@linaro.org
In addition to mutex resource, add new run and sleep resource types.
Signed-off-by: Vincent Guittot vincent.guittot@linaro.org Signed-off-by: Pi-Cheng Chen pi-cheng.chen@linaro.org --- src/rt-app.c | 49 +++++++++++++++++++++++++++++++++++------------ src/rt-app_parse_config.c | 13 ++++++++++++- src/rt-app_types.h | 8 ++++++++ src/rt-app_utils.c | 34 ++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 13 deletions(-)
diff --git a/src/rt-app.c b/src/rt-app.c index 0a4fe2e..1c1c9fc 100644 --- a/src/rt-app.c +++ b/src/rt-app.c @@ -174,10 +174,33 @@ static inline loadwait(struct timespec *exec_time) waste_cpu_cycles(load_count); }
+int get_resource(rtapp_resource_access_list_t *lock, struct timespec *usage) +{ + int busywait = 1; + + switch(lock->res->type) { + case rtapp_mutex: + pthread_mutex_lock(&(lock->res->res.mtx.obj)); + break; + case rtapp_sleep: + nanosleep(usage, NULL); + busywait = 0; + break; + } + + return busywait; +} + +void put_resource(rtapp_resource_access_list_t *lock) +{ + if (lock->res->type == rtapp_mutex) + pthread_mutex_unlock(&(lock->res->res.mtx.obj)); +} + void run(int ind, struct timespec *min, struct timespec *max, rtapp_tasks_resource_list_t *blockages, int nblockages) { - int i; + int i, is_busywait = 1; struct timespec t_exec; rtapp_resource_access_list_t *lock, *last;
@@ -185,7 +208,7 @@ void run(int ind, struct timespec *min, struct timespec *max,
for (i = 0; i < nblockages; i++) { - /* Lock resources */ + /* Lock resources sequence including the busy wait */ lock = blockages[i].acl; while (lock != NULL) { log_debug("[%d] locking %d", ind, lock->res->index); @@ -193,19 +216,21 @@ void run(int ind, struct timespec *min, struct timespec *max, log_ftrace(ft_data.marker_fd, "[%d] locking %d", ind, lock->res->index); - pthread_mutex_lock(&(lock->res->res.mtx.obj)); + is_busywait = get_resource(lock, &blockages[i].usage); last = lock; lock = lock->next; }
- /* Busy wait */ - log_debug("[%d] busywait for %lu", ind, timespec_to_usec(&blockages[i].usage)); - if (opts.ftrace) - log_ftrace(ft_data.marker_fd, - "[%d] busywait for %d", - ind, timespec_to_usec(&blockages[i].usage)); - loadwait(&blockages[i].usage); - t_exec = timespec_sub(&t_exec, &blockages[i].usage); + if (is_busywait) { + /* Busy wait */ + log_debug("[%d] busywait for %lu", ind, timespec_to_usec(&blockages[i].usage)); + if (opts.ftrace) + log_ftrace(ft_data.marker_fd, + "[%d] busywait for %d", + ind, timespec_to_usec(&blockages[i].usage)); + loadwait(&blockages[i].usage); + t_exec = timespec_sub(&t_exec, &blockages[i].usage); + }
/* Unlock resources */ lock = last; @@ -215,7 +240,7 @@ void run(int ind, struct timespec *min, struct timespec *max, log_ftrace(ft_data.marker_fd, "[%d] unlocking %d", ind, lock->res->index); - pthread_mutex_unlock(&(lock->res->res.mtx.obj)); + put_resource(lock); lock = lock->prev; } } diff --git a/src/rt-app_parse_config.c b/src/rt-app_parse_config.c index 9e08ffd..dcc3010 100644 --- a/src/rt-app_parse_config.c +++ b/src/rt-app_parse_config.c @@ -187,7 +187,18 @@ parse_resource_data(char *name, struct json_object *obj, int idx, data->index = idx; data->name = strdup(name);
- init_mutex_resource(data, opts); + /* resource type */ + resource_to_string(0, def_type); + type = get_string_value_from(obj, "type", TRUE, def_type); + if (type) { + if (string_to_resource(type, &data->type) != 0) { + log_critical(PIN2 "Invalid type of resource %s", type); + exit(EXIT_INV_CONFIG); + } + } + + if (data->type == rtapp_mutex) + init_mutex_resource(data, opts); }
static void diff --git a/src/rt-app_types.h b/src/rt-app_types.h index a1a62fd..eb7cb95 100644 --- a/src/rt-app_types.h +++ b/src/rt-app_types.h @@ -57,6 +57,13 @@ typedef enum policy_t #endif } policy_t;
+typedef enum resource_t +{ + rtapp_mutex = 0, + rtapp_sleep, + rtapp_run +} resource_t; + struct _rtapp_mutex { pthread_mutex_t obj; pthread_mutexattr_t attr; @@ -68,6 +75,7 @@ typedef struct _rtapp_resource_t { struct _rtapp_mutex mtx; } res; int index; + resource_t type; char *name; } rtapp_resource_t;
diff --git a/src/rt-app_utils.c b/src/rt-app_utils.c index 9012261..1e13ee0 100644 --- a/src/rt-app_utils.c +++ b/src/rt-app_utils.c @@ -219,6 +219,40 @@ policy_to_string(policy_t policy, char *policy_name) return 0; }
+int +string_to_resource(const char *name, resource_t *resource) +{ + if (strcmp(name, "mutex") == 0) + *resource = rtapp_mutex; + else if (strcmp(name, "sleep") == 0) + *resource = rtapp_sleep; + else if (strcmp(name, "run") == 0) + *resource = rtapp_run; + else + return 1; + + return 0; +} + +int +resource_to_string(resource_t resource, char *resource_name) +{ + switch (resource) { + case rtapp_mutex: + strcpy(resource_name, "mutex"); + break; + case rtapp_sleep: + strcpy(resource_name, "sleep"); + break; + case rtapp_run: + strcpy(resource_name, "run"); + break; + default: + return 1; + } + + return 0; +}
void ftrace_write(int mark_fd, const char *fmt, ...) {