PostgreSQL Source Code: src/include/portability/instr_time.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56#ifndef INSTR_TIME_H

57#define INSTR_TIME_H

58

59

60

61

62

63

64

65

66

67

68

70{

73

74

75

76

77#define NS_PER_S INT64CONST(1000000000)

78#define NS_PER_MS INT64CONST(1000000)

79#define NS_PER_US INT64CONST(1000)

80

81

82#ifndef WIN32

83

84

85

86

88

89

90

91

92

93

94

95

96

97

98

99

100

101#if defined(__darwin__) && defined(CLOCK_MONOTONIC_RAW)

102#define PG_INSTR_CLOCK CLOCK_MONOTONIC_RAW

103#elif defined(CLOCK_MONOTONIC)

104#define PG_INSTR_CLOCK CLOCK_MONOTONIC

105#else

106#define PG_INSTR_CLOCK CLOCK_REALTIME

107#endif

108

109

112{

114 struct timespec tmp;

115

117 now.ticks = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;

118

119 return now;

120}

121

122#define INSTR_TIME_SET_CURRENT(t) \

123 ((t) = pg_clock_gettime_ns())

124

125#define INSTR_TIME_GET_NANOSEC(t) \

126 ((int64) (t).ticks)

127

128

129#else

130

131

132

133

134

136pg_query_performance_counter(void)

137{

139 LARGE_INTEGER tmp;

140

141 QueryPerformanceCounter(&tmp);

142 now.ticks = tmp.QuadPart;

143

144 return now;

145}

146

147static inline double

148GetTimerFrequency(void)

149{

150 LARGE_INTEGER f;

151

152 QueryPerformanceFrequency(&f);

153 return (double) f.QuadPart;

154}

155

156#define INSTR_TIME_SET_CURRENT(t) \

157 ((t) = pg_query_performance_counter())

158

159#define INSTR_TIME_GET_NANOSEC(t) \

160 ((int64) ((t).ticks * ((double) NS_PER_S / GetTimerFrequency())))

161

162#endif

163

164

165

166

167

168

169#define INSTR_TIME_IS_ZERO(t) ((t).ticks == 0)

170

171

172#define INSTR_TIME_SET_ZERO(t) ((t).ticks = 0)

173

174#define INSTR_TIME_SET_CURRENT_LAZY(t) \

175 (INSTR_TIME_IS_ZERO(t) ? INSTR_TIME_SET_CURRENT(t), true : false)

176

177

178#define INSTR_TIME_ADD(x,y) \

179 ((x).ticks += (y).ticks)

180

181#define INSTR_TIME_SUBTRACT(x,y) \

182 ((x).ticks -= (y).ticks)

183

184#define INSTR_TIME_ACCUM_DIFF(x,y,z) \

185 ((x).ticks += (y).ticks - (z).ticks)

186

187

188#define INSTR_TIME_GET_DOUBLE(t) \

189 ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_S)

190

191#define INSTR_TIME_GET_MILLISEC(t) \

192 ((double) INSTR_TIME_GET_NANOSEC(t) / NS_PER_MS)

193

194#define INSTR_TIME_GET_MICROSEC(t) \

195 (INSTR_TIME_GET_NANOSEC(t) / NS_PER_US)

196

197#endif

Datum now(PG_FUNCTION_ARGS)

struct instr_time instr_time

static instr_time pg_clock_gettime_ns(void)