VirtualBox

Changeset 3465 in kBuild for trunk


Ignore:
Timestamp:
Sep 15, 2020 7:46:48 PM (4 years ago)
Author:
bird
Message:

kash: Keep the filename around in debug builds. Added profiling of CloseHandle in debug builds, verifying that is is _slow_ when modifying (e.g. appending to) existing files.

Location:
trunk/src/kash
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shfile.c

    r3458 r3465  
    156156#ifdef SHFILE_IN_USE
    157157
     158# ifdef DEBUG
     159# if K_OS == K_OS_WINDOWS
     160static KU64 shfile_nano_ts(void)
     161{
     162    static KBOOL volatile s_has_factor = K_FALSE;
     163    static double volatile s_factor;
     164    double factor;
     165    LARGE_INTEGER now;
     166    if (s_has_factor)
     167        factor = s_factor;
     168    else
     169    {
     170        QueryPerformanceFrequency(&now);
     171        s_factor = factor = (double)1000000000.0 / now.QuadPart;
     172        s_has_factor = K_TRUE;
     173    }
     174    QueryPerformanceCounter(&now);
     175    return (KU64)(now.QuadPart * factor);
     176}
     177#  endif /* K_OS_WINDOWS */
     178# endif /* DEBUG */
     179
    158180/**
    159181 * Close the specified native handle.
    160182 *
    161183 * @param   native      The native file handle.
    162  * @param   flags       The flags in case they might come in handy later.
    163  */
    164 static void shfile_native_close(intptr_t native, unsigned flags)
     184 * @param   file        The file table entry if available.
     185 */
     186static void shfile_native_close(intptr_t native, shfile *file)
    165187{
    166188# if K_OS == K_OS_WINDOWS
     189#  ifdef DEBUG
     190    KU64 ns = shfile_nano_ts();
     191    BOOL fRc2 = FlushFileBuffers((HANDLE)native);
     192    KU64 ns2 = shfile_nano_ts();
    167193    BOOL fRc = CloseHandle((HANDLE)native);
    168     assert(fRc); (void)fRc;
     194    assert(fRc); K_NOREF(fRc);
     195    ns2 -= ns;
     196    ns = shfile_nano_ts() - ns;
     197    if (ns > 1000000)
     198        TRACE2((NULL, "shfile_native_close: %u ns (%u) %p oflags=%#x %s\n",
     199                ns, ns2, native, file ? file->oflags : 0, file ? file->dbgname : NULL));
     200#  else
     201    BOOL fRc = CloseHandle((HANDLE)native);
     202    assert(fRc); K_NOREF(fRc);
     203#  endif
    169204# else
    170205    int s = errno;
     
    172207    errno = s;
    173208# endif
    174     (void)flags;
     209    K_NOREF(file);
    175210}
    176211
     
    202237            new_tab[i].shflags = 0;
    203238            new_tab[i].native = -1;
     239# ifdef DEBUG
     240            new_tab[i].dbgname = NULL;
     241# endif
    204242        }
    205243
     
    228266 * @param   fdMin       The minimum file descriptor number, pass -1 if any is ok.
    229267 * @param   who         Who we're doing this for (for logging purposes).
    230  */
    231 static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin, const char *who)
     268 * @param   dbgname     The filename, if applicable/available.
     269 */
     270static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned oflags, unsigned shflags, int fdMin,
     271                         const char *who, const char *dbgname)
    232272{
    233273    shmtxtmp tmp;
     
    240280    if (fdMin >= SHFILE_MAX)
    241281    {
    242         TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p\n", fdMin, native));
    243         shfile_native_close(native, oflags);
     282        TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p %s\n", fdMin, native, dbgname));
     283        shfile_native_close(native, NULL);
    244284        errno = EMFILE;
    245285        return -1;
     
    249289    {
    250290        int e = errno;
    251         TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p\n", e, native));
     291        TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p %s\n", e, native, dbgname));
    252292        close((int)native);
    253293        errno = e;
     
    280320        pfdtab->tab[fd].shflags = shflags;
    281321        pfdtab->tab[fd].native = native;
    282         TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x\n", fd, native, oflags, shflags));
     322#ifdef DEBUG
     323        pfdtab->tab[fd].dbgname = dbgname ? sh_strdup(NULL, dbgname) : NULL;
     324#endif
     325        TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x %s\n", fd, native, oflags, shflags, dbgname));
    283326    }
    284327    else
    285         shfile_native_close(native, oflags);
     328        shfile_native_close(native, NULL);
    286329
    287330    shmtx_leave(&pfdtab->mtx, &tmp);
     
    308351 * @param   fdMin       The minimum file descriptor number, pass -1 if any is ok.
    309352 * @param   who         Who we're doing this for (for logging purposes).
    310  */
    311 static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin, const char *who)
     353 * @param   dbgname     The filename, if applicable/available.
     354 */
     355static int shfile_copy_insert_and_close(shfdtab *pfdtab, int *pnative, unsigned oflags, unsigned shflags, int fdMin,
     356                                        const char *who, const char *dbgname)
    312357{
    313358    int fd          = -1;
     
    319364
    320365    if (native_copy != -1)
    321         fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who);
     366        fd = shfile_insert(pfdtab, native_copy, oflags, shflags, fdMin, who, dbgname);
    322367    return fd;
    323368}
     
    743788                                fFlags2 = 0;
    744789
    745                             fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init");
     790                            fd2 = shfile_insert(pfdtab, (intptr_t)h, fFlags, fFlags2, i, "shtab_init", NULL);
    746791                            assert(fd2 == i); (void)fd2;
    747792                            if (fd2 != i)
     
    769814                            else
    770815                                fFlags2 = SHFILE_FLAGS_FILE;
    771                             fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init");
     816                            fd2 = shfile_insert(pfdtab, (intptr_t)hFile, fFlags, fFlags2, i, "shtab_init", NULL);
    772817                            assert(fd2 == i); (void)fd2;
    773818                            if (fd2 != i)
     
    810855                            if (native == -1)
    811856                                native = fd;
    812                             fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init");
     857                            fd2 = shfile_insert(pfdtab, native, oflags, fFlags2, fd, "shtab_init", NULL);
    813858                            assert(fd2 == fd); (void)fd2;
    814859                            if (fd2 != fd)
     
    859904# endif
    860905                        *dst = *src;
     906# ifdef DEBUG
     907                        if (src->dbgname)
     908                            dst->dbgname = sh_strdup(NULL, src->dbgname);
     909# endif
    861910# if K_OS == K_OS_WINDOWS
    862911                        if (DuplicateHandle(GetCurrentProcess(),
     
    11931242                    && !(file->shflags & SHFILE_FLAGS_TRACE))
    11941243                {
    1195                     shfile_native_close(file->native, file->oflags);
     1244                    shfile_native_close(file->native, file);
    11961245
    11971246                    file->fd = -1;
     
    11991248                    file->shflags = 0;
    12001249                    file->native = -1;
     1250# ifdef DEBUG
     1251                    sh_free(NULL, file->dbgname);
     1252                    file->dbgname = NULL;
     1253# endif
    12011254                }
    12021255            }
     
    13151368    if (!fd)
    13161369    {
     1370#  ifdef DEBUG
     1371        KU64 ns = shfile_nano_ts();
     1372#  endif
    13171373        SetLastError(0);
    13181374        hFile = CreateFileA(absname,
     
    13231379                            dwFlagsAndAttributes,
    13241380                            NULL /* hTemplateFile */);
     1381#  ifdef DEBUG
     1382        ns = shfile_nano_ts() - ns;
     1383        if (ns > 1000000)
     1384            TRACE2((NULL, "shfile_open: %s ns hFile=%p (%d) %s\n", ns, hFile, GetLastError(), absname));
     1385#  endif
    13251386        if (hFile != INVALID_HANDLE_VALUE)
    1326             fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open");
     1387            fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, 0, -1, "shfile_open", absname);
    13271388        else
    13281389            fd = shfile_dos2errno(GetLastError());
     
    13351396        fd = open(absname, flags, mode);
    13361397        if (fd != -1)
    1337             fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open");
     1398            fd = shfile_copy_insert_and_close(pfdtab, &fd, flags, 0, -1, "shfile_open", absname);
    13381399    }
    13391400
     
    13641425    if (CreatePipe(&hRead, &hWrite, &SecurityAttributes, 4096))
    13651426    {
    1366         fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
     1427        fds[0] = shfile_insert(pfdtab, (intptr_t)hRead, O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-rd");
    13671428        if (fds[0] != -1)
    13681429        {
    1369             fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
     1430            fds[1] = shfile_insert(pfdtab, (intptr_t)hWrite, O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-wr");
    13701431            if (fds[1] != -1)
    13711432                rc = 0;
     
    13781439    if (!pipe(native_fds))
    13791440    {
    1380         fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
     1441        fds[0] = shfile_copy_insert_and_close(pfdtab, &native_fds[0], O_RDONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-rd");
    13811442        if (fds[0] != -1)
    13821443        {
    1383             fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe");
     1444            fds[1] = shfile_copy_insert_and_close(pfdtab, &native_fds[1], O_WRONLY, SHFILE_FLAGS_PIPE, -1, "shfile_pipe", "pipe-wr");
    13841445            if (fds[1] != -1)
    13851446                rc = 0;
     
    14591520        {
    14601521            if (pfdtab->tab[fdto].fd != -1)
    1461                 shfile_native_close(pfdtab->tab[fdto].native, pfdtab->tab[fdto].oflags);
     1522                shfile_native_close(pfdtab->tab[fdto].native, &pfdtab->tab[fdto]);
    14621523
    14631524            /* setup the target. */
     
    14661527            pfdtab->tab[fdto].shflags = file->shflags;
    14671528            pfdtab->tab[fdto].native  = file->native;
     1529# ifdef DEBUG
     1530            pfdtab->tab[fdto].dbgname = file->dbgname;
     1531# endif
    14681532
    14691533            /* close the source. */
     
    14721536            file->shflags   = 0;
    14731537            file->native    = -1;
     1538# ifdef DEBUG
     1539            file->dbgname   = NULL;
     1540# endif
    14741541
    14751542            rc = fdto;
     
    15291596            pfdtab->tab[fdto].shflags = file->shflags;
    15301597            pfdtab->tab[fdto].native  = file->native;
     1598# ifdef DEBUG
     1599            pfdtab->tab[fdto].dbgname = file->dbgname;
     1600# endif
    15311601
    15321602            /* close the source. */
     
    15351605            file->shflags   = 0;
    15361606            file->native    = -1;
     1607# ifdef DEBUG
     1608            file->dbgname   = NULL;
     1609# endif
    15371610        }
    15381611        else
     
    15671640    if (file)
    15681641    {
    1569         shfile_native_close(file->native, file->oflags);
     1642        shfile_native_close(file->native, file);
    15701643
    15711644        file->fd = -1;
     
    15731646        file->shflags = 0;
    15741647        file->native = -1;
     1648# ifdef DEBUG
     1649        sh_free(NULL, file->dbgname);
     1650        file->dbgname = NULL;
     1651# endif
    15751652
    15761653        shfile_put(pfdtab, file, &tmp);
     
    17571834                                    FALSE /* bInheritHandle */,
    17581835                                    DUPLICATE_SAME_ACCESS))
    1759                     rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg, "shfile_fcntl");
     1836                    rc = shfile_insert(pfdtab, (intptr_t)hNew, file->oflags, file->shflags, arg,
     1837                                       "shfile_fcntl", SHFILE_DBGNAME(file->dbgname));
    17601838                else
    17611839                    rc = shfile_dos2errno(GetLastError());
     
    17631841                int nativeNew = fcntl(file->native, F_DUPFD, SHFILE_UNIX_MIN_FD);
    17641842                if (nativeNew != -1)
    1765                     rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg, "shfile_fcntl");
     1843                    rc = shfile_insert(pfdtab, nativeNew, file->oflags, file->shflags, arg,
     1844                                       "shfile_fcntl", SHFILE_DBGNAME(file->dbgname));
    17661845                else
    17671846                    rc = -1;
  • trunk/src/kash/shfile.h

    r3458 r3465  
    106106    unsigned            shflags;        /**< The shell file descriptor flags. */
    107107    intptr_t            native;         /**< The native file descriptor number. */
     108#ifdef DEBUG
     109    char               *dbgname;        /**< The name of the file, if applicable, debug builds only. */
     110#  define SHFILE_DBGNAME(a) a
     111# else
     112#  define SHFILE_DBGNAME(a) NULL
     113#endif
    108114} shfile;
    109115
Note: See TracChangeset for help on using the changeset viewer.

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