@@ -148,6 +148,12 @@ TEST_VM_F(JfrTestThreadCPULoadSingle, SingleCpu) {
148
148
EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, 400 * NANOSECS_PER_MILLISEC, 1 ));
149
149
EXPECT_FLOAT_EQ (0.25 , event.user );
150
150
EXPECT_FLOAT_EQ (0.25 , event.system );
151
+
152
+ MockOs::user_cpu_time += 50 * NANOSECS_PER_MILLISEC;
153
+ MockOs::system_cpu_time += 50 * NANOSECS_PER_MILLISEC;
154
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
155
+ EXPECT_FLOAT_EQ (0.125 , event.user );
156
+ EXPECT_FLOAT_EQ (0.125 , event.system );
151
157
}
152
158
153
159
TEST_VM_F (JfrTestThreadCPULoadSingle, MultipleCpus) {
@@ -177,6 +183,43 @@ TEST_VM_F(JfrTestThreadCPULoadSingle, UserAboveMaximum) {
177
183
EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (200 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
178
184
EXPECT_FLOAT_EQ (0.25 , event.user );
179
185
EXPECT_FLOAT_EQ (0 , event.system );
186
+
187
+ // Third call: make sure there are no leftovers
188
+ MockOs::user_cpu_time += 50 * NANOSECS_PER_MILLISEC;
189
+ MockOs::system_cpu_time += 50 * NANOSECS_PER_MILLISEC;
190
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (200 + 400 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
191
+ EXPECT_FLOAT_EQ (0.125 , event.user );
192
+ EXPECT_FLOAT_EQ (0.125 , event.system );
193
+ }
194
+
195
+ TEST_VM_F (JfrTestThreadCPULoadSingle, UserAboveMaximumNonZeroBase) {
196
+
197
+ // Setup a non zero base
198
+ // Previously there was a bug when cur_user_time would be reset to zero and test that uses zero base would fail to detect it
199
+ MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
200
+ MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
201
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, 400 * NANOSECS_PER_MILLISEC, 1 ));
202
+ EXPECT_FLOAT_EQ (0.25 , event.user );
203
+ EXPECT_FLOAT_EQ (0.25 , event.system );
204
+
205
+ // First call will not report above 100%
206
+ MockOs::user_cpu_time += 200 * NANOSECS_PER_MILLISEC;
207
+ MockOs::system_cpu_time += 100 * NANOSECS_PER_MILLISEC;
208
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 ) * NANOSECS_PER_MILLISEC, 1 ));
209
+ EXPECT_FLOAT_EQ (0.5 , event.user );
210
+ EXPECT_FLOAT_EQ (0.5 , event.system );
211
+
212
+ // Second call will see an extra 100 millisecs user time from the remainder
213
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
214
+ EXPECT_FLOAT_EQ (0.25 , event.user );
215
+ EXPECT_FLOAT_EQ (0 , event.system );
216
+
217
+ // Third call: make sure there are no leftovers
218
+ MockOs::user_cpu_time += 50 * NANOSECS_PER_MILLISEC;
219
+ MockOs::system_cpu_time += 50 * NANOSECS_PER_MILLISEC;
220
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 + 400 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
221
+ EXPECT_FLOAT_EQ (0.125 , event.user );
222
+ EXPECT_FLOAT_EQ (0.125 , event.system );
180
223
}
181
224
182
225
TEST_VM_F (JfrTestThreadCPULoadSingle, SystemAboveMaximum) {
@@ -192,6 +235,43 @@ TEST_VM_F(JfrTestThreadCPULoadSingle, SystemAboveMaximum) {
192
235
EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (200 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
193
236
EXPECT_FLOAT_EQ (0.25 , event.user );
194
237
EXPECT_FLOAT_EQ (0.25 , event.system );
238
+
239
+ // Third call: make sure there are no leftovers
240
+ MockOs::user_cpu_time += 50 * NANOSECS_PER_MILLISEC;
241
+ MockOs::system_cpu_time += 50 * NANOSECS_PER_MILLISEC;
242
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (200 + 400 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
243
+ EXPECT_FLOAT_EQ (0.125 , event.user );
244
+ EXPECT_FLOAT_EQ (0.125 , event.system );
245
+ }
246
+
247
+ TEST_VM_F (JfrTestThreadCPULoadSingle, SystemAboveMaximumNonZeroBase) {
248
+
249
+ // Setup a non zero base
250
+ // Previously there was a bug when cur_user_time would be reset to zero and test that uses zero base would fail to detect it
251
+ MockOs::user_cpu_time = 100 * NANOSECS_PER_MILLISEC;
252
+ MockOs::system_cpu_time = 100 * NANOSECS_PER_MILLISEC;
253
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, 400 * NANOSECS_PER_MILLISEC, 1 ));
254
+ EXPECT_FLOAT_EQ (0.25 , event.user );
255
+ EXPECT_FLOAT_EQ (0.25 , event.system );
256
+
257
+ // First call will not report above 100%
258
+ MockOs::user_cpu_time += 100 * NANOSECS_PER_MILLISEC;
259
+ MockOs::system_cpu_time += 300 * NANOSECS_PER_MILLISEC;
260
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 ) * NANOSECS_PER_MILLISEC, 1 ));
261
+ EXPECT_FLOAT_EQ (0 , event.user );
262
+ EXPECT_FLOAT_EQ (1 , event.system );
263
+
264
+ // Second call will see an extra 100 millisecs user and system time from the remainder
265
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
266
+ EXPECT_FLOAT_EQ (0.25 , event.user );
267
+ EXPECT_FLOAT_EQ (0.25 , event.system );
268
+
269
+ // Third call: make sure there are no leftovers
270
+ MockOs::user_cpu_time += 50 * NANOSECS_PER_MILLISEC;
271
+ MockOs::system_cpu_time += 50 * NANOSECS_PER_MILLISEC;
272
+ EXPECT_TRUE (JfrThreadCPULoadEvent::update_event (event, thread, (400 + 200 + 400 + 400 ) * NANOSECS_PER_MILLISEC, 1 ));
273
+ EXPECT_FLOAT_EQ (0.125 , event.user );
274
+ EXPECT_FLOAT_EQ (0.125 , event.system );
195
275
}
196
276
197
277
TEST_VM_F (JfrTestThreadCPULoadSingle, SystemTimeDecreasing) {
0 commit comments