summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/composite/synchronization/nativecs_interlocked/pal_composite_native_cs.cpp
blob: 4c19d3b0dedff9465fe28fd60470e60a79730ad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
//#include <pthread.h>
//#include "mtx_critsect.cpp"
#include "mtx_critsect.h"
#include "resultbuffer.h"



#define LONGLONG long long
#define ULONGLONG unsigned LONGLONG
/*Defining Global Variables*/

int THREAD_COUNT=0;
int REPEAT_COUNT=0;
int GLOBAL_COUNTER=0;
int USE_PROCESS_COUNT = 0;
int RELATION_ID =0;
int g_counter = 0;
int MAX_PATH = 256;
LONGLONG callibrationValue = 0;

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER;
pthread_cond_t g_cv2 = PTHREAD_COND_INITIALIZER;
CRITICAL_SECTION g_cs;

/* Capture statistics for each worker thread */
struct statistics{
    unsigned int processId;
    unsigned int operationsFailed;
    unsigned int operationsPassed;
    unsigned int operationsTotal; 
    DWORD        operationTime;
    unsigned int relationId;
};


struct applicationStatistics{
    DWORD        	operationTime;
    unsigned int 	relationId;
    unsigned int 	processCount;
    unsigned int 	threadCount;
    unsigned int 	repeatCount;
    char*        		buildNumber;

};

ResultBuffer *resultBuffer;


void* waitforworkerthreads(void*);
void starttests(int);
int setuptest(void);
int cleanuptest(void);
int GetParameters( int , char **);
void incrementCounter(void);
ULONGLONG GetTicks(void);
ULONGLONG getPerfCallibrationValue(void);



extern int main(int argc, char **argv)
 {
	//Variable Declaration
	pthread_t pthreads[640];
	int threadID[640];
	int i=0; 
	int j=0;
	int rtn=0;
	ULONGLONG startTicks = 0;

	/* Variables to capture the file name and the file pointer*/
    char fileName[MAX_PATH];
    FILE *hFile;
    struct statistics* buffer;
    int statisticsSize = 0;

	/*Variable to Captutre Information at the Application Level*/
	struct applicationStatistics appStats;
	char mainFileName[MAX_PATH];
    FILE *hMainFile;

	//Get perfCallibrationValue 

	callibrationValue = getPerfCallibrationValue();
	printf("Callibration Value for this Platform %llu \n", callibrationValue);


	//Get Parameters
	if(GetParameters(argc, argv))
    {
     printf("Error in obtaining the parameters\n");
	 exit(-1);	
    }

	//Assign Values to Application Statistics Members
	appStats.relationId=RELATION_ID;
	appStats.operationTime=0;
	appStats.buildNumber = "999.99";
	appStats.processCount = USE_PROCESS_COUNT;
	appStats.threadCount = THREAD_COUNT;
	appStats.repeatCount = REPEAT_COUNT;

	printf("RELATION ID : %d\n", appStats.relationId);
	printf("Process Count : %d\n", appStats.processCount);
	printf("Thread Count : %d\n", appStats.threadCount);
	printf("Repeat Count : %d\n", appStats.repeatCount);


	//Open file for Application Statistics Collection 
	snprintf(mainFileName, MAX_PATH, "main_nativecriticalsection_%d_.txt",appStats.relationId);
	hMainFile = fopen(mainFileName, "w+");

	if(hMainFile == NULL)
	{ 
	    printf("Error in opening main file for write\n");
	}


	for (i=0;i<THREAD_COUNT;i++)
		{
			threadID[i] = i;
		}
	
	statisticsSize = sizeof(struct statistics);

	snprintf(fileName, MAX_PATH, "%d_thread_nativecriticalsection_%d_.txt", USE_PROCESS_COUNT, RELATION_ID);
	hFile = fopen(fileName, "w+");

	if(hFile == NULL)
	{ 
	    printf("Error in opening file for write for process [%d]\n", USE_PROCESS_COUNT);
	}


	// For each thread we will log operations failed (int), passed (int), total (int)
	// and number of ticks (DWORD) for the operations
	resultBuffer = new ResultBuffer( THREAD_COUNT, statisticsSize);

	//Call Test Case Setup Routine
	if (0!=setuptest())
	{
		//Error Condition
		printf("Error Initializing Test Case\n");
		exit(-1);
	}

	//Accquire Lock
    if (0!=pthread_mutex_lock(&g_mutex))
    {
       	//Error Condition
		printf("Error Accquiring Lock\n");
		exit(-1);
    }

	//USE NATIVE METHOD TO GET TICK COUNT
	startTicks = GetTicks();	
	
	/*Loop to create number THREAD_COUNT number of threads*/
	for (i=0;i< THREAD_COUNT;i++)
	{
		
		//printf("Creating Thread Count %d\n", i);
		//printf("Thread arrary value = %d\n", threadID[i]);
		rtn=pthread_create(&pthreads[i], NULL, waitforworkerthreads, &threadID[i]);
		if (0 != rtn)
			{ /* ERROR Condition */
				printf("Error:  pthread Creat, %s \n", strerror(rtn));
				exit(-1);
			}
		
	}

	
	//printf("Main Thread waits to recevie signal when all threads are done\n");
	pthread_cond_wait(&g_cv2,&g_mutex);

	//printf("Main thread has received signal\n");

	/*Signal Threads to Start Working*/
	//printf("Raise signal for all threads to start working\n");



	if (0!=pthread_cond_broadcast(&g_cv))
		{
			//Error Condition
			printf("Error Broadcasting Conditional Event\n");
			exit(-1);
		}
	
	//Release the lock
	if (0!=pthread_mutex_unlock(&g_mutex))
		{
			//Error Condition
			printf("Error Releasing Lock\n");
			exit(-1);
		}

	/*Join Threads */
	while (j < THREAD_COUNT)
	{
		if (0 != pthread_join(pthreads[j],NULL))
			{
				//Error Condition
				printf("Error Joining Threads\n");
				exit(-1);
			}
		j++;
	}


   /*Write Application Results to File*/
   //CAPTURE NATIVE TICK COUNT HERE
   appStats.operationTime = (DWORD)(GetTicks() - startTicks)/callibrationValue;
   

	/* Write Results to a file*/
    if(hFile!= NULL)
    { 
        for( i = 0; i < THREAD_COUNT; i++ )
        {  
            buffer = (struct statistics *)resultBuffer->getResultBuffer(i);
            fprintf(hFile, "%d,%d,%d,%d,%lu,%d\n", buffer->processId, buffer->operationsFailed, buffer->operationsPassed, buffer->operationsTotal, buffer->operationTime, buffer->relationId );
            //printf("Iteration %d over\n", i);
        }
    }
    fclose(hFile);

	

	//Call Test Case Cleanup Routine
	if (0!=cleanuptest())
		{
			//Error Condition
			printf("Error Cleaning up Test Case");
			exit(-1);
		}


   if(hMainFile!= NULL)
    { 
        printf("Writing to Main File \n");    
		fprintf(hMainFile, "%lu,%d,%d,%d,%d,%s\n", appStats.operationTime, appStats.relationId, appStats.processCount, appStats.threadCount, appStats.repeatCount, appStats.buildNumber);
		
    }
    fclose(hMainFile);
	return 0;
 }

void * waitforworkerthreads(void * threadId)
{

	int *threadParam = (int*) threadId;
	
//	printf("Thread ID : %d \n", *threadParam);
	
	//Accquire Lock 
       if (0!=pthread_mutex_lock(&g_mutex))
       	{
       		//Error Condition
			printf("Error Accquiring Mutex Lock in Wait for Worker Thread\n");
			exit(-1);
       	}

	//Increment Global Counter
	GLOBAL_COUNTER++;
	

	//If global counter is equal to thread count then signal main thread
	if (GLOBAL_COUNTER == THREAD_COUNT)
		{
			if (0!=pthread_cond_signal(&g_cv2))
				{
					//Error Condition
					printf("Error in setting conditional variable\n");
					exit(-1);
				}
		}

	//Wait for main thread to signal
	if (0!=pthread_cond_wait(&g_cv,&g_mutex))
		{
			//Error Condition
			printf("Error waiting on conditional variable in Worker Thread\n");
			exit(-1);
		}

	//Release the mutex lock
	if (0!=pthread_mutex_unlock(&g_mutex))
		{
			//Error Condition
			printf("Error Releasing Mutex Lock in Worker Thread\n");
			exit(-1);
		}

	//Start the test  
	starttests(*threadParam);
	
}

