Annotation of parser3/src/types/pa_vstatus.C, revision 1.49
1.1 paf 1: /** @file
2: Parser: @b status class impl.
3:
1.49 ! moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.42 moko 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.44 moko 14: #include "pa_vstring.h"
1.23 paf 15: #include "pa_threads.h"
1.2 paf 16:
1.49 ! moko 17: volatile const char * IDENT_PA_VSTATUS_C="$Id: pa_vstatus.C,v 1.48 2025/11/14 00:09:54 moko Exp $" IDENT_PA_VSTATUS_H;
1.28 moko 18:
1.30 moko 19: #ifdef _MSC_VER
1.16 paf 20: #include <windows.h>
21: #include "psapi.h"
22:
23: // should be in windows.h, but were't
24: typedef struct _IO_COUNTERS_ {
25: ULONGLONG ReadOperationCount;
26: ULONGLONG WriteOperationCount;
27: ULONGLONG OtherOperationCount;
28: ULONGLONG ReadTransferCount;
29: ULONGLONG WriteTransferCount;
30: ULONGLONG OtherTransferCount;
31: } IO_COUNTERS_;
32: typedef IO_COUNTERS_ *PIO_COUNTERS_;
33:
34: typedef unsigned __int64 ui64;
35: // kernel32.dll (NT/2K/XP)
36: typedef BOOL (WINAPI *PGETPROCESSTIMES)(HANDLE,LPFILETIME,LPFILETIME,LPFILETIME,LPFILETIME);
37: //typedef BOOL (WINAPI *PGETPROCESSHEAPS)(DWORD,PHANDLE);
38: typedef BOOL (WINAPI *GETPROCESSIOCOUNTERS)(HANDLE,PIO_COUNTERS_);
39: // psapi.dll (2K/XP)
40: typedef BOOL (WINAPI *PGETPROCESSMEMORYINFO)(HANDLE,PPROCESS_MEMORY_COUNTERS,DWORD);
41:
42: // from CRT time.c
43: /*
44: * Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
45: */
46: #define EPOCH_BIAS 116444736000000000i64
47:
48: /*
49: * Union to facilitate converting from FILETIME to unsigned __int64
50: */
51: typedef union {
1.40 moko 52: unsigned __int64 ft_scalar;
53: FILETIME ft_struct;
54: } FT;
55:
1.41 moko 56: int gettimeofday(struct timeval *tv, void *) {
1.40 moko 57: FT ft;
58: GetSystemTimeAsFileTime( &(ft.ft_struct) );
59: ft.ft_scalar -= EPOCH_BIAS;
1.41 moko 60: tv->tv_sec = (long)(ft.ft_scalar/10000000i64);
61: tv->tv_usec = (long)((ft.ft_scalar- tv->tv_sec*10000000i64)/10i64);
1.40 moko 62: return 0;
63: }
1.16 paf 64:
65: #endif
66:
1.17 paf 67: Value& rusage_element() {
68: VHash& rusage=*new VHash;
69: HashStringValue& hash=rusage.hash();
1.16 paf 70:
1.30 moko 71: #ifdef _MSC_VER
1.17 paf 72: double d1;
73: HANDLE hProc = GetCurrentProcess();
1.16 paf 74:
1.17 paf 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;
1.46 moko 86: HASH_PUT_CSTR(hash, "utime", new VDouble(d1));
1.17 paf 87:
88: /* the amount of time that the process has executed in kernel mode */
89: d1 = double((LONGLONG)KernelTime.ft_scalar)/10000000.0;
1.46 moko 90: HASH_PUT_CSTR(hash, "stime", new VDouble(d1));
1.16 paf 91: }
1.17 paf 92: }
1.16 paf 93:
1.17 paf 94: // NT/2K/XP
95: GETPROCESSIOCOUNTERS pGetProcessIoCounters = (GETPROCESSIOCOUNTERS)GetProcAddress(hMod, "GetProcessIoCounters");
96: if(pGetProcessIoCounters){
97: IO_COUNTERS_ ioc;
98: if(pGetProcessIoCounters(hProc, &ioc)){
99: /* Specifies the number of I/O operations performed, other than read and write operations */
1.46 moko 100: HASH_PUT_CSTR(hash, "OtherOperationCount", new VDouble(double((LONGLONG)ioc.OtherOperationCount)));
1.17 paf 101: /* Specifies the number of bytes transferred during operations other than read and write operations */
1.46 moko 102: HASH_PUT_CSTR(hash, "OtherTransferCount", new VDouble(double((LONGLONG)ioc.OtherTransferCount)/1024.0));
1.17 paf 103: /* Specifies the number of read operations performed */
1.46 moko 104: HASH_PUT_CSTR(hash, "ReadOperationCount", new VDouble(double((LONGLONG)ioc.ReadOperationCount)));
1.17 paf 105: /* Specifies the number of bytes read */
1.46 moko 106: HASH_PUT_CSTR(hash, "ReadTransferCount", new VDouble(double((LONGLONG)ioc.ReadTransferCount)/1024.0));
1.17 paf 107: /* Specifies the number of write operations performed */
1.46 moko 108: HASH_PUT_CSTR(hash, "WriteOperationCount", new VDouble(double((LONGLONG)ioc.WriteOperationCount)));
1.17 paf 109: /* Specifies the number of bytes written */
1.46 moko 110: HASH_PUT_CSTR(hash, "WriteTransferCount", new VDouble(double((LONGLONG)ioc.WriteTransferCount)/1024.0));
1.16 paf 111: }
112: }
1.17 paf 113: FreeLibrary(hMod);
114: /*
115: PGETPROCESSHEAPS pGetProcessHeaps = (PGETPROCESSHEAPS)GetProcAddress(hMod, "GetProcessHeaps");
116: if(pGetProcessHeaps){
117: }
118: */
119: }
1.16 paf 120:
1.17 paf 121: // 2K/XP
122: hMod = LoadLibrary("psapi.dll");
123: if(hMod){
124: PGETPROCESSMEMORYINFO pGetProcessMemoryInfo = (PGETPROCESSMEMORYINFO)GetProcAddress(hMod, "GetProcessMemoryInfo");
125: if(pGetProcessMemoryInfo){
126: PROCESS_MEMORY_COUNTERS pmc;
127: pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
128: if(pGetProcessMemoryInfo(hProc, &pmc, sizeof(PROCESS_MEMORY_COUNTERS))){
129: /* The peak working set size */
130: d1 = double(pmc.PeakWorkingSetSize)/1024.0;
1.46 moko 131: HASH_PUT_CSTR(hash, "maxrss", new VDouble(d1));
1.17 paf 132: /* The peak nonpaged pool usage */
133: d1 = double(pmc.QuotaPeakNonPagedPoolUsage)/1024.0;
1.46 moko 134: HASH_PUT_CSTR(hash, "QuotaPeakNonPagedPoolUsage", new VDouble(d1));
1.17 paf 135: /* The peak paged pool usage */
136: d1 = double(pmc.QuotaPeakPagedPoolUsage)/1024.0;
1.46 moko 137: HASH_PUT_CSTR(hash, "QuotaPeakPagedPoolUsage", new VDouble(d1));
1.17 paf 138: /* The peak pagefile usage */
139: d1 = double(pmc.PeakPagefileUsage)/1024.0;
1.46 moko 140: HASH_PUT_CSTR(hash, "PeakPagefileUsage", new VDouble(d1));
1.16 paf 141: }
142: }
1.17 paf 143: FreeLibrary(hMod);
144: }
1.16 paf 145:
146: #else
147:
148: #ifdef HAVE_GETRUSAGE
1.17 paf 149: struct rusage u;
150: if(getrusage(RUSAGE_SELF,&u)<0)
1.39 moko 151: throw Exception(0, 0, "getrusage failed (#%d)", errno);
1.17 paf 152:
1.46 moko 153: HASH_PUT_CSTR(hash, "utime", new VDouble(u.ru_utime.tv_sec+u.ru_utime.tv_usec/1000000.0));
154: HASH_PUT_CSTR(hash, "stime", new VDouble(u.ru_stime.tv_sec+u.ru_stime.tv_usec/1000000.0));
155: HASH_PUT_CSTR(hash, "maxrss", new VDouble(u.ru_maxrss));
156: HASH_PUT_CSTR(hash, "ixrss", new VDouble(u.ru_ixrss));
157: HASH_PUT_CSTR(hash, "idrss", new VDouble(u.ru_idrss));
158: HASH_PUT_CSTR(hash, "isrss", new VDouble(u.ru_isrss));
1.16 paf 159: #endif
1.13 paf 160:
1.40 moko 161: #endif
162:
1.17 paf 163: struct timeval tp;
164: if(gettimeofday(&tp, NULL)<0)
1.38 moko 165: throw Exception(0, 0, "gettimeofday failed (#%d)", errno);
1.17 paf 166:
1.46 moko 167: HASH_PUT_CSTR(hash, "tv_sec", new VDouble(tp.tv_sec));
168: HASH_PUT_CSTR(hash, "tv_usec", new VDouble(tp.tv_usec));
1.1 paf 169:
1.17 paf 170: return rusage;
171: }
172:
173: #ifndef PA_DEBUG_DISABLE_GC
174: Value& memory_element() {
175: VHash& memory=*new VHash;
176: HashStringValue& hash=memory.hash();
177: size_t heap_size=GC_get_heap_size();
178: size_t free_bytes=GC_get_free_bytes();
179: size_t bytes_since_gc=GC_get_bytes_since_gc();
180: size_t total_bytes=GC_get_total_bytes();
181:
1.46 moko 182: HASH_PUT_CSTR(hash, "used", new VDouble((heap_size-free_bytes)/1024.0));
183: HASH_PUT_CSTR(hash, "free", new VDouble(free_bytes/1024.0));
184: HASH_PUT_CSTR(hash, "ever_allocated_since_compact", new VDouble(bytes_since_gc/1024.0));
185: HASH_PUT_CSTR(hash, "ever_allocated_since_start", new VDouble(total_bytes/1024.0));
1.17 paf 186:
187: return memory;
188: }
1.16 paf 189: #endif
190:
1.44 moko 191: extern const char* parser3_mode;
1.45 moko 192: const char *parser3_log_filespec();
1.44 moko 193:
1.47 moko 194: #ifndef _MSC_VER
195: static int get_rlimit_resource(const String& aname) {
196: if(aname=="limit-cpu")
197: return RLIMIT_CPU;
198: else if(aname=="limit-mem")
199: return RLIMIT_DATA;
200: else // limit-nproc
201: return RLIMIT_NPROC;
202: }
203: #endif
204:
1.27 misha 205: Value* VStatus::get_element(const String& aname) {
1.34 moko 206: #ifndef OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL
207: // CLASS, CLASS_NAME
1.33 moko 208: if(Value* result=VStateless_class::get_element(aname))
209: return result;
1.34 moko 210: #endif
1.33 moko 211:
1.17 paf 212: // getstatus
1.22 paf 213: if(Cache_manager* manager=cache_managers->get(aname))
1.17 paf 214: return manager->get_status();
215:
1.23 paf 216: if(aname=="pid")
217: return new VInt(getpid());
218:
219: if(aname=="tid")
220: return new VInt(pa_get_thread_id());
221:
1.44 moko 222: if(aname=="mode")
223: return new VString(*new String(parser3_mode));
224:
1.45 moko 225: if(aname=="log-filename")
226: return new VString(*new String(pa_strdup(parser3_log_filespec())));
227:
1.17 paf 228: if(aname=="rusage")
229: return &rusage_element();
230:
231: #ifndef PA_DEBUG_DISABLE_GC
232: if(aname=="memory")
233: return &memory_element();
234: #endif
1.1 paf 235:
1.47 moko 236: #ifndef _MSC_VER
237: if(aname=="limit-cpu" || aname=="limit-mem" || aname=="limit-nproc") {
238: struct rlimit rlim;
239: if(getrlimit(get_rlimit_resource(aname), &rlim)<0)
240: throw Exception(0, 0, "getrlimit failed: %s (%d)", strerror(errno), errno);
241:
1.48 moko 242: return new VString(*new String(rlim.rlim_cur == RLIM_INFINITY ? "unlimited" : pa_itoa(rlim.rlim_cur)));
1.47 moko 243: }
244: #endif
1.1 paf 245: return 0;
246: }
1.47 moko 247:
248: const VJunction* VStatus::put_element(const String& aname, Value* avalue) {
249: #ifndef _MSC_VER
250: if(aname=="limit-cpu" || aname=="limit-mem" || aname=="limit-nproc") {
251: const String& value_str=avalue->as_string();
252: struct rlimit rlim;
1.48 moko 253: rlim.rlim_cur=value_str=="unlimited" ? RLIM_INFINITY : (rlim_t)pa_atoul(value_str.cstr(), 0, &value_str);
1.47 moko 254: rlim.rlim_max=rlim.rlim_cur;
255:
256: if(setrlimit(get_rlimit_resource(aname), &rlim)<0)
1.48 moko 257: throw Exception(0, 0, "setrlimit %s failed: %s (%d)", value_str.cstr(), strerror(errno), errno);
1.47 moko 258:
259: return 0;
260: }
261: #endif
262: return Value::put_element(aname, avalue);
263: }
E-mail: