Blame libfreerdp/utils/stopwatch.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Stopwatch Utils
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Stephen Erisman
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/utils/stopwatch.h>
Packit 1fb8d4
Packit 1fb8d4
#ifdef _WIN32
Packit 1fb8d4
LARGE_INTEGER stopwatch_freq = { 0, 0 };
Packit 1fb8d4
#else
Packit 1fb8d4
#include <sys/time.h>
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
static void stopwatch_set_time(UINT64* usecs)
Packit 1fb8d4
{
Packit 1fb8d4
#ifdef _WIN32
Packit 1fb8d4
	LARGE_INTEGER perfcount;
Packit 1fb8d4
	QueryPerformanceCounter(&perfcount);
Packit 1fb8d4
	*usecs = (perfcount.QuadPart * 1000000) / stopwatch_freq.QuadPart;
Packit 1fb8d4
#else
Packit 1fb8d4
	struct timeval tv;
Packit 1fb8d4
	gettimeofday(&tv, NULL);
Packit 1fb8d4
	*usecs = tv.tv_sec * 1000000 + tv.tv_usec;
Packit 1fb8d4
#endif
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
STOPWATCH* stopwatch_create(void)
Packit 1fb8d4
{
Packit 1fb8d4
	STOPWATCH* sw;
Packit 1fb8d4
#ifdef _WIN32
Packit Service 5a9772
	if (stopwatch_freq.QuadPart == 0)
Packit Service 5a9772
	{
Packit 1fb8d4
		QueryPerformanceFrequency(&stopwatch_freq);
Packit 1fb8d4
	}
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit Service 5a9772
	sw = (STOPWATCH*)malloc(sizeof(STOPWATCH));
Packit 1fb8d4
	if (!sw)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
	stopwatch_reset(sw);
Packit 1fb8d4
Packit 1fb8d4
	return sw;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void stopwatch_free(STOPWATCH* stopwatch)
Packit 1fb8d4
{
Packit 1fb8d4
	free(stopwatch);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void stopwatch_start(STOPWATCH* stopwatch)
Packit 1fb8d4
{
Packit 1fb8d4
	stopwatch_set_time(&stopwatch->start);
Packit 1fb8d4
	stopwatch->count++;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void stopwatch_stop(STOPWATCH* stopwatch)
Packit 1fb8d4
{
Packit 1fb8d4
	stopwatch_set_time(&stopwatch->end);
Packit 1fb8d4
	stopwatch->elapsed += (stopwatch->end - stopwatch->start);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void stopwatch_reset(STOPWATCH* stopwatch)
Packit 1fb8d4
{
Packit 1fb8d4
	stopwatch->start = 0;
Packit 1fb8d4
	stopwatch->end = 0;
Packit 1fb8d4
	stopwatch->elapsed = 0;
Packit 1fb8d4
	stopwatch->count = 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
double stopwatch_get_elapsed_time_in_seconds(STOPWATCH* stopwatch)
Packit 1fb8d4
{
Packit Service 5a9772
	return (stopwatch->elapsed / 1000000.0);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
void stopwatch_get_elapsed_time_in_useconds(STOPWATCH* stopwatch, UINT32* sec, UINT32* usec)
Packit 1fb8d4
{
Packit Service 5a9772
	*sec = (UINT32)stopwatch->elapsed / 1000000;
Packit Service 5a9772
	*usec = (UINT32)stopwatch->elapsed % 1000000;
Packit 1fb8d4
}