home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / profile1.zip / PROFILE.C next >
C/C++ Source or Header  |  1991-01-24  |  3KB  |  119 lines

  1. /* profile.c */
  2.  
  3. #define INCL_DOSINFOSEG
  4. #define INCL_DOSMEMMGR
  5. #define INCL_DOSPROCESS
  6. #define INCL_DOSSIGNALS
  7.  
  8. #include <os2.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "profile.h"
  14.  
  15. SEL        selMessage;
  16. volatile PROMESSAGE  *pProMessage;
  17. ULONG     aulHits[PRO_MAX];
  18. BOOL fStarted;
  19. CHAR szWork[80];
  20. time_t timeStart, timeEnd;
  21.  
  22. USHORT fnusSetPriority(VOID)
  23. {
  24.   SEL selGlobalSeg, selLocalSeg;
  25.   LINFOSEG *plis;
  26.   USHORT usRetCode;
  27.  
  28.   DosGetInfoSeg(&selGlobalSeg, &selLocalSeg);
  29.   plis = MAKEPLINFOSEG(selLocalSeg);
  30.   usRetCode = DosSetPrty(PRTYS_THREAD, PRTYC_TIMECRITICAL, -15,
  31.                          plis->tidCurrent);
  32.   if(usRetCode) {
  33.     printf("Profiler: change priority failed x:%x",usRetCode);
  34.     return TRUE;
  35.   }
  36.  
  37.   return FALSE;
  38. }
  39.  
  40. VOID fnvCloseUp(VOID)
  41. {
  42.   DosFreeSeg(selMessage);
  43. }
  44.  
  45. VOID fnvResults(VOID)
  46. {
  47.   USHORT usRow, usCol, usIndex;
  48.   ULONG ulTotal;
  49.  
  50.   ulTotal = 0;
  51.   for(usIndex = 0; usIndex < PRO_MAX; usIndex++)
  52.     ulTotal += aulHits[usIndex];
  53.   time(&timeEnd);
  54.   for(usIndex = 0; usIndex < PRO_MAX; usIndex++) {
  55.     //usRow = usIndex / 4;
  56.     //usCol = usIndex % 4;
  57.     //if(!usCol)
  58.       printf("\n");
  59.     printf("%03u:%5.4lf    ", usIndex,aulHits[usIndex]/(double)ulTotal);
  60.   }
  61.   printf("\nTotal time %lu\n",timeEnd-timeStart);
  62. }
  63.  
  64. VOID PASCAL FAR fnvInterrupt(USHORT usSigArg, USHORT usSigNum)
  65. {
  66.   _strtime(szWork);
  67.   printf("CTRL-C at %s\n",szWork);
  68.   if(fStarted)
  69.     fnvResults();
  70.   exit (0);
  71. }
  72.  
  73. int cdecl main(void)
  74. {
  75.   ULONG  ulPrevSigH;
  76.   USHORT usPrevSigA;
  77.  
  78.   fStarted = FALSE;
  79.  
  80.   if(fnusSetPriority()) {
  81.     fnvCloseUp();
  82.     return TRUE;
  83.   }
  84.  
  85.   DosAllocShrSeg(sizeof(PROMESSAGE), PRO_MEM, &selMessage);
  86.   pProMessage = MAKEP(selMessage,0);
  87.   memset((VOID * ) pProMessage, 0, sizeof(PROMESSAGE));
  88.   pProMessage->sCode = PRO_WAITING;
  89.   DosSetSigHandler(fnvInterrupt,
  90.                    (PFNSIGHANDLER FAR *) &ulPrevSigH,
  91.                    &usPrevSigA,
  92.                    SIGA_ACCEPT,
  93.                    SIG_CTRLC);
  94.  
  95.   _strtime(szWork);
  96.   printf("Profiler started at %s\n",szWork);
  97.  
  98.   while(pProMessage->sCode != PRO_EXIT) {
  99.     if(!fStarted && pProMessage->sCode == PRO_START) {
  100.       fStarted = TRUE;
  101.       _strtime(szWork);
  102.       time(&timeStart);
  103.       printf("PRO_START at %s\n",szWork);
  104.       continue;
  105.     }
  106.     if(fStarted)
  107.       if(pProMessage->sCode > 0 && pProMessage->sCode < PRO_MAX)
  108.         aulHits[pProMessage->sCode]++;
  109.     DosSleep(10L);
  110.   }
  111.   _strtime(szWork);
  112.   printf("PRO_EXIT at %s\n",szWork);
  113.  
  114.   if(fStarted)
  115.     fnvResults();
  116.  
  117.   return FALSE;
  118. }
  119.