<commit_before>/*
 * Copyright 2014-2016 Charles University in Prague
 * Copyright 2014-2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _BSD_SOURCE // For glibc 2.18 to include caddr_t
#define _DEFAULT_SOURCE // For glibc 2.20 to include caddr_t
#define _POSIX_C_SOURCE 200809L

#include "ubench.h"

#pragma warning(push, 0)
#include "cz_cuni_mff_d3s_perf_CompilationCounter.h"
#include "cz_cuni_mff_d3s_perf_Benchmark.h"
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#pragma warning(pop)

#ifdef HAS_PAPI
/*
 * Include <sys/types.h> to have caddr_t.
 * Not needed with GCC and -std=c99.
 */
#include <sys/types.h>
#include <papi.h>
#include <pthread.h>
#endif

#ifdef HAS_QUERY_PERFORMANCE_COUNTER
#pragma warning(push, 0)
#include <windows.h>
#pragma warning(pop)
#endif

static benchmark_configuration_t current_benchmark;

jint ubench_benchmark_init(void) {
#ifdef HAS_PAPI
	// TODO: check for errors
	PAPI_library_init(PAPI_VER_CURRENT);
	int papi_rc = PAPI_thread_init(pthread_self);
	assert(papi_rc == PAPI_OK);
#endif

	current_benchmark.used_backends = 0;
	while (current_benchmark.used_events_count > 0) {
		current_benchmark.used_events_count--;
		free(current_benchmark.used_events[current_benchmark.used_events_count].name);
	}
	current_benchmark.used_events = NULL;
	current_benchmark.used_events_count = 0;

#ifdef HAS_PAPI
	current_benchmark.used_papi_events_count = 0;
	current_benchmark.papi_eventset = PAPI_NULL;
#endif

	current_benchmark.data = NULL;
	current_benchmark.data_index = 0;
	current_benchmark.data_size = 0;

	ubench_event_init();

	return JNI_OK;
}


void JNICALL Java_cz_cuni_mff_d3s_perf_Benchmark_init(
		JNIEnv *env, jclass UNUSED_PARAMETER(klass),<commit_msg>Print error messages if something went wrong during PAPI initialization.<commit_after>/*
 * Copyright 2014-2016 Charles University in Prague
 * Copyright 2014-2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _BSD_SOURCE // For glibc 2.18 to include caddr_t
#define _DEFAULT_SOURCE // For glibc 2.20 to include caddr_t
#define _POSIX_C_SOURCE 200809L

#include "ubench.h"

#pragma warning(push, 0)
#include "cz_cuni_mff_d3s_perf_CompilationCounter.h"
#include "cz_cuni_mff_d3s_perf_Benchmark.h"
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#pragma warning(pop)

#ifdef HAS_PAPI
/*
 * Include <sys/types.h> to have caddr_t.
 * Not needed with GCC and -std=c99.
 */
#include <sys/types.h>
#include <papi.h>
#include <pthread.h>
#endif

#ifdef HAS_QUERY_PERFORMANCE_COUNTER
#pragma warning(push, 0)
#include <windows.h>
#pragma warning(pop)
#endif

static benchmark_configuration_t current_benchmark;


static void
__die_with_papi_error (int papi_error, const char * message) {
	fprintf (
		stderr, "error: %s: %s (%d)\n",
		message, PAPI_strerror (papi_error), papi_error
	);

	exit (1);
}


static void
__check_papi_error (int papi_error, const char *message) {
	if (papi_error < 0) {
		__die_with_papi_error (papi_error, message);
	}
}


jint ubench_benchmark_init(void) {
#ifdef HAS_PAPI
	// TODO: check for errors
	int lib_init_rc = PAPI_library_init(PAPI_VER_CURRENT);
	if (lib_init_rc != PAPI_VER_CURRENT && lib_init_rc > 0) {
		fprintf(stderr,"error: PAPI library version mismatch!\n");
		exit(1);
	}

	__check_papi_error (lib_init_rc, "PAPI library initialization");

	int thread_init_rc = PAPI_thread_init(pthread_self);
	__check_papi_error (thread_init_rc, "PAPI thread initialization");
#endif

	current_benchmark.used_backends = 0;
	while (current_benchmark.used_events_count > 0) {
		current_benchmark.used_events_count--;
		free(current_benchmark.used_events[current_benchmark.used_events_count].name);
	}
	current_benchmark.used_events = NULL;
	current_benchmark.used_events_count = 0;

#ifdef HAS_PAPI
	current_benchmark.used_papi_events_count = 0;
	current_benchmark.papi_eventset = PAPI_NULL;
#endif

	current_benchmark.data = NULL;
	current_benchmark.data_index = 0;
	current_benchmark.data_size = 0;

	ubench_event_init();

	return JNI_OK;
}


void JNICALL Java_cz_cuni_mff_d3s_perf_Benchmark_init(
		JNIEnv *env, jclass UNUSED_PARAMETER(klass),
{'fraction_non_alphanumeric': 0.09610240733664502, 'fraction_numerical': 0.01795949560565533, 'mean_word_length': 5.269461077844311, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1257085', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1840, 'n_words': 483}