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