The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
Timing.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11//====================================================================
12// Timing.cpp
13//--------------------------------------------------------------------
14//
15// Package : System (The LHCb System service)
16//
17// Description: Implementation of Systems internals
18//
19// Author : M.Frank
20// Created : 13/1/99
21// Changes :
22//====================================================================
23#define GAUDIKERNEL_TIMING_CPP
24
25#include "ProcessDescriptor.h"
26#include <GaudiKernel/Timing.h>
27
28#include <climits>
29#include <ctime>
30#include <sys/time.h>
31#include <sys/times.h>
32#include <unistd.h>
33
34static const long long UNIX_BASE_TIME = 0;
35
36// convert time from internal representation to the appropriate type
37// Internal representation for Unix: 1 clock tick (usually 10 milliseconds)
38long long System::adjustTime( TimeType typ, long long t ) {
39 if ( t != -1 ) {
41 // t /= CLK_TCK ; // needs division by clock tick unit
45
47 switch ( typ ) {
48 case Year:
49 return adjustTime<Year>( t );
50 case Month:
51 return adjustTime<Month>( t );
52 case Day:
53 return adjustTime<Day>( t );
54 case Hour:
55 return adjustTime<Hour>( t );
56 case Min:
57 return adjustTime<Min>( t );
58 case Sec:
59 return adjustTime<Sec>( t );
60 case milliSec:
61 return adjustTime<milliSec>( t );
62 case microSec:
63 return adjustTime<microSec>( t );
64 case nanoSec:
65 return adjustTime<nanoSec>( t );
66 case Native:
67 return adjustTime<Native>( t );
68 default:
69 return t;
70 }
71 }
72 return t;
73}
74
76long long System::tickCount() {
77 long long count = 10000;
78 struct tms buf;
79 count *= 10 * times( &buf );
80 return count;
81}
82
83#include <iostream>
84
86long long System::currentTime( TimeType typ ) {
87 switch ( typ ) {
88 case Year:
89 return currentTime<Year>();
90 case Month:
91 return currentTime<Month>();
92 case Day:
93 return currentTime<Day>();
94 case Hour:
95 return currentTime<Hour>();
96 case Min:
97 return currentTime<Min>();
98 case Sec:
99 return currentTime<Sec>();
100 case milliSec:
101 return currentTime<milliSec>();
102 case microSec:
103 return currentTime<microSec>();
104 case nanoSec:
105 return currentTime<nanoSec>();
106 case Native:
107 return currentTime<Native>();
108 }
109 return currentTime<Native>();
110}
111
114 static long long sys_start = 0;
115 if ( 0 == sys_start ) {
116 long long c = currentTime( microSec );
117 long long t = tickCount();
118 sys_start = 10 * c - t;
119 }
120 return adjustTime( typ, sys_start );
121}
122
124long long System::upTime( TimeType typ ) {
125 static long long sys_start = 10 * systemStart( microSec );
126 return adjustTime( typ, 10 * currentTime( microSec ) - sys_start );
127}
128
130long long System::creationTime( TimeType typ, InfoType fetch, long pid ) {
131 long long created = 0;
133 if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
134 created = adjustTime( typ, info.CreateTime - UNIX_BASE_TIME );
135 }
136 return created;
137}
138
140long long System::remainingTime( TimeType typ, InfoType fetch, long pid ) {
141 long long left = 0;
142 QUOTA_LIMITS quota;
143 if ( fetch != NoFetch && getProcess()->query( pid, fetch, &quota ) ) {
144 if ( left == -1 ) {
145 // left = _I64_MAX;
146 } else {
147 left = adjustTime( typ, quota.TimeLimit );
148 }
149 }
150 return left;
151}
152
154long long System::ellapsedTime( TimeType typ, InfoType fetch, long pid ) {
156 long long ellapsed = currentTime( microSec ) * 10;
157 if ( getProcess()->query( pid, fetch, &info ) ) {
158 ellapsed = adjustTime( typ, ellapsed + UNIX_BASE_TIME - info.CreateTime );
159 }
160 return ellapsed;
161}
162
164long long System::kernelTime( TimeType typ, InfoType fetch, long pid ) {
166 long long kerneltime = 0;
167 if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
168 kerneltime = adjustTime( typ, info.KernelTime );
169 }
170 return kerneltime;
171}
172
174long long System::userTime( TimeType typ, InfoType fetch, long pid ) {
175 long long usertime = 0;
177 if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) { usertime = adjustTime( typ, info.UserTime ); }
178 return usertime;
179}
180
182long long System::cpuTime( TimeType typ, InfoType fetch, long pid ) {
183 long long cputime = 0;
185 if ( fetch != NoFetch && getProcess()->query( pid, fetch, &info ) ) {
186 cputime = adjustTime( typ, info.KernelTime + info.UserTime );
187 }
188 return cputime;
189}
190
191namespace System {
194 if ( getProcess()->query( pid, Times, &info ) ) {
195 return ProcessTime( info.KernelTime, info.UserTime, currentTime<Native>() - info.CreateTime );
196 }
197 return ProcessTime(); // return 0s in case of problems
198 }
199} // namespace System
Simple class to hold the time information of a process.
Definition Timing.h:134
Note: OS specific details for environment resolution.
Definition Environment.h:25
GAUDI_API ProcessTime getProcessTime(long pid=-1)
Retrieve the process time data for a process.
Definition Timing.cpp:192
TimeType
Time type for conversion.
Definition Timing.h:45
@ milliSec
Definition Timing.h:45
@ Min
Definition Timing.h:45
@ Native
Definition Timing.h:45
@ Year
Definition Timing.h:45
@ Sec
Definition Timing.h:45
@ nanoSec
Definition Timing.h:45
@ Hour
Definition Timing.h:45
@ Month
Definition Timing.h:45
@ microSec
Definition Timing.h:45
@ Day
Definition Timing.h:45
GAUDI_API long long systemStart(TimeType typ=Sec)
Maximum processing time left for this process.
Definition Timing.cpp:113
GAUDI_API long long kernelTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU kernel mode time of process in milliseconds.
Definition Timing.cpp:164
InfoType
Enumeration for fetching information.
Definition SystemBase.h:15
@ NoFetch
Definition SystemBase.h:15
GAUDI_API long long creationTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Process Creation time.
Definition Timing.cpp:130
GAUDI_API long long adjustTime(TimeType typ, long long timevalue)
Convert time from OS native time to requested representation (Experts only)
Definition Timing.cpp:38
GAUDI_API long long currentTime()
Retrieve absolute system time.
Definition Timing.h:238
GAUDI_API long long ellapsedTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Elapsed time since start of process in milliseconds.
Definition Timing.cpp:154
GAUDI_API long long tickCount()
Retrieve the number of ticks since system startup.
Definition Timing.cpp:76
GAUDI_API long long upTime(TimeType typ=Hour)
Maximum processing time left for this process.
Definition Timing.cpp:124
GAUDI_API long long cpuTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
Consumed CPU time of process in milliseconds.
Definition Timing.cpp:182
GAUDI_API long long remainingTime(TimeType typ=milliSec, InfoType fetch=Quota, long pid=-1)
Maximum processing time left for this process.
Definition Timing.cpp:140
GAUDI_API long long userTime(TimeType typ=milliSec, InfoType fetch=Times, long pid=-1)
CPU user mode time of process in milliseconds.
Definition Timing.cpp:174
ProcessDescriptor * getProcess()
Retrieve Process structure.
Process/Thread System and User Time NtQueryInformationProcess using ProcessTimes NtQueryInformationTh...
Process Quotas NtQueryInformationProcess using ProcessQuotaLimits NtQueryInformationProcess using Pro...