Annotation of parser3/src/types/pa_vstatus.C, revision 1.16

1.1       paf         1: /** @file
                      2:        Parser: @b status class impl.
                      3: 
1.15      paf         4:        Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.6       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.16    ! paf         6: 
        !             7:        Win32 rusage author: Victor Fedoseev <vvf_ru@mail.ru>
1.8       paf         8: */
1.1       paf         9: 
1.16    ! paf        10: static const char* IDENT_VSTATUS_C="$Date: 2003/01/21 15:51:21 $";
1.1       paf        11: 
                     12: #include "pa_vstatus.h"
                     13: #include "pa_cache_managers.h"
1.2       paf        14: #include "pa_vhash.h"
                     15: #include "pa_vdouble.h"
                     16: 
                     17: #ifdef HAVE_SYS_RESOURCE_H
                     18: // rusage
                     19: #include <sys/resource.h>
                     20: #endif
1.1       paf        21: 
1.16    ! paf        22: #ifdef WIN32
        !            23: #include <windows.h>
        !            24: #include "psapi.h"
        !            25: 
        !            26: // should be in windows.h, but were't
        !            27: typedef struct _IO_COUNTERS_ {
        !            28:     ULONGLONG  ReadOperationCount;
        !            29:     ULONGLONG  WriteOperationCount;
        !            30:     ULONGLONG  OtherOperationCount;
        !            31:     ULONGLONG ReadTransferCount;
        !            32:     ULONGLONG WriteTransferCount;
        !            33:     ULONGLONG OtherTransferCount;
        !            34: } IO_COUNTERS_;
        !            35: typedef IO_COUNTERS_ *PIO_COUNTERS_;
        !            36: 
        !            37: typedef unsigned __int64 ui64;
        !            38: // kernel32.dll (NT/2K/XP)
        !            39: typedef BOOL (WINAPI *PGETPROCESSTIMES)(HANDLE,LPFILETIME,LPFILETIME,LPFILETIME,LPFILETIME);
        !            40: //typedef BOOL (WINAPI *PGETPROCESSHEAPS)(DWORD,PHANDLE);
        !            41: typedef BOOL (WINAPI *GETPROCESSIOCOUNTERS)(HANDLE,PIO_COUNTERS_);
        !            42: // psapi.dll (2K/XP)
        !            43: typedef BOOL (WINAPI *PGETPROCESSMEMORYINFO)(HANDLE,PPROCESS_MEMORY_COUNTERS,DWORD);
        !            44: 
        !            45: // from CRT time.c
        !            46: /*
        !            47:  * Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
        !            48:  */
        !            49: #define EPOCH_BIAS  116444736000000000i64
        !            50: 
        !            51: /*
        !            52:  * Union to facilitate converting from FILETIME to unsigned __int64
        !            53:  */
        !            54: typedef union {
        !            55:         unsigned __int64 ft_scalar;
        !            56:         FILETIME ft_struct;
        !            57:         } FT;
        !            58: 
        !            59: #endif
        !            60: 
