HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Counter.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Counter.h ( UT Library, C++)
7  *
8  * COMMENTS: Class to keep track of records and print out the data at the
9  * end of a render. You simply have to declare a static printer
10  * in the file you want the printing to occur in. The destructor
11  * will cause the stat to be printed.
12  */
13 
14 #ifndef __UT_Counter__
15 #define __UT_Counter__
16 
17 #include "UT_API.h"
18 
19 #ifdef UT_DEBUG
20 
21 #include "UT_String.h"
22 #include <SYS/SYS_AtomicInt.h>
23 #include <stdio.h>
24 
25 class UT_API UT_Counter
26 {
27 public:
28  explicit UT_Counter(const char *msg)
29  {
30  myMessage = msg;
31  }
32 
33  ~UT_Counter()
34  {
35  if (myPeak.load() > 0)
36  {
37  UT_String count, peak, incs;
38  count.itoaPretty(myCount.load());
39  peak.itoaPretty(myPeak.load());
40  incs.itoaPretty(myIncrements.load());
41  printf("%s: %s [%s peak, %s increments]\n",
42  myMessage, count.buffer(), peak.buffer(), incs.buffer());
43  fflush(stdout);
44  }
45  }
46 
47  UT_Counter(const UT_Counter &m)
48  {
49  myMessage = m.myMessage;
50  myCount.relaxedStore(m.myCount.relaxedLoad());
51  myPeak.relaxedStore(m.myPeak.relaxedLoad());
52  myIncrements.relaxedStore(m.myIncrements.relaxedLoad());
53  }
54  UT_Counter &operator=(const UT_Counter &m)
55  {
56  if (this == &m)
57  return *this;
58  myMessage = m.myMessage;
59  myCount.relaxedStore(m.myCount.relaxedLoad());
60  myPeak.relaxedStore(m.myPeak.relaxedLoad());
61  myIncrements.relaxedStore(m.myIncrements.relaxedLoad());
62  return *this;
63  }
64 
66  {
67  myPeak.maximum(v);
68  myCount.maximum(v);
69  return *this;
70  }
71  UT_Counter &operator+=(int v)
72  {
73  myPeak.maximum(myCount.add(v));
74  if (v > 0)
75  myIncrements.add(v);
76  return *this;
77  }
78  UT_Counter &operator-=(int v) { return operator+=(-v); }
79  UT_Counter &operator++(int) { return operator+=(1); }
80  UT_Counter &operator--(int) { return operator+=(-1); }
81  void reset() { myCount.store(0,
83  exint peak() const { return myPeak.load(); }
84  exint count() const { return myCount.load(); }
85  exint increments() const { return myIncrements.load(); }
86 
87 private:
88  SYS_AtomicCounter myCount;
89  SYS_AtomicCounter myPeak;
90  SYS_AtomicCounter myIncrements;
91  const char *myMessage;
92 };
93 
94 #else // UT_DEBUG
95 
96 #include <SYS/SYS_Types.h>
97 
98 // When no debugging is on, we'll make a dummy class. Hopefully, the compiler
99 // will optimize this right out.
101 {
102 public:
103  UT_Counter(const char *) {}
105 
106  UT_Counter(const UT_Counter &m) = default;
107  UT_Counter &operator=(const UT_Counter &m) = default;
108 
109  UT_Counter &max(exint) { return *this; }
110  UT_Counter &operator+=(int) { return *this; }
111  UT_Counter &operator++(int) { return *this; }
112  UT_Counter &operator-=(int) { return *this; }
113  UT_Counter &operator--(int) { return *this; }
114  void reset() { }
115  exint peak() const { return 0; }
116  exint count() const { return 0; }
117  exint increments() const { return 0; }
118 };
119 
120 #endif // UT_DEBUG
121 #endif
UT_Counter(const char *)
Definition: UT_Counter.h:103
exint peak() const
Definition: UT_Counter.h:115
const GLdouble * v
Definition: glcorearb.h:837
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
int64 exint
Definition: SYS_Types.h:125
#define UT_API
Definition: UT_API.h:14
UT_Counter & max(exint)
Definition: UT_Counter.h:109
void itoaPretty(int64 val)
bool load(UT_IStream &is)
Load string from stream. Use is.eof() to check eof status.
const char * buffer() const
Definition: UT_String.h:509
GLboolean reset
Definition: glad.h:5138
OIIO_FORCEINLINE const vint4 & operator+=(vint4 &a, const vint4 &b)
Definition: simd.h:4369
exint count() const
Definition: UT_Counter.h:116
exint increments() const
Definition: UT_Counter.h:117
UT_Counter & operator-=(int)
Definition: UT_Counter.h:112
UT_Counter & operator++(int)
Definition: UT_Counter.h:111
Any reordering the compiler or hardware chooses to do is okay.
UT_Counter & operator--(int)
Definition: UT_Counter.h:113
UT_Counter & operator+=(int)
Definition: UT_Counter.h:110
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
OIIO_FORCEINLINE const vint4 & operator-=(vint4 &a, const vint4 &b)
Definition: simd.h:4392
GLint GLsizei count
Definition: glcorearb.h:405
void reset()
Definition: UT_Counter.h:114