void starttests(int threadID)
{
	/*All threads beign executing tests cases*/	
	int i = 0;
	int Id = threadID;
	struct statistics stats;
	ULONGLONG startTime = 0;
	ULONGLONG endTime = 0;

	LONG volatile Destination;
	LONG Exchange;
	LONG Comperand;
	LONG result;

	stats.relationId = RELATION_ID;
	stats.processId = USE_PROCESS_COUNT;
	stats.operationsFailed = 0;
	stats.operationsPassed = 0;
	stats.operationsTotal  = 0;
	stats.operationTime    = 0;
		
	//Enter and Leave Critical Section in a loop REPEAT_COUNT Times
	
	
	startTime = GetTicks();

	for (i=0;i<REPEAT_COUNT;i++)
	{
		  Destination = (LONG volatile) threadID;
		  Exchange    = (LONG) i;
		  Comperand   = (LONG) threadID;
		  result = InterlockedCompareExchange(&Destination, Exchange, Comperand);

		  if( i != result )
		  {
		       stats.operationsFailed++;
		       stats.operationsTotal++;
		       continue;
		  }

	}
		  

	stats.operationTime = (DWORD)(GetTicks() - startTime)/callibrationValue;

//	printf("Operation Time %d \n", stats.operationTime);
	
	if(resultBuffer->LogResult(Id, (char *)&stats))
	{
		printf("Error while writing to shared memory, Thread Id is[??] and Process id is [%d]\n", USE_PROCESS_COUNT);
	}
	
}

int setuptest(void)
{
	
	//Initalize Critical Section
  /*
	if (0!=MTXInitializeCriticalSection( &g_cs))
		{
			return -1;
		}
  */
	return 0;
}

int cleanuptest(void)
{
	
	//Delete Critical Section
  /*
	if (0!=MTXDeleteCriticalSection(&g_cs))
		{	
			return -1;
		}
  */
	return 0;
}

int GetParameters( int argc, char **argv)
{

	if( (argc != 5) || ((argc == 1) && !strcmp(argv[1],"/?")) 
       || !strcmp(argv[1],"/h") || !strcmp(argv[1],"/H"))
    {
        printf("PAL -Composite Native Critical Section Test\n");
        printf("Usage:\n");
	 printf("\t[PROCESS_ID ( greater than 1] \n"); 
	 printf("\t[THREAD_COUNT ( greater than 1] \n"); 
        printf("\t[REPEAT_COUNT ( greater than 1]\n");
	 printf("\t[RELATION_ID  [greater than or Equal to 1]\n"); 
        return -1;
    }
 
    
    USE_PROCESS_COUNT = atoi(argv[1]);
    if( USE_PROCESS_COUNT < 0) 
    {
        printf("\nInvalid THREAD_COUNT number, Pass greater than 1\n");
        return -1;
    }

    THREAD_COUNT = atoi(argv[2]);
    if( THREAD_COUNT < 1) 
    {
        printf("\nInvalid THREAD_COUNT number, Pass greater than 1\n");
        return -1;
    }

    REPEAT_COUNT = atoi(argv[3]);
    if( REPEAT_COUNT < 1) 
    {
        printf("\nInvalid REPEAT_COUNT number, Pass greater than 1\n");
        return -1;
    }

    RELATION_ID = atoi(argv[4]);
    if( RELATION_ID < 1) 
    {
        printf("\nInvalid RELATION_ID number, Pass greater than 1\n");
        return -1;
    }


    return 0;
}

void incrementCounter(void)
{
	g_counter ++;
}


//Implementation borrowed from pertrace.c 
ULONGLONG GetTicks(void)
{
#ifdef i386
    unsigned long a, d;
    asm volatile("rdtsc":"=a" (a), "=d" (d));
    return ((ULONGLONG)((unsigned int)(d)) << 32) | (unsigned int)(a);
#else
#if defined(__sparc__) || (defined (_HPUX_) && defined(__ia64__))
    return (ULONGLONG)gethrtime();
#else
    // #error Don''t know how to get ticks on this platform
    return (ULONGLONG)gethrtime();
#endif // __sparc__
#endif // _X86_
}


/**/
ULONGLONG getPerfCallibrationValue(void)
{
	ULONGLONG startTicks;
	ULONGLONG endTicks;
	
	startTicks = GetTicks(); 
	sleep(1);
	endTicks = GetTicks();
	
	return ((endTicks-startTicks)/1000);  //Return number of Ticks in One Milliseconds

}