subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
subrata_modak | 1378205 | 2008-02-22 14:43:53 +0000 | [diff] [blame] | 3 | * Copyright © International Business Machines Corp., 2005, 2008 |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 4 | * |
| 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 Modak | 5305c4a | 2010-07-03 23:09:02 +0530 | [diff] [blame] | 23 | * This testcase verifies that the SCHED_OTHER thread can preempt |
| 24 | * the SCHED_RR thread via priority inheritance. |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 25 | * |
| 26 | * USAGE: |
| 27 | * Use run_auto.sh script in current directory to build and run test. |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 28 | * |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 29 | * 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_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 47 | |
subrata_modak | 1378205 | 2008-02-22 14:43:53 +0000 | [diff] [blame] | 48 | pthread_barrier_t barrier; |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 49 | |
| 50 | void usage(void) |
| 51 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 52 | rt_help(); |
| 53 | printf("testpi-4 specific options:\n"); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | int parse_args(int c, char *v) |
| 57 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 58 | 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_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int gettid(void) |
| 71 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 72 | return syscall(__NR_gettid); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 75 | typedef void *(*entrypoint_t)(void *); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 76 | pthread_mutex_t *glob_mutex; |
| 77 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 78 | void *func_nonrt(void *arg) |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 79 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 80 | struct thread *pthr = (struct thread *)arg; |
Subrata Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 81 | int i, tid = gettid(); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 82 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 83 | 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_modak | 1378205 | 2008-02-22 14:43:53 +0000 | [diff] [blame] | 89 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 90 | 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 Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 96 | busy_work_ms(1); |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 97 | } |
| 98 | pthread_mutex_unlock(glob_mutex); |
| 99 | return NULL; |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 102 | void *func_rt(void *arg) |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 103 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 104 | struct thread *pthr = (struct thread *)arg; |
Subrata Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 105 | int i, tid = gettid(); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 106 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 107 | 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_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 113 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 114 | /* 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 Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 125 | busy_work_ms(1); |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 126 | } |
| 127 | pthread_mutex_unlock(glob_mutex); |
| 128 | return NULL; |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 131 | void *func_noise(void *arg) |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 132 | { |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 133 | struct thread *pthr = (struct thread *)arg; |
Subrata Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 134 | int i, tid = gettid(); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 135 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 136 | printf("Noise Thread started running with prio %d\n", |
| 137 | pthr->priority); |
| 138 | pthread_barrier_wait(&barrier); |
subrata_modak | 1378205 | 2008-02-22 14:43:53 +0000 | [diff] [blame] | 139 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 140 | 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 Modak | bd2dce1 | 2010-07-03 23:09:55 +0530 | [diff] [blame] | 147 | busy_work_ms(1); |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 148 | } |
| 149 | return NULL; |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 150 | } |
| 151 | |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 152 | /* |
| 153 | * Test pthread creation at different thread priorities. |
| 154 | */ |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 155 | int main(int argc, char *argv[]) |
| 156 | { |
Subrata Modak | a21dab2 | 2010-07-03 23:06:38 +0530 | [diff] [blame] | 157 | int i, retc, nopi = 0; |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 158 | cpu_set_t mask; |
| 159 | CPU_ZERO(&mask); |
| 160 | CPU_SET(0, &mask); |
| 161 | setup(); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 162 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 163 | rt_init("h", parse_args, argc, argv); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 164 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 165 | 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_modak | d93eb3e | 2008-02-28 09:15:18 +0000 | [diff] [blame] | 170 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 171 | 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_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 181 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 182 | printf("Start %s\n", argv[0]); |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 183 | |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 184 | glob_mutex = malloc(sizeof(pthread_mutex_t)); |
| 185 | if (glob_mutex == NULL) { |
| 186 | printf("Malloc failed\n"); |
| 187 | exit(errno); |
| 188 | } |
subrata_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 189 | |
Subrata Modak | a21dab2 | 2010-07-03 23:06:38 +0530 | [diff] [blame] | 190 | if (!nopi) |
| 191 | init_pi_mutex(glob_mutex); |
Subrata Modak | b4f57c7 | 2010-07-03 23:06:16 +0530 | [diff] [blame] | 192 | |
| 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_modak | 6acdc8e | 2007-12-26 11:11:45 +0000 | [diff] [blame] | 204 | } |