log.h 20.1 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22 23 24

/*! \file log.h
* \brief openair log generator (OLG) for
* \author Navid Nikaein
nikaeinn's avatar
nikaeinn committed
25
* \date 2009 - 2014
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
* \version 0.5
* @ingroup util

*/

#ifndef __LOG_H__
#    define __LOG_H__

/*--- INCLUDES ---------------------------------------------------------------*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
laurent's avatar
laurent committed
45 46
#include <time.h>
#include <stdint.h>
47
#ifndef __STDC_FORMAT_MACROS
Raphael Defosseux's avatar
Raphael Defosseux committed
48
  #define __STDC_FORMAT_MACROS
49
#endif
laurent's avatar
laurent committed
50
#include <inttypes.h>
laurent's avatar
laurent committed
51
#ifndef _GNU_SOURCE
Raphael Defosseux's avatar
Raphael Defosseux committed
52
  #define _GNU_SOURCE
laurent's avatar
laurent committed
53
#endif
54
#include <pthread.h>
55
#include "T.h"
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*----------------------------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif

/** @defgroup _LOG LOG Generator
 * @{*/
/* @}*/

/** @defgroup _macro Macro Definition
 *  @ingroup _LOG
 *  @brief these macros are used in the code of LOG
 * @{*/
/* @}*/

/** @defgroup _max_length Maximum Length of LOG
 *  @ingroup _macro
 *  @brief the macros that describe the maximum length of LOG
 * @{*/
76

77 78 79 80 81 82 83
#define MAX_LOG_TOTAL 1500 /*!< \brief the maximum length of a log */
/* @}*/

/** @defgroup _log_level Message levels defined by LOG
 *  @ingroup _macro
 *  @brief LOG defines 9 levels of messages for users. Importance of these levels decrease gradually from 0 to 8
 * @{*/
84 85 86 87 88 89 90 91
# define  OAILOG_DISABLE -1 /*!< \brief disable all LOG messages, cannot be used in LOG macros, use only in LOG module */
# define  OAILOG_ERR      0 /*!< \brief critical error conditions, impact on "must have" fuctinalities */
# define  OAILOG_WARNING  1 /*!< \brief warning conditions, shouldn't happen but doesn't impact "must have" functionalities */
# define  OAILOG_INFO     2 /*!< \brief informational messages most people don't need, shouldn't impact real-time behavior */
# define  OAILOG_DEBUG    3 /*!< \brief first level debug-level messages, for developers , may impact real-time behavior */
# define  OAILOG_TRACE    4 /*!< \brief  second level debug-level messages, for developers ,likely impact real-time behavior*/

#define NUM_LOG_LEVEL 5 /*!< \brief the number of message levels users have with LOG (OAILOG_DISABLE is not available to user as a level, so it is not included)*/
92 93 94
/* @}*/


laurent's avatar
laurent committed
95 96 97 98 99 100 101
/** @defgroup _log_format Defined log format
 *  @ingroup _macro
 *  @brief Macro of log formats defined by LOG
 * @{*/

/* .log_format = 0x13 uncolored standard messages
 * .log_format = 0x93 colored standard messages */
102
/* keep white space in first position; switching it to 0 allows colors to be disabled*/
laurent's avatar
laurent committed
103 104 105 106 107 108 109 110 111 112 113 114 115
#define LOG_RED "\033[1;31m"  /*!< \brief VT100 sequence for bold red foreground */
#define LOG_GREEN "\033[32m"  /*!< \brief VT100 sequence for green foreground */
#define LOG_ORANGE "\033[93m"   /*!< \brief VT100 sequence for orange foreground */
#define LOG_BLUE "\033[34m" /*!< \brief VT100 sequence for blue foreground */
#define LOG_CYBL "\033[40;36m"  /*!< \brief VT100 sequence for cyan foreground on black background */
#define LOG_RESET "\033[0m" /*!< \brief VT100 sequence for reset (black) foreground */
/* @}*/


/** @defgroup _syslog_conf Macros for write in syslog.conf
 *  @ingroup _macro
 *  @brief Macros used to write lines (local/remote) in syslog.conf
 * @{*/
