99#include < chrono>
1010#include < ctime>
1111#include < thread>
12+ #include < atomic>
1213
1314#include < boost/filesystem.hpp>
1415#include < boost/interprocess/shared_memory_object.hpp>
@@ -33,6 +34,17 @@ namespace bi = boost::interprocess;
3334namespace {
3435
3536static constexpr uint32_t nanoSecToSeconds = 1000000000 ;
37+ std::atomic<bool > workloadThreadFlag (false );
38+
39+ void workloadThread (ze_command_queue_handle_t cq, uint32_t numCommandLists,
40+ ze_command_list_handle_t *phCommandLists,
41+ ze_fence_handle_t hFence) {
42+ while (workloadThreadFlag) {
43+ lzt::execute_command_lists (cq, 1 , phCommandLists, nullptr );
44+ lzt::synchronize (cq, std::numeric_limits<uint64_t >::max ());
45+ }
46+ }
47+
3648class zetMetricGroupTest : public ::testing::Test {
3749protected:
3850 ze_device_handle_t device = lzt::zeDevice::get_instance()->get_device ();
@@ -1185,9 +1197,10 @@ TEST_F(
11851197 lzt::event_host_synchronize (eventHandle, UINT64_MAX);
11861198 size_t rawDataSize = 0 ;
11871199 std::vector<uint8_t > rawData;
1188- rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle, notifyEveryNReports);
1200+ rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle,
1201+ notifyEveryNReports);
11891202 EXPECT_GT (rawDataSize, 0 );
1190- rawData.resize (rawDataSize);
1203+ rawData.resize (rawDataSize);
11911204 lzt::metric_streamer_read_data (metricStreamerHandle, notifyEveryNReports,
11921205 rawDataSize, &rawData);
11931206
@@ -1217,11 +1230,20 @@ TEST_F(
12171230 zetMetricStreamerTest,
12181231 GivenValidMetricGroupWhenTimerBasedStreamerIsCreatedThenExpectStreamerToSucceed) {
12191232
1220- // The time in seconds for the buffer to overflow would be 2 * (notifyEveryNReports * (samplingPeriod/nanoSecToSeconds)) for this test it will be 512 seconds
1221- uint32_t notifyEveryNReports = 256 ;
1222- uint32_t samplingPeriod = 1000000000 ;
1223- for (auto device : devices) {
1233+ /* This test tries to validate the readData feature of streamers.
1234+ * numberOfReportsReq is the minimum number of reports needed by this test
1235+ * after streamerOpen(). timeBeforeReadInNanoSec is the minimum time interval
1236+ * between streamerOpen() and readData() . using above definitions sampling
1237+ * period needed becomes samplingPeriod = timeBeforeReadInNanoSec /
1238+ * numberOfReportsReq
1239+ */
1240+ constexpr uint32_t maxReadAttempts = 20 ;
1241+ constexpr uint32_t numberOfReportsReq = 100 ;
1242+ constexpr uint32_t timeBeforeReadInNanoSec = 500000000 ;
1243+ uint32_t samplingPeriod = timeBeforeReadInNanoSec / numberOfReportsReq;
1244+ uint32_t notifyEveryNReports = 9000 ;
12241245
1246+ for (auto device : devices) {
12251247 ze_device_properties_t deviceProperties = {
12261248 ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, nullptr };
12271249 zeDeviceGetProperties (device, &deviceProperties);
@@ -1244,10 +1266,6 @@ TEST_F(
12441266 for (auto groupInfo : metricGroupInfo) {
12451267 LOG_INFO << " test metricGroup name " << groupInfo.metricGroupName ;
12461268 lzt::activate_metric_groups (device, 1 , &groupInfo.metricGroupHandle );
1247- ze_event_handle_t eventHandle;
1248- lzt::zeEventPool eventPool;
1249- eventPool.create_event (eventHandle, ZE_EVENT_SCOPE_FLAG_HOST,
1250- ZE_EVENT_SCOPE_FLAG_HOST);
12511269
12521270 void *a_buffer, *b_buffer, *c_buffer;
12531271 ze_group_count_t tg;
@@ -1257,24 +1275,43 @@ TEST_F(
12571275 nullptr );
12581276 lzt::close_command_list (commandList);
12591277
1278+ // Spawn a thread which continuously runs a workload
1279+ workloadThreadFlag = true ;
1280+ std::thread thread (workloadThread, commandQueue, 1 , &commandList,
1281+ nullptr );
1282+
12601283 zet_metric_streamer_handle_t metricStreamerHandle =
12611284 lzt::metric_streamer_open_for_device (
1262- device, groupInfo.metricGroupHandle , eventHandle,
1263- notifyEveryNReports, samplingPeriod);
1264- ASSERT_NE (nullptr , metricStreamerHandle);
1285+ device, groupInfo.metricGroupHandle , nullptr , notifyEveryNReports,
1286+ samplingPeriod);
12651287
1266- lzt::execute_command_lists (commandQueue, 1 , &commandList, nullptr );
1267- lzt::synchronize (commandQueue, std::numeric_limits<uint64_t >::max ());
1288+ // Sleep for timeBeforeReadInNanoSec to ensure required reports are
1289+ // generated
1290+ std::this_thread::sleep_for (
1291+ std::chrono::nanoseconds (timeBeforeReadInNanoSec));
1292+ ASSERT_NE (nullptr , metricStreamerHandle);
12681293
12691294 size_t rawDataSize = 0 ;
12701295 std::vector<uint8_t > rawData;
1271- rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle, notifyEveryNReports);
1296+ rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle,
1297+ notifyEveryNReports);
12721298 EXPECT_GT (rawDataSize, 0 );
1273- rawData.resize (rawDataSize);
1274- lzt::metric_streamer_read_data (metricStreamerHandle, notifyEveryNReports,
1275- rawDataSize, &rawData);
1299+ rawData.resize (rawDataSize);
1300+ for (uint32_t count = 0 ; count < maxReadAttempts; count++) {
1301+ lzt::metric_streamer_read_data (
1302+ metricStreamerHandle, notifyEveryNReports, rawDataSize, &rawData);
1303+ if (rawDataSize > 0 ) {
1304+ break ;
1305+ } else {
1306+ std::this_thread::sleep_for (std::chrono::nanoseconds (samplingPeriod));
1307+ }
1308+ }
12761309
12771310 LOG_INFO << " rawDataSize " << rawDataSize;
1311+ // Stop the worker thread running the workload
1312+ workloadThreadFlag = false ;
1313+ thread.join ();
1314+
12781315 lzt::validate_metrics (groupInfo.metricGroupHandle , rawDataSize,
12791316 rawData.data ());
12801317 lzt::metric_streamer_close (metricStreamerHandle);
@@ -1283,7 +1320,6 @@ TEST_F(
12831320 lzt::free_memory (a_buffer);
12841321 lzt::free_memory (b_buffer);
12851322 lzt::free_memory (c_buffer);
1286- eventPool.destroy_event (eventHandle);
12871323 lzt::reset_command_list (commandList);
12881324 }
12891325 lzt::destroy_command_queue (commandQueue);
@@ -1300,10 +1336,7 @@ TEST_F(
13001336 * ZE_RESULT_NOT_READY. Once the expected time has elapsed it will come out of
13011337 * the loop and expect the event to be generated.
13021338 */
1303-
1304- /* The time in seconds for the buffer to overflow would be 2 * (notifyEveryNReports * (samplingPeriod/nanoSecToSeconds))
1305- * For this test it will be 9 seconds. The execution time between metric_streamer_open_for_device and synchronize observed on average is less than 50% of this
1306- */
1339+
13071340 uint32_t notifyEveryNReports = 4500 ;
13081341 for (auto device : devices) {
13091342
@@ -1350,8 +1383,11 @@ TEST_F(
13501383 notifyEveryNReports, samplingPeriod);
13511384 ASSERT_NE (nullptr , metricStreamerHandle);
13521385
1353- lzt::execute_command_lists (commandQueue, 1 , &commandList, nullptr );
1354- lzt::synchronize (commandQueue, std::numeric_limits<uint64_t >::max ());
1386+ // Spawn a thread which continuously runs a workload till the event is
1387+ // generated in the main thread.
1388+ workloadThreadFlag = true ;
1389+ std::thread thread (workloadThread, commandQueue, 1 , &commandList,
1390+ nullptr );
13551391
13561392 double minimumTimeBeforeEventIsExpected =
13571393 notifyEveryNReports *
@@ -1385,6 +1421,9 @@ TEST_F(
13851421 eventResult = zeEventQueryStatus (eventHandle);
13861422 EXPECT_EQ (eventResult, ZE_RESULT_SUCCESS);
13871423
1424+ // signal the worker thread to stop running the workload.
1425+ workloadThreadFlag = false ;
1426+ thread.join ();
13881427 lzt::metric_streamer_close (metricStreamerHandle);
13891428 lzt::deactivate_metric_groups (device);
13901429 eventPool.destroy_event (eventHandle);
@@ -1402,10 +1441,13 @@ TEST_F(
14021441TEST_F (
14031442 zetMetricStreamerTest,
14041443 GivenValidMetricGroupWhenTimerBasedStreamerIsCreatedThenExpectStreamerToGenrateCorrectNumberOfReports) {
1405-
1406- /* The time in seconds for the buffer to overflow would be 2 * (notifyEveryNReports * (samplingPeriod/nanoSecToSeconds))
1407- * For this test it will be 9 seconds. The execution time between metric_streamer_open_for_device and synchronize observed on average is less than 50% of this
1408- */
1444+ /* This test computes the expected time before which events are generated by
1445+ * multiplying notifyEveryNReports and samplingPeriod. It then loops inside
1446+ * the do-while loop for the expected time and checks for event status to be
1447+ * ZE_RESULT_NOT_READY. Once the expected time has elapsed it will come out of
1448+ * the loop and expect the event to be generated and checks if correct number
1449+ * of reports have been generated.
1450+ */
14091451 uint32_t notifyEveryNReports = 4500 ;
14101452 for (auto device : devices) {
14111453
@@ -1452,8 +1494,11 @@ TEST_F(
14521494 notifyEveryNReports, samplingPeriod);
14531495 ASSERT_NE (nullptr , metricStreamerHandle);
14541496
1455- lzt::execute_command_lists (commandQueue, 1 , &commandList, nullptr );
1456- lzt::synchronize (commandQueue, std::numeric_limits<uint64_t >::max ());
1497+ // Spawn a thread which continuously runs a workload till the event is
1498+ // generated in the main thread.
1499+ workloadThreadFlag = true ;
1500+ std::thread thread (workloadThread, commandQueue, 1 , &commandList,
1501+ nullptr );
14571502
14581503 double minimumTimeBeforeEventIsExpected =
14591504 notifyEveryNReports *
@@ -1479,6 +1524,10 @@ TEST_F(
14791524 eventResult = zeEventQueryStatus (eventHandle);
14801525 EXPECT_EQ (eventResult, ZE_RESULT_SUCCESS);
14811526
1527+ // signal the worker thread to stop running the workload.
1528+ workloadThreadFlag = false ;
1529+ thread.join ();
1530+
14821531 size_t oneReportSize, allReportsSize;
14831532 oneReportSize =
14841533 lzt::metric_streamer_read_data_size (metricStreamerHandle, 1 );
@@ -1764,9 +1813,10 @@ void run_ip_sampling_with_validation(
17641813
17651814 size_t rawDataSize = 0 ;
17661815 std::vector<uint8_t > rawData;
1767- rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle, notifyEveryNReports);
1816+ rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle,
1817+ notifyEveryNReports);
17681818 EXPECT_GT (rawDataSize, 0 );
1769- rawData.resize (rawDataSize);
1819+ rawData.resize (rawDataSize);
17701820 lzt::metric_streamer_read_data (metricStreamerHandle, notifyEveryNReports,
17711821 rawDataSize, &rawData);
17721822 lzt::validate_metrics (groupInfo.metricGroupHandle , rawDataSize,
@@ -2300,11 +2350,12 @@ TEST(
23002350
23012351 size_t rawDataSize = 0 ;
23022352 std::vector<uint8_t > rawData;
2303- rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle, notifyEveryNReports);
2353+ rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle,
2354+ notifyEveryNReports);
23042355 EXPECT_GT (rawDataSize, 0 );
2305- rawData.resize (rawDataSize);
2356+ rawData.resize (rawDataSize);
23062357 lzt::metric_streamer_read_data (metricStreamerHandle, notifyEveryNReports,
2307- rawDataSize, &rawData);
2358+ rawDataSize, &rawData);
23082359 lzt::validate_metrics (groupInfo.metricGroupHandle , rawDataSize,
23092360 rawData.data ());
23102361
@@ -2385,11 +2436,12 @@ TEST(
23852436
23862437 size_t rawDataSize = 0 ;
23872438 std::vector<uint8_t > rawData;
2388- rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle, notifyEveryNReports);
2439+ rawDataSize = lzt::metric_streamer_read_data_size (metricStreamerHandle,
2440+ notifyEveryNReports);
23892441 EXPECT_GT (rawDataSize, 0 );
2390- rawData.resize (rawDataSize);
2442+ rawData.resize (rawDataSize);
23912443 lzt::metric_streamer_read_data (metricStreamerHandle, notifyEveryNReports,
2392- rawDataSize, &rawData);
2444+ rawDataSize, &rawData);
23932445 lzt::validate_metrics (groupInfo.metricGroupHandle , rawDataSize,
23942446 rawData.data ());
23952447
0 commit comments