profile.h 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file profile.h:
 * @brief Simple profiling functions for measuring code execution time.
 */

#ifndef __TPROFILE_H
#define __TPROFILE_H

#include <sys/time.h>
34
#include <string>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <stdint.h>

/**
 * Function get_tics
 * Returns the number of microseconds that have elapsed since the system was started.
 * @return uint64_t Number of microseconds.
 */
static inline uint64_t get_tics()
{
    struct timeval tv;
    gettimeofday( &tv, NULL );

    return (uint64_t) tv.tv_sec * 1000000ULL + (uint64_t) tv.tv_usec;
}

/**
 * Structure for storing data related to profiling counters.
 */
struct prof_counter
{
55 56 57 58 59 60 61 62 63 64 65
    uint64_t start, end;         // Stored timer value

    uint64_t usecs() const
    {
        return end - start;
    }

    float msecs() const
    {
        return ( end - start ) / 1000.0;
    }
66 67 68 69 70 71 72 73 74 75
};

/**
 * Function prof_start
 * Begins code execution time counting for a given profiling counter.
 * @param cnt is the counter which should be started.
 * @param use_rdtsc tells if processor's time-stamp counter should be used for time counting.
 *      Otherwise is system tics method will be used. IMPORTANT: time-stamp counter should not
 *      be used on multicore machines executing threaded code.
 */
76
static inline void prof_start( prof_counter* aCnt )
77
{
78
    aCnt->start = get_tics();
79 80 81 82 83 84 85
}

/**
 * Function prof_stop
 * Ends code execution time counting for a given profiling counter.
 * @param cnt is the counter which should be stopped.
 */
86
static inline void prof_end( prof_counter* aCnt )
87
{
88
    aCnt->end = get_tics();
89 90 91
}

#endif