File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / collections / hashtable_profiler.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:20:08 2021 UTC (3 years, 4 months ago) by misho
Branches: strongswan, MAIN
CVS tags: v5_9_2p0, HEAD
strongswan 5.9.2

    1: /*
    2:  * Copyright (C) 2020 Tobias Brunner
    3:  * HSR Hochschule fuer Technik Rapperswil
    4:  *
    5:  * This program is free software; you can redistribute it and/or modify it
    6:  * under the terms of the GNU General Public License as published by the
    7:  * Free Software Foundation; either version 2 of the License, or (at your
    8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
    9:  *
   10:  * This program is distributed in the hope that it will be useful, but
   11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   13:  * for more details.
   14:  */
   15: 
   16: #ifndef HASHTABLE_PROFILER_H_
   17: #define HASHTABLE_PROFILER_H_
   18: 
   19: #ifdef HASHTABLE_PROFILER
   20: 
   21: #include <time.h>
   22: #include <utils/backtrace.h>
   23: 
   24: typedef struct hashtable_profile_t hashtable_profile_t;
   25: 
   26: struct hashtable_profile_t {
   27: 
   28: 	/**
   29: 	 * Some stats to profile lookups in the table
   30: 	 */
   31: 	struct {
   32: 		size_t count;
   33: 		size_t probes;
   34: 		size_t longest;
   35: 	} success, failure;
   36: 
   37: 	/**
   38: 	 * Stats on the memory usage of the table
   39: 	 */
   40: 	struct {
   41: 		size_t count;
   42: 		size_t size;
   43: 	} max;
   44: 
   45: 	/**
   46: 	 * Keep track of where the hash table was created
   47: 	 */
   48: 	backtrace_t *backtrace;
   49: };
   50: 
   51: /**
   52:  * Print and cleanup profiling data
   53:  */
   54: static inline void profiler_cleanup(hashtable_profile_t *profile, u_int count,
   55: 									u_int size)
   56: {
   57: 	if (profile->success.count || profile->failure.count)
   58: 	{
   59: 		fprintf(stderr, "%zu elements [max. %zu], %zu buckets [%zu], %zu "
   60: 				"successful / %zu failed lookups, %.4f [%zu] / %.4f "
   61: 				"[%zu] avg. probes in table created at:",
   62: 				count, profile->max.count, size, profile->max.size,
   63: 				profile->success.count, profile->failure.count,
   64: 				(double)profile->success.probes/profile->success.count,
   65: 				profile->success.longest,
   66: 				(double)profile->failure.probes/profile->failure.count,
   67: 				profile->failure.longest);
   68: 		profile->backtrace->log(profile->backtrace, stderr, TRUE);
   69: 	}
   70: 	profile->backtrace->destroy(profile->backtrace);
   71: }
   72: 
   73: /**
   74:  * Initialize profiling data
   75:  */
   76: static inline void profiler_init(hashtable_profile_t *profile, int skip)
   77: {
   78: 	profile->backtrace = backtrace_create(skip);
   79: }
   80: 
   81: #define lookup_start() \
   82: 	u_int _lookup_probes = 0;
   83: 
   84: #define lookup_probing() \
   85: 	_lookup_probes++;
   86: 
   87: #define _lookup_done(profile, result) \
   88: 	(profile)->result.count++; \
   89: 	(profile)->result.probes += _lookup_probes; \
   90: 	(profile)->result.longest = max((profile)->result.longest, _lookup_probes);
   91: 
   92: #define lookup_success(profile) _lookup_done(profile, success);
   93: #define lookup_failure(profile) _lookup_done(profile, failure);
   94: 
   95: static inline void profile_size(hashtable_profile_t *profile, u_int size)
   96: {
   97: 	profile->max.size = max(profile->max.size, size);
   98: }
   99: 
  100: static inline void profile_count(hashtable_profile_t *profile, u_int count)
  101: {
  102: 	profile->max.count = max(profile->max.count, count);
  103: }
  104: 
  105: #else /* !HASHTABLE_PROFILER */
  106: 
  107: #define hashtable_profile_t struct {}
  108: #define profiler_cleanup(...) {}
  109: #define profiler_init(...) {}
  110: #define lookup_start(...) {}
  111: #define lookup_probing(...) {}
  112: #define lookup_success(...) {}
  113: #define lookup_failure(...) {}
  114: #define profile_size(...) {}
  115: #define profile_count(...) {}
  116: 
  117: #endif /* HASHTABLE_PROFILER */
  118: 
  119: #endif /* HASHTABLE_PROFILER_H_ */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>