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