Blame libfreerdp/utils/profiler.c

Packit Service fa4841
/**
Packit Service fa4841
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit Service fa4841
 * Profiler Utils
Packit Service fa4841
 *
Packit Service fa4841
 * Copyright 2011 Stephen Erisman
Packit Service fa4841
 *
Packit Service fa4841
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit Service fa4841
 * you may not use this file except in compliance with the License.
Packit Service fa4841
 * You may obtain a copy of the License at
Packit Service fa4841
 *
Packit Service fa4841
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit Service fa4841
 *
Packit Service fa4841
 * Unless required by applicable law or agreed to in writing, software
Packit Service fa4841
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit Service fa4841
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit Service fa4841
 * See the License for the specific language governing permissions and
Packit Service fa4841
 * limitations under the License.
Packit Service fa4841
 */
Packit Service fa4841
Packit Service fa4841
#ifdef HAVE_CONFIG_H
Packit Service fa4841
#include "config.h"
Packit Service fa4841
#endif
Packit Service fa4841
Packit Service fa4841
#include <stdio.h>
Packit Service fa4841
#include <stdlib.h>
Packit Service fa4841
Packit Service fa4841
#include <freerdp/utils/profiler.h>
Packit Service fa4841
#include <freerdp/log.h>
Packit Service fa4841
Packit Service fa4841
#define TAG FREERDP_TAG("utils")
Packit Service fa4841
Packit Service fa4841
struct _PROFILER
Packit Service fa4841
{
Packit Service fa4841
	char* name;
Packit Service fa4841
	STOPWATCH* stopwatch;
Packit Service fa4841
};
Packit Service fa4841
Packit Service fa4841
PROFILER* profiler_create(const char* name)
Packit Service fa4841
{
Packit Service bb5c11
	PROFILER* profiler = (PROFILER*) calloc(1, sizeof(PROFILER));
Packit Service fa4841
Packit Service fa4841
	if (!profiler)
Packit Service fa4841
		return NULL;
Packit Service fa4841
Packit Service fa4841
	profiler->name = _strdup(name);
Packit Service fa4841
	profiler->stopwatch = stopwatch_create();
Packit Service fa4841
Packit Service fa4841
	if (!profiler->name || !profiler->stopwatch)
Packit Service fa4841
		goto fail;
Packit Service fa4841
Packit Service fa4841
	return profiler;
Packit Service fa4841
fail:
Packit Service fa4841
	profiler_free(profiler);
Packit Service fa4841
	return NULL;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_free(PROFILER* profiler)
Packit Service fa4841
{
Packit Service fa4841
	if (profiler)
Packit Service fa4841
	{
Packit Service fa4841
		free(profiler->name);
Packit Service fa4841
		stopwatch_free(profiler->stopwatch);
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	free(profiler);
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_enter(PROFILER* profiler)
Packit Service fa4841
{
Packit Service fa4841
	stopwatch_start(profiler->stopwatch);
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_exit(PROFILER* profiler)
Packit Service fa4841
{
Packit Service fa4841
	stopwatch_stop(profiler->stopwatch);
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_print_header(void)
Packit Service fa4841
{
Packit Service bb5c11
	WLog_INFO(TAG, "-------------------------------+------------+-------------+-----------+-------");
Packit Service bb5c11
	WLog_INFO(TAG, "PROFILER NAME                  |      COUNT |       TOTAL |       AVG |    IPS");
Packit Service bb5c11
	WLog_INFO(TAG, "-------------------------------+------------+-------------+-----------+-------");
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_print(PROFILER* profiler)
Packit Service fa4841
{
Packit Service fa4841
	double s = stopwatch_get_elapsed_time_in_seconds(profiler->stopwatch);
Packit Service fa4841
	double avg = profiler->stopwatch->count == 0 ? 0 : s / profiler->stopwatch->count;
Packit Service bb5c11
	WLog_INFO(TAG, "%-30s | %10u | %10.4fs | %8.6fs | %6.0f",
Packit Service bb5c11
	          profiler->name, profiler->stopwatch->count, s, avg, profiler->stopwatch->count / s);
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
void profiler_print_footer(void)
Packit Service fa4841
{
Packit Service bb5c11
	WLog_INFO(TAG, "-------------------------------+------------+-------------+-----------+-------");
Packit Service fa4841
}