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

1.1       paf         1: /** @file
                      2:        Parser: @b status class impl.
                      3: 
1.42    ! moko        4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
        !             5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <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: 
                     10: #include "pa_vstatus.h"
                     11: #include "pa_cache_managers.h"
1.2       paf        12: #include "pa_vhash.h"
                     13: #include "pa_vdouble.h"
1.23      paf        14: #include "pa_threads.h"
1.2       paf        15: 
1.42    ! moko       16: volatile const char * IDENT_PA_VSTATUS_C="$Id: pa_vstatus.C,v 1.41 2023/08/15 19:44:20 moko Exp $" IDENT_PA_VSTATUS_H;
1.28      moko       17: 
1.30      moko       18: #ifdef _MSC_VER
1.16      paf        19: #include <windows.h>
                     20: #include "psapi.h"
                     21: 
                     22: // should be in windows.h, but were't
                     23: typedef struct _IO_COUNTERS_ {
                     24:     ULONGLONG  ReadOperationCount;
                     25:     ULONGLONG  WriteOperationCount;
                     26:     ULONGLONG  OtherOperationCount;
                     27:     ULONGLONG ReadTransferCount;
                     28:     ULONGLONG WriteTransferCount;
                     29:     ULONGLONG OtherTransferCount;
                     30: } IO_COUNTERS_;
                     31: typedef IO_COUNTERS_ *PIO_COUNTERS_;
                     32: 
                     33: typedef unsigned __int64 ui64;
                     34: // kernel32.dll (NT/2K/XP)
                     35: typedef BOOL (WINAPI *PGETPROCESSTIMES)(HANDLE,LPFILETIME,LPFILETIME,LPFILETIME,LPFILETIME);
                     36: //typedef BOOL (WINAPI *PGETPROCESSHEAPS)(DWORD,PHANDLE);
                     37: typedef BOOL (WINAPI *GETPROCESSIOCOUNTERS)(HANDLE,PIO_COUNTERS_);
                     38: // psapi.dll (2K/XP)
                     39: typedef BOOL (WINAPI *PGETPROCESSMEMORYINFO)(HANDLE,PPROCESS_MEMORY_COUNTERS,DWORD);
                     40: 
                     41: // from CRT time.c
                     42: /*
                     43:  * Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
                     44:  */
                     45: #define EPOCH_BIAS  116444736000000000i64
                     46: 
                     47: /*
                     48:  * Union to facilitate converting from FILETIME to unsigned __int64
                     49:  */
                     50: typedef union {
1.40      moko       51:        unsigned __int64 ft_scalar;
                     52:        FILETIME ft_struct;
                     53: } FT;
                     54: 
1.41      moko       55: int gettimeofday(struct timeval *tv, void *) {
1.40      moko       56:        FT ft;
                     57:        GetSystemTimeAsFileTime( &(ft.ft_struct) );
                     58:        ft.ft_scalar -= EPOCH_BIAS;
1.41      moko       59:        tv->tv_sec = (long)(ft.ft_scalar/10000000i64);
                     60:        tv->tv_usec = (long)((ft.ft_scalar- tv->tv_sec*10000000i64)/10i64);
1.40      moko       61:        return 0;
                     62: }
1.16      paf        63: 
                     64: #endif
                     65: 
