Skip to content

Commit

Permalink
Merge branch 'develop' of https://git-wip-us.apache.org/repos/asf/celix
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
bpetri committed Nov 20, 2015
2 parents eac117a + 4b85f42 commit 77b8e71
Show file tree
Hide file tree
Showing 18 changed files with 306 additions and 183 deletions.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (EXAMPLES)
add_subdirectory(osgi-in-action/chapter04-correct-listener)
add_subdirectory(osgi-in-action/chapter01-greeting-example)
#add_subdirectory(osgi-in-action/chapter04-paint-example) chapter4 example is still based on APR
#add_subdirectory(locking) still under construction
add_subdirectory(locking)

#add_subdirectory(embedding) embedding is still baed on APR
endif(EXAMPLES)
8 changes: 5 additions & 3 deletions examples/locking/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# specific language governing permissions and limitations
# under the License.

include_directories(services)

add_subdirectory(benchmark)
add_subdirectory(math_provider)

add_subdirectory(mutex_benchmark)
add_subdirectory(reference_benchmark)
add_subdirectory(start_stop_benchmark)
add_subdirectory(modified_bool_benchmark)
#add_subdirectory(reference_benchmark)
#add_subdirectory(start_stop_benchmark)
#add_subdirectory(modified_bool_benchmark)
10 changes: 6 additions & 4 deletions examples/locking/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
SET(BUNDLE_SYMBOLICNAME benchmark_runner)
SET(BUNDLE_VERSION 0.0.0)

bundle(benchmark_runner SOURCES
private/src/benchmark_runner_activator
)

include_directories(private/include)
include_directories(public/include)
include_directories(${PROJECT_SOURCE_DIR}/framework/public/include)
include_directories(${PROJECT_SOURCE_DIR}/utils/public/include)
include_directories(${PROJECT_SOURCE_DIR}/shell/public/include)

bundle(benchmark_runner SOURCES
private/src/benchmark_runner_activator
)

target_link_libraries(benchmark_runner celix_framework)

176 changes: 105 additions & 71 deletions examples/locking/benchmark/private/src/benchmark_runner_activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/queue.h>

#include <pthread.h>
#include <string.h>
#include <assert.h>

#include "command.h"

#include "bundle_activator.h"
#include "bundle_context.h"
#include "service_registration.h"
#include "service_tracker.h"

#include "benchmark_service.h"
Expand All @@ -46,38 +47,35 @@ static celix_status_t benchmarkRunner_addedService(void * handle, service_refere
static celix_status_t benchmarkRunner_modifiedService(void * handle, service_reference_pt reference, void * service);
static celix_status_t benchmarkRunner_removedService(void * handle, service_reference_pt reference, void * service);

static void benchmarkRunner_runBenchmark(struct activator *activator);
static void benchmarkRunner_printHeader(char *name, unsigned int nrOfSamples);
static void benchmarkRunner_printResult(benchmark_result_t result, double updateFreq, unsigned long elapsedTime);
static void benchmarkRunner_printFooter(char *name);
static void benchmarkRunner_runBenchmark(struct activator *activator, FILE *out);
static void benchmarkRunner_printHeader(char *name, unsigned int nrOfSamples, FILE *out);
static void benchmarkRunner_printResult(benchmark_result_t result, double updateFreq, unsigned long elapsedTime,
FILE *out);
static void benchmarkRunner_printFooter(char *name, FILE *out);
static celix_status_t benchmarkRunner_execute(void *handle, char * line, FILE *out, FILE *err);

struct benchmark_entry {
benchmark_service_pt benchmark;
LIST_ENTRY(benchmark_entry) entries;
};

struct activator {
bundle_context_pt context;
service_tracker_customizer_pt customizer;
service_tracker_pt tracker;
pthread_t thread;

pthread_mutex_t mutex;
array_list_pt benchmarks;
LIST_HEAD(benchmark_entries, entries) benchmarkEntries;
benchmark_service_pt benchmark;
frequency_service_pt freqService;

command_service_pt command;
service_registration_pt reg;
};

celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
struct activator * activator = malloc(sizeof(*activator));
activator->context=context;
activator->customizer = NULL;
activator->tracker= NULL;
activator->benchmarks = NULL;
activator->benchmark = NULL;
activator->freqService = NULL;

LIST_INIT(&activator->benchmarkEntries);

*userData = activator;

return CELIX_SUCCESS;
Expand All @@ -89,39 +87,47 @@ celix_status_t bundleActivator_start(void * userData, bundle_context_pt context)

pthread_mutex_init(&activator->mutex, NULL);

arrayList_create(&activator->benchmarks);

serviceTrackerCustomizer_create(activator, benchmarkRunner_addingService, benchmarkRunner_addedService, benchmarkRunner_modifiedService, benchmarkRunner_removedService, &activator->customizer);

char filter[128];
sprintf(filter, "(|(%s=%s)(%s=%s))", "objectClass", BENCHMARK_SERVICE_NAME, "objectClass", FREQUENCY_SERVICE_NAME);
serviceTracker_createWithFilter(context, filter, activator->customizer, &activator->tracker);
serviceTracker_open(activator->tracker);

pthread_create(&activator->thread, NULL, (void *)benchmarkRunner_runBenchmark, activator);

activator->reg = NULL;
activator->command = calloc(1, sizeof(*activator->command));
activator->command->handle = activator;
activator->command->executeCommand = benchmarkRunner_execute;
properties_pt props = properties_create();
properties_set(props, OSGI_SHELL_COMMAND_NAME, "benchmark");
properties_set(props, OSGI_SHELL_COMMAND_USAGE, "benchmark run");
properties_set(props, OSGI_SHELL_COMMAND_DESCRIPTION, "run the available benchmark");

bundleContext_registerService(context, (char *)OSGI_SHELL_COMMAND_SERVICE_NAME, activator->command, props, &activator->reg);

return status;
}


celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context __attribute__((unused))) {
struct activator * activator = userData;

pthread_join(activator->thread, NULL);

serviceTracker_close(activator->tracker);

serviceRegistration_unregister(activator->reg);

return CELIX_SUCCESS;
}

celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context __attribute__((unused))) {
struct activator * activator = userData;

free(activator);
return CELIX_SUCCESS;
}

static celix_status_t benchmarkRunner_addingService(void * handle, service_reference_pt reference, void **service) {
celix_status_t status = CELIX_SUCCESS;
celix_status_t status;
struct activator * activator = handle;
status = bundleContext_getService(activator->context, reference, service);
return status;
Expand All @@ -139,7 +145,7 @@ static celix_status_t benchmarkRunner_addedService(void * handle, service_refere
serviceName = properties_get(properties, "objectClass");
if (strcmp(serviceName, BENCHMARK_SERVICE_NAME) == 0) {
pthread_mutex_lock(&activator->mutex);
arrayList_add(activator->benchmarks, service);
activator->benchmark = service;
pthread_mutex_unlock(&activator->mutex);
} else if (strcmp(serviceName, FREQUENCY_SERVICE_NAME) == 0 ) {
pthread_mutex_lock(&activator->mutex);
Expand All @@ -149,9 +155,9 @@ static celix_status_t benchmarkRunner_addedService(void * handle, service_refere

return status;
}
static celix_status_t benchmarkRunner_modifiedService(void * handle, service_reference_pt reference, void * service) {
static celix_status_t benchmarkRunner_modifiedService(void * handle __attribute__((unused)), service_reference_pt reference __attribute__((unused)), void * service __attribute__((unused))) {
celix_status_t status = CELIX_SUCCESS;
struct activator * activator = handle;
//ignore
return status;
}

Expand All @@ -167,7 +173,9 @@ static celix_status_t benchmarkRunner_removedService(void * handle, service_refe
serviceName = properties_get(properties, "objectClass");
if (strcmp(serviceName, BENCHMARK_SERVICE_NAME) == 0) {
pthread_mutex_lock(&activator->mutex);
arrayList_removeElement(activator->benchmarks, service);
if (activator->benchmark == service) {
activator->benchmark = NULL;
}
pthread_mutex_unlock(&activator->mutex);
} else if (strcmp(serviceName, FREQUENCY_SERVICE_NAME) == 0 ) {
pthread_mutex_lock(&activator->mutex);
Expand All @@ -180,10 +188,10 @@ static celix_status_t benchmarkRunner_removedService(void * handle, service_refe
return status;
}

static void benchmarkRunner_runBenchmark(struct activator *activator) {
int i, k;
int nrOfBenchmarks;
double updateFrequency, measuredFrequency;
static void benchmarkRunner_runBenchmark(struct activator *activator, FILE *out) {
int k;
double updateFrequency;
double measuredFrequency = 0.0;
unsigned int measuredUpdateCounter, nrOfUpdateThreads;
int nrOfSamples;
benchmark_service_pt benchmarkServ;
Expand All @@ -193,63 +201,89 @@ static void benchmarkRunner_runBenchmark(struct activator *activator) {
unsigned long elapsedTime;
double sampleFactor;


int nrOfThreadRuns = 12;
int threads[] = {1,2,3,4,5,6,7,8,16,32,64,128};

nrOfSamples = 100 * 1000;
updateFrequency = 1000;
nrOfUpdateThreads = 100;

usleep(2000 * 1000); //wait 2 seconds to get needed services

pthread_mutex_lock(&activator->mutex);
if (activator->freqService != NULL) {
activator->freqService->setFrequency(activator->freqService->handle, updateFrequency);
activator->freqService->setNrOfThreads(activator->freqService->handle, nrOfUpdateThreads);
}
nrOfBenchmarks = arrayList_size(activator->benchmarks);
for (i = 0 ; i < nrOfBenchmarks ; i += 1) {
benchmarkServ = arrayList_get(activator->benchmarks, i);
name = benchmarkServ->name(benchmarkServ->handler);
sampleFactor = benchmarkServ->getSampleFactor(benchmarkServ->handler);
activator->freqService->setBenchmarkName(activator->freqService->handle, name);
usleep(1000);
benchmarkRunner_printHeader(name, nrOfSamples * sampleFactor);
for (k = 0 ; k < nrOfThreadRuns ; k +=1) {
if (activator->freqService != NULL) {
activator->freqService->resetCounter(activator->freqService->handle);

}
gettimeofday(&begin, NULL);
result = benchmarkServ->run(benchmarkServ->handler, threads[k], nrOfSamples * sampleFactor);
gettimeofday(&end, NULL);
elapsedTime = ((end.tv_sec - begin.tv_sec) * 1000000) + (end.tv_usec - begin.tv_usec);
if (activator->freqService != NULL) {
measuredUpdateCounter = activator->freqService->getCounter(activator->freqService->handle);
measuredFrequency = ((double)(measuredUpdateCounter) / elapsedTime * 1000000);
}
benchmarkRunner_printResult(result, measuredFrequency, elapsedTime);
benchmarkServ = activator->benchmark;
name = benchmarkServ->name(benchmarkServ->handler);
sampleFactor = benchmarkServ->getSampleFactor(benchmarkServ->handler);
activator->freqService->setBenchmarkName(activator->freqService->handle, name);
usleep(1000);
benchmarkRunner_printHeader(name, nrOfSamples * (int)sampleFactor, out);
for (k = 0 ; k < nrOfThreadRuns ; k +=1) {
if (activator->freqService != NULL) {
activator->freqService->resetCounter(activator->freqService->handle);

}
gettimeofday(&begin, NULL);
result = benchmarkServ->run(benchmarkServ->handler, threads[k], nrOfSamples * sampleFactor);
gettimeofday(&end, NULL);
elapsedTime = ((end.tv_sec - begin.tv_sec) * 1000000) + (end.tv_usec - begin.tv_usec);
if (activator->freqService != NULL) {
measuredUpdateCounter = activator->freqService->getCounter(activator->freqService->handle);
measuredFrequency = ((double)(measuredUpdateCounter) / elapsedTime * 1000000);
}
benchmarkRunner_printFooter(name);
benchmarkRunner_printResult(result, measuredFrequency, elapsedTime, out);
}
benchmarkRunner_printFooter(name, out);

pthread_mutex_unlock(&activator->mutex);
}

static void benchmarkRunner_printHeader(char *name, unsigned int nrOfSamples) {
int i;
printf("---% 35s---------------------------------------------------------------------------------------\n", name);
printf("-------samples: %10i---------------------------------------------------------------------------------------------------\n", nrOfSamples);
static void benchmarkRunner_printHeader(char *name, unsigned int nrOfSamples, FILE *out) {
fprintf(out, "---%35s---------------------------------------------------------------------------------------\n", name);
fprintf(out, "-------samples: %10i---------------------------------------------------------------------------------------------------\n", nrOfSamples);
}

static void benchmarkRunner_printResult(benchmark_result_t result, double updateFreq, unsigned long elapsedTime,
FILE *out) {
fprintf(out, "| threads %5i | ", result.nrOfThreads);
fprintf(out, "average call time: %10.2f nanoseconds | ", result.averageCallTimeInNanoseconds);
fprintf(out, "frequency calls is %10.5f MHz | ", result.callFrequencyInMhz);
fprintf(out, "update freq ~ %8.2f Hz | ", updateFreq);
fprintf(out, "elapsed time is %8.5f seconds | ", ((double)elapsedTime) / 1000000);
if (result.skips > 0 ) {
fprintf(out, "Warning skipped %i calls", result.skips);
}
fprintf(out, "\n");
}

static void benchmarkRunner_printResult(benchmark_result_t result, double updateFreq, unsigned long elapsedTime) {
printf("| threads %5i | ", result.nrOfThreads);
printf("average call time: % 10.2f nanoseconds | ", result.averageCallTimeInNanoseconds);
printf("frequency calls is % 10.5f MHz | ", result.callFrequencyInMhz);
printf("update freq ~ % 8.2f Hz | ", updateFreq);
printf("elapsed time is % 8.5f seconds | ", ((double)elapsedTime) / 1000000);
printf("\n");
static void benchmarkRunner_printFooter(char *name __attribute__((unused)), FILE *out) {
fprintf(out, "-----------------------------------------------------------------------------------------------------------------------------\n\n\n");
}

static void benchmarkRunner_printFooter(char *name) {
printf("-----------------------------------------------------------------------------------------------------------------------------\n\n\n");
static celix_status_t benchmarkRunner_execute(void *handle, char * line, FILE *out, FILE *err) {
struct activator * activator = handle;
char *savePtr;
char *token = NULL;
token = strtok_r(line, " ", &savePtr); //command name
assert(strcmp(token, "benchmark") == 0);
token = strtok_r(NULL, " ", &savePtr); //sub command
if (strcmp("run", token) == 0) {
token = strtok_r(NULL, " ", &savePtr); //possible nr of time to run
int times = 1;
int i;
if (token != NULL) {
times = atoi(token);
}
for (i = 0; i < times; i += 1) {
fprintf(out, "running benchmark %i of %i\n", i+1, times);
benchmarkRunner_runBenchmark(activator, out);
}
} else {
fprintf(err, "Unknown subcommand '%s'\n", token);
}

return CELIX_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
typedef struct benchmark_result {
unsigned int nrOfThreads;
unsigned int nrOfsamples;
unsigned int requestedNrOfSamples;
unsigned int result;
unsigned int skips;
double averageCallTimeInNanoseconds;
Expand Down
16 changes: 1 addition & 15 deletions examples/locking/benchmark/public/src/benchmark_activator.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#include "benchmark_service.h"
#include "frequency_service.h"

static celix_status_t addingService(void * handle, service_reference_pt reference, void **service);
static celix_status_t addedService(void * handle, service_reference_pt reference, void * service);
static celix_status_t modifiedService(void * handle, service_reference_pt reference, void * service);
static celix_status_t removedService(void * handle, service_reference_pt reference, void * service);

struct activator {
Expand Down Expand Up @@ -69,7 +67,7 @@ celix_status_t bundleActivator_start(void * userData, bundle_context_pt context)
struct activator * activator = userData;

status = benchmark_create(&activator->benchmark);
serviceTrackerCustomizer_create(activator, addingService, addedService, modifiedService, removedService, &activator->customizer);
serviceTrackerCustomizer_create(activator, NULL, addedService, NULL, removedService, &activator->customizer);

char filter[128];
sprintf(filter, "(&(objectClass=%s)(benchmark=%s))", MATH_SERVICE_NAME, benchmark_getName(activator->benchmark));
Expand Down Expand Up @@ -106,24 +104,12 @@ celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt contex
return CELIX_SUCCESS;
}

static celix_status_t addingService(void * handle, service_reference_pt reference, void **service) {
celix_status_t status = CELIX_SUCCESS;
struct activator * activator = handle;
status = bundleContext_getService(activator->context, reference, service);
return status;

}
static celix_status_t addedService(void * handle, service_reference_pt reference, void * service) {
celix_status_t status = CELIX_SUCCESS;
struct activator * activator = handle;
benchmark_addMathService(activator->benchmark, service);
return status;
}
static celix_status_t modifiedService(void * handle, service_reference_pt reference, void * service) {
celix_status_t status = CELIX_SUCCESS;
struct activator * activator = handle;
return status;
}

static celix_status_t removedService(void * handle, service_reference_pt reference, void * service) {
celix_status_t status = CELIX_SUCCESS;
Expand Down

0 comments on commit 77b8e71

Please sign in to comment.