<commit_before>/*****************************************************************************
 * This file is part of gfxprim library.                                     *
 *                                                                           *
 * Gfxprim is free software; you can redistribute it and/or                  *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * Gfxprim is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with gfxprim; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,                        *
 * Boston, MA  02110-1301  USA                                               *
 *                                                                           *
 * Copyright (C) 2009-2013 <NAME> <<EMAIL>>                       *
 *                                                                           *
 *****************************************************************************/

#include <errno.h>
#include <string.h>
#include <time.h>

#include "core/GP_Common.h"
#include "core/GP_Debug.h"

#include "input/GP_TimeStamp.h"

#define MS_IN_US 1000000

/*
 * Chooses fastest clock with milisecond resolution.
 */
static int choose_clock(clockid_t *clock)
{
	struct timespec ts;

#ifdef CLOCK_MONOTONIC_COARSE
	GP_DEBUG(1, "Trying CLOCK_MONOTONIC_COARSE");

	if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts)) {
		GP_DEBUG(1, "CLOCK_MONOTONIC_COARSE: %s", strerror(errno));
	} else {
		GP_DEBUG(1, "CLOCK_MONOTONIC_COARSE resolution is %lis %lins",
		         (long)ts.tv_sec, ts.tv_nsec);

		/* Depends on CONFIG_HZ */
		if (ts.tv_sec == 0 && ts.tv_nsec <= MS_IN_US) {
			*clock = CLOCK_MONOTONIC_COARSE;
			return 0;
		}
	}
#endif

	if (clock_getres(CLOCK_MONOTONIC, &ts)) {
		GP_DEBUG(1, "CLOCK_MONOTONIC: %s", strerror(errno));
		return 1;
	}

	GP_DEBUG(1, "CLOCK_MONOTONIC resulution is %lis %lins",
	         (long)ts.tv_sec, ts.tv_nsec);

	if (ts.tv_sec == 0 && ts.tv_nsec <= MS_IN_US) {
		*clock = CLOCK_MONOTONIC;
		return 0;
	}

	GP_DEBUG(1, "No suitable clock id found.");
	//TODO: Fallback to gettimeofday?

	return 1;
}

uint64_t GP_GetTimeStamp(void)
{
	struct timespec ts;
	static clockid_t clock = -1;

	if (clock == -1)
		choose_clock(&clock);

	if (clock_gettime(clock, &ts)) {
		GP_ABORT("clock_gettime(%i) failed unexpectedly: %s",
		         clock, strerror(errno));
	}

	return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / MS_IN_US;
}<commit_msg>input: TimeStamp: Fallback to CLOCK_MONOTOMIC if available<commit_after>/*****************************************************************************
 * This file is part of gfxprim library.                                     *
 *                                                                           *
 * Gfxprim is free software; you can redistribute it and/or                  *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 2.1 of the License, or (at your option) any later version.        *
 *                                                                           *
 * Gfxprim is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with gfxprim; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,                        *
 * Boston, MA  02110-1301  USA                                               *
 *                                                                           *
 * Copyright (C) 2009-2013 <NAME> <<EMAIL>>                       *
 *                                                                           *
 *****************************************************************************/

#include <errno.h>
#include <string.h>
#include <time.h>

#include "core/GP_Common.h"
#include "core/GP_Debug.h"

#include "input/GP_TimeStamp.h"

#define MS_IN_US 1000000

/*
 * Chooses fastest clock with milisecond resolution.
 */
static int choose_clock(clockid_t *clock)
{
	struct timespec ts;

#ifdef CLOCK_MONOTONIC_COARSE
	GP_DEBUG(1, "Trying CLOCK_MONOTONIC_COARSE");

	if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts)) {
		GP_DEBUG(1, "CLOCK_MONOTONIC_COARSE: %s", strerror(errno));
	} else {
		GP_DEBUG(1, "CLOCK_MONOTONIC_COARSE resolution is %lis %lins",
		         (long)ts.tv_sec, ts.tv_nsec);

		/* Depends on CONFIG_HZ */
		if (ts.tv_sec == 0 && ts.tv_nsec <= MS_IN_US) {
			*clock = CLOCK_MONOTONIC_COARSE;
			return 0;
		}
	}
#endif

	if (clock_getres(CLOCK_MONOTONIC, &ts)) {
		GP_DEBUG(1, "CLOCK_MONOTONIC: %s", strerror(errno));
		//TODO: Fallback to gettimeofday?
		return 1;
	}

	GP_DEBUG(1, "CLOCK_MONOTONIC resulution is %lis %lins",
	         (long)ts.tv_sec, ts.tv_nsec);

	if (ts.tv_sec == 0 && ts.tv_nsec <= MS_IN_US) {
		*clock = CLOCK_MONOTONIC;
		return 0;
	}

	if (ts.tv_sec) {
		GP_FATAL("No suitable clock found");
		return 1;
	}

	GP_WARN("Timers running with %lims granularity\n",
                ts.tv_nsec/MS_IN_US);

	*clock = CLOCK_MONOTONIC;

	return 1;
}

uint64_t GP_GetTimeStamp(void)
{
	struct timespec ts;
	static clockid_t clock = -1;

	if (clock == -1)
		choose_clock(&clock);

	if (clock_gettime(clock, &ts)) {
		GP_ABORT("clock_gettime(%i) failed unexpectedly: %s",
		         clock, strerror(errno));
	}

	return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / MS_IN_US;
}
{'fraction_non_alphanumeric': 0.13869176180191298, 'fraction_numerical': 0.015427337241592102, 'mean_word_length': 1.8421744848750548, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4642204', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1855, 'n_words': 522}