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