1.17      paf        66: Value& rusage_element() {
                     67:        VHash& rusage=*new VHash;
                     68:        HashStringValue& hash=rusage.hash();
1.16      paf        69: 
1.30      moko       70: #ifdef _MSC_VER
1.17      paf        71:        double d1;
                     72:        HANDLE hProc = GetCurrentProcess();
1.16      paf        73: 
1.17      paf        74:        HMODULE hMod = LoadLibrary("kernel32.dll");
                     75:        if(hMod){
                     76:                // NT/2K/XP
                     77:                PGETPROCESSTIMES pGetProcessTimes = (PGETPROCESSTIMES)GetProcAddress(hMod, "GetProcessTimes");
                     78:                if(pGetProcessTimes){
                     79:                        FILETIME CreationTime, ExitTime;
                     80:                        FT KernelTime, UserTime;
                     81:                        if(pGetProcessTimes(hProc, &CreationTime, &ExitTime, &KernelTime.ft_struct, &UserTime.ft_struct)){
                     82:                                // dwHighDateTime & dwLowDateTime - 1/10 000 000 seconds in 64 bit
                     83:                                /* the amount of time that the process has executed in user mode */
                     84:                                d1 = double((LONGLONG)UserTime.ft_scalar)/10000000.0;
1.31      moko       85:                                hash.put("utime", new VDouble(d1));
1.17      paf        86:                                
                     87:                                /* the amount of time that the process has executed in kernel mode */
                     88:                                d1 = double((LONGLONG)KernelTime.ft_scalar)/10000000.0;
1.31      moko       89:                                hash.put("stime", new VDouble(d1));
1.16      paf        90:                        }
1.17      paf        91:                }
1.16      paf        92: 
1.17      paf        93:                // NT/2K/XP
                     94:                GETPROCESSIOCOUNTERS pGetProcessIoCounters = (GETPROCESSIOCOUNTERS)GetProcAddress(hMod, "GetProcessIoCounters");
                     95:                if(pGetProcessIoCounters){
                     96:                        IO_COUNTERS_ ioc;
                     97:                        if(pGetProcessIoCounters(hProc, &ioc)){
                     98:                                /* Specifies the number of I/O operations performed, other than read and write operations */
1.31      moko       99:                                hash.put("OtherOperationCount", new VDouble(double((LONGLONG)ioc.OtherOperationCount)));
1.17      paf       100:                                /* Specifies the number of bytes transferred during operations other than read and write operations */
1.31      moko      101:                                hash.put("OtherTransferCount", new VDouble(double((LONGLONG)ioc.OtherTransferCount)/1024.0));
1.17      paf       102:                                /* Specifies the number of read operations performed */
1.31      moko      103:                                hash.put("ReadOperationCount", new VDouble(double((LONGLONG)ioc.ReadOperationCount)));
1.17      paf       104:                                /* Specifies the number of bytes read */
1.31      moko      105:                                hash.put("ReadTransferCount", new VDouble(double((LONGLONG)ioc.ReadTransferCount)/1024.0));
1.17      paf       106:                                /* Specifies the number of write operations performed */
1.31      moko      107:                                hash.put("WriteOperationCount", new VDouble(double((LONGLONG)ioc.WriteOperationCount)));
1.17      paf       108:                                /* Specifies the number of bytes written */
1.31      moko      109:                                hash.put("WriteTransferCount", new VDouble(double((LONGLONG)ioc.WriteTransferCount)/1024.0));
1.16      paf       110:                        }
                    111:                }
1.17      paf       112:                FreeLibrary(hMod);
                    113:                /*
                    114:                PGETPROCESSHEAPS pGetProcessHeaps = (PGETPROCESSHEAPS)GetProcAddress(hMod, "GetProcessHeaps");
                    115:                if(pGetProcessHeaps){
                    116:                }
                    117:                */
                    118:        }
1.16      paf       119: 
1.17      paf       120:        // 2K/XP
                    121:        hMod = LoadLibrary("psapi.dll");
                    122:        if(hMod){
                    123:                PGETPROCESSMEMORYINFO pGetProcessMemoryInfo = (PGETPROCESSMEMORYINFO)GetProcAddress(hMod, "GetProcessMemoryInfo");
                    124:                if(pGetProcessMemoryInfo){
                    125:                        PROCESS_MEMORY_COUNTERS pmc;
                    126:                        pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
                    127:                        if(pGetProcessMemoryInfo(hProc, &pmc, sizeof(PROCESS_MEMORY_COUNTERS))){
                    128:                                /* The peak working set size */
                    129:                                d1 = double(pmc.PeakWorkingSetSize)/1024.0;
1.31      moko      130:                                hash.put("maxrss", new VDouble(d1));
1.17      paf       131:                                /* The peak nonpaged pool usage */
                    132:                                d1 = double(pmc.QuotaPeakNonPagedPoolUsage)/1024.0;
1.31      moko      133:                                hash.put("QuotaPeakNonPagedPoolUsage", new VDouble(d1));
1.17      paf       134:                                /* The peak paged pool usage */
                    135:                                d1 = double(pmc.QuotaPeakPagedPoolUsage)/1024.0;
1.31      moko      136:                                hash.put("QuotaPeakPagedPoolUsage", new VDouble(d1));
1.17      paf       137:                                /* The peak pagefile usage */
                    138:                                d1 = double(pmc.PeakPagefileUsage)/1024.0;
1.31      moko      139:                                hash.put("PeakPagefileUsage", new VDouble(d1));
1.16      paf       140:                        }
                    141:                }
1.17      paf       142:                FreeLibrary(hMod);
                    143:        }
1.16      paf       144: 
                    145: #else
                    146: 
                    147: #ifdef HAVE_GETRUSAGE
1.17      paf       148:     struct rusage u;
                    149:     if(getrusage(RUSAGE_SELF,&u)<0)
1.39      moko      150:                throw Exception(0, 0, "getrusage failed (#%d)", errno);
1.17      paf       151: 
1.31      moko      152:        hash.put("utime", new VDouble(u.ru_utime.tv_sec+u.ru_utime.tv_usec/1000000.0));
                    153:        hash.put("stime", new VDouble(u.ru_stime.tv_sec+u.ru_stime.tv_usec/1000000.0));
                    154:        hash.put("maxrss", new VDouble(u.ru_maxrss));
                    155:        hash.put("ixrss", new VDouble(u.ru_ixrss));
                    156:        hash.put("idrss", new VDouble(u.ru_idrss));
                    157:        hash.put("isrss", new VDouble(u.ru_isrss));
1.16      paf       158: #endif
1.13      paf       159: 
1.40      moko      160: #endif
                    161: 
1.17      paf       162:        struct timeval tp;
                    163:        if(gettimeofday(&tp, NULL)<0)
1.38      moko      164:                throw Exception(0, 0, "gettimeofday failed (#%d)", errno);
1.17      paf       165: 
1.31      moko      166:        hash.put("tv_sec", new VDouble(tp.tv_sec));
                    167:        hash.put("tv_usec", new VDouble(tp.tv_usec));
1.1       paf       168: 
1.17      paf       169:        return rusage;
                    170: }
                    171: 
                    172: #ifndef PA_DEBUG_DISABLE_GC
                    173: Value& memory_element() {
                    174:        VHash& memory=*new VHash;
                    175:        HashStringValue& hash=memory.hash();
                    176:        size_t heap_size=GC_get_heap_size();
                    177:        size_t free_bytes=GC_get_free_bytes();
                    178:        size_t bytes_since_gc=GC_get_bytes_since_gc();
                    179:        size_t total_bytes=GC_get_total_bytes();
                    180: 
1.31      moko      181:        hash.put("used", new VDouble((heap_size-free_bytes)/1024.0));
                    182:        hash.put("free", new VDouble(free_bytes/1024.0));
                    183:        hash.put("ever_allocated_since_compact", new VDouble(bytes_since_gc/1024.0));
                    184:        hash.put("ever_allocated_since_start", new VDouble(total_bytes/1024.0));
1.17      paf       185: 
                    186:        return memory;
                    187: }
1.16      paf       188: #endif
                    189: 
1.27      misha     190: Value* VStatus::get_element(const String& aname) {
1.34      moko      191: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
                    192:        // CLASS, CLASS_NAME
1.33      moko      193:        if(Value* result=VStateless_class::get_element(aname))
                    194:                return result;
1.34      moko      195: #endif
1.33      moko      196: 
1.17      paf       197:        // getstatus
1.22      paf       198:        if(Cache_manager* manager=cache_managers->get(aname))
1.17      paf       199:                return manager->get_status();
                    200: 
1.23      paf       201:        // $pid
                    202:        if(aname=="pid")
                    203:                return new VInt(getpid());
                    204: 
                    205:        // $tid
                    206:        if(aname=="tid")
                    207:                return new VInt(pa_get_thread_id());
                    208: 
                    209:        // $rusage
1.17      paf       210:        if(aname=="rusage")
                    211:                return &rusage_element();
                    212: 
                    213: #ifndef PA_DEBUG_DISABLE_GC
1.23      paf       214:        // $memory
1.17      paf       215:        if(aname=="memory")
                    216:                return &memory_element();
                    217: #endif
1.1       paf       218: 
                    219:        return 0;
                    220: }

E-mail: