Changeset 2101 for trunk/src/kmk/misc.c


Ignore:
Timestamp:
Nov 25, 2008, 12:35:56 AM (17 years ago)
Author:
bird
Message:

kmk: Implemented new switch --print-time. Fixes #65.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/misc.c

    r2062 r2101  
    2929# if defined(__GLIBC__) || defined(HAVE_MALLINFO)
    3030#  include <malloc.h>
     31# endif
     32#endif
     33
     34#if defined (CONFIG_WITH_NANOTS) || defined (CONFIG_WITH_PRINT_TIME_SWITCH)
     35# ifdef WINDOWS32
     36#  include <Windows.h>
    3137# endif
    3238#endif
     
    11641170#ifdef CONFIG_WITH_PRINT_STATS_SWITCH
    11651171/* Print heap statistics if supported by the platform. */
    1166 void print_heap_stats (void)
     1172void
     1173print_heap_stats (void)
    11671174{
    11681175  /* Darwin / Mac OS X */
     
    12511258#endif /* CONFIG_WITH_PRINT_STATS_SWITCH */
    12521259
     1260#ifdef CONFIG_WITH_PRINT_TIME_SWITCH
     1261/* Get a nanosecond timestamp, from a monotonic time source if
     1262   possible.  Returns -1 after calling error() on failure.  */
     1263
     1264big_int
     1265nano_timestamp (void)
     1266{
     1267  big_int ts;
     1268#if defined (WINDOWS32)
     1269  static int s_state = -1;
     1270  static LARGE_INTEGER s_freq;
     1271
     1272  if (s_state == -1)
     1273    s_state = QueryPerformanceFrequency (&s_freq);
     1274  if (s_state)
     1275    {
     1276      LARGE_INTEGER pc;
     1277      if (!QueryPerformanceCounter (&pc))
     1278        {
     1279          s_state = 0;
     1280          return nano_timestamp ();
     1281        }
     1282      ts = (big_int)((long double)pc.QuadPart / (long double)s_freq.QuadPart * 1000000000);
     1283    }
     1284  else
     1285    {
     1286      /* fall back to low resolution system time. */
     1287      LARGE_INTEGER bigint;
     1288      FILETIME ft = {0,0};
     1289      GetSystemTimeAsFileTime (&ft);
     1290      bigint.u.LowPart = ft.dwLowDateTime;
     1291      bigint.u.HighPart = ft.dwLowDateTime;
     1292      ts = bigint.QuadPart * 100;
     1293    }
     1294
     1295#elif HAVE_GETTIMEOFDAY
     1296/* FIXME: Linux and others have the realtime clock_* api, detect and use it. */
     1297  struct timeval tv;
     1298  if (!gettimeofday (&tv, NULL))
     1299    ts = (big_int)tv.tv_sec * 1000000000
     1300       + tv.tv_usec * 1000;
     1301  else
     1302    {
     1303      error (NILF, _("gettimeofday failed"));
     1304      ts = -1;
     1305    }
     1306
     1307#else
     1308# error "PORTME"
     1309#endif
     1310
     1311  return ts;
     1312}
     1313
     1314/* Formats the elapsed time (nano seconds) in the manner easiest
     1315   to read, with millisecond percision for larger numbers.  */
     1316
     1317int
     1318format_elapsed_nano (char *buf, size_t size, big_int ts)
     1319{
     1320  int sz;
     1321  if (ts < 1000)
     1322    sz = sprintf (buf, "%uns", (unsigned)ts);
     1323  else if (ts < 100000)
     1324    sz = sprintf (buf, "%u.%03uus",
     1325                  (unsigned)(ts / 1000),
     1326                  (unsigned)(ts % 1000));
     1327  else
     1328    {
     1329      ts /= 1000;
     1330      if (ts < 1000)
     1331        sz = sprintf (buf, "%uus", (unsigned)ts);
     1332      else if (ts < 100000)
     1333        sz = sprintf (buf, "%u.%03ums",
     1334                      (unsigned)(ts / 1000),
     1335                      (unsigned)(ts % 1000));
     1336      else
     1337        {
     1338          ts /= 1000;
     1339          if (ts < BIG_INT_C(60000))
     1340            sz = sprintf (buf,
     1341                          "%u.%03us",
     1342                          (unsigned)(ts / 1000),
     1343                          (unsigned)(ts % 1000));
     1344          else
     1345            sz = sprintf (buf,
     1346                          "%um%u.%03us",
     1347                          (unsigned)( ts / BIG_INT_C(60000)),
     1348                          (unsigned)((ts % BIG_INT_C(60000)) / 1000),
     1349                          (unsigned)((ts % BIG_INT_C(60000)) % 1000));
     1350        }
     1351    }
     1352  if (sz >= size)
     1353    fatal (NILF, _("format_elapsed_nano buffer overflow: %d written, %d buffer"),
     1354           sz, size);
     1355  return sz;
     1356}
     1357#endif /* CONFIG_WITH_PRINT_TIME_SWITCH */
Note: See TracChangeset for help on using the changeset viewer.