3 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is furnished
10 * to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include <sys/types.h>
32 #include "cpu-boosting.h"
34 #define CPU_BOOSTING_API_NUM 7
37 #include <sys/syscall.h>
40 #define gettid() (int) syscall(SYS_gettid)
42 #error "[CPU-BOOSTING-TEST] SYS_gettid unavailable on this system"
47 static void *thread_worker(void *arg)
49 pid_t *tid = (pid_t *)arg;
57 static void test_set_cpu_boosting(resource_pid_t pid, cpu_boosting_level_e level, int timeout_msec)
61 ret = resource_set_cpu_boosting(pid, level, timeout_msec);
63 _E("[CPU-BOOSTING-TEST] error = %d", ret);
67 static void test_clear_cpu_boosting(resource_pid_t pid)
71 ret = resource_clear_cpu_boosting(pid);
73 _E("[CPU-BOOSTING-TEST] error = %d", ret);
77 static void test_get_cpu_boosting(resource_pid_t pid, cpu_boosting_level_info_t *level)
81 ret = resource_get_cpu_boosting_level(pid, level);
83 _E("[CPU-BOOSTING-TEST] error = %d", ret);
87 static void test_cpu_boosting(resource_pid_t pid)
89 cpu_boosting_level_info_t cur_level;
91 for (int level = CPU_BOOSTING_LEVEL_WEAK; level > CPU_BOOSTING_LEVEL_NONE; level--)
92 test_set_cpu_boosting(pid, level, -1);
94 test_get_cpu_boosting(pid, &cur_level); /* Expect CPU_BOOSTING_LEVEL_STRRONG */
95 test_clear_cpu_boosting(pid);
96 test_get_cpu_boosting(pid, &cur_level); /* Expect CPU_BOOSTING_LEVEL_NONE */
99 static void one_process_one_thread_test(void)
107 test_cpu_boosting(pid);
110 static void multi_process_one_thread_test(int pid_count)
118 for (int i = 1; i < pid_count; i++) {
123 one_process_one_thread_test();
131 _E("[CPU-BOOSTING-TEST] Failed to fork a new process");
135 one_process_one_thread_test();
136 while ((wpid = wait(&status)) > 0);
139 static void one_process_multi_thread_test(int tid_count)
142 int real_tid_count = 0;
148 pid.tid = (pid_t *)calloc(tid_count, sizeof(pid_t));
149 if (pid.tid == NULL) {
150 _E("[CPU-BOOSTING-TEST] Failed to allocate memory for tids");
154 for (int i = 0; i < tid_count; i++) {
156 ret = pthread_create(&thread, NULL, thread_worker, &pid.tid[i]);
159 ret = pthread_detach(thread);
161 _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
164 _E("[CPU-BOOSTING-TEST] Failed to create a new thread");
168 pid.tid_count = real_tid_count;
169 test_cpu_boosting(pid);
173 static void one_process_all_thread_test(int tid_count)
185 for (int i = 1; i < tid_count; i++) {
187 ret = pthread_create(&thread, NULL, thread_worker, NULL);
189 ret = pthread_detach(thread);
191 _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
194 _E("[CPU-BOOSTING-TEST] Failed to create a new thread");
197 test_cpu_boosting(pid);
202 _D("[CPU-BOOSTING-TEST] Start");
204 /* Case 1: Boosting a process with a single thread */
205 _D("[CPU-BOOSTING-TEST] <<<<<<<<<< One Process One Thread >>>>>>>>>>");
206 one_process_one_thread_test();
208 /* Case 2: Boosting single-threaded processes */
209 _D("[CPU-BOOSTING-TEST] <<<<<<<<<< Multi Processes One Thread >>>>>>>>>>");
210 multi_process_one_thread_test(7);
212 /* Case 3: Boosting a multi-threaded process */
213 _D("[CPU-BOOSTING-TEST] <<<<<<<<<< One Process Multi Threads >>>>>>>>>>");
214 one_process_multi_thread_test(7);
216 /* Case 4: Boosting all threads in a process */
217 _D("[CPU-BOOSTING-TEST] <<<<<<<<<< One Process All Threads >>>>>>>>>>");
218 one_process_all_thread_test(7);
220 /* Case 5: Inheritance to a process with a signle thread */
221 /* Case 6: Inheritance to single-threaded processes */
222 /* Case 7: Inheritance to a multi-threaded process */
223 /* Case 8: Inheritance to all threads in a process */