Ignore:
Timestamp:
Mar 8, 2001, 8:28:45 PM (24 years ago)
Author:
umoeller
Message:

Misc. changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/procstat.c

    r14 r44  
    114114VOID prc16FreeInfo(PQPROCSTAT16 pInfo)
    115115{
    116     free(pInfo);
     116    if (pInfo)
     117        free(pInfo);
    117118}
    118119
     
    133134    PQPROCESS16 pProcess,
    134135                pReturn = NULL;
    135     for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
    136           pProcess->ulType != 3;
    137           pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
    138                                       pProcess->usThreads * sizeof(QTHREAD16))
    139         )
    140     {
    141         CHAR    szModuleName[CCHMAXPATH];
    142         if (DosQueryModuleName(pProcess->usHModule,
    143                                sizeof(szModuleName),
    144                                szModuleName)
    145                 == NO_ERROR)
    146         {
    147             // the module name is fully qualified, so find the
    148             // file name (after the last backslash)
    149             PSZ pLastBackslash = strrchr(szModuleName, '\\');
    150             if (pLastBackslash)
    151                 // found:
    152                 if (stricmp(pLastBackslash + 1, pcszName) == 0)
    153                 {
    154                     // matches:
    155                     pReturn = pProcess;
    156                     break;
    157                 }
     136    if (pInfo)
     137    {
     138        for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
     139              pProcess->ulType != 3;
     140              pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
     141                                          pProcess->usThreads * sizeof(QTHREAD16))
     142            )
     143        {
     144            CHAR    szModuleName[CCHMAXPATH];
     145            if (DosQueryModuleName(pProcess->usHModule,
     146                                   sizeof(szModuleName),
     147                                   szModuleName)
     148                    == NO_ERROR)
     149            {
     150                // the module name is fully qualified, so find the
     151                // file name (after the last backslash)
     152                PSZ pLastBackslash = strrchr(szModuleName, '\\');
     153                if (pLastBackslash)
     154                    // found:
     155                    if (stricmp(pLastBackslash + 1, pcszName) == 0)
     156                    {
     157                        // matches:
     158                        pReturn = pProcess;
     159                        break;
     160                    }
     161            }
    158162        }
    159163    }
     
    178182    PQPROCESS16 pProcess,
    179183                pReturn = NULL;
    180     for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
    181           pProcess->ulType != 3;
    182           pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
    183                                       pProcess->usThreads * sizeof(QTHREAD16))
    184         )
    185     {
    186         if (pProcess->usPID == ulPID)
    187         {
    188             pReturn = pProcess;
    189             break;
     184    if (pInfo)
     185    {
     186        for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
     187              pProcess->ulType != 3;
     188              pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
     189                                          pProcess->usThreads * sizeof(QTHREAD16))
     190            )
     191        {
     192            if (pProcess->usPID == ulPID)
     193            {
     194                pReturn = pProcess;
     195                break;
     196            }
    190197        }
    191198    }
     
    207214VOID prcReport16(PQPROCESS16 pProcess, PPRCPROCESS pprcp)
    208215{
    209     // USHORT usPriority = 0;
    210     PQTHREAD16 pThread;
    211     int i;
    212 
    213     DosQueryModuleName(pProcess->usHModule,
    214                        sizeof(pprcp->szModuleName),
    215                        pprcp->szModuleName);
    216     // DosGetPrty(PRTYS_PROCESS, &(pprcp->usPriority), pProcess->usPID);
    217 
    218     // sum up CPU time for process
    219     for (pprcp->ulCPU = 0,
    220                 i = 0,
    221                 pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
    222          i < pProcess->usThreads;
    223          i++, pThread++ )
    224     {
    225         pprcp->ulCPU += (pThread->ulSysTime + pThread->ulUserTime);
    226     }
    227 
    228     pprcp->usPID            = pProcess->usPID;
    229     pprcp->usParentPID      = pProcess->usParentPID;
    230     pprcp->usThreads        = pProcess->usThreads;
    231     pprcp->ulSID            = pProcess->ulSID;
    232     pprcp->ulSessionType    = pProcess->ulSessionType;
    233     pprcp->ulStatus         = pProcess->ulStatus;
     216    if (pProcess)
     217    {
     218        PQTHREAD16 pThread;
     219        int i;
     220
     221        DosQueryModuleName(pProcess->usHModule,
     222                           sizeof(pprcp->szModuleName),
     223                           pprcp->szModuleName);
     224        // DosGetPrty(PRTYS_PROCESS, &(pprcp->usPriority), pProcess->usPID);
     225
     226        // sum up CPU time for process
     227        for (pprcp->ulCPU = 0,
     228                    i = 0,
     229                    pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
     230             i < pProcess->usThreads;
     231             i++, pThread++ )
     232        {
     233            pprcp->ulCPU += (pThread->ulSysTime + pThread->ulUserTime);
     234        }
     235
     236        pprcp->usPID            = pProcess->usPID;
     237        pprcp->usParentPID      = pProcess->usParentPID;
     238        pprcp->usThreads        = pProcess->usThreads;
     239        pprcp->ulSID            = pProcess->ulSID;
     240        pprcp->ulSessionType    = pProcess->ulSessionType;
     241        pprcp->ulStatus         = pProcess->ulStatus;
     242    }
    234243}
    235244
     
    243252 */
    244253
    245 BOOL prc16QueryProcessInfo(USHORT usPID,  // in: PID to query
    246                            PPRCPROCESS pprcp) // out: process info
     254BOOL prc16QueryProcessInfo(PQPROCSTAT16 pps,    // in: from prc16GetInfo
     255                           USHORT usPID,        // in: PID to query
     256                           PPRCPROCESS pprcp)   // out: process info
    247257{
    248258    BOOL rc = FALSE;
    249     PQPROCESS16 pProcess;
    250     PQPROCSTAT16 pps = (PQPROCSTAT16)malloc(0x8000);
    251     DosQProcStatus(pps, 0x8000);
    252 
    253     for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
    254           pProcess->ulType != 3;
    255           pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
    256                                       pProcess->usThreads * sizeof(QTHREAD16))
    257         )
    258     {
    259         if (pProcess->usPID == usPID)
    260         {
    261             prcReport16(pProcess, pprcp);
    262             rc = TRUE;
    263             break;
    264         }
    265     }
    266 
    267     free(pps);
     259    if (pps)
     260    {
     261        PQPROCESS16 pProcess;
     262
     263        for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
     264              pProcess->ulType != 3;
     265              pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
     266                                          pProcess->usThreads * sizeof(QTHREAD16))
     267            )
     268        {
     269            if (pProcess->usPID == usPID)
     270            {
     271                prcReport16(pProcess, pprcp);
     272                rc = TRUE;
     273                break;
     274            }
     275        }
     276    }
     277
    268278    return (rc);
    269279}
     
    315325 *      in the given process. If pid == 0, the
    316326 *      total thread count for the system is returned.
    317  */
    318 
    319 ULONG prc16QueryThreadCount(USHORT usPID)
     327 *
     328 *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
     329 */
     330
     331ULONG prc16QueryThreadCount(PQPROCSTAT16 pps, // in: from prc16GetInfo
     332                            USHORT usPID)
    320333{
    321334    ULONG       ulrc = 0;
    322     PQPROCSTAT16  pps = (PQPROCSTAT16)malloc(0x8000);
    323     DosQProcStatus(pps, 0x8000);
    324 
    325     if (usPID)
    326     {
    327         // process query:
     335
     336    if (pps)
     337    {
     338        if (usPID)
     339        {
     340            // process query:
     341            PQPROCESS16 pProcess;
     342            for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
     343                  pProcess->ulType != 3;
     344                  pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
     345                                              pProcess->usThreads * sizeof(QTHREAD16))
     346                )
     347            {
     348                if (pProcess->usPID == usPID)
     349                {
     350                    ulrc = pProcess->usThreads;
     351                    break;
     352                }
     353            }
     354        }
     355        else
     356        {
     357            // global query:
     358            PQGLOBAL16   pg;
     359            pg = (PQGLOBAL16)PTR(pps->ulGlobal, 0);
     360            ulrc = pg->ulThreads;
     361        }
     362    }
     363
     364    return (ulrc);
     365}
     366
     367/*
     368 *@@ prc16QueryThreadInfo:
     369 *      this searches for a given thread in a given process
     370 *      and fills a given PRCTHREAD structure with lots of
     371 *      information about that thread.
     372 *
     373 *      Returns FALSE upon errors.
     374 *
     375 *      Note: This function loops thru all processes which
     376 *      are currently running and is therefore not terribly
     377 *      fast. Use economically.
     378 *
     379 *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
     380 */
     381
     382BOOL prc16QueryThreadInfo(PQPROCSTAT16 pps, // in: from prc16GetInfo
     383                          USHORT usPID,
     384                          USHORT usTID,
     385                          PPRCTHREAD pprct)
     386{
     387    BOOL        brc = FALSE;
     388    if (pps)
     389    {
    328390        PQPROCESS16 pProcess;
     391
     392        // find process:
    329393        for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
    330394              pProcess->ulType != 3;
     
    335399            if (pProcess->usPID == usPID)
    336400            {
    337                 ulrc = pProcess->usThreads;
    338                 break;
     401                PQTHREAD16 pThread;
     402                int i;
     403                // process found: find thread
     404                for ( i = 0, pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
     405                      i < pProcess->usThreads;
     406                      i++, pThread++ )
     407                {
     408                    if (pThread->usTID == usTID)
     409                    {
     410                        // thread found:
     411                        pprct->usTID          = pThread->usTID;
     412                        pprct->usThreadSlotID = pThread->usThreadSlotID;
     413                        pprct->ulBlockID      = pThread->ulBlockID;
     414                        pprct->ulPriority     = pThread->ulPriority;
     415                        pprct->ulSysTime      = pThread->ulSysTime;
     416                        pprct->ulUserTime     = pThread->ulUserTime;
     417                        pprct->ucStatus       = pThread->ucStatus;
     418
     419                        brc = TRUE;
     420
     421                        break; // thread-for loop
     422                    }
     423                } // end for thread
     424                break; // process-for loop
    339425            }
    340         }
    341     }
    342     else
    343     {
    344         // global query:
    345         PQGLOBAL16   pg;
    346         pg = (PQGLOBAL16)PTR(pps->ulGlobal, 0);
    347         ulrc = pg->ulThreads;
    348     }
    349 
    350     free(pps);
    351     return (ulrc);
    352 }
    353 
    354 /*
    355  *@@ prc16QueryThreadInfo:
    356  *      this searches for a given thread in a given process
    357  *      and fills a given PRCTHREAD structure with lots of
    358  *      information about that thread.
    359  *
    360  *      Returns FALSE upon errors.
     426        } // end for process
     427    }
     428
     429    return (brc);
     430}
     431
     432/*
     433 *@@ prcQueryPriority:
     434 *      shortcut to prc16QueryThreadInfo if you want the priority only.
     435 *
     436 *      Returns -1 upon errors.
    361437 *
    362438 *      Note: This function loops thru all processes which
    363439 *      are currently running and is therefore not terribly
    364440 *      fast. Use economically.
    365  */
    366 
    367 BOOL prc16QueryThreadInfo(USHORT usPID, USHORT usTID, PPRCTHREAD pprct)
    368 {
    369     BOOL        brc = FALSE;
    370     PQPROCSTAT16  pps = (PQPROCSTAT16)malloc(0x8000);
    371     PQPROCESS16 pProcess;
    372     DosQProcStatus(pps, 0x8000);
    373 
    374     // find process:
    375     for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
    376           pProcess->ulType != 3;
    377           pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
    378                                       pProcess->usThreads * sizeof(QTHREAD16))
    379         )
    380     {
    381         if (pProcess->usPID == usPID)
    382         {
    383             PQTHREAD16 pThread;
    384             int i;
    385             // process found: find thread
    386             for ( i = 0, pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
    387                   i < pProcess->usThreads;
    388                   i++, pThread++ )
    389             {
    390                 if (pThread->usTID == usTID)
    391                 {
    392                     // thread found:
    393                     pprct->usTID          = pThread->usTID;
    394                     pprct->usThreadSlotID = pThread->usThreadSlotID;
    395                     pprct->ulBlockID      = pThread->ulBlockID;
    396                     pprct->ulPriority     = pThread->ulPriority;
    397                     pprct->ulSysTime      = pThread->ulSysTime;
    398                     pprct->ulUserTime     = pThread->ulUserTime;
    399                     pprct->ucStatus       = pThread->ucStatus;
    400 
    401                     brc = TRUE;
    402 
    403                     break; // thread-for loop
    404                 }
    405             } // end for thread
    406             break; // process-for loop
    407         }
    408     } // end for process
    409 
    410     free(pps);
    411     return (brc);
    412 }
    413 
    414 /*
    415  *@@ prcQueryPriority:
    416  *      shortcut to prc16QueryThreadInfo if you want the priority only.
    417  *
    418  *      Returns -1 upon errors.
    419  *
    420  *      Note: This function loops thru all processes which
    421  *      are currently running and is therefore not terribly
    422  *      fast. Use economically.
    423  */
    424 
    425 ULONG prc16QueryThreadPriority(USHORT usPID,
     441 *
     442 *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
     443 */
     444
     445ULONG prc16QueryThreadPriority(PQPROCSTAT16 pps, // in: from prc16GetInfo
     446                               USHORT usPID,
    426447                               USHORT usTID)
    427448{
    428449    PRCTHREAD prct;
    429450    ULONG ulrc = -1;
    430     if (prc16QueryThreadInfo(usPID, usTID, &prct))
     451    if (prc16QueryThreadInfo(pps, usPID, usTID, &prct))
    431452        ulrc = prct.ulPriority;
    432453    return (ulrc);
Note: See TracChangeset for help on using the changeset viewer.