VirtualBox

Changeset 3172 in kBuild


Ignore:
Timestamp:
Mar 21, 2018 2:21:23 PM (7 years ago)
Author:
bird
Message:

kmk/win: Make outsource the writing part of kmk_builtin_append to a worker thread to try avoid blocking the main kmk thread.

Location:
trunk/src/kmk
Files:
5 edited

Legend:

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

    r3170 r3172  
    239239
    240240    /* More frequently used commands: */
    241     BUILTIN_ENTRY(kmk_builtin_append,   "append",       FN_SIG_MAIN,            0, 0),
     241    BUILTIN_ENTRY(kmk_builtin_append,   "append",       FN_SIG_MAIN_SPAWNS,     0, 0),
    242242    BUILTIN_ENTRY(kmk_builtin_printf,   "printf",       FN_SIG_MAIN,            0, 0),
    243243    BUILTIN_ENTRY(kmk_builtin_echo,     "echo",         FN_SIG_MAIN,            0, 0),
     
    440440 * Prints the statistiscs to the given output stream.
    441441 */
    442 int kmk_builtin_print_stats(FILE *pOutput, const char *pszPrefix)
     442extern void kmk_builtin_print_stats(FILE *pOutput, const char *pszPrefix)
    443443{
    444444    const unsigned  cEntries = sizeof(g_aBuiltInStats) / sizeof(g_aBuiltInStats[0]);
  • trunk/src/kmk/kmkbuiltin.h

    r3170 r3172  
    9494typedef KMKBUILTINENTRY const *PCKMKBUILTINENTRY;
    9595
    96 extern int kmk_builtin_append(int argc, char **argv, char **envp);
     96#ifndef kmk_builtin_append
     97extern int kmk_builtin_append(int argc, char **argv, char **envp, struct child *pChild, pid_t *pPidSpawned);
     98#endif
    9799extern int kmk_builtin_cp(int argc, char **argv, char **envp);
    98100extern int kmk_builtin_cat(int argc, char **argv, char **envp);
     
    140142
    141143#ifdef CONFIG_WITH_KMK_BUILTIN_STATS
    142 int kmk_builtin_print_stats(FILE *pOutput, const char *pszPrefix);
     144extern void kmk_builtin_print_stats(FILE *pOutput, const char *pszPrefix);
    143145#endif
    144146
  • trunk/src/kmk/kmkbuiltin/append.c

    r3171 r3172  
    4545#ifdef HAVE_ALLOCA_H
    4646# include <alloca.h>
     47#endif
     48#if !defined(kmk_builtin_append) && defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
     49# include "../w32/winchildren.h"
    4750#endif
    4851#include "err.h"
     
    169172 * Appends text to a textfile, creating the textfile if necessary.
    170173 */
    171 int kmk_builtin_append(int argc, char **argv, char **envp)
     174#ifndef kmk_builtin_append
     175int kmk_builtin_append(int argc, char **argv, char **envp, struct child *pChild, pid_t *pPidSpawned)
     176#else
     177int main(int argc, char **argv, char **envp)
     178#endif
    172179{
     180#if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
     181    static const char s_szNewLine[] = "\r\n";
     182#else
     183    static const char s_szNewLine[] = "\n";
     184#endif
     185    KMKBUILTINAPPENDBUF OutBuf = { NULL, 0, 0, 0 };
    173186    const char *pszFilename;
    174187    int rc = 88;
     
    184197    int fLookForInserts = 0;
    185198#endif
    186 #if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
    187     static const char s_szNewLine[] = "\r\n";
    188 #else
    189     static const char s_szNewLine[] = "\n";
    190 #endif
    191     KMKBUILTINAPPENDBUF OutBuf = { NULL, 0, 0, 0 };
    192199
    193200    g_progname = argv[0];
     
    409416     * Write the buffer (unless we ran out of heap already).
    410417     */
     418#if !defined(kmk_builtin_append) && defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
     419    if (!OutBuf.fOutOfMemory)
     420    {
     421        rc = MkWinChildCreateAppend(pszFilename, &OutBuf.pszBuf, OutBuf.offBuf, fTruncate, pChild, pPidSpawned);
     422        if (rc != 0)
     423            rc = errx(rc, "MkWinChildCreateAppend failed: %u", rc);
     424        if (OutBuf.pszBuf)
     425            free(OutBuf.pszBuf);
     426    }
     427    else
     428#endif
    411429    if (!OutBuf.fOutOfMemory)
    412430    {
  • trunk/src/kmk/w32/winchildren.c

    r3169 r3172  
    110110    /** kmkbuiltin command. */
    111111    WINCHILDTYPE_BUILT_IN,
     112    /** kmkbuiltin_append result write out. */
     113    WINCHILDTYPE_APPEND,
    112114    /** kSubmit job. */
    113115    WINCHILDTYPE_SUBMIT,
     
    188190            char          **papszEnv;
    189191        } BuiltIn;
     192
     193        /** Data for WINCHILDTYPE_APPEND.   */
     194        struct
     195        {
     196            /** The filename. */
     197            char           *pszFilename;
     198            /** How much to append. */
     199            size_t          cbAppend;
     200            /** What to append. */
     201            char           *pszAppend;
     202            /** Whether to truncate the file. */
     203            int             fTruncate;
     204        } Append;
    190205
    191206        /** Data for WINCHILDTYPE_SUBMIT.   */
     
    16721687
    16731688/**
     1689 * Childcare worker: handle append write-out.
     1690 *
     1691 * @param   pWorker             The worker.
     1692 * @param   pChild              The kSubmit child.
     1693 */
     1694static void mkWinChildcareWorkerThreadHandleAppend(PWINCHILDCAREWORKER pWorker, PWINCHILD pChild)
     1695{
     1696    int fd = open(pChild->u.Append.pszFilename,
     1697                  pChild->u.Append.fTruncate
     1698                  ? O_WRONLY | O_TRUNC  | O_CREAT | _O_NOINHERIT | _O_BINARY
     1699                  : O_WRONLY | O_APPEND | O_CREAT | _O_NOINHERIT | _O_BINARY,
     1700                  0666);
     1701    if (fd >= 0)
     1702    {
     1703        ssize_t cbWritten = write(fd, pChild->u.Append.pszAppend, pChild->u.Append.cbAppend);
     1704        if (cbWritten == (ssize_t)pChild->u.Append.cbAppend)
     1705        {
     1706            if (close(fd) >= 0)
     1707            {
     1708                pChild->iExitCode = 0;
     1709                return;
     1710            }
     1711            fprintf(stderr, "kmk_builtin_append: close failed on '%s': %u (%s)\n",
     1712                    pChild->u.Append.pszFilename, errno, strerror(errno));
     1713        }
     1714        else
     1715            fprintf(stderr, "kmk_builtin_append: error writing %lu bytes to on '%s': %u (%s)\n",
     1716                    pChild->u.Append.cbAppend, pChild->u.Append.pszFilename, errno, strerror(errno));
     1717        close(fd);
     1718    }
     1719    else
     1720        fprintf(stderr, "kmk_builtin_append: error opening '%s': %u (%s)\n",
     1721                pChild->u.Append.pszFilename, errno, strerror(errno));
     1722    pChild->iExitCode = 1;
     1723}
     1724
     1725/**
    16741726 * Childcare worker: handle kSubmit job.
    16751727 *
     
    17841836                    case WINCHILDTYPE_BUILT_IN:
    17851837                        mkWinChildcareWorkerThreadHandleBuiltIn(pWorker, pChild);
     1838                        break;
     1839                    case WINCHILDTYPE_APPEND:
     1840                        mkWinChildcareWorkerThreadHandleAppend(pWorker, pChild);
    17861841                        break;
    17871842                    case WINCHILDTYPE_SUBMIT:
     
    20042059            break;
    20052060
     2061        case WINCHILDTYPE_APPEND:
     2062            if (pChild->u.Append.pszFilename)
     2063            {
     2064                free(pChild->u.Append.pszFilename);
     2065                pChild->u.Append.pszFilename = NULL;
     2066            }
     2067            if (pChild->u.Append.pszAppend)
     2068            {
     2069                free(pChild->u.Append.pszAppend);
     2070                pChild->u.Append.pszAppend = NULL;
     2071            }
     2072            break;
     2073
    20062074        case WINCHILDTYPE_SUBMIT:
    20072075            if (pChild->u.Submit.pvSubmitWorker)
     
    22832351
    22842352/**
     2353 * Interface used by append.c for do the slow file system part.
     2354 *
     2355 * This will append the given buffer to the specified file and free the buffer.
     2356 *
     2357 * @returns 0 on success, windows status code on failure.
     2358 *
     2359 * @param   pszFilename     The name of the file to append to.
     2360 * @param   ppszAppend      What to append.  The pointer pointed to is set to
     2361 *                          NULL once we've taken ownership of the buffer and
     2362 *                          promise to free it.
     2363 * @param   cbAppend        How much to append.
     2364 * @param   fTruncate       Whether to truncate the file before appending to it.
     2365 * @param   pMkChild        The make child structure.
     2366 * @param   pPid            Where to return the pid.
     2367 */
     2368int MkWinChildCreateAppend(const char *pszFilename, char **ppszAppend, size_t cbAppend, int fTruncate,
     2369                           struct child *pMkChild, pid_t *pPid)
     2370{
     2371    size_t    cbFilename = strlen(pszFilename) + 1;
     2372    PWINCHILD pChild     = mkWinChildNew(WINCHILDTYPE_APPEND);
     2373    pChild->pMkChild            = pMkChild;
     2374    pChild->u.Append.fTruncate  = fTruncate;
     2375    pChild->u.Append.pszAppend  = *ppszAppend;
     2376    pChild->u.Append.cbAppend   = cbAppend;
     2377    pChild->u.Append.pszFilename = (char *)memcpy(xmalloc(cbFilename), pszFilename, cbFilename);
     2378    *ppszAppend = NULL;
     2379    return mkWinChildPushToCareWorker(pChild, pPid);
     2380}
     2381
     2382/**
    22852383 * Interface used by kSubmit.c for registering stuff to wait on.
    22862384 *
     
    23372435                    pChild->iSignal = iSignal;
    23382436                    break;
    2339 
    23402437#ifdef KMK
    2341 
    23422438                case WINCHILDTYPE_SUBMIT:
    23432439                {
     
    23562452
    23572453#endif /* KMK */
    2358 
    23592454                default:
    23602455                    assert(0);
     
    24182513#ifdef KMK
    24192514        case WINCHILDTYPE_BUILT_IN:
    2420             break;
     2515        case WINCHILDTYPE_APPEND:
    24212516        case WINCHILDTYPE_SUBMIT:
    2422             break;
    24232517        case WINCHILDTYPE_REDIRECT:
    24242518            break;
  • trunk/src/kmk/w32/winchildren.h

    r3169 r3172  
    3737int     MkWinChildCreateBuiltIn(struct KMKBUILTINENTRY const *pBuiltIn, int cArgs, char **papszArgs,
    3838                                char **papszEnv, struct child *pMkChild, pid_t *pPid);
     39int     MkWinChildCreateAppend(const char *pszFilename, char **ppszAppend, size_t cbAppend, int fTruncate,
     40                               struct child *pMkChild, pid_t *pPid);
    3941int     MkWinChildCreateSubmit(intptr_t hEvent, void *pvSubmitWorker, pid_t *pPid);
    4042int     MkWinChildCreateRedirect(intptr_t hProcess, pid_t *pPid);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette