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