1.14      paf        61: Value *VStatus::get_element(const String& aname, Value& /*aself*/, bool /*looking_up*/) {
1.1       paf        62:        // getstatus
                     63:        if(Cache_manager *manager=static_cast<Cache_manager *>(cache_managers->get(aname)))
                     64:                return &manager->get_status(pool(), &aname);
                     65: 
                     66:        // rusage
                     67:        if(aname=="rusage") {
1.2       paf        68:                VHash& rusage=*NEW VHash(pool());
1.16    ! paf        69:                Hash& hash=rusage.hash(0);
        !            70: 
        !            71: #ifdef WIN32
        !            72:                double d1;
        !            73:                HANDLE hProc = GetCurrentProcess();
        !            74: 
        !            75:                HMODULE hMod = LoadLibrary("kernel32.dll");
        !            76:                if(hMod){
        !            77:                        // NT/2K/XP
        !            78:                        PGETPROCESSTIMES pGetProcessTimes = (PGETPROCESSTIMES)GetProcAddress(hMod, "GetProcessTimes");
        !            79:                        if(pGetProcessTimes){
        !            80:                                FILETIME CreationTime, ExitTime;
        !            81:                                FT KernelTime, UserTime;
        !            82:                                if(pGetProcessTimes(hProc, &CreationTime, &ExitTime, &KernelTime.ft_struct, &UserTime.ft_struct)){
        !            83:                                        // dwHighDateTime & dwLowDateTime - 1/10 000 000 seconds in 64 bit
        !            84:                                        /* the amount of time that the process has executed in user mode */
        !            85:                                        d1 = double((LONGLONG)UserTime.ft_scalar)/10000000.0;
        !            86:                                        hash.put(*NEW String(pool(), "utime"),  NEW VDouble(pool(), d1));
        !            87: //                                     hash.put(*NEW String(pool(), "UserTime"),  NEW VDouble(pool(),  d1));
        !            88:                                        
        !            89:                                        /* the amount of time that the process has executed in kernel mode */
        !            90:                                        d1 = double((LONGLONG)KernelTime.ft_scalar)/10000000.0;
        !            91:                                        hash.put(*NEW String(pool(), "stime"),  NEW VDouble(pool(), d1));
        !            92: //                                     hash.put(*NEW String(pool(), "KernelTime"),  NEW VDouble(pool(),  d1));
        !            93:                                }
        !            94:                        }
        !            95: 
        !            96:                        // NT/2K/XP
        !            97:                        GETPROCESSIOCOUNTERS pGetProcessIoCounters = (GETPROCESSIOCOUNTERS)GetProcAddress(hMod, "GetProcessIoCounters");
        !            98:                        if(pGetProcessIoCounters){
        !            99:                                IO_COUNTERS_ ioc;
        !           100:                                if(pGetProcessIoCounters(hProc, &ioc)){
        !           101:                                        /* Specifies the number of I/O operations performed, other than read and write operations */
        !           102:                                        hash.put(*NEW String(pool(),      "OtherOperationCount"),
        !           103:                                                NEW VDouble(pool(), double((LONGLONG)ioc.OtherOperationCount)));
        !           104:                                        /* Specifies the number of bytes transferred during operations other than read and write operations */
        !           105:                                        hash.put(*NEW String(pool(),      "OtherTransferCount"),
        !           106:                                                NEW VDouble(pool(), double((LONGLONG)ioc.OtherTransferCount)/1024.0));
        !           107:                                        /* Specifies the number of read operations performed */
        !           108:                                        hash.put(*NEW String(pool(),      "ReadOperationCount"),
        !           109:                                                NEW VDouble(pool(), double((LONGLONG)ioc.ReadOperationCount)));
        !           110:                                        /* Specifies the number of bytes read */
        !           111:                                        hash.put(*NEW String(pool(),      "ReadTransferCount"),
        !           112:                                                NEW VDouble(pool(), double((LONGLONG)ioc.ReadTransferCount)/1024.0));
        !           113:                                        /* Specifies the number of write operations performed */
        !           114:                                        hash.put(*NEW String(pool(),      "WriteOperationCount"),
        !           115:                                                NEW VDouble(pool(), double((LONGLONG)ioc.WriteOperationCount)));
        !           116:                                        /* Specifies the number of bytes written */
        !           117:                                        hash.put(*NEW String(pool(),      "WriteTransferCount"),
        !           118:                                                NEW VDouble(pool(), double((LONGLONG)ioc.WriteTransferCount)/1024.0));
        !           119:                                }
        !           120:                        }
        !           121:                        FreeLibrary(hMod);
        !           122:                        /*
        !           123:                        PGETPROCESSHEAPS pGetProcessHeaps = (PGETPROCESSHEAPS)GetProcAddress(hMod, "GetProcessHeaps");
        !           124:                        if(pGetProcessHeaps){
        !           125:                        }
        !           126:                        */
        !           127:                }
        !           128: 
        !           129:                // 2K/XP
        !           130:                hMod = LoadLibrary("psapi.dll");
        !           131:                if(hMod){
        !           132:                        PGETPROCESSMEMORYINFO pGetProcessMemoryInfo = (PGETPROCESSMEMORYINFO)GetProcAddress(hMod, "GetProcessMemoryInfo");
        !           133:                        if(pGetProcessMemoryInfo){
        !           134:                                PROCESS_MEMORY_COUNTERS pmc;
        !           135:                                pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
        !           136:                                if(pGetProcessMemoryInfo(hProc, &pmc, sizeof(PROCESS_MEMORY_COUNTERS))){
        !           137:                                        /* The peak working set size */
        !           138:                                        d1 = double(pmc.PeakWorkingSetSize)/1024.0;
        !           139:                                        hash.put(*NEW String(pool(), "maxrss"), NEW VDouble(pool(), d1));
        !           140: //                                     hash.put(*NEW String(pool(), "PeakWorkingSetSize"),  NEW VDouble(pool(),  d1));
        !           141:                                        /* The peak nonpaged pool usage */
        !           142:                                        d1 = double(pmc.QuotaPeakNonPagedPoolUsage)/1024.0;
        !           143: //                                     hash.put(*NEW String(pool(), "ixrss"), NEW VDouble(pool(), d1));
        !           144:                                        hash.put(*NEW String(pool(), "QuotaPeakNonPagedPoolUsage"),  NEW VDouble(pool(),  d1));
        !           145:                                        /* The peak paged pool usage */
        !           146:                                        d1 = double(pmc.QuotaPeakPagedPoolUsage)/1024.0;
        !           147: //                                     hash.put(*NEW String(pool(), "idrss"),  NEW VDouble(pool(),  d1));
        !           148:                                        hash.put(*NEW String(pool(), "QuotaPeakPagedPoolUsage"),  NEW VDouble(pool(),  d1));
        !           149:                                        /* The peak pagefile usage */
        !           150:                                        d1 = double(pmc.PeakPagefileUsage)/1024.0;
        !           151: //                                     hash.put(*NEW String(pool(), "isrss"),  NEW VDouble(pool(),  d1));
        !           152:                                        hash.put(*NEW String(pool(), "PeakPagefileUsage"),  NEW VDouble(pool(),  d1));
        !           153:                                }
        !           154:                        }
        !           155:                        FreeLibrary(hMod);
        !           156:                }
        !           157: 
        !           158:                // all windows.
        !           159:                FT ft;
        !           160:         GetSystemTimeAsFileTime( &(ft.ft_struct) );
        !           161:                ft.ft_scalar -= EPOCH_BIAS;
        !           162:                ui64 tv_sec = ft.ft_scalar/10000000i64;
        !           163:                ui64 tv_usec = (ft.ft_scalar-tv_sec*10000000i64)/10i64;
        !           164:                hash.put(*NEW String(pool(), "tv_sec"), NEW VDouble(pool(), double((LONGLONG)tv_sec)));
        !           165:                hash.put(*NEW String(pool(), "tv_usec"), NEW VDouble(pool(), double((LONGLONG)tv_usec)));
        !           166: 
        !           167: #else
        !           168: 
        !           169: #ifdef HAVE_GETRUSAGE
1.1       paf       170:            struct rusage u;
1.13      paf       171:            if(getrusage(RUSAGE_SELF,&u)<0)
1.7       paf       172:                        throw Exception(0,
1.1       paf       173:                                &aname,
1.13      paf       174:                                "getrusage failed (#%d)", errno);
                    175: 
1.2       paf       176:                hash.put(*NEW String(pool(), "utime"), NEW VDouble(pool(), 
1.4       paf       177:                        u.ru_utime.tv_sec+u.ru_utime.tv_usec/1000000.0));
1.2       paf       178:                hash.put(*NEW String(pool(), "stime"), NEW VDouble(pool(), 
1.4       paf       179:                        u.ru_stime.tv_sec+u.ru_stime.tv_usec/1000000.0));
1.2       paf       180:                hash.put(*NEW String(pool(), "maxrss"), NEW VDouble(pool(), u.ru_maxrss));
                    181:                hash.put(*NEW String(pool(), "ixrss"), NEW VDouble(pool(), u.ru_ixrss));
                    182:                hash.put(*NEW String(pool(), "idrss"), NEW VDouble(pool(), u.ru_idrss));
                    183:                hash.put(*NEW String(pool(), "isrss"), NEW VDouble(pool(), u.ru_isrss));
1.16    ! paf       184: #endif
1.13      paf       185: 
                    186: #ifdef HAVE_GETTIMEOFDAY
1.16    ! paf       187:                struct timeval tp;
        !           188:                if(gettimeofday(&tp, NULL)<0)
        !           189:                        throw Exception(0,
        !           190:                                &aname,
        !           191:                                "gettimeofday failed (#%d)", errno);
        !           192: 
1.13      paf       193:                hash.put(*NEW String(pool(), "tv_sec"), NEW VDouble(pool(), tp.tv_sec));
                    194:                hash.put(*NEW String(pool(), "tv_usec"), NEW VDouble(pool(), tp.tv_usec));
                    195: #endif
1.1       paf       196: 
1.16    ! paf       197: #endif
        !           198: 
1.1       paf       199:                return &rusage;
                    200:        }
                    201: 
                    202:        return 0;
                    203: }

E-mail: