1 /* 2 * Copyright (c) Bull S.A. 2007 All Rights Reserved. 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it would be useful, but 9 * WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11 * 12 * Further, this software is distributed without any warranty that it is 13 * free of the rightful claim of any third person regarding infringement 14 * or the like. Any license provided herein, whether implied or 15 * otherwise, applies only to this software file. Patent licenses, if 16 * any, provided herein do not apply to combinations of this program with 17 * other software, or any other product whatsoever. 18 * 19 * You should have received a copy of the GNU General Public License along 20 * with this program; if not, write the Free Software Foundation, Inc., 21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * History: 24 * Created by: Cyril Lacabanne (Cyril.Lacabanne (at) bull.net) 25 * 26 */ 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <time.h> 31 #include <pthread.h> 32 #include <tirpc/netconfig.h> 33 #include <tirpc/rpc/rpc.h> 34 #include <tirpc/rpc/types.h> 35 #include <tirpc/rpc/xdr.h> 36 #include <tirpc/rpc/svc.h> 37 #include <errno.h> 38 39 //Standard define 40 #define CALCTHREADPROC 1000 41 #define VERSNUM 1 42 43 static int *thread_array_result; 44 int run_mode; 45 int progNum; 46 char *hostname; 47 char *nettype; 48 49 struct RES { 50 double locRes; 51 double svcRes; 52 }; 53 54 struct RES *resTbl; 55 56 struct datas { 57 double a; 58 double b; 59 double c; 60 }; 61 62 bool_t xdr_datas(XDR * pt_xdr, struct datas *pt) 63 { 64 return (xdr_double(pt_xdr, &(pt->a)) && 65 xdr_double(pt_xdr, &(pt->b)) && xdr_double(pt_xdr, &(pt->c))); 66 } 67 68 double getRand(void) 69 { 70 return (drand48() * 1000); 71 } 72 73 void *my_thread_process(void *arg) 74 { 75 CLIENT *clnt = NULL; 76 struct datas vars; 77 struct timeval total_timeout; 78 struct netconfig *nconf = NULL; 79 80 total_timeout.tv_sec = 1; 81 total_timeout.tv_usec = 1; 82 83 nconf = getnetconfigent("udp"); 84 85 if ((struct netconfig *)nconf == NULL) { 86 //Test failed 87 printf("5\n"); 88 pthread_exit((void*)5l); 89 } 90 91 clnt = clnt_tp_create_timed(hostname, progNum, 92 VERSNUM, (struct netconfig *)nconf, 93 &total_timeout); 94 95 if (clnt == NULL) { 96 printf("5\n"); 97 pthread_exit((void*)5l); 98 } 99 100 if (run_mode == 1) { 101 fprintf(stderr, "Thread %ld\n", (long)arg); 102 } 103 104 vars.a = getRand(); 105 vars.b = getRand(); 106 vars.c = getRand(); 107 108 resTbl[atoi(arg)].locRes = vars.a + (vars.b * vars.c); 109 110 clnt_call((CLIENT *) clnt, CALCTHREADPROC, (xdrproc_t) xdr_datas, (char *)&vars, // xdr_in 111 (xdrproc_t) xdr_double, (char *)&resTbl[atoi(arg)].svcRes, // xdr_out 112 total_timeout); 113 114 thread_array_result[atoi(arg)] = 115 (resTbl[atoi(arg)].svcRes == resTbl[atoi(arg)].locRes) ? 0 : 1; 116 117 if (run_mode == 1) { 118 fprintf(stderr, "Thread #%d calc : %lf, received : %lf\n", 119 atoi(arg), resTbl[atoi(arg)].locRes, 120 resTbl[atoi(arg)].svcRes); 121 } 122 123 pthread_exit(0); 124 } 125 126 int main(int argn, char *argc[]) 127 { 128 //Program parameters : argc[1] : HostName or Host IP 129 // argc[2] : Server Program Number 130 // argc[3] : Number of threads 131 // other arguments depend on test case 132 133 //run_mode can switch into stand alone program or program launch by shell script 134 //1 : stand alone, debug mode, more screen information 135 //0 : launch by shell script as test case, only one printf -> result status 136 run_mode = 0; 137 int test_status = 0; //Default test result set to FAILED 138 int threadNb = atoi(argc[3]); 139 long i; 140 pthread_t *pThreadArray; 141 void *ret; 142 143 hostname = argc[1]; 144 nettype = "VISIBLE"; 145 146 resTbl = malloc(threadNb * sizeof(struct RES)); 147 148 progNum = atoi(argc[2]); 149 150 if (run_mode == 1) { 151 printf("Server #%d\n", progNum); 152 printf("Thread to create %d\n", threadNb); 153 } 154 //Initialization : create threads results array, init elements to 0 155 //Each thread will put function result (pas/fail) into array 156 thread_array_result = malloc(threadNb * sizeof(int)); 157 memset(&thread_array_result[0], 0, threadNb * sizeof(int)); 158 159 //Create all threads 160 //Run all threads 161 pThreadArray = malloc(threadNb * sizeof(pthread_t)); 162 for (i = 0; i < threadNb; i++) { 163 if (run_mode == 1) 164 fprintf(stderr, "Try to create thread %ld\n", i); 165 if (pthread_create(&pThreadArray[i], NULL, my_thread_process, (void*)i) 166 < 0) { 167 fprintf(stderr, "pthread_create error for thread 1\n"); 168 exit(1); 169 } 170 } 171 172 //Clean threads 173 for (i = 0; i < threadNb; i++) { 174 (void)pthread_join(pThreadArray[i], &ret); 175 } 176 177 //Check if all threads results are ok 178 test_status = 0; 179 for (i = 0; i < threadNb; i++) { 180 if (thread_array_result[i] != 0) { 181 test_status = 1; 182 break; 183 } 184 } 185 186 if (run_mode == 1) { 187 for (i = 0; i < threadNb; i++) { 188 fprintf(stderr, "Result[%ld]=%d\n", i, 189 thread_array_result[i]); 190 } 191 } 192 //This last printf gives the result status to the tests suite 193 //normally should be 0: test has passed or 1: test has failed 194 printf("%d\n", test_status); 195 196 return test_status; 197 } 198