1*94d3b452SApple OSS Distributions// 2*94d3b452SApple OSS Distributions// PITest.m 3*94d3b452SApple OSS Distributions// PerfIndex 4*94d3b452SApple OSS Distributions// 5*94d3b452SApple OSS Distributions// Created by Mark Hamilton on 8/21/13. 6*94d3b452SApple OSS Distributions// 7*94d3b452SApple OSS Distributions// 8*94d3b452SApple OSS Distributions 9*94d3b452SApple OSS Distributions#import "PITest.h" 10*94d3b452SApple OSS Distributions#include <dlfcn.h> 11*94d3b452SApple OSS Distributions#include <pthread.h> 12*94d3b452SApple OSS Distributions 13*94d3b452SApple OSS Distributions@implementation PITest 14*94d3b452SApple OSS Distributions 15*94d3b452SApple OSS Distributions+ (id)testWithOptions:(NSDictionary *)options 16*94d3b452SApple OSS Distributions{ 17*94d3b452SApple OSS Distributions PITest *instance = nil; 18*94d3b452SApple OSS Distributions if(instance == nil) 19*94d3b452SApple OSS Distributions instance = [[PITest alloc] init]; 20*94d3b452SApple OSS Distributions [instance setTestName:[options objectForKey:@"name"]]; 21*94d3b452SApple OSS Distributions return instance; 22*94d3b452SApple OSS Distributions} 23*94d3b452SApple OSS Distributions 24*94d3b452SApple OSS Distributions- (BOOL)loadPITestAtPath:(NSString*) path 25*94d3b452SApple OSS Distributions{ 26*94d3b452SApple OSS Distributions void* handle; 27*94d3b452SApple OSS Distributions void* f; 28*94d3b452SApple OSS Distributions 29*94d3b452SApple OSS Distributions handle = dlopen([path UTF8String], RTLD_NOW | RTLD_LOCAL); 30*94d3b452SApple OSS Distributions if(!handle) { 31*94d3b452SApple OSS Distributions return NO; 32*94d3b452SApple OSS Distributions } 33*94d3b452SApple OSS Distributions 34*94d3b452SApple OSS Distributions 35*94d3b452SApple OSS Distributions f = dlsym(handle, "setup"); 36*94d3b452SApple OSS Distributions self->setup_func = (int (*)(int, long long, int, void **))f; 37*94d3b452SApple OSS Distributions 38*94d3b452SApple OSS Distributions f = dlsym(handle, "execute"); 39*94d3b452SApple OSS Distributions self->execute_func = (int (*)(int, int, long long, int, void **))f; 40*94d3b452SApple OSS Distributions if(!self->execute_func) 41*94d3b452SApple OSS Distributions return NO; 42*94d3b452SApple OSS Distributions 43*94d3b452SApple OSS Distributions f = dlsym(handle, "cleanup"); 44*94d3b452SApple OSS Distributions self->cleanup_func = (void (*)(int, long long))f; 45*94d3b452SApple OSS Distributions return YES; 46*94d3b452SApple OSS Distributions} 47*94d3b452SApple OSS Distributions 48*94d3b452SApple OSS Distributions- (long long)lengthForTest:(NSString*) testName 49*94d3b452SApple OSS Distributions{ 50*94d3b452SApple OSS Distributions NSNumber* number; 51*94d3b452SApple OSS Distributions long long myLength; 52*94d3b452SApple OSS Distributions NSDictionary* lengths = [NSDictionary dictionaryWithObjectsAndKeys: 53*94d3b452SApple OSS Distributions @"cpu", [NSNumber numberWithLongLong:2000], 54*94d3b452SApple OSS Distributions @"syscall", [NSNumber numberWithLongLong:2500], 55*94d3b452SApple OSS Distributions @"memory", [NSNumber numberWithLongLong:1000000], 56*94d3b452SApple OSS Distributions @"fault", [NSNumber numberWithLongLong:500], 57*94d3b452SApple OSS Distributions @"zfod", [NSNumber numberWithLongLong:500], 58*94d3b452SApple OSS Distributions @"file_create", [NSNumber numberWithLongLong:10], 59*94d3b452SApple OSS Distributions @"file_read", [NSNumber numberWithLongLong:1000000], 60*94d3b452SApple OSS Distributions @"file_write", [NSNumber numberWithLongLong:1000000], 61*94d3b452SApple OSS Distributions nil]; 62*94d3b452SApple OSS Distributions 63*94d3b452SApple OSS Distributions number = (NSNumber*)[lengths objectForKey:testName]; 64*94d3b452SApple OSS Distributions if(!number) { 65*94d3b452SApple OSS Distributions myLength = 10; 66*94d3b452SApple OSS Distributions } else { 67*94d3b452SApple OSS Distributions myLength = [number longLongValue]; 68*94d3b452SApple OSS Distributions } 69*94d3b452SApple OSS Distributions 70*94d3b452SApple OSS Distributions return myLength; 71*94d3b452SApple OSS Distributions} 72*94d3b452SApple OSS Distributions 73*94d3b452SApple OSS Distributions- (BOOL)setup 74*94d3b452SApple OSS Distributions{ 75*94d3b452SApple OSS Distributions BOOL success = NO; 76*94d3b452SApple OSS Distributions int retval; 77*94d3b452SApple OSS Distributions 78*94d3b452SApple OSS Distributions NSString* testPath = [NSString stringWithFormat:@"/AppleInternal/CoreOS/perf_index/%@.dylib", [self testName]]; 79*94d3b452SApple OSS Distributions success = [self loadPITestAtPath:testPath]; 80*94d3b452SApple OSS Distributions if(!success) { 81*94d3b452SApple OSS Distributions NSLog(@"Failed to load test %@", [self testName]); 82*94d3b452SApple OSS Distributions return NO; 83*94d3b452SApple OSS Distributions } 84*94d3b452SApple OSS Distributions 85*94d3b452SApple OSS Distributions self->length = [self lengthForTest:[self testName]]; 86*94d3b452SApple OSS Distributions self->numThreads = 1; 87*94d3b452SApple OSS Distributions self->testArgc = 0; 88*94d3b452SApple OSS Distributions self->testArgv = NULL; 89*94d3b452SApple OSS Distributions 90*94d3b452SApple OSS Distributions pthread_cond_init(&self->threadsReadyCvar, NULL); 91*94d3b452SApple OSS Distributions pthread_cond_init(&self->startCvar, NULL); 92*94d3b452SApple OSS Distributions pthread_mutex_init(&self->readyThreadCountLock, NULL); 93*94d3b452SApple OSS Distributions self->readyThreadCount = 0; 94*94d3b452SApple OSS Distributions 95*94d3b452SApple OSS Distributions if(self->setup_func) { 96*94d3b452SApple OSS Distributions retval = self->setup_func(1, self->length, 0, NULL); 97*94d3b452SApple OSS Distributions if(retval != 0) { 98*94d3b452SApple OSS Distributions NSLog(@"setup_func failed"); 99*94d3b452SApple OSS Distributions return NO; 100*94d3b452SApple OSS Distributions } 101*94d3b452SApple OSS Distributions } 102*94d3b452SApple OSS Distributions 103*94d3b452SApple OSS Distributions self->threads = (pthread_t*)malloc(sizeof(pthread_t)*self->numThreads); 104*94d3b452SApple OSS Distributions 105*94d3b452SApple OSS Distributions for(int thread_index = 0; thread_index < self->numThreads; thread_index++) { 106*94d3b452SApple OSS Distributions NSNumber* my_thread_index = [NSNumber numberWithInt:thread_index]; 107*94d3b452SApple OSS Distributions NSArray *arg = [NSArray arrayWithObjects:my_thread_index, self, nil]; 108*94d3b452SApple OSS Distributions retval = pthread_create(&threads[thread_index], NULL, thread_setup, (__bridge void*)arg); 109*94d3b452SApple OSS Distributions if(retval != 0) { 110*94d3b452SApple OSS Distributions NSLog(@"pthread_create failed"); 111*94d3b452SApple OSS Distributions free(self->threads); 112*94d3b452SApple OSS Distributions return NO; 113*94d3b452SApple OSS Distributions } 114*94d3b452SApple OSS Distributions } 115*94d3b452SApple OSS Distributions 116*94d3b452SApple OSS Distributions pthread_mutex_lock(&self->readyThreadCountLock); 117*94d3b452SApple OSS Distributions if(self->readyThreadCount != self->numThreads) { 118*94d3b452SApple OSS Distributions pthread_cond_wait(&self->threadsReadyCvar, &self->readyThreadCountLock); 119*94d3b452SApple OSS Distributions } 120*94d3b452SApple OSS Distributions pthread_mutex_unlock(&self->readyThreadCountLock); 121*94d3b452SApple OSS Distributions return YES; 122*94d3b452SApple OSS Distributions} 123*94d3b452SApple OSS Distributions 124*94d3b452SApple OSS Distributions- (BOOL)execute 125*94d3b452SApple OSS Distributions{ 126*94d3b452SApple OSS Distributions pthread_cond_broadcast(&self->startCvar); 127*94d3b452SApple OSS Distributions for(int thread_index = 0; thread_index < self->numThreads; thread_index++) { 128*94d3b452SApple OSS Distributions pthread_join(self->threads[thread_index], NULL); 129*94d3b452SApple OSS Distributions } 130*94d3b452SApple OSS Distributions return YES; 131*94d3b452SApple OSS Distributions} 132*94d3b452SApple OSS Distributions 133*94d3b452SApple OSS Distributions- (void)cleanup 134*94d3b452SApple OSS Distributions{ 135*94d3b452SApple OSS Distributions free(self->threads); 136*94d3b452SApple OSS Distributions if(self->cleanup_func) 137*94d3b452SApple OSS Distributions self->cleanup_func(0, self->length); 138*94d3b452SApple OSS Distributions} 139*94d3b452SApple OSS Distributions 140*94d3b452SApple OSS Distributionsvoid* thread_setup(void* arg) 141*94d3b452SApple OSS Distributions{ 142*94d3b452SApple OSS Distributions int my_index = (int)[(NSNumber*)[(__bridge NSArray*)arg objectAtIndex:0] integerValue]; 143*94d3b452SApple OSS Distributions PITest* test = (PITest*)[(__bridge NSArray*)arg objectAtIndex:1]; 144*94d3b452SApple OSS Distributions 145*94d3b452SApple OSS Distributions long long work_size = test->length / test->numThreads; 146*94d3b452SApple OSS Distributions int work_remainder = test->length % test->numThreads; 147*94d3b452SApple OSS Distributions 148*94d3b452SApple OSS Distributions if(work_remainder > my_index) { 149*94d3b452SApple OSS Distributions work_size++; 150*94d3b452SApple OSS Distributions } 151*94d3b452SApple OSS Distributions 152*94d3b452SApple OSS Distributions pthread_mutex_lock(&test->readyThreadCountLock); 153*94d3b452SApple OSS Distributions test->readyThreadCount++; 154*94d3b452SApple OSS Distributions 155*94d3b452SApple OSS Distributions if(test->readyThreadCount == test->numThreads) 156*94d3b452SApple OSS Distributions pthread_cond_signal(&test->threadsReadyCvar); 157*94d3b452SApple OSS Distributions pthread_cond_wait(&test->startCvar, &test->readyThreadCountLock); 158*94d3b452SApple OSS Distributions pthread_mutex_unlock(&test->readyThreadCountLock); 159*94d3b452SApple OSS Distributions test->execute_func(my_index, test->numThreads, work_size, test->testArgc, test->testArgv); 160*94d3b452SApple OSS Distributions 161*94d3b452SApple OSS Distributions return NULL; 162*94d3b452SApple OSS Distributions} 163*94d3b452SApple OSS Distributions 164*94d3b452SApple OSS Distributions@end 165