diff options
Diffstat (limited to 'src/pal/tests/palsuite/composite/object_management/semaphore/nonshared')
3 files changed, 591 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/CMakeLists.txt b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/CMakeLists.txt new file mode 100644 index 0000000000..6efa228fbb --- /dev/null +++ b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + main.c + semaphore.c +) + +add_executable(paltest_semaphore_nonshared + ${SOURCES} +) + +add_dependencies(paltest_semaphore_nonshared coreclrpal) + +target_link_libraries(paltest_semaphore_nonshared + pthread + rt + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/main.c b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/main.c new file mode 100644 index 0000000000..854809c8f8 --- /dev/null +++ b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/main.c @@ -0,0 +1,228 @@ +// 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. + +/*============================================================ +** +** Source Code: main.c and semaphore.c +** main.c creates process and waits for all processes to get over +** semaphore.c creates a semaphore and then calls threads which will contend for the semaphore +** +** This test is for Object Management Test case for semaphore where Object type is not shareable. +** Algorithm +** o Create PROCESS_COUNT processes. +** o Main Thread of each process creates OBJECT_TYPE Object +** +** Author: ShamitP +** +** +**============================================================ +*/ + +#include <palsuite.h> +#include "resulttime.h" + +/* Test Input Variables */ +unsigned int PROCESS_COUNT = 2; +unsigned int THREAD_COUNT = 15; +unsigned int REPEAT_COUNT = 40000; +unsigned int RELATION_ID = 1001; + + + +struct TestStats{ + DWORD operationTime; + unsigned int relationId; + unsigned int processCount; + unsigned int threadCount; + unsigned int repeatCount; + char* buildNumber; +}; + +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 Object Management Semaphore Test\n"); + printf("Usage:\n"); + printf("main\n\t[PROCESS_COUNT [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 1]\n"); + return -1; + } + + PROCESS_COUNT = atoi(argv[1]); + if( (PROCESS_COUNT < 1) || (PROCESS_COUNT > MAXIMUM_WAIT_OBJECTS) ) + { + printf("\nMain Process:Invalid PROCESS_COUNT number, Pass greater than 1 and less than PROCESS_COUNT %d\n", MAXIMUM_WAIT_OBJECTS); + return -1; + } + + THREAD_COUNT = atoi(argv[2]); + if( (THREAD_COUNT < 1) || (THREAD_COUNT > MAXIMUM_WAIT_OBJECTS) ) + { + printf("\nInvalid THREAD_COUNT number, Pass greater than 1 and less than %d\n", MAXIMUM_WAIT_OBJECTS); + return -1; + } + + REPEAT_COUNT = atoi(argv[3]); + if( REPEAT_COUNT < 1) + { + printf("\nMain Process:Invalid REPEAT_COUNT number, Pass greater than 1\n"); + return -1; + } + + RELATION_ID = atoi(argv[4]); + if( RELATION_ID < 1) + { + printf("\nMain Process:Invalid RELATION_ID number, Pass greater than or Equal to 1\n"); + return -1; + } + + return 0; +} + + int __cdecl main(INT argc, CHAR **argv) +{ + unsigned int i = 0; + HANDLE hProcess[MAXIMUM_WAIT_OBJECTS]; + HANDLE hSemaphoreHandle[MAXIMUM_WAIT_OBJECTS]; + + STARTUPINFO si[MAXIMUM_WAIT_OBJECTS]; + PROCESS_INFORMATION pi[MAXIMUM_WAIT_OBJECTS]; + + const char *ObjName = "Semaphore"; + char lpCommandLine[MAX_PATH] = ""; + + int returnCode = 0; + DWORD processReturnCode = 0; + int testReturnCode = PASS; + + char fileName[MAX_PATH]; + FILE *pFile = NULL; + DWORD dwStartTime; + struct TestStats testStats; + + if(0 != (PAL_Initialize(argc, argv))) + { + return ( FAIL ); + } + + if(GetParameters(argc, argv)) + { + Fail("Error in obtaining the parameters\n"); + } + + /* Register the start time */ + dwStartTime = GetTickCount(); + testStats.relationId = RELATION_ID; + testStats.processCount = PROCESS_COUNT; + testStats.threadCount = THREAD_COUNT; + testStats.repeatCount = REPEAT_COUNT; + testStats.buildNumber = getBuildNumber(); + + + _snprintf(fileName, MAX_PATH, "main_semaphore_%d_.txt", RELATION_ID); + pFile = fopen(fileName, "w+"); + if(pFile == NULL) + { + Fail("Error in opening main file for write\n"); + } + + for( i = 0; i < PROCESS_COUNT; i++ ) + { + ZeroMemory( lpCommandLine, MAX_PATH ); + if ( _snprintf( lpCommandLine, MAX_PATH-1, "semaphore %d %d %d %d", i, THREAD_COUNT, REPEAT_COUNT, RELATION_ID) < 0 ) + { + Fail("Error Insufficient semaphore name string length for %s for iteration [%d]\n", ObjName, i); + } + + /* Zero the data structure space */ + ZeroMemory ( &pi[i], sizeof(pi[i]) ); + ZeroMemory ( &si[i], sizeof(si[i]) ); + + /* Set the process flags and standard io handles */ + si[i].cb = sizeof(si[i]); + + //Create Process + if(!CreateProcess( NULL, /* lpApplicationName*/ + lpCommandLine, /* lpCommandLine */ + NULL, /* lpProcessAttributes */ + NULL, /* lpThreadAttributes */ + TRUE, /* bInheritHandles */ + 0, /* dwCreationFlags, */ + NULL, /* lpEnvironment */ + NULL, /* pCurrentDirectory */ + &si[i], /* lpStartupInfo */ + &pi[i] /* lpProcessInformation */ + )) + { + Fail("Process Not created for [%d], the error code is [%d]\n", i, GetLastError()); + } + else + { + hProcess[i] = pi[i].hProcess; +// Trace("Process created for [%d]\n", i); + + } + } + + returnCode = WaitForMultipleObjects( PROCESS_COUNT, hProcess, TRUE, INFINITE); + if( WAIT_OBJECT_0 != returnCode ) + { + Trace("Wait for Object(s) @ Main thread for %d processes returned %d, and GetLastError value is %d\n", PROCESS_COUNT, returnCode, GetLastError()); + testReturnCode = FAIL; + } + + for( i = 0; i < PROCESS_COUNT; i++ ) + { + /* check the exit code from the process */ + if( ! GetExitCodeProcess( pi[i].hProcess, &processReturnCode ) ) + { + Trace( "GetExitCodeProcess call failed for iteration %d with error code %u\n", + i, GetLastError() ); + + testReturnCode = FAIL; + } + + if(processReturnCode == FAIL) + { + Trace( "Process [%d] failed and returned FAIL\n", i); + testReturnCode = FAIL; + } + + if(!CloseHandle(pi[i].hThread)) + { + Trace("Error:%d: CloseHandle failed for Process [%d] hThread\n", GetLastError(), i); + testReturnCode = FAIL; + } + + if(!CloseHandle(pi[i].hProcess) ) + { + Trace("Error:%d: CloseHandle failed for Process [%d] hProcess\n", GetLastError(), i); + testReturnCode = FAIL; + } + } + + testStats.operationTime = GetTimeDiff(dwStartTime); + fprintf(pFile, "%d,%d,%d,%d,%d,%s\n", testStats.operationTime, testStats.relationId,testStats.processCount, testStats.threadCount, testStats.repeatCount, testStats.buildNumber ); + if(fclose(pFile)) + { + Trace("Error: fclose failed for pFile\n"); + testReturnCode = FAIL; + } + + if( testReturnCode == PASS) + { + Trace("Test Passed\n"); + } + else + { + Trace("Test Failed\n"); + } + + PAL_Terminate(); + return testReturnCode; +} diff --git a/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/semaphore.c b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/semaphore.c new file mode 100644 index 0000000000..0e487f2c17 --- /dev/null +++ b/src/pal/tests/palsuite/composite/object_management/semaphore/nonshared/semaphore.c @@ -0,0 +1,342 @@ +// 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. + +/*============================================================ +** +** Source Code: main.c and semaphore.c +** main.c creates process and waits for all processes to get over +** semaphore.c creates a semaphore and then calls threads which will contend for the semaphore +** +** This test is for Object Management Test case for semaphore where Object type is not shareable. +** Algorithm +** o Create PROCESS_COUNT processes. +** o Main Thread of each process creates OBJECT_TYPE Object +** +** Author: ShamitP +** +** +**============================================================ +*/ + +#include <palsuite.h> +#include "resultbuffer.h" +#include "resulttime.h" + +#define TIMEOUT 5000 +/* Test Input Variables */ +unsigned int USE_PROCESS_COUNT = 0; +unsigned int THREAD_COUNT = 0; +unsigned int REPEAT_COUNT = 0; +unsigned int RELATION_ID = 0; + +/* Capture statistics at per thread basis */ +struct statistics{ + unsigned int processId; + unsigned int operationsFailed; + unsigned int operationsPassed; + unsigned int operationsTotal; + DWORD operationTime; + unsigned int relationId; +}; + +struct ProcessStats{ + unsigned int processId; + DWORD operationTime; + unsigned int relationId; +}; + +/* Semaphore variables */ +unsigned long lInitialCount = 1; /* Signaled */ +unsigned long lMaximumCount = 1; /* Maximum value of 1 */ + +HANDLE StartTestsEvHandle = NULL; +HANDLE hSemaphoreHandle = NULL; + +/* Results Buffer */ +ResultBuffer *resultBuffer = NULL; + +int testStatus; + +const char sTmpEventName[MAX_PATH] = "StartTestEvent"; + +void PALAPI Run_Thread(LPVOID lpParam); + +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 Object Management Semaphore Test\n"); + printf("Usage:\n"); + printf("semaphore\n\t[USE_PROCESS_COUNT ( 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 1]\n"); + return -1; + } + + // Trace("Args 1 is [%s], Arg 2 is [%s], Arg 3 is [%s]\n", argv[1], argv[2], argv[3]); + + USE_PROCESS_COUNT = atoi(argv[1]); + if( USE_PROCESS_COUNT < 0) + { + printf("\nInvalid USE_PROCESS_COUNT number, Pass greater than 1\n"); + return -1; + } + + THREAD_COUNT = atoi(argv[2]); + if( (THREAD_COUNT < 1) || (THREAD_COUNT > MAXIMUM_WAIT_OBJECTS) ) + { + printf("\nInvalid THREAD_COUNT number, Pass greater than 1 and less than %d\n", MAXIMUM_WAIT_OBJECTS); + 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("\nMain Process:Invalid RELATION_ID number, Pass greater than or Equal to 1\n"); + return -1; + } + + return 0; +} + + int __cdecl main(INT argc, CHAR **argv) +{ + unsigned int i = 0; + HANDLE hThread[MAXIMUM_WAIT_OBJECTS]; + DWORD threadId[MAXIMUM_WAIT_OBJECTS]; + + const char *ObjName = "Semaphore"; + + DWORD dwParam = 0; + + int returnCode = 0; + + /* Variables to capture the file name and the file pointer at thread level*/ + char fileName[MAX_PATH]; + FILE *pFile = NULL; + struct statistics* buffer = NULL; + int statisticsSize = 0; + + /* Variables to capture the file name and the file pointer at process level*/ + char processFileName[MAX_PATH]; + FILE *pProcessFile = NULL; + struct ProcessStats processStats; + DWORD dwStartTime; + + testStatus = PASS; + + if(0 != (PAL_Initialize(argc, argv))) + { + return ( FAIL ); + } + + if(GetParameters(argc, argv)) + { + Fail("Error in obtaining the parameters\n"); + } + // Trace("Process created, value of process count is [%d]\n", USE_PROCESS_COUNT); + + /* Register the start time */ + dwStartTime = GetTickCount(); + processStats.relationId = RELATION_ID; + processStats.processId = USE_PROCESS_COUNT; + + _snprintf(processFileName, MAX_PATH, "%d_process_semaphore_%d_.txt", USE_PROCESS_COUNT, RELATION_ID); + pProcessFile = fopen(processFileName, "w+"); + if(pProcessFile == NULL) + { + Fail("Error in opening process File file for write for process [%d]\n", USE_PROCESS_COUNT); + } + + statisticsSize = sizeof(struct statistics); + + _snprintf(fileName, MAX_PATH, "%d_thread_semaphore_%d_.txt", USE_PROCESS_COUNT, RELATION_ID); + pFile = fopen(fileName, "w+"); + if(pFile == NULL) + { + Fail("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); + + StartTestsEvHandle = CreateEvent( NULL, /* lpEventAttributes*/ + TRUE, /* bManualReset */ + FALSE, /* bInitialState */ + NULL); /* name of Event */ + + if( StartTestsEvHandle == NULL ) + { + Fail("Error:%d: Unexpected failure " + "to create %s Event for process count %d\n", GetLastError(), sTmpEventName, USE_PROCESS_COUNT ); + + } + + /* Create StartTest Event */ + hSemaphoreHandle = CreateSemaphore( + NULL, /* lpSemaphoreAttributes */ + lInitialCount, /*lInitialCount*/ + lMaximumCount, /*lMaximumCount */ + NULL + ); + + if( hSemaphoreHandle == NULL) + { + Fail("Unable to create Semaphore handle for process id [%d], returned error [%d]\n", i, GetLastError()); + } + /* We already assume that the Semaphore was created previously*/ + + for( i = 0; i < THREAD_COUNT; i++ ) + { + dwParam = (int) i; + //Create thread + hThread[i] = CreateThread( + NULL, /* no security attributes */ + 0, /* use default stack size */ + (LPTHREAD_START_ROUTINE)Run_Thread,/* thread function */ + (LPVOID)dwParam, /* argument to thread function */ + 0, /* use default creation flags */ + &threadId[i] /* returns the thread identifier*/ + ); + + + if(hThread[i] == NULL) + { + Fail("Create Thread failed for %d process, and GetLastError value is %d\n", USE_PROCESS_COUNT, GetLastError()); + } + + } + + if (!SetEvent(StartTestsEvHandle)) + { + Fail("Set Event for Start Tests failed for %d process, and GetLastError value is %d\n", USE_PROCESS_COUNT, GetLastError()); + } + + /* Test running */ + returnCode = WaitForMultipleObjects( THREAD_COUNT, hThread, TRUE, INFINITE); + + if( WAIT_OBJECT_0 != returnCode ) + { + Trace("Wait for Object(s) for %d process returned %d, and GetLastError value is %d\n", USE_PROCESS_COUNT, returnCode, GetLastError()); + testStatus = FAIL; + } + + processStats.operationTime = GetTimeDiff(dwStartTime); + + /* Write to a file*/ + if(pFile!= NULL) + { + for( i = 0; i < THREAD_COUNT; i++ ) + { + buffer = (struct statistics *)resultBuffer->getResultBuffer(i); + returnCode = fprintf(pFile, "%d,%d,%d,%d,%lu,%d\n", buffer->processId, buffer->operationsFailed, buffer->operationsPassed, buffer->operationsTotal, buffer->operationTime, buffer->relationId ); + //Trace("Iteration %d over\n", i); + + } + } + fclose(pFile); + /* Logging for the test case over, clean up the handles */ + +// Trace("Test Thread %d done\n", USE_PROCESS_COUNT); + + for( i = 0; i < THREAD_COUNT; i++ ) + { + if(!CloseHandle(hThread[i]) ) + { + Trace("Error:%d: CloseHandle failed for Process [%d] hThread[%d]\n", GetLastError(), USE_PROCESS_COUNT, i); + testStatus = FAIL; + } + } + + if(!CloseHandle(StartTestsEvHandle)) + { + Trace("Error:%d: CloseHandle failed for Process [%d] StartTestsEvHandle\n", GetLastError(), USE_PROCESS_COUNT); + testStatus = FAIL; + } + + if(!CloseHandle(hSemaphoreHandle)) + { + Trace("Error:%d: CloseHandle failed for Process [%d] hSemaphoreHandle\n", GetLastError(), USE_PROCESS_COUNT); + testStatus = FAIL; + } + + PAL_Terminate(); + return PASS; +} + +void PALAPI Run_Thread (LPVOID lpParam) +{ + unsigned int i = 0; + DWORD dwWaitResult; + + int Id=(int)lpParam; + + struct statistics stats; + DWORD dwStartTime; + + stats.relationId = RELATION_ID; + stats.processId = USE_PROCESS_COUNT; + stats.operationsFailed = 0; + stats.operationsPassed = 0; + stats.operationsTotal = 0; + stats.operationTime = 0; + + dwWaitResult = WaitForSingleObject( + StartTestsEvHandle, // handle to start test handle + TIMEOUT); + + if(dwWaitResult != WAIT_OBJECT_0) + { + Fail("Error while waiting for StartTest Event@ thread %d, RC is %d, Error is %d\n", Id, dwWaitResult, GetLastError()); + } + + dwStartTime = GetTickCount(); + + for( i = 0; i < REPEAT_COUNT; i++ ) + { + dwWaitResult = WaitForSingleObject( + hSemaphoreHandle, // handle to Semaphore + TIMEOUT); + + if(dwWaitResult != WAIT_OBJECT_0) + { +// Trace("Error while waiting for onject @ thread %d, # iter %d, RC is %d, Error is %d\n", Id, i, dwWaitResult, GetLastError()); + stats.operationsFailed += 1; + stats.operationsTotal += 1; + testStatus = FAIL; + continue; + } + if (! ReleaseSemaphore(hSemaphoreHandle, 1, NULL)) + { + // Deal with error. + // Trace("Error while releasing Semaphore @ thread %d # iter %d\n", Id, i); + stats.operationsFailed += 1; + stats.operationsTotal += 1; + // Probably need to have while true loop to attempt to release semaphore... + testStatus = FAIL; + continue; + } + + stats.operationsTotal += 1; + stats.operationsPassed += 1; +// Trace("Successs while releasing Semaphore @ iteration %d -> thread %d -> Process %d\n", i, Id, USE_PROCESS_COUNT); + + } + + stats.operationTime = GetTimeDiff(dwStartTime); + if(resultBuffer->LogResult(Id, (char *)&stats)) + { + Fail("Error:%d: while writing to shared memory, Thread Id is[%d] and Process id is [%d]\n", GetLastError(), Id, USE_PROCESS_COUNT); + } + // Trace("Thread %d over for process %d\n", Id, USE_PROCESS_COUNT); +} |