Modify tid and pid from int to pid_t
[platform/core/api/resource.git] / tests / main.c
1 /* MIT License
2  *
3  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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
21  * THE SOFTWARE. */
22
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29
30
31 #include "common.h"
32 #include "cpu-boosting.h"
33
34 #define CPU_BOOSTING_API_NUM    7
35
36 #ifndef gettid
37 #include <sys/syscall.h>
38
39 #ifdef SYS_gettid
40 #define gettid() (int) syscall(SYS_gettid)
41 #else
42 #error "[CPU-BOOSTING-TEST] SYS_gettid unavailable on this system"
43 #endif
44
45 #endif
46
47 static void *thread_worker(void *arg)
48 {
49         pid_t *tid = (pid_t *)arg;
50         if (tid)
51                 *tid = gettid();
52
53         sleep(5);
54         pthread_exit(NULL);
55 }
56
57 static void test_set_cpu_boosting(resource_pid_t pid, cpu_boosting_level_e level, int timeout_msec)
58 {
59         int ret;
60
61         ret = resource_set_cpu_boosting(pid, level, timeout_msec);
62         if (ret) {
63                 _E("[CPU-BOOSTING-TEST] error = %d", ret);
64         }
65 }
66
67 static void test_clear_cpu_boosting(resource_pid_t pid)
68 {
69         int ret;
70
71         ret = resource_clear_cpu_boosting(pid);
72         if (ret) {
73                 _E("[CPU-BOOSTING-TEST] error = %d", ret);
74         }
75 }
76
77 static void test_get_cpu_boosting(resource_pid_t pid, cpu_boosting_level_info_t *level)
78 {
79         int ret;
80
81         ret = resource_get_cpu_boosting_level(pid, level);
82         if (ret) {
83                 _E("[CPU-BOOSTING-TEST] error = %d", ret);
84         }
85 }
86
87 static void test_cpu_boosting(resource_pid_t pid)
88 {
89         cpu_boosting_level_info_t cur_level;
90
91         for (int level = CPU_BOOSTING_LEVEL_WEAK; level > CPU_BOOSTING_LEVEL_NONE; level--)
92                 test_set_cpu_boosting(pid, level, -1);
93
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 */
97 }
98
99 static void one_process_one_thread_test(void)
100 {
101         resource_pid_t pid;
102
103         pid.pid = 0;
104         pid.tid = NULL;
105         pid.tid_count = 0;
106
107         test_cpu_boosting(pid);
108 }
109
110 static void multi_process_one_thread_test(int pid_count)
111 {
112         int status;
113         pid_t pid, wpid;
114
115         if (pid_count < 1)
116                 return;
117
118         for (int i = 1; i < pid_count; i++) {
119                 pid = fork();
120
121                 /* child */
122                 if (pid == 0) {
123                         one_process_one_thread_test();
124                         exit(0);
125                 }
126                 /* parent */
127                 else if (pid > 0) {
128                         continue;
129                 }
130                 else {
131                         _E("[CPU-BOOSTING-TEST] Failed to fork a new process");
132                 }
133         }
134
135         one_process_one_thread_test();
136         while ((wpid = wait(&status)) > 0);
137 }
138
139 static void one_process_multi_thread_test(int tid_count)
140 {
141         int ret;
142         int real_tid_count = 0;
143         resource_pid_t pid;
144
145         if (tid_count < 1)
146                 return;
147
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");
151                 return;
152         }
153
154         for (int i = 0; i < tid_count; i++) {
155                 pthread_t thread;
156                 ret = pthread_create(&thread, NULL, thread_worker, &pid.tid[i]);
157                 if (ret == 0) {
158                         real_tid_count++;
159                         ret = pthread_detach(thread);
160                         if (ret)
161                                 _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
162                 }
163                 else {
164                         _E("[CPU-BOOSTING-TEST] Failed to create a new thread");
165                 }
166         }
167         pid.pid = 0;
168         pid.tid_count = real_tid_count;
169         test_cpu_boosting(pid);
170         free(pid.tid);
171 }
172
173 static void one_process_all_thread_test(int tid_count)
174 {
175         int ret;
176         resource_pid_t pid;
177
178         if (tid_count < 1)
179                 return;
180
181         pid.pid = getpid();
182         pid.tid = NULL;
183         pid.tid_count = 0;
184
185         for (int i = 1; i < tid_count; i++) {
186                 pthread_t thread;
187                 ret = pthread_create(&thread, NULL, thread_worker, NULL);
188                 if (ret == 0) {
189                         ret = pthread_detach(thread);
190                         if (ret)
191                                 _E("[CPU-BOOSTING-TEST] Failed to join a new thread");
192                 }
193                 else {
194                         _E("[CPU-BOOSTING-TEST] Failed to create a new thread");
195                 }
196         }
197         test_cpu_boosting(pid);
198 }
199
200 int main (void)
201 {
202         _D("[CPU-BOOSTING-TEST] Start");
203
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();
207
208         /* Case 2: Boosting single-threaded processes */
209         _D("[CPU-BOOSTING-TEST] <<<<<<<<<< Multi Processes One Thread >>>>>>>>>>");
210         multi_process_one_thread_test(7);
211
212         /* Case 3: Boosting a multi-threaded process */
213         _D("[CPU-BOOSTING-TEST] <<<<<<<<<< One Process Multi Threads >>>>>>>>>>");
214         one_process_multi_thread_test(7);
215
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);
219
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 */
224
225         return 0;
226 }