| 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (c) 2025 Intel Corporation |
| 4 | */ |
| 5 | #include "workload.h" |
| 6 | #include "stat.h" |
| 7 | #include <dlfcn.h> |
| 8 | |
| 9 | /* |
| 10 | * Parse input for arguments store them in argc for count and argv for vector |
| 11 | * of arguments. |
| 12 | */ |
| 13 | void string_to_argc_argv(const char *input, int *argc, char ***argv) |
| 14 | { |
| 15 | char *token; |
| 16 | int count = 1; |
| 17 | char *temp_input = strdup(input); /* Duplicate to avoid modifying original */ |
| 18 | |
| 19 | /* First pass: count the number of arguments */ |
| 20 | token = strtok(temp_input, " "); |
| 21 | while (token != NULL) { |
| 22 | count++; |
| 23 | token = strtok(NULL, " "); |
| 24 | } |
| 25 | |
| 26 | /* Allocate memory for argv array */ |
| 27 | *argv = malloc(count * sizeof(char *)); |
| 28 | if (*argv == NULL) { |
| 29 | fprintf(stderr, "Memory allocation failed\n"); |
| 30 | exit(1); |
| 31 | } |
| 32 | |
| 33 | /* Second pass: populate argv */ |
| 34 | strcpy(temp_input, input); /* Reset temp_input with original input */ |
| 35 | int i = 1; |
| 36 | token = strtok(temp_input, " "); |
| 37 | while (token != NULL) { |
| 38 | (*argv)[i++] = strdup(token); /* Duplicate token and assign */ |
| 39 | token = strtok(NULL, " "); |
| 40 | } |
| 41 | (*argv)[0] = strdup("Workload"); |
| 42 | |
| 43 | *argc = count; /* Set argc to the number of arguments */ |
| 44 | free(temp_input); /* Free the temporary input string */ |
| 45 | } |
| 46 | |
| 47 | void *workload_thread_routine(void *data) |
| 48 | { |
| 49 | struct thread_context *thread_context = data; |
| 50 | struct workload_config *wl_cfg = thread_context->workload; |
| 51 | struct timespec start_ts, timeout; |
| 52 | |
| 53 | pthread_mutex_lock(&wl_cfg->workload_mutex); |
| 54 | /* Run until we are ready to stop */ |
| 55 | while (!thread_context->stop) { |
| 56 | clock_gettime(app_config.application_clock_id, &start_ts); |
| 57 | timeout = start_ts; |
| 58 | timeout.tv_sec++; |
| 59 | /* Check for spurious wakeups */ |
| 60 | if (wl_cfg->workload_run) { |
| 61 | wl_cfg->workload_function(wl_cfg->workload_argc, wl_cfg->workload_argv); |
| 62 | wl_cfg->workload_done = 1; |
| 63 | wl_cfg->workload_run = 0; |
| 64 | stat_frame_workload(wl_cfg->associated_frame, |
| 65 | wl_cfg->workload_sequence_counter, start_ts); |
| 66 | wl_cfg->workload_sequence_counter++; |
| 67 | } |
| 68 | pthread_cond_timedwait(&wl_cfg->workload_cond, &wl_cfg->workload_mutex, &timeout); |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | void workload_context_init(struct thread_context *thread_context, char *workload_file, |
| 74 | char *workload_function, char *workload_arguments, |
| 75 | char *workload_setup_function, char *workload_setup_arguments, |
| 76 | enum stat_frame_type frame_type) |
| 77 | { |
| 78 | struct workload_config *wl_cfg = thread_context->workload; |
| 79 | char *error; |
| 80 | |
| 81 | wl_cfg->workload_handler = dlopen(workload_file, RTLD_NOW | RTLD_GLOBAL); |
| 82 | |
| 83 | if (!wl_cfg->workload_handler) { |
| 84 | error = dlerror(); |
| 85 | fprintf(stderr, "Error: Unable to open workload: %s. %s\n", workload_file, error); |
| 86 | exit(EXIT_FAILURE); |
| 87 | } |
| 88 | |
| 89 | if (workload_setup_function != NULL) { |
| 90 | wl_cfg->workload_setup_function = |
| 91 | dlsym(wl_cfg->workload_handler, workload_setup_function); |
| 92 | if (!wl_cfg->workload_setup_function) { |
| 93 | fprintf(stderr, "Error: Unable to find setup function: %s\n", |
| 94 | workload_setup_function); |
| 95 | exit(EXIT_FAILURE); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | wl_cfg->workload_function = dlsym(wl_cfg->workload_handler, workload_function); |
| 100 | if (!wl_cfg->workload_function) { |
| 101 | fprintf(stderr, "Error: Unable to find function: %s\n", workload_function); |
| 102 | exit(EXIT_FAILURE); |
| 103 | } |
| 104 | |
| 105 | if (workload_arguments != NULL) { |
| 106 | string_to_argc_argv(workload_arguments, &(wl_cfg->workload_argc), |
| 107 | &(wl_cfg->workload_argv)); |
| 108 | } else { |
| 109 | wl_cfg->workload_argc = 0; |
| 110 | wl_cfg->workload_argv = NULL; |
| 111 | } |
| 112 | |
| 113 | if (workload_setup_arguments != NULL) { |
| 114 | string_to_argc_argv(workload_setup_arguments, &(wl_cfg->workload_setup_argc), |
| 115 | &(wl_cfg->workload_setup_argv)); |
| 116 | } else { |
| 117 | wl_cfg->workload_setup_argc = 0; |
| 118 | wl_cfg->workload_setup_argv = NULL; |
| 119 | } |
| 120 | |
| 121 | pthread_mutex_init(&wl_cfg->workload_mutex, NULL); |
| 122 | pthread_cond_init(&wl_cfg->workload_cond, NULL); |
| 123 | |
| 124 | wl_cfg->associated_frame = frame_type; |
| 125 | |
| 126 | /* Call the setup function if it exists */ |
| 127 | if (wl_cfg->workload_setup_function != NULL) { |
| 128 | wl_cfg->workload_setup_function(wl_cfg->workload_setup_argc, |
| 129 | wl_cfg->workload_setup_argv); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void workload_thread_free(struct thread_context *thread_context) |
| 134 | { |
| 135 | if (!thread_context) |
| 136 | return; |
| 137 | dlclose(thread_context->workload->workload_handler); |
| 138 | free(thread_context->workload); |
| 139 | } |
| 140 | |
| 141 | void workload_thread_wait_for_finish(struct thread_context *thread_context) |
| 142 | { |
| 143 | thread_context->stop = 1; |
| 144 | if (!thread_context) |
| 145 | return; |
| 146 | |
| 147 | pthread_join(thread_context->workload->workload_task_id, NULL); |
| 148 | } |