blob: 3bd5d77e53b5150a04fbe7731b433e8a0491bee9 [file] [log] [blame]
subrata_modak6acdc8e2007-12-26 11:11:45 +00001/******************************************************************************
2 *
subrata_modak13782052008-02-22 14:43:53 +00003 * Copyright © International Business Machines Corp., 2005, 2008
subrata_modak6acdc8e2007-12-26 11:11:45 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * NAME
20 * testpi-4.c
21 *
22 * DESCRIPTION
Subrata Modak5305c4a2010-07-03 23:09:02 +053023 * This testcase verifies that the SCHED_OTHER thread can preempt
24 * the SCHED_RR thread via priority inheritance.
subrata_modak6acdc8e2007-12-26 11:11:45 +000025 *
26 * USAGE:
27 * Use run_auto.sh script in current directory to build and run test.
subrata_modak6acdc8e2007-12-26 11:11:45 +000028 *
subrata_modak6acdc8e2007-12-26 11:11:45 +000029 * AUTHOR
30 *
31 *
32 * HISTORY
33 *
34 *
35 *****************************************************************************/
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sched.h>
41#include <errno.h>
42#include <pthread.h>
43#include <sys/types.h>
44#include <sys/syscall.h>
45#include <unistd.h>
46#include <librttest.h>
subrata_modak6acdc8e2007-12-26 11:11:45 +000047
subrata_modak13782052008-02-22 14:43:53 +000048pthread_barrier_t barrier;
subrata_modak6acdc8e2007-12-26 11:11:45 +000049
50void usage(void)
51{
Subrata Modakb4f57c72010-07-03 23:06:16 +053052 rt_help();
53 printf("testpi-4 specific options:\n");
subrata_modak6acdc8e2007-12-26 11:11:45 +000054}
55
56int parse_args(int c, char *v)
57{
Subrata Modakb4f57c72010-07-03 23:06:16 +053058 int handled = 1;
59 switch (c) {
60 case 'h':
61 usage();
62 exit(0);
63 default:
64 handled = 0;
65 break;
66 }
67 return handled;
subrata_modak6acdc8e2007-12-26 11:11:45 +000068}
69
70int gettid(void)
71{
Subrata Modakb4f57c72010-07-03 23:06:16 +053072 return syscall(__NR_gettid);
subrata_modak6acdc8e2007-12-26 11:11:45 +000073}
74
Subrata Modakb4f57c72010-07-03 23:06:16 +053075typedef void *(*entrypoint_t)(void *);
subrata_modak6acdc8e2007-12-26 11:11:45 +000076pthread_mutex_t *glob_mutex;
77
Subrata Modakb4f57c72010-07-03 23:06:16 +053078void *func_nonrt(void *arg)
subrata_modak6acdc8e2007-12-26 11:11:45 +000079{
Subrata Modakb4f57c72010-07-03 23:06:16 +053080 struct thread *pthr = (struct thread *)arg;
Subrata Modakbd2dce12010-07-03 23:09:55 +053081 int i, tid = gettid();
subrata_modak6acdc8e2007-12-26 11:11:45 +000082
Subrata Modakb4f57c72010-07-03 23:06:16 +053083 printf("Thread %d started running with priority %d\n", tid,
84 pthr->priority);
85 pthread_mutex_lock(glob_mutex);
86 printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
87 tid, pthr->policy, pthr->priority);
88 pthread_barrier_wait(&barrier);
subrata_modak13782052008-02-22 14:43:53 +000089
Subrata Modakb4f57c72010-07-03 23:06:16 +053090 for (i = 0; i < 10000; i++) {
91 if (i%100 == 0) {
92 printf("Thread %d loop %d pthread pol %d pri %d\n",
93 tid, i, pthr->policy, pthr->priority);
94 fflush(NULL);
95 }
Subrata Modakbd2dce12010-07-03 23:09:55 +053096 busy_work_ms(1);
Subrata Modakb4f57c72010-07-03 23:06:16 +053097 }
98 pthread_mutex_unlock(glob_mutex);
99 return NULL;
subrata_modak6acdc8e2007-12-26 11:11:45 +0000100}
101
Subrata Modakb4f57c72010-07-03 23:06:16 +0530102void *func_rt(void *arg)
subrata_modak6acdc8e2007-12-26 11:11:45 +0000103{
Subrata Modakb4f57c72010-07-03 23:06:16 +0530104 struct thread *pthr = (struct thread *)arg;
Subrata Modakbd2dce12010-07-03 23:09:55 +0530105 int i, tid = gettid();
subrata_modak6acdc8e2007-12-26 11:11:45 +0000106
Subrata Modakb4f57c72010-07-03 23:06:16 +0530107 printf("Thread %d started running with prio %d\n", tid,
108 pthr->priority);
109 pthread_barrier_wait(&barrier);
110 pthread_mutex_lock(glob_mutex);
111 printf("Thread %d at start pthread pol %d pri %d - Got global lock\n",
112 tid, pthr->policy, pthr->priority);
subrata_modak6acdc8e2007-12-26 11:11:45 +0000113
Subrata Modakb4f57c72010-07-03 23:06:16 +0530114 /* we just use the mutex as something to slow things down,
115 * say who we are and then do nothing for a while. The aim
116 * of this is to show that high priority threads make more
117 * progress than lower priority threads..
118 */
119 for (i = 0; i < 1000; i++) {
120 if (i%100 == 0) {
121 printf("Thread %d loop %d pthread pol %d pri %d\n",
122 tid, i, pthr->policy, pthr->priority);
123 fflush(NULL);
124 }
Subrata Modakbd2dce12010-07-03 23:09:55 +0530125 busy_work_ms(1);
Subrata Modakb4f57c72010-07-03 23:06:16 +0530126 }
127 pthread_mutex_unlock(glob_mutex);
128 return NULL;
subrata_modak6acdc8e2007-12-26 11:11:45 +0000129}
130
Subrata Modakb4f57c72010-07-03 23:06:16 +0530131void *func_noise(void *arg)
subrata_modak6acdc8e2007-12-26 11:11:45 +0000132{
Subrata Modakb4f57c72010-07-03 23:06:16 +0530133 struct thread *pthr = (struct thread *)arg;
Subrata Modakbd2dce12010-07-03 23:09:55 +0530134 int i, tid = gettid();
subrata_modak6acdc8e2007-12-26 11:11:45 +0000135
Subrata Modakb4f57c72010-07-03 23:06:16 +0530136 printf("Noise Thread started running with prio %d\n",
137 pthr->priority);
138 pthread_barrier_wait(&barrier);
subrata_modak13782052008-02-22 14:43:53 +0000139
Subrata Modakb4f57c72010-07-03 23:06:16 +0530140 for (i = 0; i < 10000; i++) {
141 if (i%100 == 0) {
142 printf("Noise Thread %d loop %d pthread pol %d "\
143 "pri %d\n", tid, i, pthr->policy,
144 pthr->priority);
145 fflush(NULL);
146 }
Subrata Modakbd2dce12010-07-03 23:09:55 +0530147 busy_work_ms(1);
Subrata Modakb4f57c72010-07-03 23:06:16 +0530148 }
149 return NULL;
subrata_modak6acdc8e2007-12-26 11:11:45 +0000150}
151
subrata_modak6acdc8e2007-12-26 11:11:45 +0000152/*
153 * Test pthread creation at different thread priorities.
154 */
Subrata Modakb4f57c72010-07-03 23:06:16 +0530155int main(int argc, char *argv[])
156{
Subrata Modaka21dab22010-07-03 23:06:38 +0530157 int i, retc, nopi = 0;
Subrata Modakb4f57c72010-07-03 23:06:16 +0530158 cpu_set_t mask;
159 CPU_ZERO(&mask);
160 CPU_SET(0, &mask);
161 setup();
subrata_modak6acdc8e2007-12-26 11:11:45 +0000162
Subrata Modakb4f57c72010-07-03 23:06:16 +0530163 rt_init("h", parse_args, argc, argv);
subrata_modak6acdc8e2007-12-26 11:11:45 +0000164
Subrata Modakb4f57c72010-07-03 23:06:16 +0530165 retc = pthread_barrier_init(&barrier, NULL, 5);
166 if (retc) {
167 printf("pthread_barrier_init failed: %s\n", strerror(retc));
168 exit(retc);
169 }
subrata_modakd93eb3e2008-02-28 09:15:18 +0000170
Subrata Modakb4f57c72010-07-03 23:06:16 +0530171 retc = sched_setaffinity(0, sizeof(mask), &mask);
172 if (retc < 0) {
173 printf("Main Thread: Can't set affinity: %d %s\n", retc,
174 strerror(retc));
175 exit(-1);
176 }
177 for (i = 0; i < argc; i++) {
178 if (strcmp(argv[i], "nopi") == 0)
179 nopi = 1;
180 }
subrata_modak6acdc8e2007-12-26 11:11:45 +0000181
Subrata Modakb4f57c72010-07-03 23:06:16 +0530182 printf("Start %s\n", argv[0]);
subrata_modak6acdc8e2007-12-26 11:11:45 +0000183
Subrata Modakb4f57c72010-07-03 23:06:16 +0530184 glob_mutex = malloc(sizeof(pthread_mutex_t));
185 if (glob_mutex == NULL) {
186 printf("Malloc failed\n");
187 exit(errno);
188 }
subrata_modak6acdc8e2007-12-26 11:11:45 +0000189
Subrata Modaka21dab22010-07-03 23:06:38 +0530190 if (!nopi)
191 init_pi_mutex(glob_mutex);
Subrata Modakb4f57c72010-07-03 23:06:16 +0530192
193 create_other_thread(func_nonrt, NULL);
194 create_rr_thread(func_rt, NULL, 20);
195 create_rr_thread(func_rt, NULL, 30);
196 create_rr_thread(func_rt, NULL, 40);
197 create_rr_thread(func_noise, NULL, 40);
198
199 printf("Joining threads\n");
200 join_threads();
201 printf("Done\n");
202
203 return 0;
subrata_modak6acdc8e2007-12-26 11:11:45 +0000204}