116 117


118 119 120 121 122
#define FLAG_NOCOLOR     0x0001  /*!< \brief use colors in log messages, depending on level */
#define FLAG_THREAD      0x0008  /*!< \brief display thread name in log messages */
#define FLAG_LEVEL       0x0010  /*!< \brief display log level in log messages */
#define FLAG_TIME        0x0100
#define FLAG_INITIALIZED 0x8000
123 124 125 126 127 128

#define SET_LOG_OPTION(O)   g_log->flag = (g_log->flag | O)
#define CLEAR_LOG_OPTION(O) g_log->flag = (g_log->flag & (~O))

/** @defgroup macros to identify a debug entity
 *  @ingroup each macro is a bit mask where the unique bit set identifies an entity to be debugged
Raphael Defosseux's avatar
Raphael Defosseux committed
129
 *            it allows to dynamically activate or not blocks of code. The  LOG_MASKMAP_INIT macro
130 131 132
 *            is used to map a character string name to each debug bit, it allows to set or clear
 *            the corresponding bit via the defined name, from the configuration or from the telnet
 *            server.
Raphael Defosseux's avatar
Raphael Defosseux committed
133
 *  @brief
134
 * @{*/
135 136 137
#define DEBUG_PRACH        (1<<0)
#define DEBUG_RU           (1<<1)
#define DEBUG_UE_PHYPROC   (1<<2)
138
#define DEBUG_LTEESTIM     (1<<3)
139 140 141 142 143
#define DEBUG_DLCELLSPEC   (1<<4)
#define DEBUG_ULSCH        (1<<5)
#define DEBUG_RRC          (1<<6)
#define DEBUG_PDCP         (1<<7)
#define DEBUG_DFT          (1<<8)
144
#define DEBUG_ASN1         (1<<9)
145
#define DEBUG_CTRLSOCKET   (1<<10)
146 147
#define DEBUG_SECURITY     (1<<11)
#define DEBUG_NAS          (1<<12)
148
#define DEBUG_RLC          (1<<13)
149
#define UE_TIMING          (1<<20)
150 151


152
#define LOG_MASKMAP_INIT {\
Raphael Defosseux's avatar
Raphael Defosseux committed
153 154 155 156 157 158 159 160 161 162 163 164 165
    {"PRACH",       DEBUG_PRACH},\
    {"RU",          DEBUG_RU},\
    {"UE_PHYPROC",  DEBUG_UE_PHYPROC},\
    {"LTEESTIM",    DEBUG_LTEESTIM},\
    {"DLCELLSPEC",  DEBUG_DLCELLSPEC},\
    {"ULSCH",       DEBUG_ULSCH},\
    {"RRC",         DEBUG_RRC},\
    {"PDCP",        DEBUG_PDCP},\
    {"DFT",         DEBUG_DFT},\
    {"ASN1",        DEBUG_ASN1},\
    {"CTRLSOCKET",  DEBUG_CTRLSOCKET},\
    {"SECURITY",    DEBUG_SECURITY},\
    {"NAS",         DEBUG_NAS},\
166
    {"RLC",         DEBUG_RLC},\
Raphael Defosseux's avatar
Raphael Defosseux committed
167 168 169
    {"UE_TIMING",   UE_TIMING},\
    {NULL,-1}\
  }
170 171 172 173 174 175



#define SET_LOG_DEBUG(B)   g_log->debug_mask = (g_log->debug_mask | B)
#define CLEAR_LOG_DEBUG(B) g_log->debug_mask = (g_log->debug_mask & (~B))

176 177
#define SET_LOG_DUMP(B)   g_log->dump_mask = (g_log->dump_mask | B)
#define CLEAR_LOG_DUMP(B) g_log->dump_mask = (g_log->dump_mask & (~B))
178

laurent's avatar
laurent committed
179 180 181


typedef enum {
Raphael Defosseux's avatar
Raphael Defosseux committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  MIN_LOG_COMPONENTS = 0,
  PHY = MIN_LOG_COMPONENTS,
  MAC,
  SIM,
  OCG,
  OMG,
  OPT,
  OTG,
  OTG_LATENCY,
  OTG_LATENCY_BG,
  OTG_GP,
  OTG_GP_BG,
  OTG_JITTER,
  RLC,
  PDCP,
  RRC,
  NAS,
  PERF,
  OIP,
  MSC,
  OCM,
  UDP_,
  GTPU,
  SPGW,
  S1AP,
  SCTP,
  HW,
  OSA,
  RAL_ENB,
  RAL_UE,
  ENB_APP,
  FLEXRAN_AGENT,
  TMR,
  USIM,
  LOCALIZE,
  X2AP,
  LOADER,
  ASN,
220 221
  NFAPI_VNF,
  NFAPI_PNF,
Raphael Defosseux's avatar
Raphael Defosseux committed
222
  MAX_LOG_PREDEF_COMPONENTS,
laurent's avatar
laurent committed
223 224 225
}
comp_name_t;

226 227
#define MAX_LOG_DYNALLOC_COMPONENTS 20
#define MAX_LOG_COMPONENTS (MAX_LOG_PREDEF_COMPONENTS + MAX_LOG_DYNALLOC_COMPONENTS)
228

laurent's avatar
laurent committed
229 230

typedef struct {
Raphael Defosseux's avatar
Raphael Defosseux committed
231 232
  char *name; /*!< \brief string name of item */
  int value;  /*!< \brief integer value of mapping */
laurent's avatar
laurent committed
233 234
} mapping;

235 236
typedef int(*log_vprint_func_t)(FILE *stream, const char *format, va_list ap );
typedef int(*log_print_func_t)(FILE *stream, const char *format, ... );
laurent's avatar
laurent committed
237
typedef struct  {
Raphael Defosseux's avatar
Raphael Defosseux committed
238 239 240 241 242 243 244 245 246 247 248
  const char        *name;
  int               level;
  int               savedlevel;
  int               flag;
  int               filelog;
  char              *filelog_name;
  FILE              *stream;
  log_vprint_func_t vprint;
  log_print_func_t  print;
  /* SR: make the log buffer component relative */
  char             log_buffer[MAX_LOG_TOTAL];
laurent's avatar
laurent committed
249 250 251 252
} log_component_t;


typedef struct {
Raphael Defosseux's avatar
Raphael Defosseux committed
253 254 255 256 257 258
  log_component_t         log_component[MAX_LOG_COMPONENTS];
  char                    level2string[NUM_LOG_LEVEL];
  int                     flag;
  char                   *filelog_name;
  uint64_t                debug_mask;
  uint64_t                dump_mask;
laurent's avatar
laurent committed
259 260 261 262 263
} log_t;


#if defined(ENABLE_ITTI)
typedef enum log_instance_type_e {
Raphael Defosseux's avatar
Raphael Defosseux committed
264 265 266
  LOG_INSTANCE_UNKNOWN,
  LOG_INSTANCE_ENB,
  LOG_INSTANCE_UE,
laurent's avatar
laurent committed
267 268 269 270 271 272
} log_instance_type_t;

void log_set_instance_type (log_instance_type_t instance);
#endif


273 274 275 276
#ifdef LOG_MAIN
log_t *g_log;
#else
#ifdef __cplusplus
Raphael Defosseux's avatar
Raphael Defosseux committed
277
extern "C" {
278
#endif
Raphael Defosseux's avatar
Raphael Defosseux committed
279
  extern log_t *g_log;
280 281 282 283
#ifdef __cplusplus
}
#endif
#endif
laurent's avatar
laurent committed
284 285 286 287
/*--- INCLUDES ---------------------------------------------------------------*/
#    include "log_if.h"
/*----------------------------------------------------------------------------*/
int  logInit (void);
288
int isLogInitDone (void);
289
void logRecord_mt(const char *file, const char *func, int line,int comp, int level, const char *format, ...) __attribute__ ((format (printf, 6, 7)));
290
void vlogRecord_mt(const char *file, const char *func, int line, int comp, int level, const char *format, va_list args );
Raphael Defosseux's avatar
Raphael Defosseux committed
291
void log_dump(int component, void *buffer, int buffsize,int datatype, const char *format, ... );
292
int  set_log(int component, int level);
293 294
void set_glog(int level);

oai's avatar
oai committed
295 296
void set_glog_onlinelog(int enable);
void set_glog_filelog(int enable);
laurent's avatar
laurent committed
297
void set_component_filelog(int comp);
298 299
void close_component_filelog(int comp);
void set_component_consolelog(int comp);
laurent's avatar
laurent committed
300 301 302 303
int  map_str_to_int(mapping *map, const char *str);
char *map_int_to_str(mapping *map, int val);
void logClean (void);
int  is_newline( char *str, int size);
304

305
int register_log_component(char *name, char *fext, int compidx);
306

307 308
/* @}*/

Mongazon's avatar
Mongazon committed
309 310 311 312 313 314 315 316 317 318 319
/*!\fn int32_t write_file_matlab(const char *fname, const char *vname, void *data, int length, int dec, char format);
\brief Write output file from signal data
@param fname output file name
@param vname  output vector name (for MATLAB/OCTAVE)
@param data   point to data
@param length length of data vector to output
@param dec    decimation level
@param format data format (0 = real 16-bit, 1 = complex 16-bit,2 real 32-bit, 3 complex 32-bit,4 = real 8-bit, 5 = complex 8-bit)
*/
int32_t write_file_matlab(const char *fname, const char *vname, void *data, int length, int dec, char format);

oai's avatar
oai committed
320 321 322 323 324 325
/*----------------macro definitions for reading log configuration from the config module */
#define CONFIG_STRING_LOG_PREFIX                           "log_config"

#define LOG_CONFIG_STRING_GLOBAL_LOG_LEVEL                 "global_log_level"
#define LOG_CONFIG_STRING_GLOBAL_LOG_ONLINE                "global_log_online"
#define LOG_CONFIG_STRING_GLOBAL_LOG_INFILE                "global_log_infile"
326
#define LOG_CONFIG_STRING_GLOBAL_LOG_OPTIONS               "global_log_options"
oai's avatar
oai committed
327 328 329

#define LOG_CONFIG_LEVEL_FORMAT                            "%s_log_level"
#define LOG_CONFIG_LOGFILE_FORMAT                          "%s_log_infile"
330
#define LOG_CONFIG_DEBUG_FORMAT                            "%s_debug"
331
#define LOG_CONFIG_DUMP_FORMAT                             "%s_dump"
332 333

#define LOG_CONFIG_HELP_OPTIONS      " list of comma separated options to enable log module behavior. Available options: \n"\
Raphael Defosseux's avatar
Raphael Defosseux committed
334 335 336 337 338
  " nocolor:   disable color usage in log messages\n"\
  " level:     add log level indication in log messages\n"\
  " thread:    add threads names in log messages\n"


339

340

341
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
Raphael Defosseux's avatar
Raphael Defosseux committed
342 343
/*                                       LOG globalconfiguration parameters                                                                   */
/*   optname                            help                                                paramflags       XXXptr                    defXXXval              type   numelt */
344
/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
oai's avatar
oai committed
345
#define LOG_GLOBALPARAMS_DESC { \
Raphael Defosseux's avatar
Raphael Defosseux committed
346 347 348 349
    {LOG_CONFIG_STRING_GLOBAL_LOG_LEVEL,    "Default log level for all componemts\n",              0,         strptr:(char **)&gloglevel,    defstrval:log_level_names[2].name,    TYPE_STRING,    0}, \
    {LOG_CONFIG_STRING_GLOBAL_LOG_ONLINE,   "Default console output option, for all components\n", 0,         iptr:&(consolelog),            defintval:1,                          TYPE_INT,       0}, \
    {LOG_CONFIG_STRING_GLOBAL_LOG_OPTIONS,  LOG_CONFIG_HELP_OPTIONS,                               0,         strlistptr:NULL,               defstrlistval:NULL,                   TYPE_STRINGLIST,0} \
  }
350 351

#define LOG_OPTIONS_IDX   2
352 353


oai's avatar
oai committed
354
/*----------------------------------------------------------------------------------*/
355 356 357 358
/** @defgroup _debugging debugging macros
 *  @ingroup _macro
 *  @brief Macro used to call logIt function with different message levels
 * @{*/
359 360 361 362
#define LOG_DUMP_CHAR       0
#define LOG_DUMP_DOUBLE     1
// debugging macros
#define LOG_F  LOG_I           /* because  LOG_F was originaly to dump a message or buffer but is also used as a regular level...., to dump use LOG_DUMPMSG */
Raphael Defosseux's avatar
Raphael Defosseux committed
363 364 365 366 367 368 369
#  if T_TRACER
/* per component, level dependant macros */
#    define LOG_E(c, x...) do { if (T_stdout) { if( g_log->log_component[c].level >= OAILOG_ERR    ) logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_ERR, x)     ;} else { T(T_LEGACY_ ## c ## _ERROR, T_PRINTF(x))   ;}} while (0)
#    define LOG_W(c, x...) do { if (T_stdout) { if( g_log->log_component[c].level >= OAILOG_WARNING) logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_WARNING, x) ;} else { T(T_LEGACY_ ## c ## _WARNING, T_PRINTF(x)) ;}} while (0)
#    define LOG_I(c, x...) do { if (T_stdout) { if( g_log->log_component[c].level >= OAILOG_INFO   ) logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_INFO, x)    ;} else { T(T_LEGACY_ ## c ## _INFO, T_PRINTF(x))    ;}} while (0)
#    define LOG_D(c, x...) do { if (T_stdout) { if( g_log->log_component[c].level >= OAILOG_DEBUG  ) logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_DEBUG, x)   ;} else { T(T_LEGACY_ ## c ## _DEBUG, T_PRINTF(x))   ;}} while (0)
#    define LOG_T(c, x...) do { if (T_stdout) { if( g_log->log_component[c].level >= OAILOG_TRACE  ) logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_TRACE, x)   ;} else { T(T_LEGACY_ ## c ## _TRACE, T_PRINTF(x))   ;}} while (0)
370
#    define VLOG(c,l, f, args) do { if (T_stdout) { if( g_log->log_component[c].level >= l  ) vlogRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, l, f, args)   ;} } while (0)
Raphael Defosseux's avatar
Raphael Defosseux committed
371
/* macro used to dump a buffer or a message as in openair2/RRC/LTE/RRC_eNB.c, replaces LOG_F macro */
372
#    define LOG_DUMPMSG(c, f, b, s, x...) do {  if(g_log->dump_mask & f) log_dump(c, b, s, LOG_DUMP_CHAR, x)  ;}   while (0)  /* */
Raphael Defosseux's avatar
Raphael Defosseux committed
373
/* bitmask dependant macros, to isolate debugging code */
374 375
#    define LOG_DEBUGFLAG(D) (g_log->debug_mask & D)

Raphael Defosseux's avatar
Raphael Defosseux committed
376
/* bitmask dependant macros, to generate debug file such as matlab file or message dump */
377
#    define LOG_DUMPFLAG(D) (g_log->dump_mask & D)
378
#    define LOG_M(file, vector, data, len, dec, format) do { write_file_matlab(file, vector, data, len, dec, format);} while(0)/* */
Raphael Defosseux's avatar
Raphael Defosseux committed
379
/* define variable only used in LOG macro's */
380
#    define LOG_VAR(A,B) A B
381
#  else /* T_TRACER: remove all debugging and tracing messages, except errors */
382 383 384 385 386
#    define LOG_I(c, x...) /* */
#    define LOG_W(c, x...) /* */
#    define LOG_E(c, x...) /* */
#    define LOG_D(c, x...) /* */
#    define LOG_T(c, x...) /* */
387 388

#    define LOG_DUMPMSG(c, b, s, x...) /* */
Raphael Defosseux's avatar
Raphael Defosseux committed
389
#    define nfapi_log(FILE, FNC, LN, COMP, LVL, FMT...)
390
#    define LOG_DEBUGFLAG(D)  ( 0 )
Raphael Defosseux's avatar
Raphael Defosseux committed
391 392
#    define LOG_DUMPFLAG(D) ( 0 )
#    define LOG_M(file, vector, data, len, dec, format)
393
#    define LOG_VAR(A,B)
394
#  endif /* T_TRACER */
395
/* avoid warnings for variables only used in LOG macro's but set outside debug section */
Raphael Defosseux's avatar
Raphael Defosseux committed
396 397
#define GCC_NOTUSED   __attribute__((unused))
#define LOG_USEDINLOG_VAR(A,B) GCC_NOTUSED A B
398

399 400 401
/* unfiltered macros, usefull for simulators or messages at init time, before log is configured */
#define LOG_UM(file, vector, data, len, dec, format) do { write_file_matlab(file, vector, data, len, dec, format);} while(0)
#define LOG_UI(c, x...) do {logRecord_mt(__FILE__, __FUNCTION__, __LINE__,c, OAILOG_INFO, x) ; } while(0)
402
#define LOG_UDUMPMSG(c, b, s, f, x...) do { log_dump(c, b, s, f, x)  ;}   while (0)  /* */
403 404 405 406 407 408 409
/* @}*/


/** @defgroup _useful_functions useful functions in LOG
 *  @ingroup _macro
 *  @brief Macro of some useful functions defined by LOG
 * @{*/
410 411 412 413 414 415
#define LOG_ENTER(c) do {LOG_T(c, "Entering %s\n",__FUNCTION__);}while(0) /*!< \brief Macro to log a message with severity DEBUG when entering a function */
#define LOG_END(c) do {LOG_T(c, "End of  %s\n",__FUNCTION__);}while(0) /*!< \brief Macro to log a message with severity DEBUG when entering a function */
#define LOG_EXIT(c)  do { LOG_END(c); return;}while(0)  /*!< \brief Macro to log a message with severity TRACE when exiting a function */
#define LOG_RETURN(c,r) do {LOG_T(c,"Leaving %s (rc = %08lx)\n", __FUNCTION__ , (unsigned long)(r) );return(r);}while(0)  /*!< \brief Macro to log a function exit, including integer value, then to return a value to the calling function */


416 417
/* @}*/

laurent's avatar
laurent committed
418
static __inline__ uint64_t rdtsc(void) {
Cedric Roux's avatar
Cedric Roux committed
419
  uint32_t a, d;
laurent's avatar
laurent committed
420
  __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
Cedric Roux's avatar
Cedric Roux committed
421
  return (((uint64_t)d)<<32) | ((uint64_t)a);
422
}
423

laurent's avatar
laurent committed
424 425
#define DEBUG_REALTIME 1
#if DEBUG_REALTIME
426

laurent's avatar
laurent committed
427
extern double cpuf;
428

Raphael Defosseux's avatar
Raphael Defosseux committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static inline uint64_t checkTCPU(int timeout, char *file, int line) {
  static uint64_t __thread lastCPUTime=0;
  static uint64_t __thread last=0;
  uint64_t cur=rdtsc();
  struct timespec CPUt;
  clock_gettime(CLOCK_THREAD_CPUTIME_ID, &CPUt);
  uint64_t CPUTime=CPUt.tv_sec*1000*1000+CPUt.tv_nsec/1000;
  double microCycles=(double)(cpuf*1000);
  int duration=(int)((cur-last)/microCycles);

  if ( last!=0 && duration > timeout ) {
    //struct timespec ts;
    //clock_gettime(CLOCK_MONOTONIC, &ts);
    printf("%s:%d lte-ue delay %d (exceed %d), CPU for this period: %lld\n", file, line,
           duration, timeout, (long long)CPUTime-lastCPUTime );
  }

  last=cur;
  lastCPUTime=CPUTime;
  return cur;
laurent's avatar
laurent committed
449 450
}

Raphael Defosseux's avatar
Raphael Defosseux committed
451 452 453 454 455 456 457 458 459 460 461 462
static inline unsigned long long checkT(int timeout, char *file, int line) {
  static unsigned long long __thread last=0;
  unsigned long long cur=rdtsc();
  int microCycles=(int)(cpuf*1000);
  int duration=(int)((cur-last)/microCycles);

  if ( last!=0 && duration > timeout )
    printf("%s:%d lte-ue delay %d (exceed %d)\n", file, line,
           duration, timeout);

  last=cur;
  return cur;
laurent's avatar
laurent committed
463
}
464

laurent's avatar
laurent committed
465
typedef struct m {
Raphael Defosseux's avatar
Raphael Defosseux committed
466 467 468
  uint64_t iterations;
  uint64_t sum;
  uint64_t maxArray[11];
laurent's avatar
laurent committed
469 470
} Meas;

Raphael Defosseux's avatar
Raphael Defosseux committed
471 472 473 474 475 476 477 478 479 480
static inline void printMeas(char *txt, Meas *M, int period) {
  if (M->iterations%period == 0 ) {
    char txt2[512];
    sprintf(txt2,"%s avg=%" PRIu64 " iterations=%" PRIu64 " max=%"
            PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 "\n",
            txt,
            M->sum/M->iterations,
            M->iterations,
            M->maxArray[1],M->maxArray[2], M->maxArray[3],M->maxArray[4], M->maxArray[5],
            M->maxArray[6],M->maxArray[7], M->maxArray[8],M->maxArray[9],M->maxArray[10]);
Cedric Roux's avatar
Cedric Roux committed
481
#if DISABLE_LOG_X
Raphael Defosseux's avatar
Raphael Defosseux committed
482
    printf("%s",txt2);
Cedric Roux's avatar
Cedric Roux committed
483
#else
Raphael Defosseux's avatar
Raphael Defosseux committed
484
    LOG_W(PHY, "%s",txt2);
Cedric Roux's avatar
Cedric Roux committed
485
#endif
Raphael Defosseux's avatar
Raphael Defosseux committed
486
  }
laurent's avatar
laurent committed
487
}
488

Raphael Defosseux's avatar
Raphael Defosseux committed
489 490 491 492
static inline int cmpint(const void *a, const void *b) {
  uint64_t *aa=(uint64_t *)a;
  uint64_t *bb=(uint64_t *)b;
  return (int)(*aa-*bb);
laurent's avatar
laurent committed
493
}
494

Raphael Defosseux's avatar
Raphael Defosseux committed
495 496 497 498 499 500 501 502 503 504
static inline void updateTimes(uint64_t start, Meas *M, int period, char *txt) {
  if (start!=0) {
    uint64_t end=rdtsc();
    long long diff=(end-start)/(cpuf*1000);
    M->maxArray[0]=diff;
    M->sum+=diff;
    M->iterations++;
    qsort(M->maxArray, 11, sizeof(uint64_t), cmpint);
    printMeas(txt,M,period);
  }
laurent's avatar
laurent committed
505
}
506

laurent's avatar
laurent committed
507 508
#define check(a) do { checkT(a,__FILE__,__LINE__); } while (0)
#define checkcpu(a) do { checkTCPU(a,__FILE__,__LINE__); } while (0)
laurent's avatar
laurent committed
509
#define initRefTimes(a) static __thread Meas a= {0}
laurent's avatar
laurent committed
510
#define pickTime(a) uint64_t a=rdtsc()
laurent's avatar
laurent committed
511
#define readTime(a) a
laurent's avatar
laurent committed
512 513
#define initStaticTime(a) static __thread uint64_t a={0}
#define pickStaticTime(a) do { a=rdtsc(); } while (0)
514

laurent's avatar
laurent committed
515 516
#else
#define check(a) do {} while (0)
laurent's avatar
laurent committed
517
#define checkcpu(a) do {} while (0)
laurent's avatar
laurent committed
518 519 520 521 522 523 524
#define initRefTimes(a) do {} while (0)
#define initStaticTime(a) do {} while (0)
#define pickTime(a) do {} while (0)
#define readTime(a) 0
#define pickStaticTime(a) do {} while (0)
#define updateTimes(a,b,c,d) do {} while (0)
#define printMeas(a,b,c) do {} while (0)
525 526
#endif

527 528 529 530 531 532 533
#ifdef __cplusplus
}
#endif

#endif