Ignore:
Timestamp:
Mar 21, 2018, 1:32:27 PM (7 years ago)
Author:
bird
Message:

kmkbuiltin: stats

File:
1 edited

Legend:

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

    r3169 r3170  
    3636# include <io.h>
    3737#endif
     38
     39#include "makeint.h"
     40#include "job.h"
    3841#if defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
    39 # include "makeint.h"
    40 # include "job.h"
    4142# include "w32/winchildren.h"
    4243#endif
     
    231232 * kmk built command.
    232233 */
    233 static const KMKBUILTINENTRY g_aBuiltins[] =
     234static const KMKBUILTINENTRY g_aBuiltIns[] =
    234235{
    235236#define BUILTIN_ENTRY(a_fn, a_sz, a_uFnSignature, fMpSafe, fNeedEnv) \
     
    266267};
    267268
     269#ifdef CONFIG_WITH_KMK_BUILTIN_STATS
     270/** Statistics running in parallel to g_aBuiltIns. */
     271struct
     272{
     273    big_int         cNs;
     274    unsigned        cTimes;
     275    unsigned        cAsyncTimes;
     276} g_aBuiltInStats[sizeof(g_aBuiltIns) / sizeof(g_aBuiltIns[0])];
     277#endif
     278
    268279
    269280int kmk_builtin_command_parsed(int argc, char **argv, struct child *pChild, char ***ppapszArgvToSpawn, pid_t *pPidSpawned)
     
    325336         * Look up the builtin command in the table.
    326337         */
    327         pEntry  = &g_aBuiltins[0];
    328         cLeft   = sizeof(g_aBuiltins) / sizeof(g_aBuiltins[0]);
     338        pEntry  = &g_aBuiltIns[0];
     339        cLeft   = sizeof(g_aBuiltIns) / sizeof(g_aBuiltIns[0]);
    329340        while (cLeft-- > 0)
    330341            if (   pEntry->uName.cchAndStart != cchAndStart
     
    340351#if defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
    341352                if (pEntry->fMpSafe)
     353                {
    342354                    rc = MkWinChildCreateBuiltIn(pEntry, argc, argv, pEntry->fNeedEnv ? pChild->environment : NULL,
    343355                                                 pChild, pPidSpawned);
     356# ifdef CONFIG_WITH_KMK_BUILTIN_STATS
     357                    g_aBuiltInStats[pEntry - &g_aBuiltIns[0]].cAsyncTimes++;
     358# endif
     359                }
    344360                else
    345361#endif
     
    350366                     * Call the worker function, making sure to preserve umask.
    351367                     */
     368#ifdef CONFIG_WITH_KMK_BUILTIN_STATS
     369                    big_int nsStart = nano_timestamp();
     370#endif
    352371                    int const iUmask = umask(0);        /* save umask */
    353372                    umask(iUmask);
     
    386405                    else
    387406                        rc = 99;
     407
    388408                    g_progname = "kmk";                 /* paranoia, make sure it's not pointing at a freed argv[0]. */
    389409                    umask(iUmask);                      /* restore it */
     410
     411#ifdef CONFIG_WITH_KMK_BUILTIN_STATS
     412                    g_aBuiltInStats[pEntry - &g_aBuiltIns[0]].cTimes++;
     413                    g_aBuiltInStats[pEntry - &g_aBuiltIns[0]].cNs += nano_timestamp() - nsStart;
     414#endif
    390415                }
    391416                return rc;
     
    411436#endif
    412437
     438#ifdef CONFIG_WITH_KMK_BUILTIN_STATS
     439/**
     440 * Prints the statistiscs to the given output stream.
     441 */
     442int kmk_builtin_print_stats(FILE *pOutput, const char *pszPrefix)
     443{
     444    const unsigned  cEntries = sizeof(g_aBuiltInStats) / sizeof(g_aBuiltInStats[0]);
     445    unsigned i;
     446    fprintf(pOutput, "\n%skmk built-in command statistics:\n", pszPrefix);
     447    for (i = 0; i < cEntries; i++)
     448        if (g_aBuiltInStats[i].cTimes > 0)
     449        {
     450            char szTotal[64];
     451            char szAvg[64];
     452            format_elapsed_nano(szTotal, sizeof(szTotal), g_aBuiltInStats[i].cNs);
     453            format_elapsed_nano(szAvg, sizeof(szAvg), g_aBuiltInStats[i].cNs / g_aBuiltInStats[i].cTimes);
     454            fprintf(pOutput, "%s kmk_builtin_%-9s: %4lu times, %9s total, %9s/call\n",
     455                    pszPrefix, g_aBuiltIns[i].uName.s.sz, g_aBuiltInStats[i].cTimes, szTotal, szAvg);
     456        }
     457        else if (g_aBuiltInStats[i].cAsyncTimes > 0)
     458            fprintf(pOutput, "%s kmk_builtin_%-9s: %4lu times in worker thread\n",
     459                    pszPrefix, g_aBuiltIns[i].uName.s.sz, g_aBuiltInStats[i].cAsyncTimes);
     460}
     461#endif
     462
Note: See TracChangeset for help on using the changeset viewer.