MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UsageTimer.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  UsageTimer.h Structure for system timing
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 
32 namespace MFM
33 {
34  class UsageTimer
35  {
36  private:
37  typedef struct rusage RUsage;
38 
39  const RUsage m_rusage;
40 
41  UsageTimer(const RUsage r) : m_rusage(r)
42  { }
43 
44  public:
45  static UsageTimer Now()
46  {
47  return Now(RUSAGE_SELF);
48  }
49 
50  static UsageTimer NowThread()
51  {
52  return Now(RUSAGE_THREAD);
53  }
54 
55  static UsageTimer Now(int who)
56  {
57  RUsage r;
58  getrusage(who, &r);
59  return UsageTimer(r);
60  }
61 
62  long UserMilliseconds()
63  {
64  return m_rusage.ru_utime.tv_sec * 1000 + m_rusage.ru_utime.tv_usec / 1000;
65  }
66 
67  long SystemMilliseconds()
68  {
69  return m_rusage.ru_stime.tv_sec * 1000 + m_rusage.ru_stime.tv_usec / 1000;
70  }
71 
72  long TotalMilliseconds()
73  {
74  return UserMilliseconds() + SystemMilliseconds();
75  }
76 
77 
78  long UserMicroseconds()
79  {
80  return m_rusage.ru_utime.tv_sec * 1000000 + m_rusage.ru_utime.tv_usec;
81  }
82 
83  long SystemMicroseconds()
84  {
85  return m_rusage.ru_stime.tv_sec * 1000000 + m_rusage.ru_stime.tv_usec;
86  }
87 
88  long TotalMicroseconds()
89  {
90  return UserMicroseconds() + SystemMicroseconds();
91  }
92 
93  friend UsageTimer operator-(const UsageTimer& lhs, const UsageTimer& rhs)
94  {
95  RUsage t;
96  long uus = (lhs.m_rusage.ru_utime.tv_sec * 1000000 + lhs.m_rusage.ru_utime.tv_usec) -
97  (rhs.m_rusage.ru_utime.tv_sec * 1000000 + rhs.m_rusage.ru_utime.tv_usec);
98 
99  long sus = (lhs.m_rusage.ru_stime.tv_sec * 1000000 + lhs.m_rusage.ru_stime.tv_usec) -
100  (rhs.m_rusage.ru_stime.tv_sec * 1000000 + rhs.m_rusage.ru_stime.tv_usec);
101 
102  t.ru_utime.tv_sec = uus / 1000000;
103  t.ru_utime.tv_usec = uus % 1000000;
104 
105  t.ru_stime.tv_sec = sus / 1000000;
106  t.ru_stime.tv_usec = sus % 1000000;
107 
108  t.ru_nvcsw = lhs.m_rusage.ru_nvcsw - rhs.m_rusage.ru_nvcsw;
109  t.ru_nivcsw = lhs.m_rusage.ru_nivcsw - rhs.m_rusage.ru_nivcsw;
110  t.ru_nsignals = lhs.m_rusage.ru_nsignals - rhs.m_rusage.ru_nsignals;
111  t.ru_msgrcv = lhs.m_rusage.ru_msgrcv - rhs.m_rusage.ru_msgrcv;
112  t.ru_msgsnd = lhs.m_rusage.ru_msgsnd - rhs.m_rusage.ru_msgsnd;
113  t.ru_oublock = lhs.m_rusage.ru_oublock - rhs.m_rusage.ru_oublock;
114  t.ru_inblock = lhs.m_rusage.ru_inblock - rhs.m_rusage.ru_inblock;
115  t.ru_nswap = lhs.m_rusage.ru_nswap - rhs.m_rusage.ru_nswap;
116  t.ru_minflt = lhs.m_rusage.ru_minflt - rhs.m_rusage.ru_minflt;
117  t.ru_majflt = lhs.m_rusage.ru_majflt - rhs.m_rusage.ru_majflt;
118 
119  return UsageTimer(t);
120  }
121  };
122 }
Definition: UsageTimer.h:34