xorp

timespent.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 // $XORP: xorp/libxorp/timespent.hh,v 1.15 2008/10/02 21:57:35 bms Exp $
00023 
00024 #ifndef __LIBXORP_TIMESPENT_HH__
00025 #define __LIBXORP_TIMESPENT_HH__
00026 
00027 #include "libxorp/timeval.hh"
00028 #include "libxorp/timer.hh"
00029 
00030 static const int TIMESPENT_LIMIT = 10;  // Time allowed in seconds.
00031 
00039 class TimeSpent {
00040 public:
00041     TimeSpent(const char *function, const char *file, int line, int limit)
00042     : _function(function), _file(file), _line(line),
00043       _limit(TimeVal(limit,0))
00044     {
00045     TimerList::system_gettimeofday(&_start);
00046     }
00047 
00052     bool overlimit(TimeVal& delta)
00053     {
00054     TimeVal now;
00055     TimerList::system_gettimeofday(&now);
00056 
00057     delta = now - _start;
00058 
00059     return delta > _limit;
00060     }
00061 
00065     bool overlimit()
00066     {
00067     TimeVal delta;
00068 
00069     return overlimit(delta);
00070     }
00071 
00075     void check(const char *function, const char *file, int line)
00076     {
00077     TimeVal delta;
00078     UNUSED(function);
00079     UNUSED(file);
00080     UNUSED(line);
00081 
00082     if (overlimit(delta))
00083         XLOG_WARNING("Function %s +%d %s took %s\n", function, line, file,
00084            delta.str().c_str());
00085     }
00086 
00087     ~TimeSpent()
00088     {
00089     check(_function, _file, _line);
00090     }
00091 
00092 private:
00093     TimeVal _start;
00094     const char *_function;
00095     const char *_file;
00096     int _line;
00097     TimeVal _limit;
00098 };
00099 
00100 #ifdef  CHECK_TIME
00101 
00104 #define TIMESPENT()       TimeSpent _t(__FUNCTION__,__FILE__,__LINE__, \
00105                        TIMESPENT_LIMIT)
00106 
00111 #define TIMESPENT_CHECK()   _t.check(__FUNCTION__, __FILE__, __LINE__)
00112 
00116 #define TIMESPENT_OVERLIMIT()   _t.overlimit()
00117 
00118 #else   // ! CHECK_TIME
00119 #define TIMESPENT()
00120 #define TIMESPENT_CHECK()
00121 #define TIMESPENT_OVERLIMIT()   0
00122 #endif
00123 
00124 #endif // __LIBXORP_TIMESPENT_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations